LibreOffice Module hwpfilter (master) 1
cspline.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20// Natural, Clamped, or Periodic Cubic Splines
21//
22// Input: A list of N+1 points (x_i,a_i), 0 <= i <= N, which are sampled
23// from a function, a_i = f(x_i). The function f is unknown. Boundary
24// conditions are
25// (1) Natural splines: f"(x_0) = f"(x_N) = 0
26// (2) Clamped splines: f'(x_0) and f'(x_N) are user-specified.
27// (3) Periodic splines: f(x_0) = f(x_N) [in which case a_N = a_0 is
28// required in the input], f'(x_0) = f'(x_N), and f"(x_0) = f"(x_N).
29//
30// Output: b_i, c_i, d_i, 0 <= i <= N-1, which are coefficients for the cubic
31// spline S_i(x) = a_i + b_i(x-x_i) + c_i(x-x_i)^2 + d_i(x-x_i)^3 for
32// x_i <= x < x_{i+1}.
33//
34// The natural and clamped algorithms were implemented from
35//
36// Numerical Analysis, 3rd edition
37// Richard L. Burden and J. Douglas Faires
38// Prindle, Weber & Schmidt
39// Boston, 1985, pp. 122-124.
40//
41// The algorithm sets up a tridiagonal linear system of equations in the
42// c_i values. This can be solved in O(N) time.
43//
44// The periodic spline algorithm was implemented from my own derivation. The
45// linear system of equations is not tridiagonal. For now I use a standard
46// linear solver that does not take advantage of the sparseness of the
47// matrix. Therefore for very large N, you may have to worry about memory
48// usage.
49
50#include <sal/config.h>
51#include <memory>
52
53#include "cspline.h"
54#include "solver.h"
55
56void NaturalSpline (int N, const double* x, const double* a, std::unique_ptr<double[]>& b, std::unique_ptr<double[]>& c,
57 std::unique_ptr<double[]>& d)
58{
59 const double oneThird = 1.0/3.0;
60
61 int i;
62 std::unique_ptr<double[]> h(new double[N]);
63 std::unique_ptr<double[]> hdiff(new double[N]);
64 std::unique_ptr<double[]> alpha(new double[N]);
65
66 for (i = 0; i < N; i++){
67 h[i] = x[i+1]-x[i];
68 }
69
70 for (i = 1; i < N; i++)
71 hdiff[i] = x[i+1]-x[i-1];
72
73 for (i = 1; i < N; i++)
74 {
75 double numer = 3.0*(a[i+1]*h[i-1]-a[i]*hdiff[i]+a[i-1]*h[i]);
76 double denom = h[i-1]*h[i];
77 alpha[i] = numer/denom;
78 }
79
80 std::unique_ptr<double[]> ell(new double[N+1]);
81 std::unique_ptr<double[]> mu(new double[N]);
82 std::unique_ptr<double[]> z(new double[N+1]);
83 double recip;
84
85 ell[0] = 1.0;
86 mu[0] = 0.0;
87 z[0] = 0.0;
88
89 for (i = 1; i < N; i++)
90 {
91 ell[i] = 2.0*hdiff[i]-h[i-1]*mu[i-1];
92 recip = 1.0/ell[i];
93 mu[i] = recip*h[i];
94 z[i] = recip*(alpha[i]-h[i-1]*z[i-1]);
95 }
96 ell[N] = 1.0;
97 z[N] = 0.0;
98
99 b.reset(new double[N]);
100 c.reset(new double[N+1]);
101 d.reset(new double[N]);
102
103 c[N] = 0.0;
104
105 for (i = N-1; i >= 0; i--)
106 {
107 c[i] = z[i]-mu[i]*c[i+1];
108 recip = 1.0/h[i];
109 b[i] = recip*(a[i+1]-a[i])-h[i]*(c[i+1]+2.0*c[i])*oneThird;
110 d[i] = oneThird*recip*(c[i+1]-c[i]);
111 }
112}
113
114void PeriodicSpline (int N, const double* x, const double* a, std::unique_ptr<double[]>& b, std::unique_ptr<double[]>& c,
115 std::unique_ptr<double[]>& d)
116{
117 std::unique_ptr<double[]> h(new double[N]);
118 int i;
119 for (i = 0; i < N; i++)
120 h[i] = x[i+1]-x[i];
121
122 std::unique_ptr<std::unique_ptr<double[]>[]> mat = mgcLinearSystemD::NewMatrix(N+1); // guaranteed to be zeroed memory
123 c = mgcLinearSystemD::NewVector(N+1); // guaranteed to be zeroed memory
124
125 // c[0] - c[N] = 0
126 mat[0][0] = +1.0f;
127 mat[0][N] = -1.0f;
128
129 // h[i-1]*c[i-1]+2*(h[i-1]+h[i])*c[i]+h[i]*c[i+1] =
130 // 3*{(a[i+1]-a[i])/h[i] - (a[i]-a[i-1])/h[i-1]}
131 for (i = 1; i <= N-1; i++)
132 {
133 mat[i][i-1] = h[i-1];
134 mat[i][i ] = 2.0f*(h[i-1]+h[i]);
135 mat[i][i+1] = h[i];
136 c[i] = 3.0f*((a[i+1]-a[i])/h[i] - (a[i]-a[i-1])/h[i-1]);
137 }
138
139 // "wrap around equation" for periodicity
140 // h[N-1]*c[N-1]+2*(h[N-1]+h[0])*c[0]+h[0]*c[1] =
141 // 3*{(a[1]-a[0])/h[0] - (a[0]-a[N-1])/h[N-1]}
142 mat[N][N-1] = h[N-1];
143 mat[N][0 ] = 2.0f*(h[N-1]+h[0]);
144 mat[N][1 ] = h[0];
145 c[N] = 3.0f*((a[1]-a[0])/h[0] - (a[0]-a[N-1])/h[N-1]);
146
147 // solve for c[0] through c[N]
148 mgcLinearSystemD::Solve(N+1,mat,c.get());
149
150 const double oneThird = 1.0/3.0;
151 b.reset(new double[N]);
152 d.reset(new double[N]);
153 for (i = 0; i < N; i++)
154 {
155 b[i] = (a[i+1]-a[i])/h[i] - oneThird*(c[i+1]+2.0f*c[i])*h[i];
156 d[i] = oneThird*(c[i+1]-c[i])/h[i];
157 }
158}
159
160/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double d
static bool Solve(int N, std::unique_ptr< std::unique_ptr< double[]>[]> const &A, double *b)
Definition: solver.cxx:47
static std::unique_ptr< double[]> NewVector(int N)
Definition: solver.cxx:38
static std::unique_ptr< std::unique_ptr< double[]>[]> NewMatrix(int N)
Definition: solver.cxx:25
void PeriodicSpline(int N, const double *x, const double *a, std::unique_ptr< double[]> &b, std::unique_ptr< double[]> &c, std::unique_ptr< double[]> &d)
Definition: cspline.cxx:114
void NaturalSpline(int N, const double *x, const double *a, std::unique_ptr< double[]> &b, std::unique_ptr< double[]> &c, std::unique_ptr< double[]> &d)
Definition: cspline.cxx:56
float x
float z
uno_Any a
constexpr double alpha[nDetails]
int i
sal_Int32 h
#define N