Taylor expansion in Symbolics

I found the very useful library

and was able to use it to generate the Taylor series coefficients for a problem I was working on.

I thought to myself this would be a really cool thing to implement in Symbolics, just for fun, and immediately got into trouble :wink:

the goal: create a function to generate a Taylor series by generating the derivatives of a function and returning the coefficients of that Taylor series, and for bonus points return a function which can be evaluated in code.

As i pointed out this can all be done with the TaylorSeries package, this is a learning experience for me, and I thought it might be useful to others.

I did the obvious thing, trying to generate the 1st derivative:

using Symbolics

function main()
    @variables x
    f(x) = log(1+x)
    df = Symbolics.derivative(f(x),x)
    # @syms df(x)


and saw:

(1 + x)^-1
ERROR: LoadError: Sym (1 + x)^-1 is not callable. Use @syms (1 + x)^-1(var1, var2,...) to create it as a callable.
ste code here

I then tried a bunch of stuff based what i was reading in the documentation to try and get df(2) and df(3) to yield a numeric value but the best i could ever do was get β€œdf(2)” and β€œdf(3)” to print.

hoping i could get some advice and how to move this further along.

thank you!

Instead of df(2) do substitute(df,2)

No success.


`ERROR: LoadError: MethodError: no method matching haskey(::Int64, ::Num)
Closest candidates are:
haskey(::DataStructures.SwissDict, ::Any) at /home/briand/.julia/packages/DataStructures/ixwFs/src/swiss_dict.jl:534

looking at the documentation, it seems to me like substitute is meant to substitute a different expression, not to evaluate an expression numerically.

FYI, i have tried using build_function in various ways without success, thinking that I might be able to get that to return something that could be used to evaluate at a value.

The answer is…

My goal is to of course generate a function which is compiled in as code, but give me some time to get through the documentation :slight_smile:

Please post the answer to the above, others might be interested too!