Evaluate constants in symbolic expression?

asked 2021-02-25 20:00:22 +0200

MaybE_Tree gravatar image

Hello. If I have a symbolic expression where all of the variables are known, I can use numerical_approx to evaluate it:

var('a b')
x = a / b
x(a=1, b=3)
> 1/3
x(a=1, b=3).numerical_approx()
> 0.333333333333333

However, if there is an unknown variable, numerical_approx fails:

var('a b c')
x = a / b * c
x(a=1, b=3)
> 1/3*c
x(a=1, b=3).numerical_approx()
> TypeError: cannot evaluate symbolic expression numerically

Is there a way to approximate the values of all constants / coefficients in a symbolic expression? Essentially, something like

var('a b c')
x = a / b * c
x(a=1, b=3).numerical_approx_constants()
> 0.33333333333*c

Thank you in advance.

edit retag flag offensive close merge delete



Using https://ask.sagemath.org/question/460... you can do SubstituteNumericalApprox()(x(a=1, b=3)).

rburing gravatar imagerburing ( 2021-02-25 20:32:59 +0200 )edit

As another option, don't substitute constants by integers, but by floats:

sage: x(a=1., b=3.)
Juanjo gravatar imageJuanjo ( 2021-02-25 20:33:39 +0200 )edit