📄 spline3.cs
字号:
/*************************************************************************
Copyright (c) 2007, Sergey Bochkanov (ALGLIB project).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer listed
in this license in the documentation and/or other materials
provided with the distribution.
- Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
using System;
class spline3
{
/*************************************************************************
This subroutine builds linear spline coefficients table.
Input parameters:
X - spline nodes, array[0..N-1]
Y - function values, array[0..N-1]
N - points count, N>=2
Output parameters:
C - coefficients table. Used by SplineInterpolation and other
subroutines from this file.
-- ALGLIB PROJECT --
Copyright 24.06.2007 by Bochkanov Sergey
*************************************************************************/
public static void buildlinearspline(double[] x,
double[] y,
int n,
ref double[] c)
{
int i = 0;
int tblsize = 0;
x = (double[])x.Clone();
y = (double[])y.Clone();
System.Diagnostics.Debug.Assert(n>=2, "BuildLinearSpline: N<2!");
//
// Sort points
//
heapsortpoints(ref x, ref y, n);
//
// Fill C:
// C[0] - length(C)
// C[1] - type(C):
// 3 - general cubic spline
// C[2] - N
// C[3]...C[3+N-1] - x[i], i = 0...N-1
// C[3+N]...C[3+N+(N-1)*4-1] - coefficients table
//
tblsize = 3+n+(n-1)*4;
c = new double[tblsize-1+1];
c[0] = tblsize;
c[1] = 3;
c[2] = n;
for(i=0; i<=n-1; i++)
{
c[3+i] = x[i];
}
for(i=0; i<=n-2; i++)
{
c[3+n+4*i+0] = y[i];
c[3+n+4*i+1] = (y[i+1]-y[i])/(x[i+1]-x[i]);
c[3+n+4*i+2] = 0;
c[3+n+4*i+3] = 0;
}
}
/*************************************************************************
This subroutine builds cubic spline coefficients table.
Input parameters:
X - spline nodes, array[0..N-1]
Y - function values, array[0..N-1]
N - points count, N>=2
BoundLType - boundary condition type for the left boundary
BoundL - left boundary condition (first or second derivative,
depending on the BoundLType)
BoundRType - boundary condition type for the right boundary
BoundR - right boundary condition (first or second derivative,
depending on the BoundRType)
Output parameters:
C - coefficients table. Used by SplineInterpolation and
other subroutines from this file.
The BoundLType/BoundRType parameters can have the following values:
* 0, which corresponds to the parabolically terminated spline
(BoundL/BoundR are ignored).
* 1, which corresponds to the first derivative boundary condition
* 2, which corresponds to the second derivative boundary condition
-- ALGLIB PROJECT --
Copyright 23.06.2007 by Bochkanov Sergey
*************************************************************************/
public static void buildcubicspline(double[] x,
double[] y,
int n,
int boundltype,
double boundl,
int boundrtype,
double boundr,
ref double[] c)
{
double[] a1 = new double[0];
double[] a2 = new double[0];
double[] a3 = new double[0];
double[] b = new double[0];
double[] d = new double[0];
int i = 0;
int tblsize = 0;
double delta = 0;
double delta2 = 0;
double delta3 = 0;
x = (double[])x.Clone();
y = (double[])y.Clone();
System.Diagnostics.Debug.Assert(n>=2, "BuildCubicSpline: N<2!");
System.Diagnostics.Debug.Assert(boundltype==0 | boundltype==1 | boundltype==2, "BuildCubicSpline: incorrect BoundLType!");
System.Diagnostics.Debug.Assert(boundrtype==0 | boundrtype==1 | boundrtype==2, "BuildCubicSpline: incorrect BoundRType!");
a1 = new double[n-1+1];
a2 = new double[n-1+1];
a3 = new double[n-1+1];
b = new double[n-1+1];
//
// Special case:
// * N=2
// * parabolic terminated boundary condition on both ends
//
if( n==2 & boundltype==0 & boundrtype==0 )
{
//
// Change task type
//
boundltype = 2;
boundl = 0;
boundrtype = 2;
boundr = 0;
}
//
//
// Sort points
//
heapsortpoints(ref x, ref y, n);
//
// Left boundary conditions
//
if( boundltype==0 )
{
a1[0] = 0;
a2[0] = 1;
a3[0] = 1;
b[0] = 2*(y[1]-y[0])/(x[1]-x[0]);
}
if( boundltype==1 )
{
a1[0] = 0;
a2[0] = 1;
a3[0] = 0;
b[0] = boundl;
}
if( boundltype==2 )
{
a1[0] = 0;
a2[0] = 2;
a3[0] = 1;
b[0] = 3*(y[1]-y[0])/(x[1]-x[0])-0.5*boundl*(x[1]-x[0]);
}
//
// Central conditions
//
for(i=1; i<=n-2; i++)
{
a1[i] = x[i+1]-x[i];
a2[i] = 2*(x[i+1]-x[i-1]);
a3[i] = x[i]-x[i-1];
b[i] = 3*(y[i]-y[i-1])/(x[i]-x[i-1])*(x[i+1]-x[i])+3*(y[i+1]-y[i])/(x[i+1]-x[i])*(x[i]-x[i-1]);
}
//
// Right boundary conditions
//
if( boundrtype==0 )
{
a1[n-1] = 1;
a2[n-1] = 1;
a3[n-1] = 0;
b[n-1] = 2*(y[n-1]-y[n-2])/(x[n-1]-x[n-2]);
}
if( boundrtype==1 )
{
a1[n-1] = 0;
a2[n-1] = 1;
a3[n-1] = 0;
b[n-1] = boundr;
}
if( boundrtype==2 )
{
a1[n-1] = 1;
a2[n-1] = 2;
a3[n-1] = 0;
b[n-1] = 3*(y[n-1]-y[n-2])/(x[n-1]-x[n-2])+0.5*boundr*(x[n-1]-x[n-2]);
}
//
// Solve
//
solvetridiagonal(a1, a2, a3, b, n, ref d);
//
// Now problem is reduced to the cubic Hermite spline
//
buildhermitespline(x, y, d, n, ref c);
}
/*************************************************************************
This subroutine builds cubic Hermite spline coefficients table.
Input parameters:
X - spline nodes, array[0..N-1]
Y - function values, array[0..N-1]
D - derivatives, array[0..N-1]
N - points count, N>=2
Output parameters:
C - coefficients table. Used by SplineInterpolation and
other subroutines from this file.
-- ALGLIB PROJECT --
Copyright 23.06.2007 by Bochkanov Sergey
*************************************************************************/
public static void buildhermitespline(double[] x,
double[] y,
double[] d,
int n,
ref double[] c)
{
int i = 0;
int tblsize = 0;
double delta = 0;
double delta2 = 0;
double delta3 = 0;
x = (double[])x.Clone();
y = (double[])y.Clone();
d = (double[])d.Clone();
System.Diagnostics.Debug.Assert(n>=2, "BuildHermiteSpline: N<2!");
//
// Sort points
//
heapsortdpoints(ref x, ref y, ref d, n);
//
// Fill C:
// C[0] - length(C)
// C[1] - type(C):
// 3 - general cubic spline
// C[2] - N
// C[3]...C[3+N-1] - x[i], i = 0...N-1
// C[3+N]...C[3+N+(N-1)*4-1] - coefficients table
//
tblsize = 3+n+(n-1)*4;
c = new double[tblsize-1+1];
c[0] = tblsize;
c[1] = 3;
c[2] = n;
for(i=0; i<=n-1; i++)
{
c[3+i] = x[i];
}
for(i=0; i<=n-2; i++)
{
delta = x[i+1]-x[i];
delta2 = AP.Math.Sqr(delta);
delta3 = delta*delta2;
c[3+n+4*i+0] = y[i];
c[3+n+4*i+1] = d[i];
c[3+n+4*i+2] = (3*(y[i+1]-y[i])-2*d[i]*delta-d[i+1]*delta)/delta2;
c[3+n+4*i+3] = (2*(y[i]-y[i+1])+d[i]*delta+d[i+1]*delta)/delta3;
}
}
/*************************************************************************
This subroutine builds Akima spline coefficients table.
Input parameters:
X - spline nodes, array[0..N-1]
Y - function values, array[0..N-1]
N - points count, N>=5
Output parameters:
C - coefficients table. Used by SplineInterpolation and
other subroutines from this file.
-- ALGLIB PROJECT --
Copyright 24.06.2007 by Bochkanov Sergey
*************************************************************************/
public static void buildakimaspline(double[] x,
double[] y,
int n,
ref double[] c)
{
int i = 0;
double[] d = new double[0];
double[] w = new double[0];
double[] diff = new double[0];
x = (double[])x.Clone();
y = (double[])y.Clone();
System.Diagnostics.Debug.Assert(n>=5, "BuildAkimaSpline: N<5!");
//
// Sort points
//
heapsortpoints(ref x, ref y, n);
//
// Prepare W (weights), Diff (divided differences)
//
w = new double[n-2+1];
diff = new double[n-2+1];
for(i=0; i<=n-2; i++)
{
diff[i] = (y[i+1]-y[i])/(x[i+1]-x[i]);
}
for(i=1; i<=n-2; i++)
{
w[i] = Math.Abs(diff[i]-diff[i-1]);
}
//
// Prepare Hermite interpolation scheme
//
d = new double[n-1+1];
for(i=2; i<=n-3; i++)
{
if( Math.Abs(w[i-1])+Math.Abs(w[i+1])!=0 )
{
d[i] = (w[i+1]*diff[i-1]+w[i-1]*diff[i])/(w[i+1]+w[i-1]);
}
else
{
d[i] = ((x[i+1]-x[i])*diff[i-1]+(x[i]-x[i-1])*diff[i])/(x[i+1]-x[i-1]);
}
}
d[0] = diffthreepoint(x[0], x[0], y[0], x[1], y[1], x[2], y[2]);
d[1] = diffthreepoint(x[1], x[0], y[0], x[1], y[1], x[2], y[2]);
d[n-2] = diffthreepoint(x[n-2], x[n-3], y[n-3], x[n-2], y[n-2], x[n-1], y[n-1]);
d[n-1] = diffthreepoint(x[n-1], x[n-3], y[n-3], x[n-2], y[n-2], x[n-1], y[n-1]);
//
// Build Akima spline using Hermite interpolation scheme
//
buildhermitespline(x, y, d, n, ref c);
}
/*************************************************************************
This subroutine calculates the value of the spline at the given point X.
Input parameters:
C - coefficients table. Built by BuildLinearSpline,
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
X - point
Result:
S(x)
-- ALGLIB PROJECT --
Copyright 23.06.2007 by Bochkanov Sergey
*************************************************************************/
public static double splineinterpolation(ref double[] c,
double x)
{
double result = 0;
int n = 0;
int l = 0;
int r = 0;
int m = 0;
System.Diagnostics.Debug.Assert((int)Math.Round(c[1])==3, "SplineInterpolation: incorrect C!");
n = (int)Math.Round(c[2]);
//
// Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
//
l = 3;
r = 3+n-2+1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -