Skip to content

Commit 5c02ff4

Browse files
committed
[MathMore] allow using several methods of Polynomial.h in const context
- Remove unused `fRoots` data member (it was cleared before each root computation and provided no persistent state). - Return polynomial roots by value instead of via internal storage. - Mark the affected query/utility methods as `const` to make the class usable in const contexts. - Apply clang-format to the modified sections.
1 parent 4ac2999 commit 5c02ff4

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

math/mathmore/inc/Math/Polynomial.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,20 @@ class Polynomial : public ParamFunction<IParamGradFunction>,
116116
equation is very small. In that case it might be more robust to use the numerical method, by calling directly FindNumRoots()
117117
118118
*/
119-
const std::vector<std::complex <double> > & FindRoots();
119+
const std::vector<std::complex<double>> &FindRoots() const;
120120

121121
/**
122122
Find the only the real polynomial roots.
123123
For n <= 4, the roots are found analytically while for larger order an iterative numerical method is used
124124
The numerical method used is from GSL (see <A HREF="https://www.gnu.org/software/gsl/doc/html/poly.html">documentation</A> )
125125
*/
126-
std::vector<double > FindRealRoots();
126+
std::vector<double> FindRealRoots() const;
127127

128128
/**
129129
Find the polynomial roots using always an iterative numerical methods
130130
The numerical method used is from GSL (see <A HREF="https://www.gnu.org/software/gsl/doc/html/poly.html">documentation</A> )
131131
*/
132-
const std::vector<std::complex <double> > & FindNumRoots();
132+
const std::vector<std::complex<double>> &FindNumRoots() const;
133133

134134
/**
135135
Order of Polynomial
@@ -166,9 +166,7 @@ class Polynomial : public ParamFunction<IParamGradFunction>,
166166
mutable std::vector<double> fDerived_params;
167167

168168
// roots
169-
170-
std::vector< std::complex < double > > fRoots;
171-
169+
mutable std::vector<std::complex<double>> fRoots;
172170
};
173171

174172
} // namespace Math

math/mathmore/src/Polynomial.cxx

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,12 @@ IGenFunction * Polynomial::Clone() const {
147147
return f;
148148
}
149149

150+
const std::vector<std::complex<double>> &Polynomial::FindRoots() const
151+
{
150152

151-
const std::vector< std::complex <double> > & Polynomial::FindRoots(){
152-
153-
154-
// check if order is correct
155-
unsigned int n = fOrder;
156-
while ( Parameters()[n] == 0 ) {
153+
// check if order is correct
154+
unsigned int n = fOrder;
155+
while (Parameters()[n] == 0) {
157156
n--;
158157
}
159158

@@ -230,26 +229,25 @@ const std::vector< std::complex <double> > & Polynomial::FindRoots(){
230229
}
231230

232231
return fRoots;
233-
234-
}
235-
236-
237-
std::vector< double > Polynomial::FindRealRoots(){
238-
FindRoots();
239-
std::vector<double> roots;
240-
roots.reserve(fOrder);
241-
for (unsigned int i = 0; i < fOrder; ++i) {
242-
if (fRoots[i].imag() == 0)
243-
roots.push_back( fRoots[i].real() );
244-
}
245-
return roots;
246232
}
247-
const std::vector< std::complex <double> > & Polynomial::FindNumRoots(){
248233

234+
std::vector<double> Polynomial::FindRealRoots() const
235+
{
236+
FindRoots();
237+
std::vector<double> roots;
238+
roots.reserve(fOrder);
239+
for (unsigned int i = 0; i < fOrder; ++i) {
240+
if (fRoots[i].imag() == 0)
241+
roots.push_back(fRoots[i].real());
242+
}
243+
return roots;
244+
}
245+
const std::vector<std::complex<double>> &Polynomial::FindNumRoots() const
246+
{
249247

250-
// check if order is correct
251-
unsigned int n = fOrder;
252-
while ( Parameters()[n] == 0 ) {
248+
// check if order is correct
249+
unsigned int n = fOrder;
250+
while (Parameters()[n] == 0) {
253251
n--;
254252
}
255253
fRoots.clear();
@@ -271,6 +269,5 @@ const std::vector< std::complex <double> > & Polynomial::FindNumRoots(){
271269
return fRoots;
272270
}
273271

274-
275272
} // namespace Math
276273
} // namespace ROOT

0 commit comments

Comments
 (0)