Ask Your Question
0

convert a symbolic var into a numeric var

asked 11 years ago

jeanpat gravatar image

Hello, How to compute y or to draw a point of coord (sol, 0) ?

Thank you

sol=solve(2*x+1==7,x)
print sol
print sol[0], type(sol), type(sol[0])
#y=2*sol+7
#print y

#plot(sol,0)
Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 11 years ago

tmonteil gravatar image

updated 11 years ago

Your sol is a list, with 1 element:

sage: sol
[x == 3]

Then, sol[0] is a symbolic expression. To get its right hand side, it suffice to use the .rhs() method:

sage: sol[0]
x == 3

sage: sol[0].rhs()
3

Then, it should be enough to work since the symbolic variable 3 should be automatically coerced by Sage into a numeric value if needed (and plot should understand symbolic numbers).

sage: sol[0].rhs().parent()
Symbolic Ring

That said, it may be a good idea to escape the symbolic ring. If you want to have a genuine integer, you can do:

sage: ZZ(sol[0].rhs())
3
sage: ZZ(sol[0].rhs()).parent()
Integer Ring

If you want an algebraic number:

sage: AA(sol[0].rhs())
3
sage: AA(sol[0].rhs()).parent()
Algebraic Real Field

If you want a floating number:

sage: RR(sol[0].rhs())
3.00000000000000
sage: RR(sol[0].rhs()).parent()
Real Field with 53 bits of precision
Preview: (hide)
link
2

answered 11 years ago

calc314 gravatar image

Here is how to plot the point:

sol=solve(2*x+1==7,x)
x0=sol[0].rhs()
point((x0,2*x0+1),size=40)
Preview: (hide)
link

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: 11 years ago

Seen: 1,722 times

Last updated: Dec 12 '13