JuliaApproximation / ApproxFun.jl

Julia package for function approximation
http://juliaapproximation.github.io/ApproxFun.jl/
Other
528 stars 70 forks source link

Incorrect results for sin and cosine functions #939

Closed ay111 closed 2 months ago

ay111 commented 2 months ago

ApproxFun (version 0.13.26) returns incorrect values for sine and cosine of some known angles in Julia 1.10. See the examples below.

julia> using ApproxFun

julia> f = Fun(cos) Fun(Chebyshev(), [0.765198, 0.0, -0.229807, 0.0, 0.00495328, 0.0, -4.18767e-5, 0.0, 1.88447e-7, 0.0, -5.26123e-10, 0.0, 9.99916e-13])

julia> f(pi) # correct answer is -1 0.0

julia> f(2pi) # correct answer is 1 0.0

julia> g = Fun(sin) Fun(Chebyshev(), [0.0, 0.880101, 0.0, -0.0391267, 0.0, 0.000499515, 0.0, -3.00465e-6, 0.0, 1.04985e-8, 0.0, -2.39601e-11, 0.0, 3.85525e-14])

julia> g(pi/2) # correct answer is 1 0.0

julia> g(3pi/2) # correct answer is -1 0.0

jishnub commented 2 months ago

The issue here is that the values lie outside the domain, and a Fun does not extrapolate (it always evaluates to zero outside the domain). You probably want to define it on a larger domain. By default, the domain is -1..1, corresponding to the Chebyshev interval.

For example:

julia> g = Fun(sin, -2pi..2pi)
Fun(Chebyshev(-6.283185307179586 .. 6.283185307179586), [0.0, -0.424765, 0.0, -0.0582244, 0.0, 0.745649, 0.0, -0.315042, 0.0, 0.0582478  …  0.0, -1.38959e-11, 0.0, 2.35992e-13, 0.0, -3.4972e-15, 0.0, -2.60209e-17, 0.0, -6.93889e-17])

julia> g(pi/2)
1.0
ay111 commented 2 months ago

Thank you for the explanation.

On Fri, 5 Apr 2024, 5:46 pm Jishnu Bhattacharya, @.***> wrote:

The issue here is that the values lie outside the domain, and a Fun does not extrapolate (it always evaluates to zero outside the domain). You probably want to define it on a larger domain. For example:

julia> g = Fun(sin, -2pi..2pi)Fun(Chebyshev(-6.283185307179586 .. 6.283185307179586), [0.0, -0.424765, 0.0, -0.0582244, 0.0, 0.745649, 0.0, -0.315042, 0.0, 0.0582478 … 0.0, -1.38959e-11, 0.0, 2.35992e-13, 0.0, -3.4972e-15, 0.0, -2.60209e-17, 0.0, -6.93889e-17])

julia> g(pi/2)1.0

— Reply to this email directly, view it on GitHub https://github.com/JuliaApproximation/ApproxFun.jl/issues/939#issuecomment-2040134194, or unsubscribe https://github.com/notifications/unsubscribe-auth/AK4NBYYLH6DJ3PAVSURS7Q3Y33BNVAVCNFSM6AAAAABFZLOMLGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBQGEZTIMJZGQ . You are receiving this because you authored the thread.Message ID: @.***>