merged tag ooo/DEV300_m102
[LibreOffice.git] / hwpfilter / source / cspline.cpp
blob7a07c23d5f842ea432cc67528ea9bdd6be445482
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // Natural, Clamped, or Periodic Cubic Splines
30 // Input: A list of N+1 points (x_i,a_i), 0 <= i <= N, which are sampled
31 // from a function, a_i = f(x_i). The function f is unknown. Boundary
32 // conditions are
33 // (1) Natural splines: f"(x_0) = f"(x_N) = 0
34 // (2) Clamped splines: f'(x_0) and f'(x_N) are user-specified.
35 // (3) Periodic splines: f(x_0) = f(x_N) [in which case a_N = a_0 is
36 // required in the input], f'(x_0) = f'(x_N), and f"(x_0) = f"(x_N).
38 // Output: b_i, c_i, d_i, 0 <= i <= N-1, which are coefficients for the cubic
39 // spline S_i(x) = a_i + b_i(x-x_i) + c_i(x-x_i)^2 + d_i(x-x_i)^3 for
40 // x_i <= x < x_{i+1}.
42 // The natural and clamped algorithms were implemented from
44 // Numerical Analysis, 3rd edition
45 // Richard L. Burden and J. Douglas Faires
46 // Prindle, Weber & Schmidt
47 // Boston, 1985, pp. 122-124.
49 // The algorithm sets up a tridiagonal linear system of equations in the
50 // c_i values. This can be solved in O(N) time.
52 // The periodic spline algorithm was implemented from my own derivation. The
53 // linear system of equations is not tridiagonal. For now I use a standard
54 // linear solver that does not take advantage of the sparseness of the
55 // matrix. Therefore for very large N, you may have to worry about memory
56 // usage.
58 #include "solver.h"
59 //-----------------------------------------------------------------------------
60 void NaturalSpline (int N, double* x, double* a, double*& b, double*& c,
61 double*& d)
63 const double oneThird = 1.0/3.0;
65 int i;
66 double* h = new double[N];
67 double* hdiff = new double[N];
68 double* alpha = new double[N];
70 for (i = 0; i < N; i++){
71 h[i] = x[i+1]-x[i];
74 for (i = 1; i < N; i++)
75 hdiff[i] = x[i+1]-x[i-1];
77 for (i = 1; i < N; i++)
79 double numer = 3.0*(a[i+1]*h[i-1]-a[i]*hdiff[i]+a[i-1]*h[i]);
80 double denom = h[i-1]*h[i];
81 alpha[i] = numer/denom;
84 double* ell = new double[N+1];
85 double* mu = new double[N];
86 double* z = new double[N+1];
87 double recip;
89 ell[0] = 1.0;
90 mu[0] = 0.0;
91 z[0] = 0.0;
93 for (i = 1; i < N; i++)
95 ell[i] = 2.0*hdiff[i]-h[i-1]*mu[i-1];
96 recip = 1.0/ell[i];
97 mu[i] = recip*h[i];
98 z[i] = recip*(alpha[i]-h[i-1]*z[i-1]);
100 ell[N] = 1.0;
101 z[N] = 0.0;
103 b = new double[N];
104 c = new double[N+1];
105 d = new double[N];
107 c[N] = 0.0;
109 for (i = N-1; i >= 0; i--)
111 c[i] = z[i]-mu[i]*c[i+1];
112 recip = 1.0/h[i];
113 b[i] = recip*(a[i+1]-a[i])-h[i]*(c[i+1]+2.0*c[i])*oneThird;
114 d[i] = oneThird*recip*(c[i+1]-c[i]);
117 delete[] h;
118 delete[] hdiff;
119 delete[] alpha;
120 delete[] ell;
121 delete[] mu;
122 delete[] z;
125 void PeriodicSpline (int N, double* x, double* a, double*& b, double*& c,
126 double*& d)
128 double* h = new double[N];
129 int i;
130 for (i = 0; i < N; i++)
131 h[i] = x[i+1]-x[i];
133 mgcLinearSystemD sys;
134 double** mat = sys.NewMatrix(N+1); // guaranteed to be zeroed memory
135 c = sys.NewVector(N+1); // guaranteed to be zeroed memory
137 // c[0] - c[N] = 0
138 mat[0][0] = +1.0f;
139 mat[0][N] = -1.0f;
141 // h[i-1]*c[i-1]+2*(h[i-1]+h[i])*c[i]+h[i]*c[i+1] =
142 // 3*{(a[i+1]-a[i])/h[i] - (a[i]-a[i-1])/h[i-1]}
143 for (i = 1; i <= N-1; i++)
145 mat[i][i-1] = h[i-1];
146 mat[i][i ] = 2.0f*(h[i-1]+h[i]);
147 mat[i][i+1] = h[i];
148 c[i] = 3.0f*((a[i+1]-a[i])/h[i] - (a[i]-a[i-1])/h[i-1]);
151 // "wrap around equation" for periodicity
152 // h[N-1]*c[N-1]+2*(h[N-1]+h[0])*c[0]+h[0]*c[1] =
153 // 3*{(a[1]-a[0])/h[0] - (a[0]-a[N-1])/h[N-1]}
154 mat[N][N-1] = h[N-1];
155 mat[N][0 ] = 2.0f*(h[N-1]+h[0]);
156 mat[N][1 ] = h[0];
157 c[N] = 3.0f*((a[1]-a[0])/h[0] - (a[0]-a[N-1])/h[N-1]);
159 // solve for c[0] through c[N]
160 sys.Solve(N+1,mat,c);
162 const double oneThird = 1.0/3.0;
163 b = new double[N];
164 d = new double[N];
165 for (i = 0; i < N; i++)
167 b[i] = (a[i+1]-a[i])/h[i] - oneThird*(c[i+1]+2.0f*c[i])*h[i];
168 d[i] = oneThird*(c[i+1]-c[i])/h[i];
171 delete[] h;
172 sys.DeleteMatrix(N+1,mat);