# Evaluate constants in symbolic expression?

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

1

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

( 2021-02-25 20:32:59 +0200 )edit
2

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

sage: x(a=1., b=3.)
0.333333333333333*c

( 2021-02-25 20:33:39 +0200 )edit