# Taylor expansion in Symbolics

**URL:** <https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [January 9, 2022, 10:18pm UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309 "2022-01-09T22:18:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 9, 2022, 10:18pm UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/1 "2022-01-09T22:18:04Z")

</div>

I found the very useful library

[https://github.com/JuliaDiff/TaylorSeries.jl](https://github.com/JuliaDiff/TaylorSeries.jl)

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 😉

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:

```julia
using Symbolics

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

main()

```

and saw:

```julia
(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!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 9, 2022, 11:06pm UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/2 "2022-01-09T23:06:22Z")

</div>

> [@purplishrock](#):
>
> `df(2)`

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

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 10, 2022, 12:01am UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/3 "2022-01-10T00:01:16Z")

</div>

No success.

```julia
println(SymbolicUtils.substitute(df,2))

```

`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.

edit:  
The answer is…  
` println(SymbolicUtils.substitute(df,Dict(x=>2.0)))`

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 🙂

---

<div class="post-metadata">

**Author:** ![pitsianis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitsianis/32/26588_2.png) [@pitsianis](https://discourse.julialang.org/u/pitsianis)\
**Post date:** [April 1, 2022, 6:54am UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/4 "2022-04-01T06:54:30Z")

</div>

> [@purplishrock](#):
>
> My goal is to of course generate a function which is compiled in as code

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

---

<div class="post-metadata">

**Author:** ![lbenet](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@lbenet](https://discourse.julialang.org/u/lbenet)\
**Post date:** [May 31, 2022, 5:13pm UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/5 "2022-05-31T17:13:55Z")

</div>

Some time ago, I was playing a little bit with this; you may have a look in the [lb/Symbolics branch](https://github.com/JuliaDiff/TaylorSeries.jl/tree/lb/Symbolics). The following is produced using that branch:

```julia
julia> using Symbolics, TaylorSeries

julia> x = t₀ + Taylor1(5)
 t₀ + 1.0 t + 𝒪(t⁶)

julia> sin(x)
 sin(t₀) + cos(t₀) t + -0.5sin(t₀) t² + -0.16666666666666666cos(t₀) t³ + 0.041666666666666664sin(t₀) t⁴ + 0.008333333333333333cos(t₀) t⁵ + 𝒪(t⁶)

julia> 1/x
 1 / t₀ + (-(1 / t₀)) / t₀ t + (-((-(1 / t₀)) / t₀)) / t₀ t² + (-((-((-(1 / t₀)) / t₀)) / t₀)) / t₀ t³ + (-((-((-((-(1 / t₀)) / t₀)) / t₀)) / t₀)) / t₀ t⁴ + (-((-((-((-((-(1 / t₀)) / t₀)) / t₀)) / t₀)) / t₀)) / t₀ t⁵ + 𝒪(t⁶)

```

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [June 25, 2022, 5:59am UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/6 "2022-06-25T05:59:59Z")

</div>

I would love to. Gave up. Couldn’t figure it out.

Should probably give it another try, because i’d generally like to be able to do that for functions other than just Taylor series expansions.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 25, 2022, 7:28am UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/7 "2022-06-25T07:28:04Z")

</div>

Did you look at Luis’ branch?

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [June 25, 2022, 7:29am UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/8 "2022-06-25T07:29:08Z")

</div>

I have not, but thanks for pointing it out. i’ll be sure an take a look.

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [August 14, 2022, 8:58pm UTC](https://discourse.julialang.org/t/taylor-expansion-in-symbolics/74309/9 "2022-08-14T20:58:42Z")

</div>

Is this useful to you?

```julia
using Symbolics, TaylorSeries

@variables x e
f = sin(x)

dx = Differential(x)
order = 5
taylor_seriesₓ₊ₑ = f + sum((dx^i)(f)*e^i/factorial(BigInt(i)) for i in 1:order) |> expand_derivatives
substitute(taylor_seriesₓ₊ₑ, Dict(x=>0.0)) # coefficients

# alternative
# @variables x₀
# series = substitute(f, Dict(x=>x₀)) + sum(substitute((dx^i)(f)|>expand_derivatives, Dict(x=>x₀))*(x-x₀)^i/factorial(i) for i in 1:order)
# substitute(series, Dict(x₀=>0.0))
#

t = Taylor1(5)
sin(t) # same coefficients with TaylorSeries.jl

f_taylor_5_order = Symbolics.build_function(taylor_seriesₓ₊ₑ, x, e) |> eval # callable julia function
f_taylor_5_order(0.0, 0.1) # example

```
