alglib 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/Manifest.txt +253 -0
- data/README.txt +33 -0
- data/Rakefile +27 -0
- data/ext/Rakefile +24 -0
- data/ext/alglib.i +24 -0
- data/ext/alglib/Makefile +157 -0
- data/ext/alglib/airyf.cpp +372 -0
- data/ext/alglib/airyf.h +81 -0
- data/ext/alglib/alglib.cpp +8558 -0
- data/ext/alglib/alglib_util.cpp +19 -0
- data/ext/alglib/alglib_util.h +14 -0
- data/ext/alglib/ap.cpp +877 -0
- data/ext/alglib/ap.english.html +364 -0
- data/ext/alglib/ap.h +666 -0
- data/ext/alglib/ap.russian.html +442 -0
- data/ext/alglib/apvt.h +754 -0
- data/ext/alglib/bdss.cpp +1500 -0
- data/ext/alglib/bdss.h +251 -0
- data/ext/alglib/bdsvd.cpp +1339 -0
- data/ext/alglib/bdsvd.h +164 -0
- data/ext/alglib/bessel.cpp +1226 -0
- data/ext/alglib/bessel.h +331 -0
- data/ext/alglib/betaf.cpp +105 -0
- data/ext/alglib/betaf.h +74 -0
- data/ext/alglib/bidiagonal.cpp +1328 -0
- data/ext/alglib/bidiagonal.h +350 -0
- data/ext/alglib/binomialdistr.cpp +247 -0
- data/ext/alglib/binomialdistr.h +153 -0
- data/ext/alglib/blas.cpp +576 -0
- data/ext/alglib/blas.h +132 -0
- data/ext/alglib/cblas.cpp +226 -0
- data/ext/alglib/cblas.h +57 -0
- data/ext/alglib/cdet.cpp +138 -0
- data/ext/alglib/cdet.h +92 -0
- data/ext/alglib/chebyshev.cpp +216 -0
- data/ext/alglib/chebyshev.h +76 -0
- data/ext/alglib/chisquaredistr.cpp +157 -0
- data/ext/alglib/chisquaredistr.h +144 -0
- data/ext/alglib/cholesky.cpp +285 -0
- data/ext/alglib/cholesky.h +86 -0
- data/ext/alglib/cinverse.cpp +298 -0
- data/ext/alglib/cinverse.h +111 -0
- data/ext/alglib/clu.cpp +337 -0
- data/ext/alglib/clu.h +120 -0
- data/ext/alglib/correlation.cpp +280 -0
- data/ext/alglib/correlation.h +77 -0
- data/ext/alglib/correlationtests.cpp +726 -0
- data/ext/alglib/correlationtests.h +134 -0
- data/ext/alglib/crcond.cpp +826 -0
- data/ext/alglib/crcond.h +148 -0
- data/ext/alglib/creflections.cpp +310 -0
- data/ext/alglib/creflections.h +165 -0
- data/ext/alglib/csolve.cpp +312 -0
- data/ext/alglib/csolve.h +99 -0
- data/ext/alglib/ctrinverse.cpp +387 -0
- data/ext/alglib/ctrinverse.h +98 -0
- data/ext/alglib/ctrlinsolve.cpp +297 -0
- data/ext/alglib/ctrlinsolve.h +81 -0
- data/ext/alglib/dawson.cpp +234 -0
- data/ext/alglib/dawson.h +74 -0
- data/ext/alglib/descriptivestatistics.cpp +436 -0
- data/ext/alglib/descriptivestatistics.h +112 -0
- data/ext/alglib/det.cpp +140 -0
- data/ext/alglib/det.h +94 -0
- data/ext/alglib/dforest.cpp +1819 -0
- data/ext/alglib/dforest.h +316 -0
- data/ext/alglib/elliptic.cpp +497 -0
- data/ext/alglib/elliptic.h +217 -0
- data/ext/alglib/estnorm.cpp +429 -0
- data/ext/alglib/estnorm.h +107 -0
- data/ext/alglib/expintegrals.cpp +422 -0
- data/ext/alglib/expintegrals.h +108 -0
- data/ext/alglib/faq.english.html +258 -0
- data/ext/alglib/faq.russian.html +272 -0
- data/ext/alglib/fdistr.cpp +202 -0
- data/ext/alglib/fdistr.h +163 -0
- data/ext/alglib/fresnel.cpp +211 -0
- data/ext/alglib/fresnel.h +91 -0
- data/ext/alglib/gammaf.cpp +338 -0
- data/ext/alglib/gammaf.h +104 -0
- data/ext/alglib/gqgengauss.cpp +235 -0
- data/ext/alglib/gqgengauss.h +92 -0
- data/ext/alglib/gqgenhermite.cpp +268 -0
- data/ext/alglib/gqgenhermite.h +63 -0
- data/ext/alglib/gqgenjacobi.cpp +297 -0
- data/ext/alglib/gqgenjacobi.h +72 -0
- data/ext/alglib/gqgenlaguerre.cpp +265 -0
- data/ext/alglib/gqgenlaguerre.h +69 -0
- data/ext/alglib/gqgenlegendre.cpp +300 -0
- data/ext/alglib/gqgenlegendre.h +62 -0
- data/ext/alglib/gqgenlobatto.cpp +305 -0
- data/ext/alglib/gqgenlobatto.h +97 -0
- data/ext/alglib/gqgenradau.cpp +232 -0
- data/ext/alglib/gqgenradau.h +95 -0
- data/ext/alglib/hbisinv.cpp +480 -0
- data/ext/alglib/hbisinv.h +183 -0
- data/ext/alglib/hblas.cpp +228 -0
- data/ext/alglib/hblas.h +64 -0
- data/ext/alglib/hcholesky.cpp +339 -0
- data/ext/alglib/hcholesky.h +91 -0
- data/ext/alglib/hermite.cpp +114 -0
- data/ext/alglib/hermite.h +49 -0
- data/ext/alglib/hessenberg.cpp +370 -0
- data/ext/alglib/hessenberg.h +152 -0
- data/ext/alglib/hevd.cpp +247 -0
- data/ext/alglib/hevd.h +107 -0
- data/ext/alglib/hsschur.cpp +1316 -0
- data/ext/alglib/hsschur.h +108 -0
- data/ext/alglib/htridiagonal.cpp +734 -0
- data/ext/alglib/htridiagonal.h +180 -0
- data/ext/alglib/ialglib.cpp +6 -0
- data/ext/alglib/ialglib.h +9 -0
- data/ext/alglib/ibetaf.cpp +960 -0
- data/ext/alglib/ibetaf.h +125 -0
- data/ext/alglib/igammaf.cpp +430 -0
- data/ext/alglib/igammaf.h +157 -0
- data/ext/alglib/inv.cpp +274 -0
- data/ext/alglib/inv.h +115 -0
- data/ext/alglib/inverseupdate.cpp +480 -0
- data/ext/alglib/inverseupdate.h +185 -0
- data/ext/alglib/jacobianelliptic.cpp +164 -0
- data/ext/alglib/jacobianelliptic.h +94 -0
- data/ext/alglib/jarquebera.cpp +2271 -0
- data/ext/alglib/jarquebera.h +80 -0
- data/ext/alglib/kmeans.cpp +356 -0
- data/ext/alglib/kmeans.h +76 -0
- data/ext/alglib/laguerre.cpp +94 -0
- data/ext/alglib/laguerre.h +48 -0
- data/ext/alglib/lbfgs.cpp +1167 -0
- data/ext/alglib/lbfgs.h +218 -0
- data/ext/alglib/lda.cpp +434 -0
- data/ext/alglib/lda.h +133 -0
- data/ext/alglib/ldlt.cpp +1130 -0
- data/ext/alglib/ldlt.h +124 -0
- data/ext/alglib/leastsquares.cpp +1252 -0
- data/ext/alglib/leastsquares.h +290 -0
- data/ext/alglib/legendre.cpp +107 -0
- data/ext/alglib/legendre.h +49 -0
- data/ext/alglib/linreg.cpp +1185 -0
- data/ext/alglib/linreg.h +380 -0
- data/ext/alglib/logit.cpp +1523 -0
- data/ext/alglib/logit.h +333 -0
- data/ext/alglib/lq.cpp +399 -0
- data/ext/alglib/lq.h +160 -0
- data/ext/alglib/lu.cpp +462 -0
- data/ext/alglib/lu.h +119 -0
- data/ext/alglib/mannwhitneyu.cpp +4490 -0
- data/ext/alglib/mannwhitneyu.h +115 -0
- data/ext/alglib/minlm.cpp +918 -0
- data/ext/alglib/minlm.h +312 -0
- data/ext/alglib/mlpbase.cpp +3375 -0
- data/ext/alglib/mlpbase.h +589 -0
- data/ext/alglib/mlpe.cpp +1369 -0
- data/ext/alglib/mlpe.h +552 -0
- data/ext/alglib/mlptrain.cpp +1056 -0
- data/ext/alglib/mlptrain.h +283 -0
- data/ext/alglib/nearunityunit.cpp +91 -0
- data/ext/alglib/nearunityunit.h +17 -0
- data/ext/alglib/normaldistr.cpp +377 -0
- data/ext/alglib/normaldistr.h +175 -0
- data/ext/alglib/nsevd.cpp +1869 -0
- data/ext/alglib/nsevd.h +140 -0
- data/ext/alglib/pca.cpp +168 -0
- data/ext/alglib/pca.h +87 -0
- data/ext/alglib/poissondistr.cpp +143 -0
- data/ext/alglib/poissondistr.h +130 -0
- data/ext/alglib/polinterpolation.cpp +685 -0
- data/ext/alglib/polinterpolation.h +206 -0
- data/ext/alglib/psif.cpp +173 -0
- data/ext/alglib/psif.h +88 -0
- data/ext/alglib/qr.cpp +414 -0
- data/ext/alglib/qr.h +168 -0
- data/ext/alglib/ratinterpolation.cpp +134 -0
- data/ext/alglib/ratinterpolation.h +72 -0
- data/ext/alglib/rcond.cpp +705 -0
- data/ext/alglib/rcond.h +140 -0
- data/ext/alglib/reflections.cpp +504 -0
- data/ext/alglib/reflections.h +165 -0
- data/ext/alglib/rotations.cpp +473 -0
- data/ext/alglib/rotations.h +128 -0
- data/ext/alglib/rsolve.cpp +221 -0
- data/ext/alglib/rsolve.h +99 -0
- data/ext/alglib/sbisinv.cpp +217 -0
- data/ext/alglib/sbisinv.h +171 -0
- data/ext/alglib/sblas.cpp +185 -0
- data/ext/alglib/sblas.h +64 -0
- data/ext/alglib/schur.cpp +156 -0
- data/ext/alglib/schur.h +102 -0
- data/ext/alglib/sdet.cpp +193 -0
- data/ext/alglib/sdet.h +101 -0
- data/ext/alglib/sevd.cpp +116 -0
- data/ext/alglib/sevd.h +99 -0
- data/ext/alglib/sinverse.cpp +672 -0
- data/ext/alglib/sinverse.h +138 -0
- data/ext/alglib/spddet.cpp +138 -0
- data/ext/alglib/spddet.h +96 -0
- data/ext/alglib/spdgevd.cpp +842 -0
- data/ext/alglib/spdgevd.h +200 -0
- data/ext/alglib/spdinverse.cpp +509 -0
- data/ext/alglib/spdinverse.h +122 -0
- data/ext/alglib/spdrcond.cpp +421 -0
- data/ext/alglib/spdrcond.h +118 -0
- data/ext/alglib/spdsolve.cpp +275 -0
- data/ext/alglib/spdsolve.h +105 -0
- data/ext/alglib/spline2d.cpp +1192 -0
- data/ext/alglib/spline2d.h +301 -0
- data/ext/alglib/spline3.cpp +1264 -0
- data/ext/alglib/spline3.h +290 -0
- data/ext/alglib/srcond.cpp +595 -0
- data/ext/alglib/srcond.h +127 -0
- data/ext/alglib/ssolve.cpp +895 -0
- data/ext/alglib/ssolve.h +139 -0
- data/ext/alglib/stdafx.h +0 -0
- data/ext/alglib/stest.cpp +131 -0
- data/ext/alglib/stest.h +94 -0
- data/ext/alglib/studenttdistr.cpp +222 -0
- data/ext/alglib/studenttdistr.h +115 -0
- data/ext/alglib/studentttests.cpp +377 -0
- data/ext/alglib/studentttests.h +178 -0
- data/ext/alglib/svd.cpp +620 -0
- data/ext/alglib/svd.h +126 -0
- data/ext/alglib/tdbisinv.cpp +2608 -0
- data/ext/alglib/tdbisinv.h +228 -0
- data/ext/alglib/tdevd.cpp +1229 -0
- data/ext/alglib/tdevd.h +115 -0
- data/ext/alglib/tridiagonal.cpp +594 -0
- data/ext/alglib/tridiagonal.h +171 -0
- data/ext/alglib/trigintegrals.cpp +490 -0
- data/ext/alglib/trigintegrals.h +131 -0
- data/ext/alglib/trinverse.cpp +345 -0
- data/ext/alglib/trinverse.h +98 -0
- data/ext/alglib/trlinsolve.cpp +926 -0
- data/ext/alglib/trlinsolve.h +73 -0
- data/ext/alglib/tsort.cpp +405 -0
- data/ext/alglib/tsort.h +54 -0
- data/ext/alglib/variancetests.cpp +245 -0
- data/ext/alglib/variancetests.h +134 -0
- data/ext/alglib/wsr.cpp +6285 -0
- data/ext/alglib/wsr.h +96 -0
- data/ext/ap.i +97 -0
- data/ext/correlation.i +24 -0
- data/ext/extconf.rb +6 -0
- data/ext/logit.i +89 -0
- data/lib/alglib.rb +71 -0
- data/lib/alglib/correlation.rb +26 -0
- data/lib/alglib/linearregression.rb +63 -0
- data/lib/alglib/logit.rb +42 -0
- data/test/test_alglib.rb +52 -0
- data/test/test_correlation.rb +44 -0
- data/test/test_correlationtest.rb +45 -0
- data/test/test_linreg.rb +35 -0
- data/test/test_logit.rb +43 -0
- data/test/test_pca.rb +27 -0
- metadata +326 -0
@@ -0,0 +1,301 @@
|
|
1
|
+
/*************************************************************************
|
2
|
+
Copyright (c) 2007, Sergey Bochkanov (ALGLIB project).
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are
|
6
|
+
met:
|
7
|
+
|
8
|
+
- Redistributions of source code must retain the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
- Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer listed
|
13
|
+
in this license in the documentation and/or other materials
|
14
|
+
provided with the distribution.
|
15
|
+
|
16
|
+
- Neither the name of the copyright holders nor the names of its
|
17
|
+
contributors may be used to endorse or promote products derived from
|
18
|
+
this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*************************************************************************/
|
32
|
+
|
33
|
+
#ifndef _spline2d_h
|
34
|
+
#define _spline2d_h
|
35
|
+
|
36
|
+
#include "ap.h"
|
37
|
+
#include "ialglib.h"
|
38
|
+
|
39
|
+
#include "spline3.h"
|
40
|
+
|
41
|
+
|
42
|
+
/*************************************************************************
|
43
|
+
This subroutine builds bilinear spline coefficients table.
|
44
|
+
|
45
|
+
Input parameters:
|
46
|
+
X - spline abscissas, array[0..N-1]
|
47
|
+
Y - spline ordinates, array[0..M-1]
|
48
|
+
F - function values, array[0..M-1,0..N-1]
|
49
|
+
M,N - grid size, M>=2, N>=2
|
50
|
+
|
51
|
+
Output parameters:
|
52
|
+
C - coefficients table. Used by SplineInterpolation2D and other
|
53
|
+
subroutines from this file.
|
54
|
+
|
55
|
+
-- ALGLIB PROJECT --
|
56
|
+
Copyright 05.07.2007 by Bochkanov Sergey
|
57
|
+
*************************************************************************/
|
58
|
+
void buildbilinearspline(ap::real_1d_array x,
|
59
|
+
ap::real_1d_array y,
|
60
|
+
ap::real_2d_array f,
|
61
|
+
int m,
|
62
|
+
int n,
|
63
|
+
ap::real_1d_array& c);
|
64
|
+
|
65
|
+
|
66
|
+
/*************************************************************************
|
67
|
+
This subroutine builds bicubic spline coefficients table.
|
68
|
+
|
69
|
+
Input parameters:
|
70
|
+
X - spline abscissas, array[0..N-1]
|
71
|
+
Y - spline ordinates, array[0..M-1]
|
72
|
+
F - function values, array[0..M-1,0..N-1]
|
73
|
+
M,N - grid size, M>=2, N>=2
|
74
|
+
|
75
|
+
Output parameters:
|
76
|
+
C - coefficients table. Used by SplineInterpolation2D and other
|
77
|
+
subroutines from this file.
|
78
|
+
|
79
|
+
-- ALGLIB PROJECT --
|
80
|
+
Copyright 05.07.2007 by Bochkanov Sergey
|
81
|
+
*************************************************************************/
|
82
|
+
void buildbicubicspline(ap::real_1d_array x,
|
83
|
+
ap::real_1d_array y,
|
84
|
+
ap::real_2d_array f,
|
85
|
+
int m,
|
86
|
+
int n,
|
87
|
+
ap::real_1d_array& c);
|
88
|
+
|
89
|
+
|
90
|
+
/*************************************************************************
|
91
|
+
This subroutine calculates the value of the bilinear or bicubic spline at
|
92
|
+
the given point X.
|
93
|
+
|
94
|
+
Input parameters:
|
95
|
+
C - coefficients table.
|
96
|
+
Built by BuildBilinearSpline or BuildBicubicSpline.
|
97
|
+
X, Y- point
|
98
|
+
|
99
|
+
Result:
|
100
|
+
S(x,y)
|
101
|
+
|
102
|
+
-- ALGLIB PROJECT --
|
103
|
+
Copyright 05.07.2007 by Bochkanov Sergey
|
104
|
+
*************************************************************************/
|
105
|
+
double splineinterpolation2d(const ap::real_1d_array& c, double x, double y);
|
106
|
+
|
107
|
+
|
108
|
+
/*************************************************************************
|
109
|
+
This subroutine calculates the value of the bilinear or bicubic spline at
|
110
|
+
the given point X and its derivatives.
|
111
|
+
|
112
|
+
Input parameters:
|
113
|
+
C - coefficients table.
|
114
|
+
Built by BuildBilinearSpline or BuildBicubicSpline.
|
115
|
+
X, Y- point
|
116
|
+
|
117
|
+
Output parameters:
|
118
|
+
F - S(x,y)
|
119
|
+
FX - dS(x,y)/dX
|
120
|
+
FY - dS(x,y)/dY
|
121
|
+
FXY - d2S(x,y)/dXdY
|
122
|
+
|
123
|
+
-- ALGLIB PROJECT --
|
124
|
+
Copyright 05.07.2007 by Bochkanov Sergey
|
125
|
+
*************************************************************************/
|
126
|
+
void splinedifferentiation2d(const ap::real_1d_array& c,
|
127
|
+
double x,
|
128
|
+
double y,
|
129
|
+
double& f,
|
130
|
+
double& fx,
|
131
|
+
double& fy,
|
132
|
+
double& fxy);
|
133
|
+
|
134
|
+
|
135
|
+
/*************************************************************************
|
136
|
+
This subroutine unpacks two-dimensional spline into the coefficients table
|
137
|
+
|
138
|
+
Input parameters:
|
139
|
+
C - coefficients table.
|
140
|
+
|
141
|
+
Result:
|
142
|
+
M, N- grid size (x-axis and y-axis)
|
143
|
+
Tbl - coefficients table, unpacked format,
|
144
|
+
[0..(N-1)*(M-1)-1, 0..19].
|
145
|
+
For I = 0...M-2, J=0..N-2:
|
146
|
+
K = I*(N-1)+J
|
147
|
+
Tbl[K,0] = X[j]
|
148
|
+
Tbl[K,1] = X[j+1]
|
149
|
+
Tbl[K,2] = Y[i]
|
150
|
+
Tbl[K,3] = Y[i+1]
|
151
|
+
Tbl[K,4] = C00
|
152
|
+
Tbl[K,5] = C01
|
153
|
+
Tbl[K,6] = C02
|
154
|
+
Tbl[K,7] = C03
|
155
|
+
Tbl[K,8] = C10
|
156
|
+
Tbl[K,9] = C11
|
157
|
+
...
|
158
|
+
Tbl[K,19] = C33
|
159
|
+
On each grid square spline is equals to:
|
160
|
+
S(x) = SUM(c[i,j]*(x^i)*(y^j), i=0..3, j=0..3)
|
161
|
+
t = x-x[j]
|
162
|
+
u = y-y[i]
|
163
|
+
|
164
|
+
-- ALGLIB PROJECT --
|
165
|
+
Copyright 29.06.2007 by Bochkanov Sergey
|
166
|
+
*************************************************************************/
|
167
|
+
void splineunpack2d(const ap::real_1d_array& c,
|
168
|
+
int& m,
|
169
|
+
int& n,
|
170
|
+
ap::real_2d_array& tbl);
|
171
|
+
|
172
|
+
|
173
|
+
/*************************************************************************
|
174
|
+
This subroutine performs linear transformation of the spline argument.
|
175
|
+
|
176
|
+
Input parameters:
|
177
|
+
C - coefficients table.
|
178
|
+
AX, BX - transformation coefficients: x = A*t + B
|
179
|
+
AY, BY - transformation coefficients: y = A*u + B
|
180
|
+
Result:
|
181
|
+
C - transformed spline
|
182
|
+
|
183
|
+
-- ALGLIB PROJECT --
|
184
|
+
Copyright 30.06.2007 by Bochkanov Sergey
|
185
|
+
*************************************************************************/
|
186
|
+
void spline2dlintransxy(ap::real_1d_array& c,
|
187
|
+
double ax,
|
188
|
+
double bx,
|
189
|
+
double ay,
|
190
|
+
double by);
|
191
|
+
|
192
|
+
|
193
|
+
/*************************************************************************
|
194
|
+
This subroutine performs linear transformation of the spline.
|
195
|
+
|
196
|
+
Input parameters:
|
197
|
+
C - coefficients table. Built by BuildLinearSpline,
|
198
|
+
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
|
199
|
+
A, B- transformation coefficients: S2(x,y) = A*S(x,y) + B
|
200
|
+
|
201
|
+
Output parameters:
|
202
|
+
C - transformed spline
|
203
|
+
|
204
|
+
-- ALGLIB PROJECT --
|
205
|
+
Copyright 30.06.2007 by Bochkanov Sergey
|
206
|
+
*************************************************************************/
|
207
|
+
void spline2dlintransf(ap::real_1d_array& c, double a, double b);
|
208
|
+
|
209
|
+
|
210
|
+
/*************************************************************************
|
211
|
+
This subroutine makes the copy of the spline.
|
212
|
+
|
213
|
+
Input parameters:
|
214
|
+
C - coefficients table.
|
215
|
+
|
216
|
+
Output parameters:
|
217
|
+
CC - spline copy
|
218
|
+
|
219
|
+
-- ALGLIB PROJECT --
|
220
|
+
Copyright 29.06.2007 by Bochkanov Sergey
|
221
|
+
*************************************************************************/
|
222
|
+
void spline2dcopy(const ap::real_1d_array& c, ap::real_1d_array& cc);
|
223
|
+
|
224
|
+
|
225
|
+
/*************************************************************************
|
226
|
+
Bicubic spline resampling
|
227
|
+
|
228
|
+
Input parameters:
|
229
|
+
A - function values at the old grid,
|
230
|
+
array[0..OldHeight-1, 0..OldWidth-1]
|
231
|
+
OldHeight - old grid height, OldHeight>1
|
232
|
+
OldWidth - old grid width, OldWidth>1
|
233
|
+
NewHeight - new grid height, NewHeight>1
|
234
|
+
NewWidth - new grid width, NewWidth>1
|
235
|
+
|
236
|
+
Output parameters:
|
237
|
+
B - function values at the new grid,
|
238
|
+
array[0..NewHeight-1, 0..NewWidth-1]
|
239
|
+
|
240
|
+
-- ALGLIB routine --
|
241
|
+
15 May, 2007
|
242
|
+
Copyright by Bochkanov Sergey
|
243
|
+
*************************************************************************/
|
244
|
+
void bicubicresamplecartesian(const ap::real_2d_array& a,
|
245
|
+
int oldheight,
|
246
|
+
int oldwidth,
|
247
|
+
ap::real_2d_array& b,
|
248
|
+
int newheight,
|
249
|
+
int newwidth);
|
250
|
+
|
251
|
+
|
252
|
+
/*************************************************************************
|
253
|
+
Bilinear spline resampling
|
254
|
+
|
255
|
+
Input parameters:
|
256
|
+
A - function values at the old grid,
|
257
|
+
array[0..OldHeight-1, 0..OldWidth-1]
|
258
|
+
OldHeight - old grid height, OldHeight>1
|
259
|
+
OldWidth - old grid width, OldWidth>1
|
260
|
+
NewHeight - new grid height, NewHeight>1
|
261
|
+
NewWidth - new grid width, NewWidth>1
|
262
|
+
|
263
|
+
Output parameters:
|
264
|
+
B - function values at the new grid,
|
265
|
+
array[0..NewHeight-1, 0..NewWidth-1]
|
266
|
+
|
267
|
+
-- ALGLIB routine --
|
268
|
+
09.07.2007
|
269
|
+
Copyright by Bochkanov Sergey
|
270
|
+
*************************************************************************/
|
271
|
+
void bilinearresamplecartesian(const ap::real_2d_array& a,
|
272
|
+
int oldheight,
|
273
|
+
int oldwidth,
|
274
|
+
ap::real_2d_array& b,
|
275
|
+
int newheight,
|
276
|
+
int newwidth);
|
277
|
+
|
278
|
+
|
279
|
+
/*************************************************************************
|
280
|
+
Obsolete subroutine for backwards compatibility
|
281
|
+
*************************************************************************/
|
282
|
+
void bicubicresample(int oldwidth,
|
283
|
+
int oldheight,
|
284
|
+
int newwidth,
|
285
|
+
int newheight,
|
286
|
+
const ap::real_2d_array& a,
|
287
|
+
ap::real_2d_array& b);
|
288
|
+
|
289
|
+
|
290
|
+
/*************************************************************************
|
291
|
+
Obsolete subroutine for backwards compatibility
|
292
|
+
*************************************************************************/
|
293
|
+
void bilinearresample(int oldwidth,
|
294
|
+
int oldheight,
|
295
|
+
int newwidth,
|
296
|
+
int newheight,
|
297
|
+
const ap::real_2d_array& a,
|
298
|
+
ap::real_2d_array& b);
|
299
|
+
|
300
|
+
|
301
|
+
#endif
|
@@ -0,0 +1,1264 @@
|
|
1
|
+
/*************************************************************************
|
2
|
+
Copyright (c) 2007, Sergey Bochkanov (ALGLIB project).
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are
|
6
|
+
met:
|
7
|
+
|
8
|
+
- Redistributions of source code must retain the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
- Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer listed
|
13
|
+
in this license in the documentation and/or other materials
|
14
|
+
provided with the distribution.
|
15
|
+
|
16
|
+
- Neither the name of the copyright holders nor the names of its
|
17
|
+
contributors may be used to endorse or promote products derived from
|
18
|
+
this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*************************************************************************/
|
32
|
+
|
33
|
+
#include <stdafx.h>
|
34
|
+
#include "spline3.h"
|
35
|
+
|
36
|
+
static void heapsortpoints(ap::real_1d_array& x, ap::real_1d_array& y, int n);
|
37
|
+
static void heapsortdpoints(ap::real_1d_array& x,
|
38
|
+
ap::real_1d_array& y,
|
39
|
+
ap::real_1d_array& d,
|
40
|
+
int n);
|
41
|
+
static void solvetridiagonal(ap::real_1d_array a,
|
42
|
+
ap::real_1d_array b,
|
43
|
+
ap::real_1d_array c,
|
44
|
+
ap::real_1d_array d,
|
45
|
+
int n,
|
46
|
+
ap::real_1d_array& x);
|
47
|
+
static double diffthreepoint(double t,
|
48
|
+
double x0,
|
49
|
+
double f0,
|
50
|
+
double x1,
|
51
|
+
double f1,
|
52
|
+
double x2,
|
53
|
+
double f2);
|
54
|
+
|
55
|
+
/*************************************************************************
|
56
|
+
This subroutine builds linear spline coefficients table.
|
57
|
+
|
58
|
+
Input parameters:
|
59
|
+
X - spline nodes, array[0..N-1]
|
60
|
+
Y - function values, array[0..N-1]
|
61
|
+
N - points count, N>=2
|
62
|
+
|
63
|
+
Output parameters:
|
64
|
+
C - coefficients table. Used by SplineInterpolation and other
|
65
|
+
subroutines from this file.
|
66
|
+
|
67
|
+
-- ALGLIB PROJECT --
|
68
|
+
Copyright 24.06.2007 by Bochkanov Sergey
|
69
|
+
*************************************************************************/
|
70
|
+
void buildlinearspline(ap::real_1d_array x,
|
71
|
+
ap::real_1d_array y,
|
72
|
+
int n,
|
73
|
+
ap::real_1d_array& c)
|
74
|
+
{
|
75
|
+
int i;
|
76
|
+
int tblsize;
|
77
|
+
|
78
|
+
ap::ap_error::make_assertion(n>=2, "BuildLinearSpline: N<2!");
|
79
|
+
|
80
|
+
//
|
81
|
+
// Sort points
|
82
|
+
//
|
83
|
+
heapsortpoints(x, y, n);
|
84
|
+
|
85
|
+
//
|
86
|
+
// Fill C:
|
87
|
+
// C[0] - length(C)
|
88
|
+
// C[1] - type(C):
|
89
|
+
// 3 - general cubic spline
|
90
|
+
// C[2] - N
|
91
|
+
// C[3]...C[3+N-1] - x[i], i = 0...N-1
|
92
|
+
// C[3+N]...C[3+N+(N-1)*4-1] - coefficients table
|
93
|
+
//
|
94
|
+
tblsize = 3+n+(n-1)*4;
|
95
|
+
c.setbounds(0, tblsize-1);
|
96
|
+
c(0) = tblsize;
|
97
|
+
c(1) = 3;
|
98
|
+
c(2) = n;
|
99
|
+
for(i = 0; i <= n-1; i++)
|
100
|
+
{
|
101
|
+
c(3+i) = x(i);
|
102
|
+
}
|
103
|
+
for(i = 0; i <= n-2; i++)
|
104
|
+
{
|
105
|
+
c(3+n+4*i+0) = y(i);
|
106
|
+
c(3+n+4*i+1) = (y(i+1)-y(i))/(x(i+1)-x(i));
|
107
|
+
c(3+n+4*i+2) = 0;
|
108
|
+
c(3+n+4*i+3) = 0;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
/*************************************************************************
|
114
|
+
This subroutine builds cubic spline coefficients table.
|
115
|
+
|
116
|
+
Input parameters:
|
117
|
+
X - spline nodes, array[0..N-1]
|
118
|
+
Y - function values, array[0..N-1]
|
119
|
+
N - points count, N>=2
|
120
|
+
BoundLType - boundary condition type for the left boundary
|
121
|
+
BoundL - left boundary condition (first or second derivative,
|
122
|
+
depending on the BoundLType)
|
123
|
+
BoundRType - boundary condition type for the right boundary
|
124
|
+
BoundR - right boundary condition (first or second derivative,
|
125
|
+
depending on the BoundRType)
|
126
|
+
|
127
|
+
Output parameters:
|
128
|
+
C - coefficients table. Used by SplineInterpolation and
|
129
|
+
other subroutines from this file.
|
130
|
+
|
131
|
+
The BoundLType/BoundRType parameters can have the following values:
|
132
|
+
* 0, which corresponds to the parabolically terminated spline
|
133
|
+
(BoundL/BoundR are ignored).
|
134
|
+
* 1, which corresponds to the first derivative boundary condition
|
135
|
+
* 2, which corresponds to the second derivative boundary condition
|
136
|
+
|
137
|
+
-- ALGLIB PROJECT --
|
138
|
+
Copyright 23.06.2007 by Bochkanov Sergey
|
139
|
+
*************************************************************************/
|
140
|
+
void buildcubicspline(ap::real_1d_array x,
|
141
|
+
ap::real_1d_array y,
|
142
|
+
int n,
|
143
|
+
int boundltype,
|
144
|
+
double boundl,
|
145
|
+
int boundrtype,
|
146
|
+
double boundr,
|
147
|
+
ap::real_1d_array& c)
|
148
|
+
{
|
149
|
+
ap::real_1d_array a1;
|
150
|
+
ap::real_1d_array a2;
|
151
|
+
ap::real_1d_array a3;
|
152
|
+
ap::real_1d_array b;
|
153
|
+
ap::real_1d_array d;
|
154
|
+
int i;
|
155
|
+
int tblsize;
|
156
|
+
double delta;
|
157
|
+
double delta2;
|
158
|
+
double delta3;
|
159
|
+
|
160
|
+
ap::ap_error::make_assertion(n>=2, "BuildCubicSpline: N<2!");
|
161
|
+
ap::ap_error::make_assertion(boundltype==0||boundltype==1||boundltype==2, "BuildCubicSpline: incorrect BoundLType!");
|
162
|
+
ap::ap_error::make_assertion(boundrtype==0||boundrtype==1||boundrtype==2, "BuildCubicSpline: incorrect BoundRType!");
|
163
|
+
a1.setbounds(0, n-1);
|
164
|
+
a2.setbounds(0, n-1);
|
165
|
+
a3.setbounds(0, n-1);
|
166
|
+
b.setbounds(0, n-1);
|
167
|
+
|
168
|
+
//
|
169
|
+
// Special case:
|
170
|
+
// * N=2
|
171
|
+
// * parabolic terminated boundary condition on both ends
|
172
|
+
//
|
173
|
+
if( n==2&&boundltype==0&&boundrtype==0 )
|
174
|
+
{
|
175
|
+
|
176
|
+
//
|
177
|
+
// Change task type
|
178
|
+
//
|
179
|
+
boundltype = 2;
|
180
|
+
boundl = 0;
|
181
|
+
boundrtype = 2;
|
182
|
+
boundr = 0;
|
183
|
+
}
|
184
|
+
|
185
|
+
//
|
186
|
+
//
|
187
|
+
// Sort points
|
188
|
+
//
|
189
|
+
heapsortpoints(x, y, n);
|
190
|
+
|
191
|
+
//
|
192
|
+
// Left boundary conditions
|
193
|
+
//
|
194
|
+
if( boundltype==0 )
|
195
|
+
{
|
196
|
+
a1(0) = 0;
|
197
|
+
a2(0) = 1;
|
198
|
+
a3(0) = 1;
|
199
|
+
b(0) = 2*(y(1)-y(0))/(x(1)-x(0));
|
200
|
+
}
|
201
|
+
if( boundltype==1 )
|
202
|
+
{
|
203
|
+
a1(0) = 0;
|
204
|
+
a2(0) = 1;
|
205
|
+
a3(0) = 0;
|
206
|
+
b(0) = boundl;
|
207
|
+
}
|
208
|
+
if( boundltype==2 )
|
209
|
+
{
|
210
|
+
a1(0) = 0;
|
211
|
+
a2(0) = 2;
|
212
|
+
a3(0) = 1;
|
213
|
+
b(0) = 3*(y(1)-y(0))/(x(1)-x(0))-0.5*boundl*(x(1)-x(0));
|
214
|
+
}
|
215
|
+
|
216
|
+
//
|
217
|
+
// Central conditions
|
218
|
+
//
|
219
|
+
for(i = 1; i <= n-2; i++)
|
220
|
+
{
|
221
|
+
a1(i) = x(i+1)-x(i);
|
222
|
+
a2(i) = 2*(x(i+1)-x(i-1));
|
223
|
+
a3(i) = x(i)-x(i-1);
|
224
|
+
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));
|
225
|
+
}
|
226
|
+
|
227
|
+
//
|
228
|
+
// Right boundary conditions
|
229
|
+
//
|
230
|
+
if( boundrtype==0 )
|
231
|
+
{
|
232
|
+
a1(n-1) = 1;
|
233
|
+
a2(n-1) = 1;
|
234
|
+
a3(n-1) = 0;
|
235
|
+
b(n-1) = 2*(y(n-1)-y(n-2))/(x(n-1)-x(n-2));
|
236
|
+
}
|
237
|
+
if( boundrtype==1 )
|
238
|
+
{
|
239
|
+
a1(n-1) = 0;
|
240
|
+
a2(n-1) = 1;
|
241
|
+
a3(n-1) = 0;
|
242
|
+
b(n-1) = boundr;
|
243
|
+
}
|
244
|
+
if( boundrtype==2 )
|
245
|
+
{
|
246
|
+
a1(n-1) = 1;
|
247
|
+
a2(n-1) = 2;
|
248
|
+
a3(n-1) = 0;
|
249
|
+
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));
|
250
|
+
}
|
251
|
+
|
252
|
+
//
|
253
|
+
// Solve
|
254
|
+
//
|
255
|
+
solvetridiagonal(a1, a2, a3, b, n, d);
|
256
|
+
|
257
|
+
//
|
258
|
+
// Now problem is reduced to the cubic Hermite spline
|
259
|
+
//
|
260
|
+
buildhermitespline(x, y, d, n, c);
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
/*************************************************************************
|
265
|
+
This subroutine builds cubic Hermite spline coefficients table.
|
266
|
+
|
267
|
+
Input parameters:
|
268
|
+
X - spline nodes, array[0..N-1]
|
269
|
+
Y - function values, array[0..N-1]
|
270
|
+
D - derivatives, array[0..N-1]
|
271
|
+
N - points count, N>=2
|
272
|
+
|
273
|
+
Output parameters:
|
274
|
+
C - coefficients table. Used by SplineInterpolation and
|
275
|
+
other subroutines from this file.
|
276
|
+
|
277
|
+
-- ALGLIB PROJECT --
|
278
|
+
Copyright 23.06.2007 by Bochkanov Sergey
|
279
|
+
*************************************************************************/
|
280
|
+
void buildhermitespline(ap::real_1d_array x,
|
281
|
+
ap::real_1d_array y,
|
282
|
+
ap::real_1d_array d,
|
283
|
+
int n,
|
284
|
+
ap::real_1d_array& c)
|
285
|
+
{
|
286
|
+
int i;
|
287
|
+
int tblsize;
|
288
|
+
double delta;
|
289
|
+
double delta2;
|
290
|
+
double delta3;
|
291
|
+
|
292
|
+
ap::ap_error::make_assertion(n>=2, "BuildHermiteSpline: N<2!");
|
293
|
+
|
294
|
+
//
|
295
|
+
// Sort points
|
296
|
+
//
|
297
|
+
heapsortdpoints(x, y, d, n);
|
298
|
+
|
299
|
+
//
|
300
|
+
// Fill C:
|
301
|
+
// C[0] - length(C)
|
302
|
+
// C[1] - type(C):
|
303
|
+
// 3 - general cubic spline
|
304
|
+
// C[2] - N
|
305
|
+
// C[3]...C[3+N-1] - x[i], i = 0...N-1
|
306
|
+
// C[3+N]...C[3+N+(N-1)*4-1] - coefficients table
|
307
|
+
//
|
308
|
+
tblsize = 3+n+(n-1)*4;
|
309
|
+
c.setbounds(0, tblsize-1);
|
310
|
+
c(0) = tblsize;
|
311
|
+
c(1) = 3;
|
312
|
+
c(2) = n;
|
313
|
+
for(i = 0; i <= n-1; i++)
|
314
|
+
{
|
315
|
+
c(3+i) = x(i);
|
316
|
+
}
|
317
|
+
for(i = 0; i <= n-2; i++)
|
318
|
+
{
|
319
|
+
delta = x(i+1)-x(i);
|
320
|
+
delta2 = ap::sqr(delta);
|
321
|
+
delta3 = delta*delta2;
|
322
|
+
c(3+n+4*i+0) = y(i);
|
323
|
+
c(3+n+4*i+1) = d(i);
|
324
|
+
c(3+n+4*i+2) = (3*(y(i+1)-y(i))-2*d(i)*delta-d(i+1)*delta)/delta2;
|
325
|
+
c(3+n+4*i+3) = (2*(y(i)-y(i+1))+d(i)*delta+d(i+1)*delta)/delta3;
|
326
|
+
}
|
327
|
+
}
|
328
|
+
|
329
|
+
|
330
|
+
/*************************************************************************
|
331
|
+
This subroutine builds Akima spline coefficients table.
|
332
|
+
|
333
|
+
Input parameters:
|
334
|
+
X - spline nodes, array[0..N-1]
|
335
|
+
Y - function values, array[0..N-1]
|
336
|
+
N - points count, N>=5
|
337
|
+
|
338
|
+
Output parameters:
|
339
|
+
C - coefficients table. Used by SplineInterpolation and
|
340
|
+
other subroutines from this file.
|
341
|
+
|
342
|
+
-- ALGLIB PROJECT --
|
343
|
+
Copyright 24.06.2007 by Bochkanov Sergey
|
344
|
+
*************************************************************************/
|
345
|
+
void buildakimaspline(ap::real_1d_array x,
|
346
|
+
ap::real_1d_array y,
|
347
|
+
int n,
|
348
|
+
ap::real_1d_array& c)
|
349
|
+
{
|
350
|
+
int i;
|
351
|
+
ap::real_1d_array d;
|
352
|
+
ap::real_1d_array w;
|
353
|
+
ap::real_1d_array diff;
|
354
|
+
|
355
|
+
ap::ap_error::make_assertion(n>=5, "BuildAkimaSpline: N<5!");
|
356
|
+
|
357
|
+
//
|
358
|
+
// Sort points
|
359
|
+
//
|
360
|
+
heapsortpoints(x, y, n);
|
361
|
+
|
362
|
+
//
|
363
|
+
// Prepare W (weights), Diff (divided differences)
|
364
|
+
//
|
365
|
+
w.setbounds(1, n-2);
|
366
|
+
diff.setbounds(0, n-2);
|
367
|
+
for(i = 0; i <= n-2; i++)
|
368
|
+
{
|
369
|
+
diff(i) = (y(i+1)-y(i))/(x(i+1)-x(i));
|
370
|
+
}
|
371
|
+
for(i = 1; i <= n-2; i++)
|
372
|
+
{
|
373
|
+
w(i) = fabs(diff(i)-diff(i-1));
|
374
|
+
}
|
375
|
+
|
376
|
+
//
|
377
|
+
// Prepare Hermite interpolation scheme
|
378
|
+
//
|
379
|
+
d.setbounds(0, n-1);
|
380
|
+
for(i = 2; i <= n-3; i++)
|
381
|
+
{
|
382
|
+
if( fabs(w(i-1))+fabs(w(i+1))!=0 )
|
383
|
+
{
|
384
|
+
d(i) = (w(i+1)*diff(i-1)+w(i-1)*diff(i))/(w(i+1)+w(i-1));
|
385
|
+
}
|
386
|
+
else
|
387
|
+
{
|
388
|
+
d(i) = ((x(i+1)-x(i))*diff(i-1)+(x(i)-x(i-1))*diff(i))/(x(i+1)-x(i-1));
|
389
|
+
}
|
390
|
+
}
|
391
|
+
d(0) = diffthreepoint(x(0), x(0), y(0), x(1), y(1), x(2), y(2));
|
392
|
+
d(1) = diffthreepoint(x(1), x(0), y(0), x(1), y(1), x(2), y(2));
|
393
|
+
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));
|
394
|
+
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));
|
395
|
+
|
396
|
+
//
|
397
|
+
// Build Akima spline using Hermite interpolation scheme
|
398
|
+
//
|
399
|
+
buildhermitespline(x, y, d, n, c);
|
400
|
+
}
|
401
|
+
|
402
|
+
|
403
|
+
/*************************************************************************
|
404
|
+
This subroutine calculates the value of the spline at the given point X.
|
405
|
+
|
406
|
+
Input parameters:
|
407
|
+
C - coefficients table. Built by BuildLinearSpline,
|
408
|
+
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
|
409
|
+
X - point
|
410
|
+
|
411
|
+
Result:
|
412
|
+
S(x)
|
413
|
+
|
414
|
+
-- ALGLIB PROJECT --
|
415
|
+
Copyright 23.06.2007 by Bochkanov Sergey
|
416
|
+
*************************************************************************/
|
417
|
+
double splineinterpolation(const ap::real_1d_array& c, double x)
|
418
|
+
{
|
419
|
+
double result;
|
420
|
+
int n;
|
421
|
+
int l;
|
422
|
+
int r;
|
423
|
+
int m;
|
424
|
+
|
425
|
+
ap::ap_error::make_assertion(ap::round(c(1))==3, "SplineInterpolation: incorrect C!");
|
426
|
+
n = ap::round(c(2));
|
427
|
+
|
428
|
+
//
|
429
|
+
// Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
|
430
|
+
//
|
431
|
+
l = 3;
|
432
|
+
r = 3+n-2+1;
|
433
|
+
while(l!=r-1)
|
434
|
+
{
|
435
|
+
m = (l+r)/2;
|
436
|
+
if( c(m)>=x )
|
437
|
+
{
|
438
|
+
r = m;
|
439
|
+
}
|
440
|
+
else
|
441
|
+
{
|
442
|
+
l = m;
|
443
|
+
}
|
444
|
+
}
|
445
|
+
|
446
|
+
//
|
447
|
+
// Interpolation
|
448
|
+
//
|
449
|
+
x = x-c(l);
|
450
|
+
m = 3+n+4*(l-3);
|
451
|
+
result = c(m)+x*(c(m+1)+x*(c(m+2)+x*c(m+3)));
|
452
|
+
return result;
|
453
|
+
}
|
454
|
+
|
455
|
+
|
456
|
+
/*************************************************************************
|
457
|
+
This subroutine differentiates the spline.
|
458
|
+
|
459
|
+
Input parameters:
|
460
|
+
C - coefficients table. Built by BuildLinearSpline,
|
461
|
+
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
|
462
|
+
X - point
|
463
|
+
|
464
|
+
Result:
|
465
|
+
S - S(x)
|
466
|
+
DS - S'(x)
|
467
|
+
D2S - S''(x)
|
468
|
+
|
469
|
+
-- ALGLIB PROJECT --
|
470
|
+
Copyright 24.06.2007 by Bochkanov Sergey
|
471
|
+
*************************************************************************/
|
472
|
+
void splinedifferentiation(const ap::real_1d_array& c,
|
473
|
+
double x,
|
474
|
+
double& s,
|
475
|
+
double& ds,
|
476
|
+
double& d2s)
|
477
|
+
{
|
478
|
+
int n;
|
479
|
+
int l;
|
480
|
+
int r;
|
481
|
+
int m;
|
482
|
+
|
483
|
+
ap::ap_error::make_assertion(ap::round(c(1))==3, "SplineInterpolation: incorrect C!");
|
484
|
+
n = ap::round(c(2));
|
485
|
+
|
486
|
+
//
|
487
|
+
// Binary search
|
488
|
+
//
|
489
|
+
l = 3;
|
490
|
+
r = 3+n-2+1;
|
491
|
+
while(l!=r-1)
|
492
|
+
{
|
493
|
+
m = (l+r)/2;
|
494
|
+
if( c(m)>=x )
|
495
|
+
{
|
496
|
+
r = m;
|
497
|
+
}
|
498
|
+
else
|
499
|
+
{
|
500
|
+
l = m;
|
501
|
+
}
|
502
|
+
}
|
503
|
+
|
504
|
+
//
|
505
|
+
// Differentiation
|
506
|
+
//
|
507
|
+
x = x-c(l);
|
508
|
+
m = 3+n+4*(l-3);
|
509
|
+
s = c(m)+x*(c(m+1)+x*(c(m+2)+x*c(m+3)));
|
510
|
+
ds = c(m+1)+2*x*c(m+2)+3*ap::sqr(x)*c(m+3);
|
511
|
+
d2s = 2*c(m+2)+6*x*c(m+3);
|
512
|
+
}
|
513
|
+
|
514
|
+
|
515
|
+
/*************************************************************************
|
516
|
+
This subroutine makes the copy of the spline.
|
517
|
+
|
518
|
+
Input parameters:
|
519
|
+
C - coefficients table. Built by BuildLinearSpline,
|
520
|
+
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
|
521
|
+
|
522
|
+
Result:
|
523
|
+
CC - spline copy
|
524
|
+
|
525
|
+
-- ALGLIB PROJECT --
|
526
|
+
Copyright 29.06.2007 by Bochkanov Sergey
|
527
|
+
*************************************************************************/
|
528
|
+
void splinecopy(const ap::real_1d_array& c, ap::real_1d_array& cc)
|
529
|
+
{
|
530
|
+
int s;
|
531
|
+
|
532
|
+
s = ap::round(c(0));
|
533
|
+
cc.setbounds(0, s-1);
|
534
|
+
ap::vmove(&cc(0), &c(0), ap::vlen(0,s-1));
|
535
|
+
}
|
536
|
+
|
537
|
+
|
538
|
+
/*************************************************************************
|
539
|
+
This subroutine unpacks the spline into the coefficients table.
|
540
|
+
|
541
|
+
Input parameters:
|
542
|
+
C - coefficients table. Built by BuildLinearSpline,
|
543
|
+
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
|
544
|
+
X - point
|
545
|
+
|
546
|
+
Result:
|
547
|
+
Tbl - coefficients table, unpacked format, array[0..N-2, 0..5].
|
548
|
+
For I = 0...N-2:
|
549
|
+
Tbl[I,0] = X[i]
|
550
|
+
Tbl[I,1] = X[i+1]
|
551
|
+
Tbl[I,2] = C0
|
552
|
+
Tbl[I,3] = C1
|
553
|
+
Tbl[I,4] = C2
|
554
|
+
Tbl[I,5] = C3
|
555
|
+
On [x[i], x[i+1]] spline is equals to:
|
556
|
+
S(x) = C0 + C1*t + C2*t^2 + C3*t^3
|
557
|
+
t = x-x[i]
|
558
|
+
|
559
|
+
-- ALGLIB PROJECT --
|
560
|
+
Copyright 29.06.2007 by Bochkanov Sergey
|
561
|
+
*************************************************************************/
|
562
|
+
void splineunpack(const ap::real_1d_array& c, int& n, ap::real_2d_array& tbl)
|
563
|
+
{
|
564
|
+
int i;
|
565
|
+
|
566
|
+
ap::ap_error::make_assertion(ap::round(c(1))==3, "SplineUnpack: incorrect C!");
|
567
|
+
n = ap::round(c(2));
|
568
|
+
tbl.setbounds(0, n-2, 0, 5);
|
569
|
+
|
570
|
+
//
|
571
|
+
// Fill
|
572
|
+
//
|
573
|
+
for(i = 0; i <= n-2; i++)
|
574
|
+
{
|
575
|
+
tbl(i,0) = c(3+i);
|
576
|
+
tbl(i,1) = c(3+i+1);
|
577
|
+
tbl(i,2) = c(3+n+4*i);
|
578
|
+
tbl(i,3) = c(3+n+4*i+1);
|
579
|
+
tbl(i,4) = c(3+n+4*i+2);
|
580
|
+
tbl(i,5) = c(3+n+4*i+3);
|
581
|
+
}
|
582
|
+
}
|
583
|
+
|
584
|
+
|
585
|
+
/*************************************************************************
|
586
|
+
This subroutine performs linear transformation of the spline argument.
|
587
|
+
|
588
|
+
Input parameters:
|
589
|
+
C - coefficients table. Built by BuildLinearSpline,
|
590
|
+
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
|
591
|
+
A, B- transformation coefficients: x = A*t + B
|
592
|
+
Result:
|
593
|
+
C - transformed spline
|
594
|
+
|
595
|
+
-- ALGLIB PROJECT --
|
596
|
+
Copyright 30.06.2007 by Bochkanov Sergey
|
597
|
+
*************************************************************************/
|
598
|
+
void splinelintransx(ap::real_1d_array& c, double a, double b)
|
599
|
+
{
|
600
|
+
int i;
|
601
|
+
int n;
|
602
|
+
double v;
|
603
|
+
double dv;
|
604
|
+
double d2v;
|
605
|
+
ap::real_1d_array x;
|
606
|
+
ap::real_1d_array y;
|
607
|
+
ap::real_1d_array d;
|
608
|
+
|
609
|
+
ap::ap_error::make_assertion(ap::round(c(1))==3, "SplineLinTransX: incorrect C!");
|
610
|
+
n = ap::round(c(2));
|
611
|
+
|
612
|
+
//
|
613
|
+
// Special case: A=0
|
614
|
+
//
|
615
|
+
if( a==0 )
|
616
|
+
{
|
617
|
+
v = splineinterpolation(c, b);
|
618
|
+
for(i = 0; i <= n-2; i++)
|
619
|
+
{
|
620
|
+
c(3+n+4*i) = v;
|
621
|
+
c(3+n+4*i+1) = 0;
|
622
|
+
c(3+n+4*i+2) = 0;
|
623
|
+
c(3+n+4*i+3) = 0;
|
624
|
+
}
|
625
|
+
return;
|
626
|
+
}
|
627
|
+
|
628
|
+
//
|
629
|
+
// General case: A<>0.
|
630
|
+
// Unpack, X, Y, dY/dX.
|
631
|
+
// Scale and pack again.
|
632
|
+
//
|
633
|
+
x.setbounds(0, n-1);
|
634
|
+
y.setbounds(0, n-1);
|
635
|
+
d.setbounds(0, n-1);
|
636
|
+
for(i = 0; i <= n-1; i++)
|
637
|
+
{
|
638
|
+
x(i) = c(3+i);
|
639
|
+
splinedifferentiation(c, x(i), v, dv, d2v);
|
640
|
+
x(i) = (x(i)-b)/a;
|
641
|
+
y(i) = v;
|
642
|
+
d(i) = a*dv;
|
643
|
+
}
|
644
|
+
buildhermitespline(x, y, d, n, c);
|
645
|
+
}
|
646
|
+
|
647
|
+
|
648
|
+
/*************************************************************************
|
649
|
+
This subroutine performs linear transformation of the spline.
|
650
|
+
|
651
|
+
Input parameters:
|
652
|
+
C - coefficients table. Built by BuildLinearSpline,
|
653
|
+
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
|
654
|
+
A, B- transformation coefficients: S2(x) = A*S(x) + B
|
655
|
+
Result:
|
656
|
+
C - transformed spline
|
657
|
+
|
658
|
+
-- ALGLIB PROJECT --
|
659
|
+
Copyright 30.06.2007 by Bochkanov Sergey
|
660
|
+
*************************************************************************/
|
661
|
+
void splinelintransy(ap::real_1d_array& c, double a, double b)
|
662
|
+
{
|
663
|
+
int i;
|
664
|
+
int n;
|
665
|
+
double v;
|
666
|
+
double dv;
|
667
|
+
double d2v;
|
668
|
+
ap::real_1d_array x;
|
669
|
+
ap::real_1d_array y;
|
670
|
+
ap::real_1d_array d;
|
671
|
+
|
672
|
+
ap::ap_error::make_assertion(ap::round(c(1))==3, "SplineLinTransX: incorrect C!");
|
673
|
+
n = ap::round(c(2));
|
674
|
+
|
675
|
+
//
|
676
|
+
// Special case: A=0
|
677
|
+
//
|
678
|
+
for(i = 0; i <= n-2; i++)
|
679
|
+
{
|
680
|
+
c(3+n+4*i) = a*c(3+n+4*i)+b;
|
681
|
+
c(3+n+4*i+1) = a*c(3+n+4*i+1);
|
682
|
+
c(3+n+4*i+2) = a*c(3+n+4*i+2);
|
683
|
+
c(3+n+4*i+3) = a*c(3+n+4*i+3);
|
684
|
+
}
|
685
|
+
}
|
686
|
+
|
687
|
+
|
688
|
+
/*************************************************************************
|
689
|
+
This subroutine integrates the spline.
|
690
|
+
|
691
|
+
Input parameters:
|
692
|
+
C - coefficients table. Built by BuildLinearSpline,
|
693
|
+
BuildHermiteSpline, BuildCubicSpline, BuildAkimaSpline.
|
694
|
+
X - right bound of the integration interval [a, x]
|
695
|
+
Result:
|
696
|
+
integral(S(t)dt,a,x)
|
697
|
+
|
698
|
+
-- ALGLIB PROJECT --
|
699
|
+
Copyright 23.06.2007 by Bochkanov Sergey
|
700
|
+
*************************************************************************/
|
701
|
+
double splineintegration(const ap::real_1d_array& c, double x)
|
702
|
+
{
|
703
|
+
double result;
|
704
|
+
int n;
|
705
|
+
int i;
|
706
|
+
int l;
|
707
|
+
int r;
|
708
|
+
int m;
|
709
|
+
double w;
|
710
|
+
|
711
|
+
ap::ap_error::make_assertion(ap::round(c(1))==3, "SplineIntegration: incorrect C!");
|
712
|
+
n = ap::round(c(2));
|
713
|
+
|
714
|
+
//
|
715
|
+
// Binary search in the [ x[0], ..., x[n-2] ] (x[n-1] is not included)
|
716
|
+
//
|
717
|
+
l = 3;
|
718
|
+
r = 3+n-2+1;
|
719
|
+
while(l!=r-1)
|
720
|
+
{
|
721
|
+
m = (l+r)/2;
|
722
|
+
if( c(m)>=x )
|
723
|
+
{
|
724
|
+
r = m;
|
725
|
+
}
|
726
|
+
else
|
727
|
+
{
|
728
|
+
l = m;
|
729
|
+
}
|
730
|
+
}
|
731
|
+
|
732
|
+
//
|
733
|
+
// Integration
|
734
|
+
//
|
735
|
+
result = 0;
|
736
|
+
for(i = 3; i <= l-1; i++)
|
737
|
+
{
|
738
|
+
w = c(i+1)-c(i);
|
739
|
+
m = 3+n+4*(i-3);
|
740
|
+
result = result+c(m)*w;
|
741
|
+
result = result+c(m+1)*ap::sqr(w)/2;
|
742
|
+
result = result+c(m+2)*ap::sqr(w)*w/3;
|
743
|
+
result = result+c(m+3)*ap::sqr(ap::sqr(w))/4;
|
744
|
+
}
|
745
|
+
w = x-c(l);
|
746
|
+
m = 3+n+4*(l-3);
|
747
|
+
result = result+c(m)*w;
|
748
|
+
result = result+c(m+1)*ap::sqr(w)/2;
|
749
|
+
result = result+c(m+2)*ap::sqr(w)*w/3;
|
750
|
+
result = result+c(m+3)*ap::sqr(ap::sqr(w))/4;
|
751
|
+
return result;
|
752
|
+
}
|
753
|
+
|
754
|
+
|
755
|
+
/*************************************************************************
|
756
|
+
Obsolete subroutine, left for backward compatibility.
|
757
|
+
*************************************************************************/
|
758
|
+
void spline3buildtable(int n,
|
759
|
+
const int& diffn,
|
760
|
+
ap::real_1d_array x,
|
761
|
+
ap::real_1d_array y,
|
762
|
+
const double& boundl,
|
763
|
+
const double& boundr,
|
764
|
+
ap::real_2d_array& ctbl)
|
765
|
+
{
|
766
|
+
bool c;
|
767
|
+
int e;
|
768
|
+
int g;
|
769
|
+
double tmp;
|
770
|
+
int nxm1;
|
771
|
+
int i;
|
772
|
+
int j;
|
773
|
+
double dx;
|
774
|
+
double dxj;
|
775
|
+
double dyj;
|
776
|
+
double dxjp1;
|
777
|
+
double dyjp1;
|
778
|
+
double dxp;
|
779
|
+
double dyp;
|
780
|
+
double yppa;
|
781
|
+
double yppb;
|
782
|
+
double pj;
|
783
|
+
double b1;
|
784
|
+
double b2;
|
785
|
+
double b3;
|
786
|
+
double b4;
|
787
|
+
|
788
|
+
n = n-1;
|
789
|
+
g = (n+1)/2;
|
790
|
+
do
|
791
|
+
{
|
792
|
+
i = g;
|
793
|
+
do
|
794
|
+
{
|
795
|
+
j = i-g;
|
796
|
+
c = true;
|
797
|
+
do
|
798
|
+
{
|
799
|
+
if( x(j)<=x(j+g) )
|
800
|
+
{
|
801
|
+
c = false;
|
802
|
+
}
|
803
|
+
else
|
804
|
+
{
|
805
|
+
tmp = x(j);
|
806
|
+
x(j) = x(j+g);
|
807
|
+
x(j+g) = tmp;
|
808
|
+
tmp = y(j);
|
809
|
+
y(j) = y(j+g);
|
810
|
+
y(j+g) = tmp;
|
811
|
+
}
|
812
|
+
j = j-1;
|
813
|
+
}
|
814
|
+
while(j>=0&&c);
|
815
|
+
i = i+1;
|
816
|
+
}
|
817
|
+
while(i<=n);
|
818
|
+
g = g/2;
|
819
|
+
}
|
820
|
+
while(g>0);
|
821
|
+
ctbl.setbounds(0, 4, 0, n);
|
822
|
+
n = n+1;
|
823
|
+
if( diffn==1 )
|
824
|
+
{
|
825
|
+
b1 = 1;
|
826
|
+
b2 = 6/(x(1)-x(0))*((y(1)-y(0))/(x(1)-x(0))-boundl);
|
827
|
+
b3 = 1;
|
828
|
+
b4 = 6/(x(n-1)-x(n-2))*(boundr-(y(n-1)-y(n-2))/(x(n-1)-x(n-2)));
|
829
|
+
}
|
830
|
+
else
|
831
|
+
{
|
832
|
+
b1 = 0;
|
833
|
+
b2 = 2*boundl;
|
834
|
+
b3 = 0;
|
835
|
+
b4 = 2*boundr;
|
836
|
+
}
|
837
|
+
nxm1 = n-1;
|
838
|
+
if( n>=2 )
|
839
|
+
{
|
840
|
+
if( n>2 )
|
841
|
+
{
|
842
|
+
dxj = x(1)-x(0);
|
843
|
+
dyj = y(1)-y(0);
|
844
|
+
j = 2;
|
845
|
+
while(j<=nxm1)
|
846
|
+
{
|
847
|
+
dxjp1 = x(j)-x(j-1);
|
848
|
+
dyjp1 = y(j)-y(j-1);
|
849
|
+
dxp = dxj+dxjp1;
|
850
|
+
ctbl(1,j-1) = dxjp1/dxp;
|
851
|
+
ctbl(2,j-1) = 1-ctbl(1,j-1);
|
852
|
+
ctbl(3,j-1) = 6*(dyjp1/dxjp1-dyj/dxj)/dxp;
|
853
|
+
dxj = dxjp1;
|
854
|
+
dyj = dyjp1;
|
855
|
+
j = j+1;
|
856
|
+
}
|
857
|
+
}
|
858
|
+
ctbl(1,0) = -b1/2;
|
859
|
+
ctbl(2,0) = b2/2;
|
860
|
+
if( n!=2 )
|
861
|
+
{
|
862
|
+
j = 2;
|
863
|
+
while(j<=nxm1)
|
864
|
+
{
|
865
|
+
pj = ctbl(2,j-1)*ctbl(1,j-2)+2;
|
866
|
+
ctbl(1,j-1) = -ctbl(1,j-1)/pj;
|
867
|
+
ctbl(2,j-1) = (ctbl(3,j-1)-ctbl(2,j-1)*ctbl(2,j-2))/pj;
|
868
|
+
j = j+1;
|
869
|
+
}
|
870
|
+
}
|
871
|
+
yppb = (b4-b3*ctbl(2,nxm1-1))/(b3*ctbl(1,nxm1-1)+2);
|
872
|
+
i = 1;
|
873
|
+
while(i<=nxm1)
|
874
|
+
{
|
875
|
+
j = n-i;
|
876
|
+
yppa = ctbl(1,j-1)*yppb+ctbl(2,j-1);
|
877
|
+
dx = x(j)-x(j-1);
|
878
|
+
ctbl(3,j-1) = (yppb-yppa)/dx/6;
|
879
|
+
ctbl(2,j-1) = yppa/2;
|
880
|
+
ctbl(1,j-1) = (y(j)-y(j-1))/dx-(ctbl(2,j-1)+ctbl(3,j-1)*dx)*dx;
|
881
|
+
yppb = yppa;
|
882
|
+
i = i+1;
|
883
|
+
}
|
884
|
+
for(i = 1; i <= n; i++)
|
885
|
+
{
|
886
|
+
ctbl(0,i-1) = y(i-1);
|
887
|
+
ctbl(4,i-1) = x(i-1);
|
888
|
+
}
|
889
|
+
}
|
890
|
+
}
|
891
|
+
|
892
|
+
|
893
|
+
/*************************************************************************
|
894
|
+
Obsolete subroutine, left for backward compatibility.
|
895
|
+
*************************************************************************/
|
896
|
+
double spline3interpolate(int n, const ap::real_2d_array& c, const double& x)
|
897
|
+
{
|
898
|
+
double result;
|
899
|
+
int i;
|
900
|
+
int l;
|
901
|
+
int half;
|
902
|
+
int first;
|
903
|
+
int middle;
|
904
|
+
|
905
|
+
n = n-1;
|
906
|
+
l = n;
|
907
|
+
first = 0;
|
908
|
+
while(l>0)
|
909
|
+
{
|
910
|
+
half = l/2;
|
911
|
+
middle = first+half;
|
912
|
+
if( c(4,middle)<x )
|
913
|
+
{
|
914
|
+
first = middle+1;
|
915
|
+
l = l-half-1;
|
916
|
+
}
|
917
|
+
else
|
918
|
+
{
|
919
|
+
l = half;
|
920
|
+
}
|
921
|
+
}
|
922
|
+
i = first-1;
|
923
|
+
if( i<0 )
|
924
|
+
{
|
925
|
+
i = 0;
|
926
|
+
}
|
927
|
+
result = c(0,i)+(x-c(4,i))*(c(1,i)+(x-c(4,i))*(c(2,i)+c(3,i)*(x-c(4,i))));
|
928
|
+
return result;
|
929
|
+
}
|
930
|
+
|
931
|
+
|
932
|
+
/*************************************************************************
|
933
|
+
Internal subroutine. Heap sort.
|
934
|
+
*************************************************************************/
|
935
|
+
static void heapsortpoints(ap::real_1d_array& x, ap::real_1d_array& y, int n)
|
936
|
+
{
|
937
|
+
int i;
|
938
|
+
int j;
|
939
|
+
int k;
|
940
|
+
int t;
|
941
|
+
double tmp;
|
942
|
+
bool isascending;
|
943
|
+
bool isdescending;
|
944
|
+
|
945
|
+
|
946
|
+
//
|
947
|
+
// Test for already sorted set
|
948
|
+
//
|
949
|
+
isascending = true;
|
950
|
+
isdescending = true;
|
951
|
+
for(i = 1; i <= n-1; i++)
|
952
|
+
{
|
953
|
+
isascending = isascending&&x(i)>x(i-1);
|
954
|
+
isdescending = isdescending&&x(i)<x(i-1);
|
955
|
+
}
|
956
|
+
if( isascending )
|
957
|
+
{
|
958
|
+
return;
|
959
|
+
}
|
960
|
+
if( isdescending )
|
961
|
+
{
|
962
|
+
for(i = 0; i <= n-1; i++)
|
963
|
+
{
|
964
|
+
j = n-1-i;
|
965
|
+
if( j<=i )
|
966
|
+
{
|
967
|
+
break;
|
968
|
+
}
|
969
|
+
tmp = x(i);
|
970
|
+
x(i) = x(j);
|
971
|
+
x(j) = tmp;
|
972
|
+
tmp = y(i);
|
973
|
+
y(i) = y(j);
|
974
|
+
y(j) = tmp;
|
975
|
+
}
|
976
|
+
return;
|
977
|
+
}
|
978
|
+
|
979
|
+
//
|
980
|
+
// Special case: N=1
|
981
|
+
//
|
982
|
+
if( n==1 )
|
983
|
+
{
|
984
|
+
return;
|
985
|
+
}
|
986
|
+
|
987
|
+
//
|
988
|
+
// General case
|
989
|
+
//
|
990
|
+
i = 2;
|
991
|
+
do
|
992
|
+
{
|
993
|
+
t = i;
|
994
|
+
while(t!=1)
|
995
|
+
{
|
996
|
+
k = t/2;
|
997
|
+
if( x(k-1)>=x(t-1) )
|
998
|
+
{
|
999
|
+
t = 1;
|
1000
|
+
}
|
1001
|
+
else
|
1002
|
+
{
|
1003
|
+
tmp = x(k-1);
|
1004
|
+
x(k-1) = x(t-1);
|
1005
|
+
x(t-1) = tmp;
|
1006
|
+
tmp = y(k-1);
|
1007
|
+
y(k-1) = y(t-1);
|
1008
|
+
y(t-1) = tmp;
|
1009
|
+
t = k;
|
1010
|
+
}
|
1011
|
+
}
|
1012
|
+
i = i+1;
|
1013
|
+
}
|
1014
|
+
while(i<=n);
|
1015
|
+
i = n-1;
|
1016
|
+
do
|
1017
|
+
{
|
1018
|
+
tmp = x(i);
|
1019
|
+
x(i) = x(0);
|
1020
|
+
x(0) = tmp;
|
1021
|
+
tmp = y(i);
|
1022
|
+
y(i) = y(0);
|
1023
|
+
y(0) = tmp;
|
1024
|
+
t = 1;
|
1025
|
+
while(t!=0)
|
1026
|
+
{
|
1027
|
+
k = 2*t;
|
1028
|
+
if( k>i )
|
1029
|
+
{
|
1030
|
+
t = 0;
|
1031
|
+
}
|
1032
|
+
else
|
1033
|
+
{
|
1034
|
+
if( k<i )
|
1035
|
+
{
|
1036
|
+
if( x(k)>x(k-1) )
|
1037
|
+
{
|
1038
|
+
k = k+1;
|
1039
|
+
}
|
1040
|
+
}
|
1041
|
+
if( x(t-1)>=x(k-1) )
|
1042
|
+
{
|
1043
|
+
t = 0;
|
1044
|
+
}
|
1045
|
+
else
|
1046
|
+
{
|
1047
|
+
tmp = x(k-1);
|
1048
|
+
x(k-1) = x(t-1);
|
1049
|
+
x(t-1) = tmp;
|
1050
|
+
tmp = y(k-1);
|
1051
|
+
y(k-1) = y(t-1);
|
1052
|
+
y(t-1) = tmp;
|
1053
|
+
t = k;
|
1054
|
+
}
|
1055
|
+
}
|
1056
|
+
}
|
1057
|
+
i = i-1;
|
1058
|
+
}
|
1059
|
+
while(i>=1);
|
1060
|
+
}
|
1061
|
+
|
1062
|
+
|
1063
|
+
/*************************************************************************
|
1064
|
+
Internal subroutine. Heap sort.
|
1065
|
+
*************************************************************************/
|
1066
|
+
static void heapsortdpoints(ap::real_1d_array& x,
|
1067
|
+
ap::real_1d_array& y,
|
1068
|
+
ap::real_1d_array& d,
|
1069
|
+
int n)
|
1070
|
+
{
|
1071
|
+
int i;
|
1072
|
+
int j;
|
1073
|
+
int k;
|
1074
|
+
int t;
|
1075
|
+
double tmp;
|
1076
|
+
bool isascending;
|
1077
|
+
bool isdescending;
|
1078
|
+
|
1079
|
+
|
1080
|
+
//
|
1081
|
+
// Test for already sorted set
|
1082
|
+
//
|
1083
|
+
isascending = true;
|
1084
|
+
isdescending = true;
|
1085
|
+
for(i = 1; i <= n-1; i++)
|
1086
|
+
{
|
1087
|
+
isascending = isascending&&x(i)>x(i-1);
|
1088
|
+
isdescending = isdescending&&x(i)<x(i-1);
|
1089
|
+
}
|
1090
|
+
if( isascending )
|
1091
|
+
{
|
1092
|
+
return;
|
1093
|
+
}
|
1094
|
+
if( isdescending )
|
1095
|
+
{
|
1096
|
+
for(i = 0; i <= n-1; i++)
|
1097
|
+
{
|
1098
|
+
j = n-1-i;
|
1099
|
+
if( j<=i )
|
1100
|
+
{
|
1101
|
+
break;
|
1102
|
+
}
|
1103
|
+
tmp = x(i);
|
1104
|
+
x(i) = x(j);
|
1105
|
+
x(j) = tmp;
|
1106
|
+
tmp = y(i);
|
1107
|
+
y(i) = y(j);
|
1108
|
+
y(j) = tmp;
|
1109
|
+
tmp = d(i);
|
1110
|
+
d(i) = d(j);
|
1111
|
+
d(j) = tmp;
|
1112
|
+
}
|
1113
|
+
return;
|
1114
|
+
}
|
1115
|
+
|
1116
|
+
//
|
1117
|
+
// Special case: N=1
|
1118
|
+
//
|
1119
|
+
if( n==1 )
|
1120
|
+
{
|
1121
|
+
return;
|
1122
|
+
}
|
1123
|
+
|
1124
|
+
//
|
1125
|
+
// General case
|
1126
|
+
//
|
1127
|
+
i = 2;
|
1128
|
+
do
|
1129
|
+
{
|
1130
|
+
t = i;
|
1131
|
+
while(t!=1)
|
1132
|
+
{
|
1133
|
+
k = t/2;
|
1134
|
+
if( x(k-1)>=x(t-1) )
|
1135
|
+
{
|
1136
|
+
t = 1;
|
1137
|
+
}
|
1138
|
+
else
|
1139
|
+
{
|
1140
|
+
tmp = x(k-1);
|
1141
|
+
x(k-1) = x(t-1);
|
1142
|
+
x(t-1) = tmp;
|
1143
|
+
tmp = y(k-1);
|
1144
|
+
y(k-1) = y(t-1);
|
1145
|
+
y(t-1) = tmp;
|
1146
|
+
tmp = d(k-1);
|
1147
|
+
d(k-1) = d(t-1);
|
1148
|
+
d(t-1) = tmp;
|
1149
|
+
t = k;
|
1150
|
+
}
|
1151
|
+
}
|
1152
|
+
i = i+1;
|
1153
|
+
}
|
1154
|
+
while(i<=n);
|
1155
|
+
i = n-1;
|
1156
|
+
do
|
1157
|
+
{
|
1158
|
+
tmp = x(i);
|
1159
|
+
x(i) = x(0);
|
1160
|
+
x(0) = tmp;
|
1161
|
+
tmp = y(i);
|
1162
|
+
y(i) = y(0);
|
1163
|
+
y(0) = tmp;
|
1164
|
+
tmp = d(i);
|
1165
|
+
d(i) = d(0);
|
1166
|
+
d(0) = tmp;
|
1167
|
+
t = 1;
|
1168
|
+
while(t!=0)
|
1169
|
+
{
|
1170
|
+
k = 2*t;
|
1171
|
+
if( k>i )
|
1172
|
+
{
|
1173
|
+
t = 0;
|
1174
|
+
}
|
1175
|
+
else
|
1176
|
+
{
|
1177
|
+
if( k<i )
|
1178
|
+
{
|
1179
|
+
if( x(k)>x(k-1) )
|
1180
|
+
{
|
1181
|
+
k = k+1;
|
1182
|
+
}
|
1183
|
+
}
|
1184
|
+
if( x(t-1)>=x(k-1) )
|
1185
|
+
{
|
1186
|
+
t = 0;
|
1187
|
+
}
|
1188
|
+
else
|
1189
|
+
{
|
1190
|
+
tmp = x(k-1);
|
1191
|
+
x(k-1) = x(t-1);
|
1192
|
+
x(t-1) = tmp;
|
1193
|
+
tmp = y(k-1);
|
1194
|
+
y(k-1) = y(t-1);
|
1195
|
+
y(t-1) = tmp;
|
1196
|
+
tmp = d(k-1);
|
1197
|
+
d(k-1) = d(t-1);
|
1198
|
+
d(t-1) = tmp;
|
1199
|
+
t = k;
|
1200
|
+
}
|
1201
|
+
}
|
1202
|
+
}
|
1203
|
+
i = i-1;
|
1204
|
+
}
|
1205
|
+
while(i>=1);
|
1206
|
+
}
|
1207
|
+
|
1208
|
+
|
1209
|
+
/*************************************************************************
|
1210
|
+
Internal subroutine. Tridiagonal solver.
|
1211
|
+
*************************************************************************/
|
1212
|
+
static void solvetridiagonal(ap::real_1d_array a,
|
1213
|
+
ap::real_1d_array b,
|
1214
|
+
ap::real_1d_array c,
|
1215
|
+
ap::real_1d_array d,
|
1216
|
+
int n,
|
1217
|
+
ap::real_1d_array& x)
|
1218
|
+
{
|
1219
|
+
int k;
|
1220
|
+
double t;
|
1221
|
+
|
1222
|
+
x.setbounds(0, n-1);
|
1223
|
+
a(0) = 0;
|
1224
|
+
c(n-1) = 0;
|
1225
|
+
for(k = 1; k <= n-1; k++)
|
1226
|
+
{
|
1227
|
+
t = a(k)/b(k-1);
|
1228
|
+
b(k) = b(k)-t*c(k-1);
|
1229
|
+
d(k) = d(k)-t*d(k-1);
|
1230
|
+
}
|
1231
|
+
x(n-1) = d(n-1)/b(n-1);
|
1232
|
+
for(k = n-2; k >= 0; k--)
|
1233
|
+
{
|
1234
|
+
x(k) = (d(k)-c(k)*x(k+1))/b(k);
|
1235
|
+
}
|
1236
|
+
}
|
1237
|
+
|
1238
|
+
|
1239
|
+
/*************************************************************************
|
1240
|
+
Internal subroutine. Three-point differentiation
|
1241
|
+
*************************************************************************/
|
1242
|
+
static double diffthreepoint(double t,
|
1243
|
+
double x0,
|
1244
|
+
double f0,
|
1245
|
+
double x1,
|
1246
|
+
double f1,
|
1247
|
+
double x2,
|
1248
|
+
double f2)
|
1249
|
+
{
|
1250
|
+
double result;
|
1251
|
+
double a;
|
1252
|
+
double b;
|
1253
|
+
|
1254
|
+
t = t-x0;
|
1255
|
+
x1 = x1-x0;
|
1256
|
+
x2 = x2-x0;
|
1257
|
+
a = (f2-f0-x2/x1*(f1-f0))/(ap::sqr(x2)-x1*x2);
|
1258
|
+
b = (f1-f0-a*ap::sqr(x1))/x1;
|
1259
|
+
result = 2*a*t+b;
|
1260
|
+
return result;
|
1261
|
+
}
|
1262
|
+
|
1263
|
+
|
1264
|
+
|