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
smichr on master
point2D now will subclass Evalf… fixed failing tests test evalf for entities Also, … and 1 more (compare)
Float
refers to the specific way the numbers are represented, i.e., floating-point. Not all real numbers are represented that way. For instance, Rational(1, 3)
, sqrt(2)
and pi
are real numbers but not floating point, and in fact they can't be exactly represented as floating-point. A floating point number uses a very specific representation of $man\times 2^{exp}$ (see https://mpmath.org/doc/current/technical.html#representation-of-numbers).
Float(x) + Float(y) != Float(x + y)
in general.
x.is_real
will tell you if x is a real number. The Python classes are based on the implementation, which is why they are named after the representation. Calling Float
Real would be inaccurate because it is only one possible representation of real numbers, and one that can't even represent all possible ones at that.
>>> from sympy import *
>>> i = Symbol('i')
>>> a = 3 * i
>>> Sum(a, (i, 1, 5))
Sum(3*i, (i, 1, 5))
>>> Sum(a, (i, 1, 5)).doit()
45
>>> b = Sum(i, (i, 1, 10)) / Sum(i + 1, (i, 1, 10))
>>> b
Sum(i, (i, 1, 10))/Sum(i + 1, (i, 1, 10))
>>> b.doit()
11/13
>>> pprint(Sum(a, (i, 1, 5)))
5
___
╲
╲
╱ 3⋅i
╱
‾‾‾
i = 1
>>> pprint(b)
10
___
╲
╲
╱ i
╱
‾‾‾
i = 1
─────────────
10
___
╲
╲
╱ (i + 1)
╱
‾‾‾
i = 1
solve([x - 5], x)
simply return [5]
?
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.