Skip to content

Instantly share code, notes, and snippets.

@karajan9
Created July 26, 2019 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karajan9/f15c49be6d25409d1eea0358c7a44a35 to your computer and use it in GitHub Desktop.
Save karajan9/f15c49be6d25409d1eea0358c7a44a35 to your computer and use it in GitHub Desktop.
Interpolations on an irregular grid
#=
The MIT License (MIT)
Copyright (c) 2015: Luke Stagner, Jens Adam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=#
module IrregularInterpolate
using LinearAlgebra
export PolyharmonicSpline, interpolate
struct PolyharmonicSpline{T}
dim::Int
order::Int
coeff::Vector{T}
centers::Matrix{T}
error::T
end
function polyharmonicK(r, K)
if iseven(K)
return r < 1 ? r^(K-1) * log(r^r) : (r^K) * log(r)
else
return r^K
end
end
function PolyharmonicSpline(K::Int, centers::Matrix{T}, values::Array{T}; s = zero(T)) where {T}
N, dim = size(centers)
N != length(values) && throw(DimensionMismatch())
phK = Matrix{T}(undef, N, N)
for i in 1:N
for j in 1:N
dist = 0.0
for d in 1:dim
dist += (centers[i, d] - centers[j, d])^2
end
phK[i, j] = polyharmonicK(sqrt(dist), K)
end
end
A = copy(phK)
B = zeros(T, N, dim+1)
for i in 1:N
B[i, 1] = 1
B[i, 2:end] = centers[i, :]
end
A .+= s .* Diagonal(I, N)
L = [A B; B' zeros(T, dim+1, dim+1)]
w = pinv(L) * [values; zeros(T, dim+1)]
ivalues = zeros(T, N)
for i in 1:N
tmp = 0.0
for j in 1:N
tmp += w[j] * phK[i, j]
end
tmp += w[N+1]
for j in 2:dim+1
tmp += w[N+j] * centers[i, j-1]
end
ivalues[i] = tmp
end
error = norm(values .- ivalues)
return PolyharmonicSpline(dim, K, w, centers, error)
end
function PolyharmonicSpline(K::Int, centers::Vector{T}, values::Vector{T}; s = zero(T)) where {T}
# PolyharmonicSpline(K, centers'', values, s = s)
PolyharmonicSpline(K, centers, values, s = s)
end
function interpolate(S::PolyharmonicSpline{T}, x::Matrix{T}) where {T}
m, n = size(x)
n != S.dim && throw(DimensionMismatch("$m != $(S.dim)"))
l = length(S.coeff) - (n+1)
interpolates = zeros(T, m)
for i in 1:m
tmp = 0.0
for j in 1:l
dist = 0.0
for d in 1:n
dist += (x[i, d] - S.centers[j, d])^2
end
tmp += S.coeff[j] * polyharmonicK(sqrt(dist), S.order)
end
tmp += S.coeff[l+1]
for j in 2:n+1
tmp += S.coeff[l+j] * x[i, j-1]
end
interpolates[i] = tmp
end
return interpolates
end
function interpolate(S::PolyharmonicSpline{T}, x::Vector{T}) where {T}
return interpolate(S, x)
end
function interpolate(S::PolyharmonicSpline{T}, x::Vector{T}, y::Vector{T}) where {T}
return interpolate(S, [x y])
end
function interpolate(S::PolyharmonicSpline{T}, x::Vector{T}, y::Vector{T}, z::Vector{T}) where {T}
return interpolate(S, [x y z])
end
end # module
@karajan9
Copy link
Author

Ported from this script.

Example usage:

f(x, y, z) = (x - y - 1.0) + exp(z)
x = rand(100)
y = rand(100)
z = rand(100)
a = f.(x, y, z)

p = PolyharmonicSpline(3, [x y z], a)  # 3 is the order

x2 = rand(10)
y2 = rand(10)
z2 = rand(10)
interpolate(p, [x2 y2 z2])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment