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 function-like behavior for Taylor1, HomogeneousPolynomial and TaylorN? #116

Closed
PerezHz opened this issue Aug 22, 2017 · 5 comments
Closed

Comments

@PerezHz
Copy link
Contributor

PerezHz commented Aug 22, 2017

Hi! I was reading the Function-like objects section of the Julia language documentation, and also @dpsanders's recent blog post, and thought that perhaps it would be nice to add function-like behavior for Taylor1, TaylorN and HomogeneousPolynomial variables? My idea is to implement something like:

julia> using TaylorSeries

julia> t = Taylor1(16)
 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¹⁵ + 𝒪(t¹⁷)

julia> p(0.1)
ERROR: MethodError: objects of type TaylorSeries.Taylor1{Float64} are not callable

julia> (p::Taylor1)(x) = evaluate(p, x)

julia> p(0.1)
0.09983341664682815

julia> sin(0.1) #just comparing last answer to the actual result of evaluating sin at 0.1
0.09983341664682815

And similar stuff for HomogeneousPolynomials and TaylorNs... If you think this is a worthwhile feature to add to TaylorSeries, I'd be more than happy to submit a PR! 😄

@lbenet
Copy link
Member

lbenet commented Aug 22, 2017

It sounds as it could be interesting. Any comments or ideas, @dpsanders ?

@dpsanders
Copy link
Contributor

Seems like a good idea! (Is it common to evaluate Taylor series like this?)

@PerezHz
Copy link
Contributor Author

PerezHz commented Aug 25, 2017

Thanks for the feedback! Well I guess it wouldn't hurt to be able to do p(0.1) as a shorter (and perhaps more legible) alternative to evaluate(p, 0.1), where p is a Taylor1 (and equivalent things for TaylorNs). Moreover, mathematically I think it does make sense to think about p(0.1) as the value of a polynomial function p at 0.1 (is that the question you're asking @dpsanders or did I misunderstand you?)

This notation is helpful, for example, when working with Legendre polynomials. Using this trick, one would be able to do things such as:

julia> using TaylorSeries

julia> t = Taylor1(16) #the independent variable
 1.0 t + 𝒪(t¹⁷)

julia> P_2 = (5t^3-3t)/2 #2nd-degree Legendre polynomial
 - 1.5 t + 2.5+ 𝒪(t¹⁷)

julia> (p::Taylor1)(x) = evaluate(p, x) #add function-like behavior for Taylor1s

julia> P_2
 - 1.5 t + 2.5+ 𝒪(t¹⁷)

julia> P_2(-1.0) #what is the value of P_2 at -1.0?
-1.0

julia> P_2(cos(t)) #what is the Taylor expansion of P_2(cos(t)), up to 16th order?
 1.0 - 3.0+ 2.125 t⁴ - 0.6333333333333333 t⁶ + 0.10171130952380952 t⁸ - 0.010170304232804232 t¹⁰ + 0.0006934235710277377 t¹² - 3.429014217704694e-5 t¹⁴ + 1.2858801882549402e-6 t¹⁶ + 𝒪(t¹⁷)

Thus, we get a nice, legible interface for evaluating polynomials, thanks to the design of evaluate

@blas-ko
Copy link
Contributor

blas-ko commented Aug 25, 2017

I like the idea too! I think it becomes much more clear! It's also natural to extend the definition into TaylorN polynomials

using TaylorSeries

julia> xN = TaylorN(2) + TaylorN(1)
1.0 x₁ + 1.0 x₂ + 𝒪(‖x‖⁷)

julia> (p::TaylorN)(x) = evaluate(p,x)

julia> xN([1,2])
3.0

@lbenet
Copy link
Member

lbenet commented Sep 29, 2017

#118 is merged, so I am closing this.

@lbenet lbenet closed this as completed Sep 29, 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

4 participants