Ask Your Question
1

Cast expression to rational number

asked 2016-01-14 16:09:56 +0100

sagenibble gravatar image

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,

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-01-14 21:59:54 +0100

vdelecroix gravatar image

updated 2016-01-14 22:30:22 +0100

slelievre gravatar image

Note 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
True
edit flag offensive delete link more

Comments

1

Then 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 s
slelievre gravatar imageslelievre ( 2016-01-14 22:33:34 +0100 )edit
1

Or condensed ;-)

def sqrtceil(x):
    s, r = x.sqrtrem()
    return s + bool(r)
vdelecroix gravatar imagevdelecroix ( 2016-01-14 23:15:55 +0100 )edit
1

answered 2016-01-14 17:28:59 +0100

slelievre gravatar image

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 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'
edit flag offensive delete link more

Comments

Interesting, but is there no way to force the Symbolic Ring to produce a flattened number?

sagenibble gravatar imagesagenibble ( 2016-01-18 11:29:39 +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

Stats

Asked: 2016-01-14 16:09:56 +0100

Seen: 943 times

Last updated: Jan 14 '16