# How to extract the integer part from a symbolic expressions?

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 close merge delete

Sort by » oldest newest most voted

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())

more

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)[x]
379
sage: solve(qe, x, solution_dict=True)[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)[x]
sage: a.pyobject()
379


Vincent

more

1

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