Skip to content

Instantly share code, notes, and snippets.

@lstagner
Last active March 8, 2016 21:55
Show Gist options
  • Save lstagner/83f91cc138d4767523a3 to your computer and use it in GitHub Desktop.
Save lstagner/83f91cc138d4767523a3 to your computer and use it in GitHub Desktop.
#=
The MIT License (MIT)
Copyright (c) 2015 Luke Stagner
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.
=#
type CubicSpline{T} # Cubic Hermite Spline
n::Int # Number of knots
x::Array{T,1} # x position of knots
y::Array{T,1} # y position of knots
m::Array{T,1} # Tangent at knots
end
function CubicSpline{T<:Real}(x::Array{T,1},y::Array{T,1})
issorted(x) || throw(ArgumentError("x points must be in ascending order"))
nx = length(x)
m = zeros(nx)
m[1] = (y[2] - y[1])/(x[2]-x[1])
for i=2:nx-1
m[i] = 0.5*((y[i+1]-y[i])/(x[i+1]-x[i]) + (y[i]-y[i-1])/(x[i]-x[i-1]))
end
m[nx] = (y[nx]-y[nx-1])/(x[nx]-x[nx-1])
return CubicSpline(nx,x,y,m)
end
function MonotoneCubicSpline{T<:Real}(x::Array{T,1},y::Array{T,1})
issorted(x) || throw(ArgumentError("x points must be in ascending order"))
nx = length(x)
m = zeros(nx)
m[1] = (y[2] - y[1])/(x[2]-x[1])
for i=2:nx-1
hi = x[i+1]-x[i]
hi_1 = x[i]-x[i-1]
di = (y[i+1]-y[i])/hi
di_1 = (y[i]-y[i-1])/hi_1
m[i] = sign(di) != sign(di_1) ? 0.0 : 3*(hi_1+hi)*(((2*hi+hi_1)/di_1)+((hi+2*hi_1)/di))^(-1.0)
end
m[nx] = (y[nx]-y[nx-1])/(x[nx]-x[nx-1])
return CubicSpline(nx,x,y,m)
end
function interpolate{T<:Real}(S::CubicSpline{T},x::Union(T,Array{T,1}))
xrange = extrema(S.x)
any((x .< xrange[1]) | (x .> xrange[2])) && throw(ArgumentError("Outside of Range"))
nx = length(x)
yout = zeros(nx)
for i = 1:nx
xr = searchsorted(S.x,x[i])
i1 = xr.stop
i2 = xr.start
if i1 != i2
dx = (S.x[i2] - S.x[i1])
t = (x[i] - S.x[i1])/dx
h00 = 2*t^3 - 3*t^2 + 1
h10 = t^3 - 2*t^2 + t
h01 = -2*t^3 + 3*t^2
h11 = t^3 - t^2
yout[i] = h00*S.y[i1] + h10*dx*S.m[i1] + h01*S.y[i2] + h11*dx*S.m[i2]
else
yout[i] = S.y[i1]
end
end
return yout
end
function deriv{T<:Real}(S::CubicSpline{T},x::Union(T,Array{T,1}))
xrange = extrema(S.x)
any((x .< xrange[1]) | (x .> xrange[2])) && throw(ArgumentError("Outside of Range"))
nx = length(x)
yout = zeros(nx)
for i = 1:nx
xr = searchsorted(S.x,x[i])
i1 = xr.stop
i2 = xr.start
if i1 != i2
dx = (S.x[i2] - S.x[i1])
t = (x[i] - S.x[i1])/dx
h00 = (6*t^2 - 6*t)/dx
h10 = (3*t^2 - 4*t + 1)/dx
h01 = (-6*t^2 + 6*t)/dx
h11 = (3*t^2 - 2*t)/dx
yout[i] = h00*S.y[i1] + h10*dx*S.m[i1] + h01*S.y[i2] + h11*dx*S.m[i2]
else
yout[i] = S.m[i1]
end
end
return yout
end
function hess{T<:Real}(S::CubicSpline{T},x::Union(T,Array{T,1}))
xrange = extrema(S.x)
any((x .< xrange[1]) | (x .> xrange[2])) && throw(ArgumentError("Outside of Range"))
nx = length(x)
yout = zeros(nx)
for i = 1:nx
xr = searchsorted(S.x,x[i])
i1 = xr.stop
i2 = xr.start
if i1 != i2
dx = (S.x[i2] - S.x[i1])
t = (x[i] - S.x[i1])/dx
h00 = (12*t - 6)/(dx*dx)
h10 = (6*t - 4)/(dx*dx)
h01 = (-12*t + 6)/(dx*dx)
h11 = (6*t - 2)/(dx*dx)
yout[i] = h00*S.y[i1] + h10*dx*S.m[i1] + h01*S.y[i2] + h11*dx*S.m[i2]
else
yout[i] = 0
end
end
return yout
end
@lstagner
Copy link
Author

lstagner commented Mar 8, 2016

I can believe I missed this comment. Its MIT Licensed so go right ahead

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