JuliaSymbolics / Symbolics.jl

Symbolic programming for the next generation of numerical software
https://docs.sciml.ai/Symbolics/stable/
Other
1.37k stars 153 forks source link

Stack overflow in type inference when using large matrix-valued functions from Symbolics.build_function #990

Open Krebbekx opened 1 year ago

Krebbekx commented 1 year ago

When using Symbolics.build_function to generate a numerically usable Julia function from a symbolic function, the resulting function is very slow and raises a stack overflow error when calling it. The following example reproduces the error:

using Symbolics

N = 100  # for N=10, no error.

@variables xarray[1:N]

funexpr = xarray.^2  # example vector-valued function of length N

jacobianfun = eval(Symbolics.build_function(Symbolics.jacobian(funexpr, xarray), xarray)[1])  # build function from symbolic expression

xvalue = ones(N)

jacobianfun(xvalue)  # raises overflow error

generating the error

Internal error: stack overflow in type inference of #29(Array{Float64, 1}).
This might be caused by recursion over very long tuples or argument lists.

This limits the usage of symbolics to efficiently generate functions from symbolic expressions.

Krebbekx commented 1 year ago

This does not solve the issue, but by using Symbolics.sparsejacobian, the problem does not occur.