Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sympy - Comparing expressions

Is there a way to check if two expressions are mathematically equal? I expected tg(x)cos(x) == sin(x) to output True, but it outputs False. Is there a way to make such comparisons with sympy? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False.

I found some similar questions, but none covered this exact problem.

like image 247
Xico Sim Avatar asked May 09 '16 09:05

Xico Sim


People also ask

How do you compare two expressions in Python?

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 .

How do you evaluate an expression in SymPy?

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 .

How do you compare two algebraic expressions?

When you compare algebraic expressions whether they are super simple or complex the first thing you need to do is to treat each expression as separate and then evaluate each expression. You will want to have each value in the simplest form possible. From that moment further, you are free to just compare each value.

How do you substitute values in SymPy?

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.


1 Answers

From the SymPy documentation

== represents exact structural equality testing. “Exact” here means that two expressions will compare equal with == only if they are exactly equal structurally. Here, (x+1)^2 and x^2+2x+1 are not the same symbolically. One is the power of an addition of two terms, and the other is the addition of three terms.

It turns out that when using SymPy as a library, having == test for exact symbolic equality is far more useful than having it represent symbolic equality, or having it test for mathematical equality. However, as a new user, you will probably care more about the latter two. We have already seen an alternative to representing equalities symbolically, Eq. 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. This method is not infallible—in fact, it can be theoretically proven that it is impossible to determine if two symbolic expressions are identically equal in general—but for most common expressions, it works quite well.

As a demo for your particular question, we can use the subtraction of equivalent expressions and compare to 0 like so

>>> from sympy import simplify >>> from sympy.abc import x,y >>> vers1 = (x+y)**2 >>> vers2 = x**2 + 2*x*y + y**2 >>> simplify(vers1-vers2) == 0 True >>> simplify(vers1+vers2) == 0 False 
like image 85
miradulo Avatar answered Sep 30 '22 07:09

miradulo