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

Implement an expand function? #68

Closed
blas-ko opened this issue Oct 13, 2016 · 5 comments
Closed

Implement an expand function? #68

blas-ko opened this issue Oct 13, 2016 · 5 comments

Comments

@blas-ko
Copy link
Contributor

blas-ko commented Oct 13, 2016

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 like

function expand(f::Function,order::Int64=15)
    a = Taylor1(order)
    return f(a)
end

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

@PerezHz
Copy link
Contributor

PerezHz commented Oct 13, 2016

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+ 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- 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- 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- 0.07232916974242763+ 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- 0.07232916974242763+ 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)

@lbenet
Copy link
Member

lbenet commented Oct 18, 2016

What about defining it as a macro?

While I like the idea, I'm not sure whether expand or @expand are the best names; my point is that expand exists in Base, and actually has a quite different functionality.

@dpsanders What do you think?

@blas-ko
Copy link
Contributor Author

blas-ko commented Oct 19, 2016

Perhaps it would be helpful to be able to choose the initial point of expansion?

Sounds great, @PerezHz 👍
Maybe, so it doesn't clash with Base's expand, we can define it as expand_taylor or @expand_taylor. By the way, @lbenet, what would be the advantage of using a macro here instead of a normal function?

@lbenet
Copy link
Member

lbenet commented Oct 28, 2016

Sorry for replying this late... The advantage would be that the replacement would be done at parse time.

@lbenet
Copy link
Member

lbenet commented Oct 8, 2017

Implemented in #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