Ask Your Question
0

How to extract the integer part from a symbolic expressions?

asked 2014-10-27 09:36:46 -0500

updated 2014-10-27 09:51:09 -0500

Suppose I solve the following quadratic equation using SAGE and store the solution in two variables, a and b :

sage: x = var('x')
sage: qe = (x^2 - 618*x + 90581 == 0)
sage: a, b = solve(qe, x)

Which gives me the following output:

sage: a
x == 379
sage: b  
x == 239
sage: type(a), type(b)
(sage.symbolic.expression.Expression, sage.symbolic.expression.Expression)

I wish to get the output in this form:

sage: a
379
sage: b  
239
sage: type(a), type(b)
(sage.rings.integer.Integer, sage.rings.integer.Integer)

How can I do this easily?

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
2

answered 2014-10-27 09:53:15 -0500

updated 2014-10-27 12:15:34 -0500

Or you can set the solution_dict to True:

sage: solve(qe, x, solution_dict=True)
[{x: 379}, {x: 239}]
sage: solve(qe, x, solution_dict=True)[0][x]
379
sage: solve(qe, x, solution_dict=True)[1][x]
239

EDIT: as mentioned in comments, the output are not integers but element of the symbolic ring. In order to get integers the fastest is

sage: a = solve(qe, x, solution_dict=True)[0][x]
sage: a.pyobject()
379

Vincent

edit flag offensive delete link more

Comments

1

This is not the requested solution. First a = solve(qe, x, solution_dict=True)[0][x] then type(a) still gives <type sage.symbolic.expression.expression="">.

Peter Luschny gravatar imagePeter Luschny ( 2014-10-27 12:08:18 -0500 )edit

Right, you have to use the second part of @tmonteil answer to finish.

vdelecroix gravatar imagevdelecroix ( 2014-10-27 12:13:21 -0500 )edit
2

answered 2014-10-27 09:49:02 -0500

tmonteil gravatar image

updated 2014-10-27 09:51:57 -0500

To get the right hand side of a, you can do

sage: a.rhs()

If you want a Sage integer, not a symbolic expression, you can do

sage: ZZ(a.rhs())
edit flag offensive delete link more

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: 2014-10-27 09:36:46 -0500

Seen: 141 times

Last updated: Oct 27 '14