Ask Your Question
2

numerical evaluation with variables

asked 2012-09-30 08:00:20 +0200

planimeterer gravatar image

Hello,

a few days ago I started working with sage. So very soon I run into into questions. Here is one of them:

I defined an equation:

sage: x, y = var('x, y')

sage: eq = y == (sqrt(5) + 3) * x^3 + e^pi * x^2 + sqrt(2) * x + 1/27

And I want to get from that something like:

y == 5.236 * x^3 + 23.141 * x^2 + 1.414 * x + 0.037

the numerical_approx() seems to be designed for another purpose. How do I get my desired result?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2012-09-30 10:17:13 +0200

DSM gravatar image

One way is to use _convert to coerce the symbolic coefficients to the Real Field:

sage: var("x y")
(x, y)
sage: eq = y == (sqrt(5) + 3) * x^3 + e^pi * x^2 + sqrt(2) * x + 1/27
sage: eq._convert(RR)
y == 5.23606797749979*x^3 + 23.1406926327793*x^2 + 1.41421356237310*x + 0.0370370370370370

There's a ticket -- trac #12577 -- which proposes to expose this (by which I mean get rid of the initial _). Unfortunately the convert method without an underscore already exists, and doesn't seem to be really useful, but we could use something more explicit like change_coefficient_ring. Actually, it could even take a function argument as well as a ring, and could be used for quick transformations without needing to implement an expression walker..

edit flag offensive delete link more

Comments

Thank you, that is exactly what I needed.

planimeterer gravatar imageplanimeterer ( 2012-09-30 11:12:13 +0200 )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

Stats

Asked: 2012-09-30 08:00:20 +0200

Seen: 842 times

Last updated: Sep 30 '12