Consider the following example:
import sympy
x = sympy.Symbol(x, real=True)
expr = sympy.sin(x) + 1
can Sympy somehow determine that expr >= 0
is always true?
To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .
To test if two things are equal, it is best to recall the basic fact that if a=b, then a−b=0. Thus, the best way to check if a=b is to take a−b and simplify it, and see if it goes to 0. We will learn later that the function to do this is called simplify .
The Power of Symbolic Computation The real power of a symbolic computation system such as SymPy is the ability to do all sorts of computations symbolically. SymPy can simplify expressions, compute derivatives, integrals, and limits, solve equations, work with matrices, and much, much more, and do it all symbolically.
The subs() function in SymPy replaces all occurrences of first parameter with second. This function is useful if we want to evaluate a certain expression. For example, we want to calculate values of following expression by substituting a with 5.
You could try solving the inequality for x
:
>>> from sympy.solvers.inequalities import solve_univariate_inequality
>>> solve_univariate_inequality(expr >= 0, x)
And(-oo < x, x < oo)
So here SymPy tells you that the inequality holds true for any real number.
You can also use the assumption system to inquire about the attributes of an expression. There was a recent question about this here wherein Nair gives some good references. But for your case, simply try
>>> from sympy import *
>>> var('x', real=True)
x
>>> (sin(x)+1).is_positive
>>> (sin(x)+1).is_nonnegative
The result will either be True, False or (in this case) None. None
means either that the result is unknown or that the determination has not been implemented. In this case, the result for nonnegative should have been True. Improving the assumptions system is an active work with SymPy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With