Polynomial.h
Go to the documentation of this file.
1 #ifndef _utl_Polynomial_h_
2 #define _utl_Polynomial_h_
3 
4 #include <utl/AugerException.h>
5 #include <utl/Math.h>
6 #include <vector>
7 #include <iostream>
8 
9 
10 namespace utl {
11 
22  class Polynomial {
23 
24  public:
25  Polynomial(const double coeff = 0) : fCoeff(1, coeff) { }
26 
27  Polynomial(const std::vector<double>& coeff) : fCoeff(coeff) { Check(); }
28 
29  template<typename T, unsigned int n>
30  Polynomial(const T (&coeff)[n]) : fCoeff(coeff, coeff + n) { Check(); }
31 
32  double operator()(const double x) const { return EvalPoly(fCoeff, x); }
33 
34  const std::vector<double>& GetCoefficients() const { return fCoeff; }
35 
36  bool
37  IsZero()
38  const
39  {
40  for (const auto& c : fCoeff)
41  if (c)
42  return false;
43  return true;
44  }
45 
46  explicit operator bool() const { return !IsZero(); }
47 
48  private:
49  void
50  Check()
51  const
52  {
53  if (fCoeff.empty())
54  throw DoesNotComputeException("Polynomial should have at least one coefficient!");
55  }
56 
57  std::vector<double> fCoeff;
58 
59  };
60 
61 
62  std::istream& operator>>(std::istream& is, Polynomial& p);
63  std::ostream& operator<<(std::ostream& is, const Polynomial& p);
64 
65 }
66 
67 
68 #endif
std::istream & operator>>(std::istream &is, SVector< n, T > &v)
Definition: SVector.h:346
Simple polynomial container.
Definition: Polynomial.h:22
Polynomial(const double coeff=0)
Definition: Polynomial.h:25
std::vector< double > fCoeff
Definition: Polynomial.h:57
bool is(const double a, const double b)
Definition: testlib.cc:113
Polynomial(const std::vector< double > &coeff)
Definition: Polynomial.h:27
double operator()(const double x) const
Definition: Polynomial.h:32
Stream & operator<<(Stream &s, MessageLoggerConfig &mlc)
Applies the configuration to the given stream.
double EvalPoly(const T &a, const double x)
Simple polynomial evaluator.
Polynomial(const T(&coeff)[n])
Definition: Polynomial.h:30
void Check(const Iterator &i, const Iterator &e, const int id)
Base class for inconsistency/illogicality exceptions.
const std::vector< double > & GetCoefficients() const
Definition: Polynomial.h:34

, generated on Tue Sep 26 2023.