1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (c) 1983-2001 Kevin Rosenberg
5 ** $Id: cubicinterp.cpp,v 1.1 2001/02/09 01:54:21 kevin Exp $
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License (version 2) as
9 ** published by the Free Software Foundation.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ******************************************************************************/
22 #include "ctsupport.h"
23 #include "cubicinterp.h"
26 CubicInterpolator::CubicInterpolator (const double* const y, const int n)
29 m_pdY2 = new double [n];
30 m_pdY2[0] = 0.0; // second deriviative = 0 at beginning and end
33 double* temp = new double [n - 1];
35 for (int i = 1; i < n - 1; i++) {
36 double t = 2 + (0.5 * m_pdY2[i-1]);
37 temp[i] = y[i+1] + y[i-1] - y[i] - y[i];
38 temp[i] = (3 * temp[i] - 0.5 * temp[i-1]) / t;
42 for (i = n - 2; i >= 0; i--)
43 m_pdY2[i] = temp[i] + m_pdY2[i] * m_pdY2[i + 1];
48 CubicInterpolator::~CubicInterpolator ()
55 CubicInterpolator::interpolate (double x)
57 const static double oneSixth = (1. / 6.);
58 int lo = static_cast<int>(floor(x));
61 if (lo < 0 || hi >= m_n) {
62 sys_error (ERR_SEVERE, "X range out of bounds [CubicInterpolator::interpolate]");
68 double y = loFr * m_pdY[lo] + hiFr * m_pdY[hi];
69 y += oneSixth * ((loFr*loFr*loFr - loFr) * m_pdY2[lo] + (hiFr*hiFr*hiFr - hiFr) * m_pdY2[hi]);