ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 01 Oct 2021 17:30:07 +0200Is it possible to ask sage check symbolic equality by comparing parts within equations?https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/ 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?Thu, 30 Sep 2021 05:52:02 +0200https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/Comment by Emmanuel Charpentier for <p>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 <code>2*x+4==2*x+4 and print(True)</code> could print <code>True</code> properly, but when functions grow more complicate, it couldn't.</p>
<p>But when I call <code>show()</code> 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?</p>
https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?comment=59215#post-id-59215Thierry, you are right... Editing my answer.Fri, 01 Oct 2021 17:30:07 +0200https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?comment=59215#post-id-59215Comment by tolga for <p>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 <code>2*x+4==2*x+4 and print(True)</code> could print <code>True</code> properly, but when functions grow more complicate, it couldn't.</p>
<p>But when I call <code>show()</code> 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?</p>
https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?comment=59195#post-id-59195You 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:
(equation1-equation2).expand().canonicalize_radical().full_simplify()Thu, 30 Sep 2021 06:49:15 +0200https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?comment=59195#post-id-59195Answer by Emmanuel Charpentier for <p>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 <code>2*x+4==2*x+4 and print(True)</code> could print <code>True</code> properly, but when functions grow more complicate, it couldn't.</p>
<p>But when I call <code>show()</code> 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?</p>
https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?answer=59196#post-id-59196**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
True
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)
True
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())
True
HTH,Thu, 30 Sep 2021 10:14:10 +0200https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?answer=59196#post-id-59196Comment by tmonteil for <p><strong>EDIT :</strong> This answer is at least partially <strong>false</strong> ; as far as I can tell, <code>tmonteil</code> below is right...</p>
<p>Kept for the edification of future users...</p>
<p>Sagemath will reduce an equation to <code>True</code> if the canonical form of its arguments (the "sides" of the equation) are the same.</p>
<pre><code>sage: 1+1==2
True
</code></pre>
<p>This prints <code>True</code> because the canonoical form of <code>1+1</code> is <code>2</code>, identical to the canonical form of the other argument, and this of these forms are "simple enough" (not formally defined). Counter-example :</p>
<pre><code>sage: var("a, b")
(a, b)
sage: sin(a+b)==sin(a+b)
sin(a + b) == sin(a + b)
</code></pre>
<p>In less obvious cases, the canonical forms of the (mathematically equal) arguments are different :</p>
<pre><code>sage: sin(x)^2+cos(x)^2==1
cos(x)^2 + sin(x)^2 == 1
</code></pre>
<p>The <code>bool</code> function actively tries to find a common form of its arguments : </p>
<pre><code>sage: bool(sin(x)^2+cos(x)^2==1)
True
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())
True
</code></pre>
<p>HTH,</p>
https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?comment=59200#post-id-59200The 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.
See:
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.Thu, 30 Sep 2021 10:44:26 +0200https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?comment=59200#post-id-59200Answer by tmonteil for <p>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 <code>2*x+4==2*x+4 and print(True)</code> could print <code>True</code> properly, but when functions grow more complicate, it couldn't.</p>
<p>But when I call <code>show()</code> 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?</p>
https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?answer=59199#post-id-59199In 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.Thu, 30 Sep 2021 10:41:28 +0200https://ask.sagemath.org/question/59194/is-it-possible-to-ask-sage-check-symbolic-equality-by-comparing-parts-within-equations/?answer=59199#post-id-59199