JuliaMath / Interpolations.jl

Fast, continuous interpolation of discrete datasets in Julia
http://juliamath.github.io/Interpolations.jl/
Other
524 stars 110 forks source link

How to 3d parameter spline #460

Closed wssyj closed 2 years ago

wssyj commented 3 years ago

Sorry to disturb you.I am a beginner for your library, after seeing the part of parameter spline, I would like to ask how to use 3d parameter spline.

Here is your two-dimensional example:

using Interpolations
t = 0:.1:1
x = sin.(2π*t)
y = cos.(2π*t)
A = hcat(x,y)
itp = Interpolations.scale(interpolate(A, (BSpline(Cubic(Natural(OnGrid()))), NoInterp())), t, 1:2)
tfne = 0:.01:1
xs, ys = [itp(t,1) for t in tfine], [itp(t,2) for t in tfine]
mkitti commented 3 years ago
julia> A = rand(4,4,4)
4×4×4 Array{Float64, 3}:
[:, :, 1] =
 0.602447  0.961579  0.302673  0.0647496
 0.93965   0.959496  0.227365  0.66319
 0.331185  0.100806  0.920701  0.215262
 0.608488  0.518511  0.896318  0.668993

[:, :, 2] =
 0.081988  0.970282   0.306302  0.868053
 0.959003  0.414357   0.491382  0.385106
 0.247292  0.610567   0.629706  0.779416
 0.467251  0.0550997  0.405658  0.995051

[:, :, 3] =
 0.310974  0.840416  0.492628   0.490059
 0.510787  0.110757  0.0374218  0.643901
 0.509094  0.551814  0.289287   0.675418
 0.81481   0.845827  0.275486   0.314155

[:, :, 4] =
 0.67629   0.683418  0.645005  0.285094
 0.650935  0.550022  0.237134  0.152433
 0.368105  0.689606  0.708606  0.647533
 0.669374  0.76532   0.698094  0.789102

julia> nodes = ([x^2 for x in 1:4], [0.2y for y in 1:4], [z^3/2 for z in 1:4])
([1, 4, 9, 16], [0.2, 0.4, 0.6000000000000001, 0.8], [0.5, 4.0, 13.5, 32.0])

julia> itp = interpolate(nodes, A, Gridded(Linear()))
4×4×4 interpolate((::Vector{Int64},::Vector{Float64},::Vector{Float64}), ::Array{Float64, 3}, Gridded(Linear())) with element type Float64:
[:, :, 1] =
 0.602447  0.961579  0.302673  0.0647496
 0.93965   0.959496  0.227365  0.66319
 0.331185  0.100806  0.920701  0.215262
 0.608488  0.518511  0.896318  0.668993

[:, :, 2] =
 0.081988  0.970282   0.306302  0.868053
 0.959003  0.414357   0.491382  0.385106
 0.247292  0.610567   0.629706  0.779416
 0.467251  0.0550997  0.405658  0.995051

[:, :, 3] =
 0.310974  0.840416  0.492628   0.490059
 0.510787  0.110757  0.0374218  0.643901
 0.509094  0.551814  0.289287   0.675418
 0.81481   0.845827  0.275486   0.314155

[:, :, 4] =
 0.67629   0.683418  0.645005  0.285094
 0.650935  0.550022  0.237134  0.152433
 0.368105  0.689606  0.708606  0.647533
 0.669374  0.76532   0.698094  0.789102

julia> itp(2, 0.5, 2)
0.600909185850235

See http://juliamath.github.io/Interpolations.jl/latest/control/#Gridded-interpolation

Does this help?

wssyj commented 3 years ago

Thank you very much!!!

wssyj commented 3 years ago

Sorry to bother you again, but I have one more question For this example

using Interpolations
t = 0:.1:1
x = sin.(2π*t)
y = cos.(2π*t)
A = hcat(x,y)
itp = Interpolations.scale(interpolate(A, (BSpline(Cubic(Natural(OnGrid()))), NoInterp())), t, 1:2)
tfne = 0:.01:1
xs, ys = [itp(t,1) for t in tfine], [itp(t,2) for t in tfine]

x is not unique and sorted in increasing order.

How do I interpolate if the nodes in your example are not unique and not additive For example

nodes = ([1,3,1,0], [1,3,1,0], [z^3/2 for z in 1:4])
mkitti commented 3 years ago

If they are not sorted, then you can sort them. If the nodes are not unique, I'm not clear what you expect the value to be at the nodes.