ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 19 Dec 2011 02:50:50 -0600How to robustly simplify an object (including a number)https://ask.sagemath.org/question/8576/how-to-robustly-simplify-an-object-including-a-number/Hi,
When writing a package, I met the following problem:
Say, I want the variable 'a' to be an expression, which is passed to the package by user. Then I want to call a.simplify() to simplify the expression.
However, as an input, a could naturally be the following cases:
# case 1
a = var('t')
# case 2
a = 1
# case 3
a = 0.1
a.simplify() will work on 1, but not work on 2 or 3, which crashes the whole package.
I could do something as
try:
b = a.simplify()
except:
b = a
But this really seems like a workaround instead of something formal. Is there a better way to handle this case? Thanks!
Mon, 19 Dec 2011 02:14:40 -0600https://ask.sagemath.org/question/8576/how-to-robustly-simplify-an-object-including-a-number/Answer by Jason Grout for <p>Hi, </p>
<p>When writing a package, I met the following problem:</p>
<p>Say, I want the variable 'a' to be an expression, which is passed to the package by user. Then I want to call a.simplify() to simplify the expression.</p>
<p>However, as an input, a could naturally be the following cases:</p>
<pre><code># case 1
a = var('t')
# case 2
a = 1
# case 3
a = 0.1
</code></pre>
<p>a.simplify() will work on 1, but not work on 2 or 3, which crashes the whole package.</p>
<p>I could do something as</p>
<pre><code>try:
b = a.simplify()
except:
b = a
</code></pre>
<p>But this really seems like a workaround instead of something formal. Is there a better way to handle this case? Thanks!</p>
https://ask.sagemath.org/question/8576/how-to-robustly-simplify-an-object-including-a-number/?answer=13040#post-id-13040The simplify method exists on symbolic objects in Sage, but not on numbers. So I would either do your try/except block (but except the specific error about not having the simplify attribute), or test using hasattr whether you have the simplify attribute.
Alternatively, you could convert whatever the user passed in to a symbolic object:
a=SR(a)
a.simplify()Mon, 19 Dec 2011 02:20:55 -0600https://ask.sagemath.org/question/8576/how-to-robustly-simplify-an-object-including-a-number/?answer=13040#post-id-13040Comment by DSM for <p>The simplify method exists on symbolic objects in Sage, but not on numbers. So I would either do your try/except block (but except the specific error about not having the simplify attribute), or test using hasattr whether you have the simplify attribute.</p>
<p>Alternatively, you could convert whatever the user passed in to a symbolic object:</p>
<pre><code>a=SR(a)
a.simplify()
</code></pre>
https://ask.sagemath.org/question/8576/how-to-robustly-simplify-an-object-including-a-number/?comment=20688#post-id-20688There's also the simplify function, but that doesn't extend to the other members of the simplify_* set, so this is probably the most general sol'n. (I guess we could coerce back to the original type as well.)Mon, 19 Dec 2011 02:25:56 -0600https://ask.sagemath.org/question/8576/how-to-robustly-simplify-an-object-including-a-number/?comment=20688#post-id-20688Comment by tririver for <p>The simplify method exists on symbolic objects in Sage, but not on numbers. So I would either do your try/except block (but except the specific error about not having the simplify attribute), or test using hasattr whether you have the simplify attribute.</p>
<p>Alternatively, you could convert whatever the user passed in to a symbolic object:</p>
<pre><code>a=SR(a)
a.simplify()
</code></pre>
https://ask.sagemath.org/question/8576/how-to-robustly-simplify-an-object-including-a-number/?comment=20687#post-id-20687@Jason Grout: Thanks a lot! Previously I naively tried Expression(a) without luck. SR(a) is what I was looking for. Mon, 19 Dec 2011 02:50:50 -0600https://ask.sagemath.org/question/8576/how-to-robustly-simplify-an-object-including-a-number/?comment=20687#post-id-20687