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

derivative of ParametricSpline is type-unstable and allocating #103

Open
stevengj opened this issue Feb 4, 2025 · 1 comment
Open

derivative of ParametricSpline is type-unstable and allocating #103

stevengj opened this issue Feb 4, 2025 · 1 comment

Comments

@stevengj
Copy link
Member

stevengj commented Feb 4, 2025

The return value of derivative is inferred as Any (i.e., it is type-unstable). This seems like a bug?

Furthermore, it allocates a Vector{Float64} for its return value. Since the dimensionality is known from the ParametricSpline type, couldn't we use SVector{2,Float64} instead? StaticArrays.jl is pretty widely used these days, so it is not crazy to add a dependency on it. (Alternatively, you could add an API that returns a tuple.)

julia> using Dierckx, Test

julia> spl = ParametricSpline(1:10, rand(2, 10))
ParametricSpline(knots=[1.0,3.0  8.0,10.0] (8 elements), k=3, extrapolation="nearest", residual=0.0)

julia> derivative(spl, 5.1) # allocates
2-element Vector{Float64}:
 -0.6756394220200724
  0.1203327362940756

julia> @inferred derivative(spl, 5.1) # type-unstable
ERROR: return type Vector{Float64} does not match inferred return type Any
@stevengj
Copy link
Member Author

stevengj commented Feb 4, 2025

Looks like the type-instability comes from the dims=[2] pattern that is used in the source, which is type-unstable because the length of the dims array is not known until runtime.

The fix should be simple: use dims=2 instead of dims=[2]. (Or if there are multiple dims, use a tuple.)

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

1 participant