A computer algebra system written in pure Python http://sympy.org/ . To get started to with contributing https://github.com/sympy/sympy/wiki/Introduction-to-contributing
Hi everyone, can someone give me some recommendation to the next problem, I have a large expression lambdified, the problem is that has Nan results when I evaluate. I have found one term which causes this behavior and given by:
from sympy import init_printing,symbols,log,sqrt
init_printing()
m = symbols('m')
f = 0.5*log(1 - 3.13400648e+22/(1.56700324e+22*sqrt(1 - 1.5976993129893e-11*m)*sqrt(1.5976993129893e-11*m + 1) + 1.56700324e+22))
Then, when I substitute in $m=1e-3$
f.subs(m,1e-3)
Which give $\tilde{\infty}$. However, if I use evalf command I obtain
f.evalf(subs={m:1e-3})
-32.4607738174251 + 1.5707963267949*I
Then, I don't know what result is correct. Is this a problem of the precision of subs command?.
lambdify
, you are going to run into this same problem, because you are evaluating it naively. You will need to rewrite the expression somehow to avoid the massive cancellation.
log1p
.
@asmeurer Thanks for the suggestions and the explanation. My expression is too large because I obtained after some SymPy calculations and I do not how to rewrite the log function following your suggestions of multiply for a big number inside of log. I have tried to create a new log function like log_rewrite(x) = log(x*y) - log(y) with y a big number and after using lambdify command to the f function
from numpy import log as nplog
def log_rewritted(x):
y = 1e30
return nplog(x*y)-nplog(y)
fnp = lambdify([m],f,modules=[{'log':log_rewritted}])
However, I obtained the same result when I evaluate f,
fnp(1e-3)
-inf.
Sum(x**2, (x, 1, k)).evalf()
and expression which derivative is not a constant raise TypeErroreuler_maclaurin
when it compare a symbolic expression to a float...I found some unusual behaviour in randprime and I try to fix it.
from sympy import randprime
print(randprime(-2,0))
ValueError: no preceding primes
Can someone please review this PR : #20949
class Data:
def __init__(self, string,symbol, value):
self.string = string
self.symbol = sm.Symbol(symbol)
self.value = value
def __add__(self, other):
return self.value + other.value if isinstance(other, Data) else other
def __str__(self):
return self.string
def _repr_latex_(self):
return display(Latex(' '.join([self.string, f'({self.symbol})' ,r'\equals', self.value] )))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
c:\users\dell\onedrive\desktop\courses sem8\venv\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
343 method = get_real_method(obj, self.print_method)
344 if method is not None:
--> 345 return method()
346 return None
347 else:
<ipython-input-49-83e3f780e89d> in _repr_latex_(self)
11 return self.string
12 def _repr_latex_(self):
---> 13 return display(Latex(' '.join([self.string, f'({self.symbol})' ,r'\equals', self.value] )))
14 # area_each_plate = Data("Area of each plate", 15.8*15.8*SI.cm**2)
15 # inlet_pressure = Data("Area of each plate", 15.8*15.8*SI.cm**2)
TypeError: sequence item 3: expected str instance, Quantity found
self.value
. join
requires that every item is converted to a string, so use latex(self.value)
.
_repr_latex
should return a string, not call display().