-
Notifications
You must be signed in to change notification settings - Fork 53
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
Implement an expand
function?
#68
Comments
Sounds interesting! julia> import Base.expand
julia> function expand(f::Function,order::Int64=15)
a = Taylor1(order)
return f(a)
end
expand (generic function with 3 methods)
julia> expand(sin) #it works!
1.0 t - 0.16666666666666666 t³ + 0.008333333333333333 t⁵ - 0.0001984126984126984 t⁷ + 2.7557319223985893e-6 t⁹ - 2.505210838544172e-8 t¹¹ + 1.6059043836821616e-10 t¹³ - 7.647163731819817e-13 t¹⁵ + 𝒪(t¹⁶)
julia> expand(λ->sin(cos(λ))) #it works for compositions of functions, too!
0.8414709848078965 - 0.2701511529340699 t² - 0.08267127702314792 t⁴ + 0.028036523686489446 t⁶ - 0.0019241418790800977 t⁸ - 0.0004838563431246893 t¹⁰ + 0.00013040017931079692 t¹² - 1.2102311792022246e-5 t¹⁴ + 𝒪(t¹⁶)
julia> x = Taylor1(15)
1.0 t + 𝒪(t¹⁶)
julia> sin(cos(x))
0.8414709848078965 - 0.2701511529340699 t² - 0.08267127702314792 t⁴ + 0.028036523686489446 t⁶ - 0.0019241418790800977 t⁸ - 0.0004838563431246893 t¹⁰ + 0.00013040017931079692 t¹² - 1.2102311792022246e-5 t¹⁴ + 𝒪(t¹⁶)
julia> sin(cos(x)) == expand(λ->sin(cos(λ)))
true Perhaps it would be helpful to be able to choose the initial point of expansion? Otherwise, it will always expand around zero. I mean something like: julia> using TaylorSeries
julia> import Base.expand
julia> function expand{T<:Number}(f::Function, x0::T, order::Int64=15) #a Taylor expansion around x0
a = Taylor1([x0, one(x0)], order)
return f(a)
end
expand (generic function with 3 methods)
julia> expand(λ->sin(cos(λ)), -1000.0)
0.5332003773089166 + 0.6995309807177265 t - 0.4201657380038325 t² - 0.07232916974242763 t³ + 0.15121493557921284 t⁴ - 0.02436406884321303 t⁵ - 0.04058717887907187 t⁶ + 0.011469509530976701 t⁷ + 0.006593656037068762 t⁸ - 0.0032816768978896783 t⁹ - 0.000619734660543075 t¹⁰ + 0.0007119205288158886 t¹¹ + 2.058485176905429e-6 t¹² - 0.00011987116840574068 t¹³ + 1.4786492212018731e-5 t¹⁴ + 1.6049672402596366e-5 t¹⁵ + 𝒪(t¹⁶)
julia> sin(cos(-1000.0)) #The 0-th order Taylor coeff must be equal to the function evaluated at the point of expansion
0.5332003773089166
julia> x = Taylor1([-1000.0, 1.0], 15)
- 1000.0 + 1.0 t + 𝒪(t¹⁶)
julia> sin(cos(x))
0.5332003773089166 + 0.6995309807177265 t - 0.4201657380038325 t² - 0.07232916974242763 t³ + 0.15121493557921284 t⁴ - 0.02436406884321303 t⁵ - 0.04058717887907187 t⁶ + 0.011469509530976701 t⁷ + 0.006593656037068762 t⁸ - 0.0032816768978896783 t⁹ - 0.000619734660543075 t¹⁰ + 0.0007119205288158886 t¹¹ + 2.058485176905429e-6 t¹² - 0.00011987116840574068 t¹³ + 1.4786492212018731e-5 t¹⁴ + 1.6049672402596366e-5 t¹⁵ + 𝒪(t¹⁶)
julia> sin(cos(x)) == expand(λ->sin(cos(λ)), -1000.0) #test whether what we're doing is consistent
true I really like the idea! (I did this on julia 0.5.0) |
What about defining it as a macro? While I like the idea, I'm not sure whether @dpsanders What do you think? |
Sounds great, @PerezHz 👍 |
Sorry for replying this late... The advantage would be that the replacement would be done at parse time. |
Implemented in #121. Closing! |
I was thinking of implementing a function so it expands an independent variable of a given order around a function
f
? This could be something likeI don't know if it's very efficient because the function doesn't know a priori the output's type, but I think it's practical for
TaylorSeries
users.The text was updated successfully, but these errors were encountered: