Ask Your Question
0

How to extract the integer part from a symbolic expressions?

asked 2014-10-27 15:36:46 +0100

abelian-grape gravatar image

updated 2014-10-27 15:51:09 +0100

vdelecroix gravatar image

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 15:49:02 +0100

tmonteil gravatar image

updated 2014-10-27 15:51:57 +0100

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
2

answered 2014-10-27 15:53:15 +0100

vdelecroix gravatar image

updated 2014-10-27 18:15:34 +0100

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 18:08:18 +0100 )edit

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

vdelecroix gravatar imagevdelecroix ( 2014-10-27 18:13:21 +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: 2014-10-27 15:36:46 +0100

Seen: 1,482 times

Last updated: Oct 27 '14