Ask Your Question

Is it possible to ask sage check symbolic equality by comparing parts within equations?

asked 2021-09-30 05:52:02 +0100

andrewyg gravatar image

updated 2021-09-30 10:54:08 +0100

tmonteil gravatar image

So I have a case with two complicated equations, when substitutes with certain parameters, those two functions equal each other. I know that sage is capable on comparing simple equations like 2*x+4==2*x+4 and print(True) could print True properly, but when functions grow more complicate, it couldn't.

But when I call show() on my functions that sage return to me after symbolic substitution, they're identical, like everything. Which makes me wonder since everything are exactly the same, why sage couldn't properly evaluate equality? And is there any way I could hack around it? Perhaps like breaking them into small parts and ensure each parts are identical?

edit retag flag offensive close merge delete


You can try comparing the coefficients if they are polynomials. Or better, expanding the difference of the equations (equation1-equation2) and simplifying the result would be more practical for complicated equations. I would try:

tolga gravatar imagetolga ( 2021-09-30 06:49:15 +0100 )edit

Thierry, you are right... Editing my answer.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2021-10-01 17:30:07 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2021-09-30 10:41:28 +0100

tmonteil gravatar image

updated 2021-09-30 10:57:00 +0100

In Python (which is the programmng language on top of which Sage is built), the call of a == b is supposed to return the boolean True whenever a is equal to b, and False otherwise.

In mathematics, when $x$ is a symbol or an indeterminate, you want to be able to deal with expressions such as $2x+4 = 3x+3$ or $2x+4 < 42$ (e.g. in order to solve them as equations, or to simplify them, etc).

To be able to construct such expressions in Sage, we break the promise that a == b will return a boolean, so that 2*x+4 == 3*x+3 and 2*x+4 < 42 will return symbolic expressions.

For this, the __eq__ method that is called when testing for equality between 2 objects is overriden so that instead of returning a boolean, it returns an expression. Hence, if a and b are symbolic expressions, then a == b is a symbolic expression as well, not a boolean as it should be [1].

If you want to test whether the symbolic expression a is equal to the symbolic expression b, you have to type bool(a == b).

Note however that, since a boolean is either True or False, but not Unknown, when Sage is not able to prove that a is equal to b, it will return False even if the two expressions are equal.

Note [1] : i omit the coercion layer to make the answer understandable.

edit flag offensive delete link more

answered 2021-09-30 10:14:10 +0100

Emmanuel Charpentier gravatar image

updated 2021-10-01 17:33:25 +0100

EDIT : This answer is at least partially false ; as far as I can tell, tmonteil below is right...

Kept for the edification of future users...

Sagemath will reduce an equation to True if the canonical form of its arguments (the "sides" of the equation) are the same.

sage: 1+1==2

This prints True because the canonoical form of 1+1 is 2, identical to the canonical form of the other argument, and this of these forms are "simple enough" (not formally defined). Counter-example :

sage: var("a, b")
(a, b)
sage: sin(a+b)==sin(a+b)
sin(a + b) == sin(a + b)

In less obvious cases, the canonical forms of the (mathematically equal) arguments are different :

sage: sin(x)^2+cos(x)^2==1
cos(x)^2 + sin(x)^2 == 1

The bool function actively tries to find a common form of its arguments :

sage: bool(sin(x)^2+cos(x)^2==1)
sage: sin(a+b)==sin(a+b).trig_expand()
sin(a + b) == cos(b)*sin(a) + cos(a)*sin(b)
sage: bool(sin(a+b)==sin(a+b).trig_expand())


edit flag offensive delete link more


The fact that 1+1==2 returns a boolean and sin(a+b)==sin(a+b) is not related to Sage capabilities, but to the fact that 1+1 and 2 are Sage integers while sin(a+b) is a symbolic expression.


sage: SR(1) == SR(2)
1 == 2

This is not a matter of "actively trying", if a and b are symbolic expressions, a==b will always be a symbolic expression, not a boolean. See my answer for more details.

tmonteil gravatar imagetmonteil ( 2021-09-30 10:44:26 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-09-30 05:52:02 +0100

Seen: 1,469 times

Last updated: Oct 01 '21