Can sympy determine if an expression is positive?

Consider the following example:

import sympy x = sympy.Symbol(x, real=True) expr = sympy.sin(x) + 1 

can sympy somehow determine that expr >= 0 always true?

+5
source share
2 answers

You can try to solve 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 is true for any real number.

+5
source

You can also use the assumptions system to learn about the attributes of an expression. There was a recent question about this here , where Nair gives good links. But for your case, just try

 >>> from sympy import * >>> var('x', real=True) x >>> (sin(x)+1).is_positive >>> (sin(x)+1).is_nonnegative 

The result will be either True or False, or (in this case) None. None means that the result is unknown or that the definition was not implemented. In this case, the result for non-negative should be True. Improving the assumptions system is an active work with SymPy.

+4
source

Source: https://habr.com/ru/post/1215551/


All Articles