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.Mon, 18 Jan 2016 11:29:39 +0100Cast expression to rational numberhttps://ask.sagemath.org/question/32195/cast-expression-to-rational-number/ Hello,
I am trying to perform the ceil() function on the result of a square root operation. My code is as follows:
x = 10
x = x.sqrt()
x = x.ceil()
However, I get the following error:
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'ceil'
I have tried searching the the documentation, but I can't seem to find a straight forward way to "flatten" this expression to a rational number. How can I go about this?
Thanks,Thu, 14 Jan 2016 16:09:56 +0100https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/Answer by vdelecroix for <p>Hello,</p>
<p>I am trying to perform the ceil() function on the result of a square root operation. My code is as follows:</p>
<pre><code>x = 10
x = x.sqrt()
x = x.ceil()
</code></pre>
<p>However, I get the following error:</p>
<pre><code>AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'ceil'
</code></pre>
<p>I have tried searching the the documentation, but I can't seem to find a straight forward way to "flatten" this expression to a rational number. How can I go about this?</p>
<p>Thanks,</p>
https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?answer=32198#post-id-32198Note that if you are taking the square root of an integer, the fastest might be the method `sqrtrem` that returns the floor of the square root and the remainder:
sage: x = 10
sage: s,r = x.sqrtrem()
sage: print s
3
sage: print r
1
sage: x == s^2 + r
TrueThu, 14 Jan 2016 21:59:54 +0100https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?answer=32198#post-id-32198Comment by slelievre for <p>Note that if you are taking the square root of an integer, the fastest might be the method <code>sqrtrem</code> that returns the floor of the square root and the remainder:</p>
<pre><code>sage: x = 10
sage: s,r = x.sqrtrem()
sage: print s
3
sage: print r
1
sage: x == s^2 + r
True
</code></pre>
https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?comment=32199#post-id-32199Then you could get the ceiling as follows.
def sqrtceil(x):
"""
Return the ceiling of the square root of this integer
""""
s, r = x.sqrtrem()
if r: # meaning if r nonzero, then sqrtceil = sqrtfloor + 1
return s + 1
else: # meaning r is zero, ie x is a square, so sqrtfloor = sqrtceil
return sThu, 14 Jan 2016 22:33:34 +0100https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?comment=32199#post-id-32199Comment by vdelecroix for <p>Note that if you are taking the square root of an integer, the fastest might be the method <code>sqrtrem</code> that returns the floor of the square root and the remainder:</p>
<pre><code>sage: x = 10
sage: s,r = x.sqrtrem()
sage: print s
3
sage: print r
1
sage: x == s^2 + r
True
</code></pre>
https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?comment=32200#post-id-32200Or condensed ;-)
def sqrtceil(x):
s, r = x.sqrtrem()
return s + bool(r)Thu, 14 Jan 2016 23:15:55 +0100https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?comment=32200#post-id-32200Answer by slelievre for <p>Hello,</p>
<p>I am trying to perform the ceil() function on the result of a square root operation. My code is as follows:</p>
<pre><code>x = 10
x = x.sqrt()
x = x.ceil()
</code></pre>
<p>However, I get the following error:</p>
<pre><code>AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'ceil'
</code></pre>
<p>I have tried searching the the documentation, but I can't seem to find a straight forward way to "flatten" this expression to a rational number. How can I go about this?</p>
<p>Thanks,</p>
https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?answer=32196#post-id-32196When you take the square root of an integer number that is not a square, you get an object in Sage's "Symbolic Ring", where the `ceil` method is not available.
You could start with a floating-point 10 rather than an integer 10:
sage: x = RDF(10)
sage: x
10.0
sage: x.parent()
Real Double Field
sage: x = x.sqrt()
sage: x
3.1622776601683795
sage: x.parent()
Real Double Field
sage: x = x.ceil()
sage: x
4
sage: x.parent()
Integer Ring
Compare with your original computations:
sage: x = 10
sage: x
10
sage: x.parent()
Integer Ring
sage: x = x.sqrt()
sage: x
sqrt(10)
sage: x.parent()
Symbolic Ring
sage: x = x.ceil()
...
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'ceil'
Thu, 14 Jan 2016 17:28:59 +0100https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?answer=32196#post-id-32196Comment by sagenibble for <p>When you take the square root of an integer number that is not a square, you get an object in Sage's "Symbolic Ring", where the <code>ceil</code> method is not available.</p>
<p>You could start with a floating-point 10 rather than an integer 10:</p>
<pre><code>sage: x = RDF(10)
sage: x
10.0
sage: x.parent()
Real Double Field
sage: x = x.sqrt()
sage: x
3.1622776601683795
sage: x.parent()
Real Double Field
sage: x = x.ceil()
sage: x
4
sage: x.parent()
Integer Ring
</code></pre>
<p>Compare with your original computations:</p>
<pre><code>sage: x = 10
sage: x
10
sage: x.parent()
Integer Ring
sage: x = x.sqrt()
sage: x
sqrt(10)
sage: x.parent()
Symbolic Ring
sage: x = x.ceil()
...
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'ceil'
</code></pre>
https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?comment=32274#post-id-32274Interesting, but is there no way to force the Symbolic Ring to produce a flattened number?Mon, 18 Jan 2016 11:29:39 +0100https://ask.sagemath.org/question/32195/cast-expression-to-rational-number/?comment=32274#post-id-32274