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
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
59 //-----------------------------------------------------------------------------
60 void NaturalSpline (int N
, double* x
, double* a
, double*& b
, double*& c
,
63 const double oneThird
= 1.0/3.0;
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
++){
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];
93 for (i
= 1; i
< N
; i
++)
95 ell
[i
] = 2.0*hdiff
[i
]-h
[i
-1]*mu
[i
-1];
98 z
[i
] = recip
*(alpha
[i
]-h
[i
-1]*z
[i
-1]);
109 for (i
= N
-1; i
>= 0; i
--)
111 c
[i
] = z
[i
]-mu
[i
]*c
[i
+1];
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
]);
125 void PeriodicSpline (int N
, double* x
, double* a
, double*& b
, double*& c
,
128 double* h
= new double[N
];
130 for (i
= 0; i
< N
; 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
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
]);
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]);
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;
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
];
172 sys
.DeleteMatrix(N
+1,mat
);