Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for evaluating a TaylorN on a Vector{TaylorN} #119

Closed
PerezHz opened this issue Aug 30, 2017 · 3 comments
Closed

Add method for evaluating a TaylorN on a Vector{TaylorN} #119

PerezHz opened this issue Aug 30, 2017 · 3 comments

Comments

@PerezHz
Copy link
Contributor

PerezHz commented Aug 30, 2017

While working on PR #118 (see also discussion in #116), I was thinking that perhaps it'd be nice to have defined evaluate{T<:NumberNotSeries}(a::TaylorN{T}, vals::Array{TaylorN{T},1}), or something similar, in order to be able to compose Taylor expansions of N-variable functions. There is already a similar method for Taylor1, which allows to evaluate a Taylor1 on a Taylor1.

What I'm trying to say (using syntax from #118) is that whereas it is possible to compose two Taylor1s,

julia> using TaylorSeries

julia> t = Taylor1(25)
 1.0 t + 𝒪(t²⁶)

julia> p = sin(t)
 1.0 t - 0.16666666666666666+ 0.008333333333333333 t⁵ - 0.0001984126984126984 t⁷ + 2.7557319223985893e-6 t⁹ - 2.505210838544172e-8 t¹¹ + 1.6059043836821616e-10 t¹³ - 7.647163731819817e-13 t¹⁵ + 2.811457254345521e-15 t¹⁷ - 8.220635246624331e-18 t¹⁹ + 1.9572941063391263e-20 t²¹ - 3.868170170630684e-23 t²³ + 6.446950284384474e-26 t²⁵ + 𝒪(t²⁶)

julia> q = cos(t)
 1.0 - 0.5+ 0.041666666666666664 t⁴ - 0.001388888888888889 t⁶ + 2.48015873015873e-5 t⁸ - 2.7557319223985894e-7 t¹⁰ + 2.08767569878681e-9 t¹² - 1.1470745597729726e-11 t¹⁴ + 4.779477332387386e-14 t¹⁶ - 1.5619206968586228e-16 t¹⁸ + 4.1103176233121653e-19 t²⁰ - 8.896791392450574e-22 t²² + 1.6117375710961184e-24 t²⁴ + 𝒪(t²⁶)

julia> p(q) #evaluate a Taylor1 on a Taylor1; compare with: sc = x->sin(cos(x)); sc(t)
 0.8414709848078965 - 0.2701511529340699- 0.08267127702314792 t⁴ + 0.028036523686489453 t⁶ - 0.0019241418790800983 t⁸ - 0.0004838563431246892 t¹⁰ + 0.00013040017931079692 t¹² - 1.210231179202225e-5 t¹⁴ - 3.316644672471871e-7 t¹⁶ + 2.429814411744226e-7 t¹⁸ - 3.288747013801372e-8 t²⁰ + 1.8233411897292427e-9 t²² + 1.2200826998513497e-10 t²⁴ + 𝒪(t²⁶)

julia> [p, q]([q, p]) #evaluate an array of Taylor1s on an array of Taylor1s
2-element Array{TaylorSeries.Taylor1{Float64},1}:
  0.8414709848078965 - 0.2701511529340699- 0.08267127702314792 t⁴ + 0.028036523686489453 t⁶ - 0.0019241418790800983 t⁸ - 0.0004838563431246892 t¹⁰ + 0.00013040017931079692 t¹² - 1.210231179202225e-5 t¹⁴ - 3.316644672471871e-7 t¹⁶ + 2.429814411744226e-7 t¹⁸ - 3.288747013801372e-8 t²⁰ + 1.8233411897292427e-9 t²² + 1.2200826998513497e-10 t²⁴ + 𝒪(t²⁶)
                                    1.0 - 0.5+ 0.20833333333333331 t⁴ - 0.05138888888888889 t⁶ + 0.011334325396825395 t⁸ - 0.0022511574074074074 t¹⁰ + 0.00039391308922558923 t¹² - 6.30631883732082e-5 t¹⁴ + 9.459548466503244e-6 t¹⁶ - 1.3341203587285381e-6 t¹⁸ + 1.777225182337089e-7 t²⁰ - 2.2544681245974032e-8 t²² + 2.739783349785652e-9 t²⁴ + 𝒪(t²⁶)

analogous things cannot currently be done for TaylorNs:

julia> using TaylorSeries

julia> dx = set_variables("x", numvars=4, order=10);

julia> v = [1.0,2,3,4];

julia> dx[1](v) #TaylorN evaluated at a Vector{Float64}
1.0

julia> dx(v) #Vector{TaylorN} evaluated at a Vector{Float64}
4-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0

julia> dx[1](dx)#TaylorN evaluated at a Vector{TaylorN}
ERROR: MethodError: no method matching evaluate(::TaylorSeries.TaylorN{Float64}, ::Array{TaylorSeries.TaylorN{Float64},1})
Closest candidates are:
  evaluate(::TaylorSeries.TaylorN{T<:Number}) where T<:Number at /Users/Jorge/forks/TaylorSeries.jl/src/evaluate.jl:211
  evaluate(::TaylorSeries.TaylorN{T<:Number}, ::Array{S<:Union{Complex, Real},1}) where {T<:Number, S<:Union{Complex, Real}} at /Users/Jorge/forks/TaylorSeries.jl/src/evaluate.jl:164
  evaluate(::TaylorSeries.TaylorN{T<:Number}, ::Array{TaylorSeries.Taylor1{S<:Union{Complex, Real}},1}) where {T<:Number, S<:Union{Complex, Real}} at /Users/Jorge/forks/TaylorSeries.jl/src/evaluate.jl:188
  ...
Stacktrace:
 [1] (::TaylorSeries.TaylorN{Float64})(::Array{TaylorSeries.TaylorN{Float64},1}) at /Users/Jorge/forks/TaylorSeries.jl/src/evaluate.jl:229

julia> dx(dx) #Vector{TaylorN} evaluated at a Vector{TaylorN}
ERROR: MethodError: no method matching evaluate(::Array{TaylorSeries.TaylorN{Float64},1}, ::Array{TaylorSeries.TaylorN{Float64},1})
Closest candidates are:
  evaluate(::Array{TaylorSeries.TaylorN{T<:Union{Complex, Real}},1}, ::Array{S<:Union{Complex, Real},1}) where {T<:Union{Complex, Real}, S<:Union{Complex, Real}} at /Users/Jorge/forks/TaylorSeries.jl/src/evaluate.jl:220
  evaluate(::Array{TaylorSeries.TaylorN{T<:Number},1}) where T<:Number at /Users/Jorge/forks/TaylorSeries.jl/src/evaluate.jl:226
  evaluate(::Array{TaylorSeries.TaylorN{T<:Number},1}, ::Array{T<:Number,1}) where T<:Number at /Users/Jorge/forks/TaylorSeries.jl/src/evaluate.jl:214
Stacktrace:
 [1] (::Array{TaylorSeries.TaylorN{Float64},1})(::Array{TaylorSeries.TaylorN{Float64},1}) at /Users/Jorge/forks/TaylorSeries.jl/src/evaluate.jl:234

I have some ideas on this that actually I was already testing while working on #118, so if there's interest I can submit a PR!

@lbenet
Copy link
Member

lbenet commented Aug 31, 2017

I though also about this some time ago, and while I think it would be nice, it is quite tricky because the way things are currently. But, please, go ahead and submit a PR.

@blas-ko
Copy link
Contributor

blas-ko commented Sep 7, 2017

Also thought of this! This would also be useful if we address the many variable update! functionality discussed in #80 and #121.

@lbenet
Copy link
Member

lbenet commented Oct 8, 2017

Solved by #121. Closing.

@lbenet lbenet closed this as completed Oct 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants