libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
pappso::CubicSplineModel Class Reference

#include <cubicsplinemodel.h>

Public Member Functions

 CubicSplineModel ()
 CubicSplineModel (const QList< double > &x_values, const QList< double > &y_values)
 CubicSplineModel (const QMap< double, double > &x_y_values_map)
 CubicSplineModel (const CubicSplineModel &other)
CubicSplineModelclone (const CubicSplineModel &other)
virtual ~CubicSplineModel ()
void setup (const QList< double > &x_values, const QList< double > &y_values)
CubicSplineModeloperator= (const CubicSplineModel &other)
const QList< double > & getKnots () const
double evalSplineAt (double x_value) const
double derivative (const double x_value) const
double derivatives (const double x_value, unsigned order) const

Protected Attributes

QString m_name = "prova"
QList< double > m_constCoeffs
QList< double > m_linearCoeffs
QList< double > m_quadraticCoeffs
QList< double > m_cubicCoeffs
QList< double > m_knots

Detailed Description

Definition at line 23 of file cubicsplinemodel.h.

Constructor & Destructor Documentation

◆ CubicSplineModel() [1/4]

pappso::CubicSplineModel::CubicSplineModel ( )

Definition at line 24 of file cubicsplinemodel.cpp.

25{
26 // qDebug() << "Allocating:" << this;
27}

Referenced by CubicSplineModel(), CubicSplineModel(), clone(), and operator=().

◆ CubicSplineModel() [2/4]

pappso::CubicSplineModel::CubicSplineModel ( const QList< double > & x_values,
const QList< double > & y_values )
explicit

Definition at line 29 of file cubicsplinemodel.cpp.

31{
32 // qDebug() << "Allocating:" << this;
33
34 Q_ASSERT(x_values.size() == y_values.size());
35 Q_ASSERT(x_values.size() >= 2);
36
37 double eps = std::numeric_limits<double>::epsilon();
38
39 // Ensure the x_values are sorted increasingly.
40
41 Q_ASSERT(std::adjacent_find(
42 x_values.cbegin(), x_values.cend(), [=](double a, double b) {
43 return !(b > a + eps);
44 }) == x_values.cend());
45
46 setup(x_values, y_values);
47
48 // qDebug() << "Number of knots:" << m_knots.size();
49}
void setup(const QList< double > &x_values, const QList< double > &y_values)

References pappso::a, pappso::b, and setup().

◆ CubicSplineModel() [3/4]

pappso::CubicSplineModel::CubicSplineModel ( const QMap< double, double > & x_y_values_map)
explicit

Definition at line 52 of file cubicsplinemodel.cpp.

53 : CubicSplineModel(x_y_values_map.keys(), x_y_values_map.values())
54{
55}

References CubicSplineModel().

◆ CubicSplineModel() [4/4]

pappso::CubicSplineModel::CubicSplineModel ( const CubicSplineModel & other)

Definition at line 57 of file cubicsplinemodel.cpp.

58{
59 // qDebug() << "Allocating:" << this;
60 m_constCoeffs = other.m_constCoeffs;
61 m_linearCoeffs = other.m_linearCoeffs;
62 m_quadraticCoeffs = other.m_quadraticCoeffs;
63 m_cubicCoeffs = other.m_cubicCoeffs;
64 m_knots = other.m_knots;
65}
QList< double > m_quadraticCoeffs
QList< double > m_linearCoeffs
QList< double > m_cubicCoeffs
QList< double > m_constCoeffs

References CubicSplineModel(), m_constCoeffs, m_cubicCoeffs, m_knots, m_linearCoeffs, and m_quadraticCoeffs.

◆ ~CubicSplineModel()

pappso::CubicSplineModel::~CubicSplineModel ( )
virtual

Definition at line 83 of file cubicsplinemodel.cpp.

84{
85 // qDebug() << "Destructing the cubic spline model";
86}

Member Function Documentation

◆ clone()

CubicSplineModel * pappso::CubicSplineModel::clone ( const CubicSplineModel & other)

Definition at line 68 of file cubicsplinemodel.cpp.

69{
70 // qDebug() << "Cloning.";
71 CubicSplineModel *copy_p = new CubicSplineModel();
72
73 copy_p->m_name = other.m_name;
74 copy_p->m_constCoeffs = other.m_constCoeffs;
75 copy_p->m_linearCoeffs = other.m_linearCoeffs;
76 copy_p->m_quadraticCoeffs = other.m_quadraticCoeffs;
77 copy_p->m_cubicCoeffs = other.m_cubicCoeffs;
78 copy_p->m_knots = other.m_knots;
79
80 return copy_p;
81}

References CubicSplineModel(), m_constCoeffs, m_cubicCoeffs, m_knots, m_linearCoeffs, m_name, and m_quadraticCoeffs.

◆ derivative()

double pappso::CubicSplineModel::derivative ( const double x_value) const

Definition at line 197 of file cubicsplinemodel.cpp.

198{
199 return derivatives(x_value, 1);
200}
double derivatives(const double x_value, unsigned order) const

References derivatives().

Referenced by pappso::spline_bisection().

◆ derivatives()

double pappso::CubicSplineModel::derivatives ( const double x_value,
unsigned order ) const

Definition at line 203 of file cubicsplinemodel.cpp.

204{
205 // qDebug() << "Size of m_knots:" << m_knots.size();
206
207 Q_ASSERT(x_value >= m_knots.front() && x_value <= m_knots.back());
208
209 Q_ASSERT(order >= 1 && order <= 3);
210
211 // determine index of closest node left of (or exactly at) x_value
212 unsigned i = static_cast<unsigned>(
213 std::lower_bound(m_knots.begin(), m_knots.end(), x_value) -
214 m_knots.begin());
215
216 if(m_knots[i] > x_value ||
217 m_knots.back() ==
218 x_value) // also, i must not point to last index in 'm_knots',
219 // since all other vectors are one element shorter
220 {
221 --i;
222 }
223
224 const double delta_x = x_value - m_knots[i];
225 if(order == 1)
226 {
227 return m_linearCoeffs[i] + 2 * m_quadraticCoeffs[i] * delta_x +
228 3 * m_cubicCoeffs[i] * delta_x * delta_x;
229 }
230 else if(order == 2)
231 {
232 return 2 * m_quadraticCoeffs[i] + 6 * m_cubicCoeffs[i] * delta_x;
233 }
234 else
235 {
236 return 6 * m_cubicCoeffs[i];
237 }
238}

References m_cubicCoeffs, m_knots, m_linearCoeffs, and m_quadraticCoeffs.

Referenced by derivative().

◆ evalSplineAt()

double pappso::CubicSplineModel::evalSplineAt ( double x_value) const

Definition at line 169 of file cubicsplinemodel.cpp.

170{
171 Q_ASSERT(x_value >= m_knots.front() || x_value <= m_knots.back());
172
173 // What is the index of the closest knot left of x_value (or the same).
174 unsigned i = static_cast<unsigned>(
175 std::lower_bound(m_knots.begin(), m_knots.end(), x_value) -
176 m_knots.begin());
177
178 if(m_knots[i] > x_value || m_knots.back() == x_value)
179 {
180 --i;
181 }
182
183 const double delta_x = x_value - m_knots[i];
184
185 double eval_value =
186 ((m_cubicCoeffs[i] * delta_x + m_quadraticCoeffs[i]) * delta_x +
187 m_linearCoeffs[i]) *
188 delta_x +
189 m_constCoeffs[i];
190
191 // qDebug() << "Now returning evaluation value:" << eval_value;
192
193 return eval_value;
194}

References m_constCoeffs, m_cubicCoeffs, m_knots, m_linearCoeffs, and m_quadraticCoeffs.

Referenced by pappso::spline_bisection().

◆ getKnots()

const QList< double > & pappso::CubicSplineModel::getKnots ( ) const

Definition at line 163 of file cubicsplinemodel.cpp.

164{
165 return m_knots;
166}

References m_knots.

◆ operator=()

CubicSplineModel & pappso::CubicSplineModel::operator= ( const CubicSplineModel & other)

Definition at line 147 of file cubicsplinemodel.cpp.

148{
149 if(&other == this)
150 return *this;
151
152 m_name = other.m_name;
153 m_constCoeffs = other.m_constCoeffs;
154 m_linearCoeffs = other.m_linearCoeffs;
155 m_quadraticCoeffs = other.m_quadraticCoeffs;
156 m_cubicCoeffs = other.m_cubicCoeffs;
157 m_knots = other.m_knots;
158
159 return *this;
160}

References CubicSplineModel(), m_constCoeffs, m_cubicCoeffs, m_knots, m_linearCoeffs, m_name, and m_quadraticCoeffs.

◆ setup()

void pappso::CubicSplineModel::setup ( const QList< double > & x_values,
const QList< double > & y_values )

Definition at line 89 of file cubicsplinemodel.cpp.

91{
92 // qDebug() << "Setting up with x_values size:" << x_values.size();
93
94 Q_ASSERT(x_values.size() == y_values.size());
95
96 const size_t n = x_values.size() - 1;
97
98 std::vector<double> h;
99 h.reserve(n);
100 m_constCoeffs.reserve(n);
101 m_knots.reserve(n + 1);
102 // do the 0'th element manually, since the loop below only starts at 1
103 h.push_back(x_values[1] - x_values[0]);
104 m_knots.push_back(x_values[0]);
105 m_constCoeffs.push_back(y_values[0]);
106
107 std::vector<double> mu(n, 0.0);
108 std::vector<double> z(n, 0.0);
109 for(unsigned i = 1; i < n; ++i)
110 {
111 h.push_back(x_values[i + 1] - x_values[i]);
112 const double l =
113 2 * (x_values[i + 1] - x_values[i - 1]) - h[i - 1] * mu[i - 1];
114 mu[i] = h[i] / l;
115 z[i] = (3 *
116 (y_values[i + 1] * h[i - 1] -
117 y_values[i] * (x_values[i + 1] - x_values[i - 1]) +
118 y_values[i - 1] * h[i]) /
119 (h[i - 1] * h[i]) -
120 h[i - 1] * z[i - 1]) /
121 l;
122 // store x,y -- required for evaluation later on
123 m_knots.push_back(x_values[i]);
124 m_constCoeffs.push_back(y_values[i]);
125 }
126 // 'm_knots' needs to be full length (all other member vectors (except
127 // m_quadraticCoeffs) are one element shorter)
128 m_knots.push_back(x_values[n]);
129
130 m_linearCoeffs.resize(n);
131 m_cubicCoeffs.resize(n);
132 m_quadraticCoeffs.resize(n + 1);
133 m_quadraticCoeffs.back() = 0;
134 for(int j = static_cast<int>(n) - 1; j >= 0; --j)
135 {
136 m_quadraticCoeffs[j] = z[j] - mu[j] * m_quadraticCoeffs[j + 1];
137 m_linearCoeffs[j] =
138 (y_values[j + 1] - y_values[j]) / h[j] -
139 h[j] * (m_quadraticCoeffs[j + 1] + 2 * m_quadraticCoeffs[j]) / 3;
140 m_cubicCoeffs[j] =
141 (m_quadraticCoeffs[j + 1] - m_quadraticCoeffs[j]) / (3 * h[j]);
142 }
143 // qDebug() << "m_knots size:" << m_knots.size();
144}

References m_constCoeffs, m_cubicCoeffs, m_knots, m_linearCoeffs, m_quadraticCoeffs, and pappso::z.

Referenced by CubicSplineModel().

Member Data Documentation

◆ m_constCoeffs

QList<double> pappso::CubicSplineModel::m_constCoeffs
protected

Definition at line 49 of file cubicsplinemodel.h.

Referenced by CubicSplineModel(), clone(), evalSplineAt(), operator=(), and setup().

◆ m_cubicCoeffs

QList<double> pappso::CubicSplineModel::m_cubicCoeffs
protected

Definition at line 52 of file cubicsplinemodel.h.

Referenced by CubicSplineModel(), clone(), derivatives(), evalSplineAt(), operator=(), and setup().

◆ m_knots

QList<double> pappso::CubicSplineModel::m_knots
protected

◆ m_linearCoeffs

QList<double> pappso::CubicSplineModel::m_linearCoeffs
protected

Definition at line 50 of file cubicsplinemodel.h.

Referenced by CubicSplineModel(), clone(), derivatives(), evalSplineAt(), operator=(), and setup().

◆ m_name

QString pappso::CubicSplineModel::m_name = "prova"
protected

Definition at line 48 of file cubicsplinemodel.h.

Referenced by clone(), and operator=().

◆ m_quadraticCoeffs

QList<double> pappso::CubicSplineModel::m_quadraticCoeffs
protected

Definition at line 51 of file cubicsplinemodel.h.

Referenced by CubicSplineModel(), clone(), derivatives(), evalSplineAt(), operator=(), and setup().


The documentation for this class was generated from the following files: