Numerically evaluate a symbolic expression

First steps using Symbolics.jl.
I’d like to program an elementary calculus trainer:

  • An expression is shown
  • As solution, the expanded expression is shown
  • As proof, the expression is evaluated with numerical values.

What is the best way for the last point, using substitute or build_function?

using Symbolics

@variables a b

f = (a + 2*b)^2
f_exp = expand(f)

println(f)      # (a + 2b)^2
println(f_exp)  # a^2 + 4(b^2) + 4a*b

# f_num = build_function(f, a, b) # not working
# f_num(2, 3)   # 64


substitute unless you call it many times.

Thanks Chris.

substitute(f, Dict(a=>2, b=>3)) # 64
f_fun = eval(build_function(f, a,b))
f_fun(2,3) # 64
1 Like