ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 01 Dec 2012 10:45:50 -0600equating formal functionshttp://ask.sagemath.org/question/9579/equating-formal-functions/Why does the following not result in 'True'?
var('x')
f=function('f',nargs=1)
f(x)==f(x)
or, speaking differently, can I define formal functions such that the preceding will result in 'True'?Sat, 01 Dec 2012 06:04:56 -0600http://ask.sagemath.org/question/9579/equating-formal-functions/Answer by DSM for <p>Why does the following not result in 'True'?</p>
<pre><code>var('x')
f=function('f',nargs=1)
f(x)==f(x)
</code></pre>
<p>or, speaking differently, can I define formal functions such that the preceding will result in 'True'?</p>
http://ask.sagemath.org/question/9579/equating-formal-functions/?answer=14335#post-id-14335It seems to work fine for me:
sage: var('x')
x
sage: f=function('f',nargs=1)
sage: f
f
sage: f == f
True
sage: f(x) == f(x)
f(x) == f(x)
sage: bool(f(x) == f(x))
True
sage: if f(x) == f(x):
....: print 'equal!'
....:
equal!
Do you mean that you want `f(x) == f(x)` to be automatically simplified to `True`, instead of returning an equation? That can't work in general, because in Sage, an equation is `False` if Sage can't prove that it's true. So if you had an equation like
sage: x^2 - 4 == 0
x^2 - 4 == 0
it would instead give you `False` rather than the equation, because it's possible that `x` is something other than `2` or `-2`..
Long story short, if you want the truth value, call `bool`. You might be able to construct a subclass which gives you the behaviour you want but ISTM it'll only lead to headaches.Sat, 01 Dec 2012 06:55:53 -0600http://ask.sagemath.org/question/9579/equating-formal-functions/?answer=14335#post-id-14335Comment by Mark for <p>It seems to work fine for me:</p>
<pre><code>sage: var('x')
x
sage: f=function('f',nargs=1)
sage: f
f
sage: f == f
True
sage: f(x) == f(x)
f(x) == f(x)
sage: bool(f(x) == f(x))
True
sage: if f(x) == f(x):
....: print 'equal!'
....:
equal!
</code></pre>
<p>Do you mean that you want <code>f(x) == f(x)</code> to be automatically simplified to <code>True</code>, instead of returning an equation? That can't work in general, because in Sage, an equation is <code>False</code> if Sage can't prove that it's true. So if you had an equation like</p>
<pre><code>sage: x^2 - 4 == 0
x^2 - 4 == 0
</code></pre>
<p>it would instead give you <code>False</code> rather than the equation, because it's possible that <code>x</code> is something other than <code>2</code> or <code>-2</code>..</p>
<p>Long story short, if you want the truth value, call <code>bool</code>. You might be able to construct a subclass which gives you the behaviour you want but ISTM it'll only lead to headaches.</p>
http://ask.sagemath.org/question/9579/equating-formal-functions/?comment=18598#post-id-18598Yes, I was expecting f(x)==f(x) to automatically simplify to True. (Mathematica will actually do so.) The bool()-typing workaround you suggest is kind of ok for me, yet, as I read your answer, it seems really strange that x-x==0 does not result in True but rather in 0==0 ... and moreover, if you type 0==0 at the sage prompt you will obviously get True ... Sat, 01 Dec 2012 10:45:50 -0600http://ask.sagemath.org/question/9579/equating-formal-functions/?comment=18598#post-id-18598