TaylorSeries.jl icon indicating copy to clipboard operation
TaylorSeries.jl copied to clipboard

Implement sinc

Open PerezHz opened this issue 8 years ago • 7 comments

(Edit: I corrected the expression for sinc(x); the way I originally wrote it was mistaken)

Currently (TaylorSeries v0.3.0.), one can implement $sinc(x)=sin(\pi x)/(\pi x)$ for Taylor1 variables manually, doing, for example

julia> using TaylorSeries #one of the most awesome Julia packages in the world

julia> import Base.sinc #so that we don't overwrite any methods for sinc

julia> sinc{T}(x::Taylor1{T}) = sin(pi*x)/(pi*x) #implementing manually sinc(x)=sin(pi*x)/(pi*x)
sinc (generic function with 7 methods)

julia> x = Taylor1([0.0, 1.0], 16) #a Taylor1 variable
 1.0 t + 𝒪(t¹⁷)

julia> sinc(x) #evaluate sinc for a Taylor1 variable
 1.0 - 1.6449340668482262 t² + 0.8117424252833536 t⁴ - 0.1907518241220842 t⁶ + 0.026147847817654793 t⁸ - 0.002346081035455823 t¹⁰ + 0.00014842879303107094 t¹² - 6.975873661656378e-6 t¹⁴ + 𝒪(t¹⁷)

But perhaps it would be nice to have this implemented within TaylorSeries without having to do it manually :)

PerezHz avatar Oct 11 '16 20:10 PerezHz

Indeed, it would be a good idea to implement it from scratch!

Cc: @blas-ko

lbenet avatar Oct 11 '16 20:10 lbenet

What do you mean by "from scratch"? Isn't the definition given by @PerezHz enough?

dpsanders avatar Oct 11 '16 21:10 dpsanders

What I mean is that you may write down a differential equation, whose solution is the sinc function. That differential equation will define a recursion formula which you can implement and get the result.

I agree that the solution of @PerezHz is good enough (though, does it work with TaylorN?), and eventually (if needed) the recursion formula could be implemented...

lbenet avatar Oct 11 '16 21:10 lbenet

My solution doesn't work for TaylorN :(

julia> using TaylorSeries

julia> x, y = set_variables("x y", order=25)
2-element Array{TaylorSeries.TaylorN{Float64},1}:
  1.0 x + 𝒪(‖x‖²⁶)
  1.0 y + 𝒪(‖x‖²⁶)

julia> sin(pi*x)/(pi*x)
ERROR: AssertionError: b0 != zero(b0)
 in /(::TaylorSeries.TaylorN{Float64}, ::TaylorSeries.TaylorN{Float64}) at /Users/Jorge/.julia/v0.5/TaylorSeries/src/TaylorN.jl:496

Would it be feasible to implement sinc for TaylorN's using a recursion formula as you described, @lbenet ?

PerezHz avatar Oct 12 '16 05:10 PerezHz

Why sin(x*pi) and not sinpi(x)?

freemin7 avatar Aug 02 '20 12:08 freemin7

Why sin(x*pi) and not sinpi(x)?

Thanks for bringing this up @freemin7 ! The short answer, I think, is because we haven't implemented sinpi and related functions. As far as I know, sinpi(x) is more accurate than sin(pi*x), but I do not know the details. You are welcome to make a pull request!

lbenet avatar Aug 02 '20 14:08 lbenet

Would it be feasible to implement sinc for TaylorN's using a recursion formula as you described, @lbenet ?

@PerezHz Sorry for having overlooked this, and getting back too it sooo late. First, the reason that it doesn't work for TaylorN essentially is because the division for Taylor1 allows "to factorize", whereas this is not (yet) possible to do for TaylorN. Now, would it work if we implement it from a "differential equation" (recursion formula); I think so, but I do not know.

lbenet avatar Aug 02 '20 14:08 lbenet