Bayeux  3.4.1
Core Foundation library for SuperNEMO
polynomial.h
Go to the documentation of this file.
1 
3 #ifndef MYGSL_POLYNOMIAL_H
4 #define MYGSL_POLYNOMIAL_H 1
5 
6 // Standard library:
7 #include <vector>
8 
9 // Third party:
10 // - Bayeux/datatools :
12 #include <datatools/i_cloneable.h>
13 
14 // This project:
15 #include <mygsl/i_unary_function.h>
16 
17 namespace mygsl {
18 
20  class polynomial :
21  public i_unary_function,
24  {
25  public:
26 
28  bool is_valid() const;
29 
31  polynomial();
32 
34  explicit polynomial(unsigned int sz_);
35 
37  explicit polynomial(double c0_);
38 
40  polynomial(double c0_, double c1_);
41 
43  polynomial(double c0_, double c1_, double c2_);
44 
46  polynomial(const std::vector<double>& c_);
47 
49  polynomial(const polynomial& p_);
50 
52  virtual ~polynomial();
53 
54  void set_coefficients(const std::vector<double>& c_);
55 
56  const std::vector<double> & get_coefficients() const;
57 
58  unsigned int get_degree() const;
59 
60  unsigned int get_ncoeffs() const;
61 
62  double get_coeff(unsigned int i_) const;
63 
64  void set_coeff(unsigned int i_, double coeff_);
65 
66  void print(std::ostream& out_ = std::clog,
67  int format_ = 0,
68  bool eol_ = false) const;
69 
71  virtual bool is_initialized() const;
72 
74  virtual void initialize(const datatools::properties & config_,
75  const unary_function_dict_type & functors_);
76 
78  void reset();
79 
80  /*
81  * L(x) = p0 + p1 * x^1
82  *
83  */
84  static bool solve_linear(double p0_, double p1_, unsigned int& nsols_,
85  double& x0_);
86 
87  /*
88  * Q(x) = p0 + p1 * x^1 + p2 * x^2
89  *
90  */
91  static bool solve_quadratic(double p0_, double p1_, double p2_,
92  unsigned int & nsols_, double & x0_, double & x1_);
93 
94  /*
95  * C(x) = p0 + p1 * x^1 + p2 * x^2 + x^3
96  *
97  */
98  static bool solve_cubic(double p0_, double p1_, double p2_,
99  unsigned int & nsols_,
100  double & x0_, double & x1_, double & x2_);
101 
102  /*
103  * C(x) = p0 + p1 * x^1 + p2 * x^2 + p3 * x^3
104  *
105  */
106  static bool solve_cubic(double p0_, double p1_, double p2_, double p3_,
107  unsigned int & nsols_,
108  double & x0_, double & x1_, double & x2_);
109 
116  static bool compute_parabola(double x1_, double x2_, double x3_,
117  double y1_, double y2_, double y3_,
118  double & p2_, double & p1_, double & p0_);
119 
126  static bool compute_line(double x1_, double x2_,
127  double y1_, double y2_,
128  double & p1_, double & p0_);
129 
130  bool make_line(double x1_, double x2_,
131  double y1_, double y2_);
132 
133  bool make_parabola(double x1_, double x2_, double x3_,
134  double y1_, double y2_, double y3_);
135 
137  virtual void tree_dump(std::ostream & out_ = std::clog,
138  const std::string & title_ = "",
139  const std::string & indent_ = "",
140  bool inherit_ = false) const;
141 
142  protected :
143 
144  double _eval(double x_) const;
145 
146  public:
147 
148  class solver {
149  public:
150  solver(unsigned int sz_ = 0);
151  virtual ~solver();
152  bool is_initialized() const;
153  bool solve(const polynomial& p_);
154  bool is_success() const;
155  size_t fetch_solutions(std::vector<std::complex<double> > & zsols_);
156  size_t fetch_real_solutions(std::vector<double> & rsols_, double precision_ = -1.0);
157 
158  private:
159  void _init_(unsigned int);
160  void _reset_();
161 
162  private:
163  struct solver_impl;
164  solver_impl *pImpl;
165  };
166 
167  private:
168 
169  std::vector<double> _c_;
170 
172 
173 
175 
178 
179  };
180 
182 
183 } // namespace mygsl
184 
185 #endif // MYGSL_POLYNOMIAL_H
186 
187 // Local Variables: --
188 // mode: c++ --
189 // c-file-style: "gnu" --
190 // tab-width: 2 --
191 // End: --
#define DATATOOLS_CLONEABLE_DECLARATION(Copyable)
Definition: i_cloneable.h:113
Base abstract class of all serializable (and possibly introspectable) classes.
Definition: i_serializable.h:51
double _eval(double x_) const
The function evaluation abstract method.
virtual bool is_initialized() const
Check initialization status.
bool make_parabola(double x1_, double x2_, double x3_, double y1_, double y2_, double y3_)
double get_coeff(unsigned int i_) const
size_t fetch_solutions(std::vector< std::complex< double > > &zsols_)
bool is_valid() const
Check validity of the polynomial.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart printing.
void print(std::ostream &out_=std::clog, int format_=0, bool eol_=false) const
#define DATATOOLS_SERIALIZATION_DECLARATION()
Definition: i_serializable.h:266
virtual ~polynomial()
Destructor.
unsigned int get_ncoeffs() const
const std::vector< double > & get_coefficients() const
static bool solve_cubic(double p0_, double p1_, double p2_, unsigned int &nsols_, double &x0_, double &x1_, double &x2_)
void reset()
Reset the polynomial.
static bool solve_linear(double p0_, double p1_, unsigned int &nsols_, double &x0_)
Definition: polynomial.h:148
virtual void initialize(const datatools::properties &config_, const unary_function_dict_type &functors_)
Initialization from a container of parameters and a dictionary of functors.
static bool compute_line(double x1_, double x2_, double y1_, double y2_, double &p1_, double &p0_)
polynomial()
Default constructor.
Abstract interface for unary functions : R -> R.
Definition: i_unary_function.h:44
size_t fetch_real_solutions(std::vector< double > &rsols_, double precision_=-1.0)
bool make_line(double x1_, double x2_, double y1_, double y2_)
static bool compute_parabola(double x1_, double x2_, double x3_, double y1_, double y2_, double y3_, double &p2_, double &p1_, double &p0_)
void set_coeff(unsigned int i_, double coeff_)
void set_coefficients(const std::vector< double > &c_)
Top-level namespace of the Bayeux/mygsl module library.
Definition: base_decay_driver.h:47
A pure abstract class (interface) for inherited cloneable classes.
Definition: i_cloneable.h:75
std::map< std::string, unary_function_handle_type > unary_function_dict_type
Alias for dictionary of handles to unary function objects.
Definition: unary_function_utils.h:30
#define MYGSL_UNARY_FUNCTOR_REGISTRATION_INTERFACE(UnaryFunctorClass)
Definition: i_unary_function.h:206
static bool solve_quadratic(double p0_, double p1_, double p2_, unsigned int &nsols_, double &x0_, double &x1_)
solver(unsigned int sz_=0)
bool solve(const polynomial &p_)
unsigned int get_degree() const
A real polynomial of arbitrary degree.
Definition: polynomial.h:20
A dictionary of arbitrary properties.
Definition: properties.h:125