iRoCS Toolbox  1.1.0
ATBPolynomial.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 /*======================================================================*/
29 /*======================================================================*/
30 
31 #ifndef ATBPOLYNOMIAL_HH
32 #define ATBPOLYNOMIAL_HH
33 
34 #ifdef HAVE_CONFIG_H
35 #include <config.hh>
36 #endif
37 
38 #include "TypeTraits.hh"
39 
40 #include <complex>
41 #include <vector>
42 #include <iostream>
43 #include <cmath>
44 
45 // #define PROFILE
46 
47 #ifdef PROFILE
48 #include "ATBTiming.hh"
49 #endif
50 
51 namespace atb
52 {
53 /*======================================================================*/
63 /*======================================================================*/
64  template<typename CoeffT>
65  class Polynomial
66  {
67 
68  public:
69 
70 /*======================================================================*/
75 /*======================================================================*/
76  Polynomial();
77 
78 /*======================================================================*/
88 /*======================================================================*/
89  Polynomial(const std::vector<CoeffT>& coeffs);
90 
91 /*======================================================================*/
101 /*======================================================================*/
102  Polynomial(const Polynomial<CoeffT>& p);
103 
104 /*======================================================================*/
108 /*======================================================================*/
109  ~Polynomial();
110 
111 /*======================================================================*/
121 /*======================================================================*/
123 
124 /*======================================================================*/
130 /*======================================================================*/
131  size_t degree() const;
132 
133 /*======================================================================*/
140 /*======================================================================*/
141  const CoeffT& coefficient(size_t i) const;
142 
143 /*======================================================================*/
158 /*======================================================================*/
159  CoeffT& coefficient(size_t i);
160 
161 /*======================================================================*/
168 /*======================================================================*/
169  template<typename DomainT>
170  CoeffT operator()(DomainT x) const;
171 
172 /*======================================================================*/
184 /*======================================================================*/
185  std::vector<typename traits<CoeffT>::ComplexT> roots(
186  bool polish = true, bool sort = false);
187 
188 /*======================================================================*/
196 /*======================================================================*/
197  Polynomial<CoeffT> derivative(size_t degree = 1);
198 
199 /*======================================================================*/
207 /*======================================================================*/
208  Polynomial<CoeffT> indefiniteIntegral(size_t degree = 1);
209 
210 /*======================================================================*/
219 /*======================================================================*/
220  bool operator==(const Polynomial<CoeffT>& p);
221 
222 /*======================================================================*/
231 /*======================================================================*/
232  bool operator!=(const Polynomial<CoeffT>& p);
233 
234 /*======================================================================*/
240 /*======================================================================*/
242 
243 /*======================================================================*/
250 /*======================================================================*/
252 
253 /*======================================================================*/
261 /*======================================================================*/
263 
264 /*======================================================================*/
272 /*======================================================================*/
274 
275 /*======================================================================*/
283 /*======================================================================*/
284  Polynomial<CoeffT>& operator*=(const CoeffT& alpha);
285 
286 /*======================================================================*/
294 /*======================================================================*/
295  Polynomial<CoeffT>& operator/=(const CoeffT& alpha);
296 
297 /*======================================================================*/
303 /*======================================================================*/
304  static Polynomial<CoeffT> const &zero();
305 
306 /*======================================================================*/
313 /*======================================================================*/
314  static Polynomial<CoeffT> const &one();
315 
316 /*======================================================================*/
340 /*======================================================================*/
342  int n, double sigma = 1.0 / std::sqrt(2.0));
343 
344  private:
345 
346  void _trimCoefficients();
347 
348  static std::complex<double>& _laguer(
349  const std::vector< std::complex<double> >& a,
350  std::complex<double>& x);
351 
352  std::vector<CoeffT> _coeffs;
353  mutable size_t _degree;
354  mutable bool _degreeCached;
355 
356  static CoeffT const _zeroCoeff;
357 
358  static Polynomial<CoeffT> const _zero;
359  static Polynomial<CoeffT> const _one;
360 
361 #ifdef PROFILE
362  mutable ptrdiff_t __p1, __p2, __p3;
363  mutable ptrdiff_t __n1, __n2, __n3;
364 #endif
365 
366  };
367 
368 /*======================================================================*/
376 /*======================================================================*/
377  template<typename CoeffT>
379  const Polynomial<CoeffT>& p2);
380 
381 /*======================================================================*/
389 /*======================================================================*/
390  template<typename CoeffT>
392  const Polynomial<CoeffT>& p2);
393 
394 /*======================================================================*/
402 /*======================================================================*/
403  template<typename CoeffT>
405  const Polynomial<CoeffT>& p2);
406 
407 /*======================================================================*/
415 /*======================================================================*/
416  template<typename CoeffT>
417  Polynomial<CoeffT> pow(const Polynomial<CoeffT>& p, int exponential);
418 
419 /*======================================================================*/
427 /*======================================================================*/
428  template<typename CoeffT>
430  const CoeffT& alpha);
431 
432 /*======================================================================*/
440 /*======================================================================*/
441  template<typename CoeffT>
442  Polynomial<CoeffT> operator*(const CoeffT& alpha,
443  const Polynomial<CoeffT>& p);
444 
445 /*======================================================================*/
453 /*======================================================================*/
454  template<typename CoeffT>
456  const CoeffT& alpha);
457 
458 /*======================================================================*/
467 /*======================================================================*/
468  template<typename CoeffT>
469  std::ostream& operator<<(std::ostream& os, const Polynomial<CoeffT>& p);
470 
471 }
472 
473 #include "ATBPolynomial.icc"
474 
475 #endif
static Polynomial< CoeffT > hermite(int n, double sigma=1.0/std::sqrt(2.0))
Get the normalized Hermite polynomial of given degree.
Polynomial< CoeffT > & operator*=(const Polynomial< CoeffT > &p)
Multiplication assignment operator.
bool operator!=(const Polynomial< CoeffT > &p)
Comparison of the given polynomial with this polynomial.
~Polynomial()
Destructor.
const CoeffT & coefficient(size_t i) const
Read-Only accessor to the i&#39;th polynomial coefficient.
CoeffT operator()(DomainT x) const
Evaluates the polynomial at position x.
Polynomial< CoeffT > & operator/=(const CoeffT &alpha)
Division assignment operator.
Polynomial< CoeffT > operator-() const
Unary minus operator.
Polynomial< CoeffT > operator/(const Polynomial< CoeffT > &p, const CoeffT &alpha)
Division operator.
The Polynomial class provides arithmetical operations with polynomials as well as a root finding algo...
Query specific information about different data types.
ATBTiming.hh provides the MyDateTime and Timer classes for high accuracy profiling.
bool operator==(const Polynomial< CoeffT > &p)
Comparison of the given polynomial with this polynomial.
Polynomial< CoeffT > derivative(size_t degree=1)
Computes the n&#39;th derivative of this polynomial.
Polynomial< CoeffT > & operator-=(const Polynomial< CoeffT > &p)
Minus assignment operator.
size_t degree() const
Retrieve the degree of the polynomial.
Polynomial< CoeffT > & operator=(const Polynomial< CoeffT > &p)
Copy assignment operator.
Polynomial< CoeffT > operator*(const Polynomial< CoeffT > &p1, const Polynomial< CoeffT > &p2)
Multiplication operator.
Polynomial< CoeffT > & operator+=(const Polynomial< CoeffT > &p)
Plus assignment operator.
Polynomial< CoeffT > operator+(const Polynomial< CoeffT > &p1, const Polynomial< CoeffT > &p2)
Plus operator.
std::vector< typename traits< CoeffT >::ComplexT > roots(bool polish=true, bool sort=false)
Computes and returns the vector of zero crossings of the polynomial.
Polynomial< CoeffT > indefiniteIntegral(size_t degree=1)
Computes the n&#39;th indefinite Integral of this polynomial.
static Polynomial< CoeffT > const & zero()
Get the neutral element for addition of the Polynomial ring.
static Polynomial< CoeffT > const & one()
Get the neutral element for multiplication of the Polynomial ring.
Polynomial()
Default constructor.
Polynomial< CoeffT > pow(const Polynomial< CoeffT > &p, int exponential)
Power operator.