poly.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2020, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_POLY_H
24 #define O2SCL_POLY_H
25 
26 /** \file poly.h
27  \brief Classes for solving polynomials
28 
29  \warning
30  One must be careful about using pow() in functions using
31  complex<double> since pow(((complex<double>)0.0),3.0) returns
32  (nan,nan). Instead, we should use pow(((complex<double>)0.0),3)
33  which takes an integer for the second argument. The sqrt()
34  function, always succeeds i.e. sqrt(((complex<double>)0.0))=0.0
35 
36  \future The quartics are tested only for a4=1, which should
37  probably be generalized.
38 */
39 
40 #include <iostream>
41 #include <complex>
42 #include <gsl/gsl_math.h>
43 #include <gsl/gsl_complex_math.h>
44 #include <gsl/gsl_complex.h>
45 #include <gsl/gsl_poly.h>
46 #include <o2scl/constants.h>
47 #include <o2scl/err_hnd.h>
48 
49 #ifndef DOXYGEN_NO_O2NS
50 namespace o2scl {
51 #endif
52 
53  /** \brief Solve a quadratic polynomial with real coefficients and
54  real roots [abstract base]
55 
56  \todo Compute discriminant
57  */
59 
60  public:
61 
62  virtual ~quadratic_real() {}
63 
64  /** \brief Solves the polynomial \f$ a_2 x^2 + b_2 x + c_2 = 0 \f$
65  giving the two solutions \f$ x=x_1 \f$ and \f$ x=x_2 \f$ .
66  */
67  virtual int solve_r(const double a2, const double b2, const double c2,
68  double &x1, double &x2)=0;
69 
70  /// Return a string denoting the type ("quadratic_real")
71  const char *type() { return "quadratic_real"; }
72  };
73 
74  /** \brief Solve a quadratic polynomial with real coefficients and
75  complex roots [abstract base]
76  */
78 
79  public:
80 
81  virtual ~quadratic_real_coeff() {}
82 
83  /** \brief Solves the polynomial \f$ a_2 x^2 + b_2 x + c_2 = 0 \f$
84  giving the two solutions \f$ x=x_1 \f$ and \f$ x=x_2 \f$ .
85  */
86  virtual int solve_r(const double a2, const double b2, const double c2,
87  double &x1, double &x2);
88 
89  /** \brief Solves the polynomial \f$ a_2 x^2 + b_2 x + c_2 = 0 \f$
90  giving the two complex solutions \f$ x=x_1 \f$ and \f$ x=x_2 \f$
91  */
92  virtual int solve_rc(const double a2, const double b2, const double c2,
93  std::complex<double> &x1,
94  std::complex<double> &x2)=0;
95 
96  /// Return a string denoting the type ("quadratic_real_coeff")
97  const char *type() { return "quadratic_real_coeff"; }
98  };
99 
100  /** \brief Solve a quadratic polynomial with complex coefficients and
101  complex roots [abstract base]
102  */
104  public:
105 
106  virtual ~quadratic_complex() {}
107 
108  /** \brief Solves the polynomial \f$ a_2 x^2 + b_2 x + c_2 = 0 \f$
109  giving the two solutions \f$ x=x_1 \f$ and \f$ x=x_2 \f$ .
110  */
111  virtual int solve_r(const double a2, const double b2, const double c2,
112  double &x1, double &x2);
113 
114  /** \brief Solves the polynomial \f$ a_2 x^2 + b_2 x + c_2 = 0 \f$
115  giving the two complex solutions \f$ x=x_1 \f$ and \f$ x=x_2 \f$
116  */
117  virtual int solve_rc(const double a2, const double b2, const double c2,
118  std::complex<double> &x1, std::complex<double> &x2);
119 
120  /** \brief Solves the complex polynomial \f$ a_2 x^2 + b_2 x + c_2 = 0 \f$
121  giving the two complex solutions \f$ x=x_1 \f$ and \f$ x=x_2 \f$
122  */
123  virtual int solve_c(const std::complex<double> a2,
124  const std::complex<double> b2,
125  const std::complex<double> c2,
126  std::complex<double> &x1, std::complex<double> &x2)=0;
127 
128  /// Return a string denoting the type ("quadratic_complex")
129  const char *type() { return "quadratic_complex"; }
130  };
131 
132  /** \brief Solve a cubic polynomial with real coefficients and real roots
133  [abstract base]
134 
135  \todo Compute discriminant
136  */
137  class cubic_real {
138  public:
139 
140  virtual ~cubic_real() {}
141 
142  /** \brief Solves the polynomial
143  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$ giving the three
144  solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ , and \f$ x=x_3 \f$ .
145  */
146  virtual int solve_r(const double a3, const double b3, const double c3,
147  const double d3, double &x1, double &x2,
148  double &x3)=0;
149 
150  /// Return a string denoting the type ("cubic_real")
151  const char *type() { return "cubic_real"; }
152  };
153 
154  /** \brief Solve a cubic polynomial with real coefficients and
155  complex roots [abstract base]
156  */
157  class cubic_real_coeff : public cubic_real {
158 
159  public:
160 
161  virtual ~cubic_real_coeff() {}
162 
163  /** \brief Solves the polynomial
164  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$ giving the three
165  solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ , and \f$ x=x_3 \f$ .
166  */
167  virtual int solve_r(const double a3, const double b3, const double c3,
168  const double d3, double &x1, double &x2, double &x3);
169 
170  /** \brief Solves the polynomial
171  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$
172  giving the real solution \f$ x=x_1 \f$ and two complex solutions
173  \f$ x=x_2 \f$ and \f$ x=x_3 \f$ .
174  */
175  virtual int solve_rc(const double a3, const double b3, const double c3,
176  const double d3, double &x1, std::complex<double> &x2,
177  std::complex<double> &x3)=0;
178 
179  /// Return a string denoting the type ("cubic_real_coeff")
180  const char *type() { return "cubic_real_coeff"; }
181  };
182 
183  /** \brief Solve a cubic polynomial with complex coefficients and
184  complex roots [abstract base]
185  */
187 
188  public:
189 
190  virtual ~cubic_complex() {}
191 
192  /** \brief Solves the polynomial
193  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$ giving the three
194  solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ , and \f$ x=x_3 \f$ .
195  */
196  virtual int solve_r(const double a3, const double b3, const double c3,
197  const double d3, double &x1, double &x2, double &x3);
198 
199  /** \brief Solves the polynomial
200  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$ giving the real
201  solution \f$ x=x_1 \f$ and two complex solutions
202  \f$ x=x_2 \f$ and \f$ x=x_3 \f$ .
203  */
204  virtual int solve_rc(const double a3, const double b3, const double c3,
205  const double d3, double &x1, std::complex<double> &x2,
206  std::complex<double> &x3);
207 
208  /** \brief Solves the complex polynomial
209  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$
210  giving the three complex solutions \f$ x=x_1 \f$ ,
211  \f$ x=x_2 \f$ , and \f$ x=x_3 \f$ .
212  */
213  virtual int solve_c(const std::complex<double> a3,
214  const std::complex<double> b3,
215  const std::complex<double> c3,
216  const std::complex<double> d3,
217  std::complex<double> &x1, std::complex<double> &x2,
218  std::complex<double> &x3)=0;
219 
220  /// Return a string denoting the type ("cubic_complex")
221  const char *type() { return "cubic_complex"; }
222  };
223 
224  /** \brief Solve a quartic polynomial with real coefficients and
225  real roots [abstract base]
226  */
227  class quartic_real {
228 
229  public:
230 
231  virtual ~quartic_real() {}
232 
233  /** \brief Solves the polynomial
234  \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 + d_4 x + e_4 = 0 \f$
235  giving the four solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ ,
236  \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
237  */
238  virtual int solve_r(const double a4, const double b4, const double c4,
239  const double d4, const double e4,
240  double &x1, double &x2,
241  double &x3, double &x4)=0;
242 
243 
244  /** \brief Compute the discriminant
245 
246  The discriminant is zero if and only if at least two roots are
247  equal. If the discriminant is non-zero, the discriminant is
248  negative if there are two real roots and two complex conjugate
249  roots, and it is positive if the roots are either all real or
250  all non-real.
251  */
252  virtual double disc_r(const double a, const double b, const double c,
253  const double d, const double e);
254 
255  /// Return a string denoting the type ("quartic_real")
256  const char *type() { return "quartic_real"; }
257  };
258 
259  /** \brief Solve a quartic polynomial with real coefficients and
260  complex roots [abstract base]
261  */
263 
264  public:
265 
266  virtual ~quartic_real_coeff() {}
267 
268  /** \brief Solves the polynomial
269  \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 + d_4 x + e_4 = 0 \f$
270  giving the four solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ ,
271  \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
272  */
273  virtual int solve_r(const double a4, const double b4, const double c4,
274  const double d4, const double e4, double &x1,
275  double &x2, double &x3, double &x4);
276 
277  /** \brief Solves the polynomial
278  \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 + d_4 x + e_4 = 0 \f$
279  giving the four complex solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ ,
280  \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
281  */
282  virtual int solve_rc(const double a4, const double b4, const double c4,
283  const double d4, const double e4,
284  std::complex<double> &x1, std::complex<double> &x2,
285  std::complex<double> &x3,
286  std::complex<double> &x4)=0;
287 
288  /// Return a string denoting the type ("quartic_real_coeff")
289  const char *type() { return "quartic_real_coeff"; }
290  };
291 
292  /** \brief Solve a quartic polynomial with complex coefficients and
293  complex roots [abstract base]
294  */
296 
297  public:
298 
299  virtual ~quartic_complex() {}
300 
301  /** \brief Solves the polynomial
302  \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 + d_4 x + e_4 = 0 \f$
303  giving the four solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ ,
304  \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
305  */
306  virtual int solve_r(const double a4, const double b4, const double c4,
307  const double d4, const double e4, double &x1,
308  double &x2,
309  double &x3, double &x4);
310 
311  /** \brief Solves the polynomial
312  \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 + d_4 x + e_4 = 0 \f$
313  giving the four complex solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ ,
314  \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
315  */
316  virtual int solve_rc(const double a4, const double b4, const double c4,
317  const double d4, const double e4,
318  std::complex<double> &x1, std::complex<double> &x2,
319  std::complex<double> &x3, std::complex<double> &x4);
320 
321  /** \brief Solves the complex polynomial
322  \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 + d_4 x + e_4 = 0 \f$
323  giving the four complex solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ ,
324  \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
325  */
326  virtual int solve_c(const std::complex<double> a4,
327  const std::complex<double> b4,
328  const std::complex<double> c4,
329  const std::complex<double> d4,
330  const std::complex<double> e4,
331  std::complex<double> &x1,
332  std::complex<double> &x2, std::complex<double> &x3,
333  std::complex<double> &x4)=0;
334 
335  /// Return a string denoting the type ("quartic_complex")
336  const char *type() { return "quartic_complex"; }
337  };
338 
339  /** \brief Solve a general polynomial with real
340  coefficients and complex roots [abstract base]
341  */
343  public cubic_real_coeff, public quartic_real_coeff {
344 
345  public:
346 
347  virtual ~poly_real_coeff() {}
348 
349  /** \brief Solve the n-th order polynomial
350 
351  The coefficients are stored in co[], with the leading coefficient
352  as co[0] and the constant term as co[n]. The roots are returned
353  in ro[0],...,ro[n-1].
354  */
355  virtual int solve_rc(int n, const double co[],
356  std::complex<double> ro[])=0;
357 
358  /// Return a string denoting the type ("poly_real_coeff")
359  const char *type() { return "poly_real_coeff"; }
360  };
361 
362  /** \brief Solve a general polynomial with complex
363  coefficients [abstract base]
364  */
366  public cubic_complex, public quartic_complex {
367 
368  public:
369 
370  virtual ~poly_complex() {}
371 
372  /** \brief Solve the n-th order polynomial
373 
374  The coefficients are stored in co[], with the leading coefficient
375  as co[0] and the constant term as co[n]. The roots are returned
376  in ro[0],...,ro[n-1].
377  */
378  virtual int solve_c(int n, const std::complex<double> co[],
379  std::complex<double> ro[])=0;
380 
381  /// Polish the roots
382  virtual int polish_c(int n, const std::complex<double> co[],
383  std::complex<double> *ro)=0;
384 
385  /// Return a string denoting the type ("poly_complex")
386  const char *type() { return "poly_complex"; }
387  };
388 
389  /** \brief Solve a cubic with real coefficients and complex roots
390  (CERNLIB)
391 
392  \note The function rrteq3() is based on the CERNLIB routine of
393  the same name, but differs slightly. See the documentation of
394  that function for details.
395  */
397 
398  public:
399 
401  eps=1.0e-6;
402  delta=1.0e-15;
403  improve_scale=true;
404  }
405 
406  /// Numerical tolerance (default \f$ 10^{-6} \f$)
407  double eps;
408 
409  /// Numerical tolerance (default \f$ 10^{-15} \f$)
410  double delta;
411 
412  /// Improve algorithm for poorly-scaled roots (default true)
414 
415  virtual ~cubic_real_coeff_cern() {}
416 
417  /** \brief Solves the polynomial
418  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$ giving the real
419  solution \f$ x=x_1 \f$ and two complex solutions
420  \f$ x=x_2 \f$ , and \f$ x=x_3 \f$ .
421  */
422  virtual int solve_rc(const double a3, const double b3, const double c3,
423  const double d3, double &x1,
424  std::complex<double> &x2, std::complex<double> &x3);
425 
426  /** \brief The CERNLIB-like interface
427 
428  This function computes the roots of the cubic equation
429  \f[
430  x^3 + r x^2 + s x + t =0
431  \f]
432  returning the value of the discriminant in \c d and the roots
433  in the array \c x. If the discriminant is negative, then all
434  three real roots are stored in \c x. Otherwise, the real root
435  is stored in <tt>x[0]</tt> and the real and imaginary parts of
436  the complex conjugate roots are stored in <tt>x[1]</tt> and
437  <tt>x[2]</tt>, respectively. This differs from the CERNLIB
438  routine where the results were stored in <tt>x[1]</tt>,
439  <tt>x[2]</tt>, and <tt>x[3]</tt> instead.
440 
441  Another small change is that the discriminant for the
442  resolvent cubic is evaluated slightly differently in order to
443  improve the properties in the case where the roots are not all
444  of order unity. The default CERNLIB behavior can be restored
445  by setting improve_scale to \c false.
446  */
447  virtual int rrteq3(double r, double s, double t, double x[], double &d);
448 
449  /// Return a string denoting the type ("cubic_real_coeff_cern")
450  const char *type() { return "cubic_real_coeff_cern"; }
451  };
452 
453  /** \brief Solve a quartic with real coefficients and complex
454  roots (CERNLIB)
455  */
457 
458  public:
459 
460  virtual ~quartic_real_coeff_cern() {}
461 
462  /** \brief Solves the polynomial \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 +
463  d_4 x + e_4= 0 \f$ giving the four complex solutions \f$ x=x_1
464  \f$ , \f$ x=x_2 \f$ , \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
465  */
466  virtual int solve_rc(const double a4, const double b4, const double c4,
467  const double d4, const double e4,
468  std::complex<double> &x1, std::complex<double> &x2,
469  std::complex<double> &x3, std::complex<double> &x4);
470 
471  /// The CERNLIB-like interface
472  virtual int rrteq4(double a, double b, double c, double d,
473  std::complex<double> z[], double &dc,
474  int &mt);
475 
476  /// Return a string denoting the type ("quartic_real_coeff_cern")
477  const char *type() { return "quartic_real_coeff_cern"; }
478 
479 #ifndef DOXYGEN_INTERNAL
480 
481  protected:
482 
483  /// The object to solve for the associated cubic
485 
486 #endif
487 
488  };
489 
490  /** \brief Solve a quadratic with real coefficients and complex roots (GSL)
491  */
493 
494  public:
495 
496  virtual ~quadratic_real_coeff_gsl() {}
497 
498  /** \brief Solves the polynomial \f$ a_2 x^2 + b_2 x + c_2 = 0 \f$
499  giving the two complex solutions \f$ x=x_1 \f$ and \f$ x=x_2 \f$
500  */
501  virtual int solve_rc(const double a2, const double b2, const double c2,
502  std::complex<double> &x1, std::complex<double> &x2);
503 
504  /// Return a string denoting the type ("quadratic_real_coeff_gsl")
505  const char *type() { return "quadratic_real_coeff_gsl"; }
506 
507  };
508 
509  /** \brief Solve a cubic with real coefficients and complex roots (GSL)
510  */
512 
513  public:
514 
515  virtual ~cubic_real_coeff_gsl() {}
516 
517  /** \brief Solves the polynomial
518  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$
519  giving the real solution \f$ x=x_1 \f$ and two complex solutions
520  \f$ x=x_2 \f$ and \f$ x=x_3 \f$ .
521  */
522  virtual int solve_rc(const double a3, const double b3, const double c3,
523  const double d3, double &x1,
524  std::complex<double> &x2, std::complex<double> &x3);
525 
526  /// Return a string denoting the type ("cubic_real_coeff_gsl")
527  const char *type() { return "cubic_real_coeff_gsl"; }
528 
529  /** \brief An alternative to \c gsl_poly_complex_solve_cubic()
530 
531  This is an alternative to the function
532  <tt>gsl_poly_complex_solve_cubic()</tt> with some small
533  corrections to ensure finite values for some cubics. See
534  <tt>src/other/poly_ts.cpp</tt> for more.
535 
536  \future I think the GSL function is now fixed, so we
537  can fall back to the original GSL function here.
538  */
539  int gsl_poly_complex_solve_cubic2(double a, double b, double c,
540  gsl_complex *z0, gsl_complex *z1,
541  gsl_complex *z2);
542 
543  };
544 
545  /** \brief Solve a quartic with real coefficients and real roots (GSL)
546 
547  This class internally uses the GSL functions to solve the
548  resolvent cubic and associated quadratics, while
549  \ref quartic_real_gsl2 contains explicit code to solve
550  them instead.
551 
552  \future Optimize value of \c cube_root_tol and compare
553  more clearly to \ref o2scl::quartic_real_gsl2
554  */
556 
557  public:
558 
559  quartic_real_gsl() {
560  cube_root_tol=1.0e-7;
561  }
562 
563  virtual ~quartic_real_gsl() {}
564 
565  /** \brief A tolerance for determining the proper cube root
566  (default \f$ 10^{-4} \f$ )
567  */
569 
570  /** \brief Solves the polynomial \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 +
571  d_4 x + e_4= 0 \f$ giving the four real solutions \f$ x=x_1
572  \f$ , \f$ x=x_2 \f$ , \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
573  */
574  virtual int solve_r(const double a4, const double b4, const double c4,
575  const double d4, const double e4, double &x1,
576  double &x2, double &x3, double &x4);
577 
578  /// Return a string denoting the type ("quartic_real_gsl")
579  const char *type() { return "quartic_real_gsl"; }
580 
581  };
582 
583  /** \brief Solve a quartic with real coefficients and real roots (GSL)
584 
585  This class directly solves
586  resolvent cubic and associated quadratics without using
587  the GSL functions (as done in \ref quartic_real_gsl).
588 
589  \future Optimize value of \c cube_root_tol and compare
590  more clearly to \ref o2scl::quartic_real_gsl
591  */
593 
594  public:
595 
597  cube_root_tol=1.0e-7;
598  }
599 
600  virtual ~quartic_real_gsl2() {}
601 
602  /** \brief A tolerance for determining the proper cube root
603  (default \f$ 10^{-7} \f$ )
604  */
606 
607  /** \brief Solves the polynomial \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 +
608  d_4 x + e_4= 0 \f$ giving the four real solutions \f$ x=x_1
609  \f$ , \f$ x=x_2 \f$ , \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
610  */
611  virtual int solve_r(const double a4, const double b4, const double c4,
612  const double d4, const double e4, double &x1,
613  double &x2,
614  double &x3, double &x4);
615 
616  /// Return a string denoting the type ("quartic_real_gsl2")
617  const char *type() { return "quartic_real_gsl2"; }
618  };
619 
620  /** \brief Solve a general polynomial with real coefficients (GSL)
621  */
623 
624  public:
625 
627 
628  virtual ~poly_real_coeff_gsl();
629 
630  /** \brief Solve a generic polynomial given <tt>n+1</tt> coefficients
631 
632  \note In order to be consistent with the other solve_rc()
633  functions, the ordering of the coefficients is reversed with
634  respect to gsl_poly_complex_solve(). The leading coefficient
635  is stored in <tt>co[0]</tt> and the constant term is stored in
636  <tt>co[n]</tt>.
637  */
638  virtual int solve_rc(int n, const double co[],
639  std::complex<double> ro[]);
640 
641  /** \brief Solve a cubic polynomial with real coefficients
642  */
643  virtual int solve_rc(const double a3, const double b3, const double c3,
644  const double d3, double &x1,
645  std::complex<double> &x2,
646  std::complex<double> &x3);
647 
648  /** \brief Solve a quadratic polynomial with real coefficients
649  */
650  virtual int solve_rc(const double a2, const double b2, const double c2,
651  std::complex<double> &x1,
652  std::complex<double> &x2);
653 
654  /** \brief Solve a quartic polynomial with real coefficients
655  */
656  virtual int solve_rc(const double a4, const double b4, const double c4,
657  const double d4, const double e4,
658  std::complex<double> &x1, std::complex<double> &x2,
659  std::complex<double> &x3, std::complex<double> &x4);
660 
661  /// Return a string denoting the type ("poly_real_coeff_gsl")
662  const char *type() { return "poly_real_coeff_gsl"; }
663 
664  protected:
665 
666 #ifndef DOXYGEN_INTERNAL
667 
668  /// Workspace for quadratic polynomials
669  gsl_poly_complex_workspace *w2;
670 
671  /// Workspace for cubic polynomials
672  gsl_poly_complex_workspace *w3;
673 
674  /// Workspace for quartic polynomials
675  gsl_poly_complex_workspace *w4;
676 
677  /// Workspace for general polynomials
678  gsl_poly_complex_workspace *wgen;
679 
680  /// The size of the workspace \ref wgen
681  int gen_size;
682 
683 #endif
684 
685  };
686 
687  /** \brief Solve a quadratic with complex coefficients and complex roots
688  */
690 
691  public:
692 
693  virtual ~quadratic_complex_std() {}
694 
695  /** \brief Solves the complex polynomial \f$ a_2 x^2 + b_2 x + c_2 = 0 \f$
696  giving the two complex solutions \f$ x=x_1 \f$ and \f$ x=x_2 \f$
697  */
698  virtual int solve_c(const std::complex<double> a2,
699  const std::complex<double> b2,
700  const std::complex<double> c2,
701  std::complex<double> &x1, std::complex<double> &x2);
702 
703  /// Return a string denoting the type ("quadratic_complex_std")
704  const char *type() { return "quadratic_complex_std"; }
705  };
706 
707  /** \brief Solve a cubic with complex coefficients and complex roots
708  */
710 
711  public:
712 
713  virtual ~cubic_complex_std() {}
714 
715  /** \brief Solves the complex polynomial
716  \f$ a_3 x^3 + b_3 x^2 + c_3 x + d_3= 0 \f$
717  giving the three complex solutions \f$ x=x_1 \f$ ,
718  \f$ x=x_2 \f$ , and \f$ x=x_3 \f$ .
719  */
720  virtual int solve_c(const std::complex<double> a3,
721  const std::complex<double> b3,
722  const std::complex<double> c3,
723  const std::complex<double> d3,
724  std::complex<double> &x1, std::complex<double> &x2,
725  std::complex<double> &x3);
726 
727  /// Return a string denoting the type ("cubic_complex_std")
728  const char *type() { return "cubic_complex_std"; }
729  };
730 
731  /** \brief Solve a quartic with real coefficients and real roots
732  */
734 
735  public:
736 
738  cube_root_tol=1.0e-6;
739  }
740 
741  virtual ~quartic_real_simple() {}
742 
743  virtual int solve_r(const double a4, const double b4, const double c4,
744  const double d4, const double e4, double &x1,
745  double &x2, double &x3, double &x4);
746 
747  /// Return a string denoting the type ("quartic_real_simple")
748  const char *type() { return "quartic_real_simple"; }
749 
750  /** \brief A tolerance for determining the proper cube root
751  (default \f$ 10^{-6} \f$ )
752  */
754  };
755 
756  /** \brief Solve a quartic with complex coefficients and complex roots
757  */
759 
760  public:
761 
762  virtual ~quartic_complex_simple() {}
763 
764  /** \brief Solves the complex polynomial
765  \f$ a_4 x^4 + b_4 x^3 + c_4 x^2 + d_4 x + e_4 = 0 \f$
766  giving the four complex solutions \f$ x=x_1 \f$ , \f$ x=x_2 \f$ ,
767  \f$ x=x_3 \f$ , and \f$ x=x_4 \f$ .
768  */
769  virtual int solve_c(const std::complex<double> a4,
770  const std::complex<double> b4,
771  const std::complex<double> c4,
772  const std::complex<double> d4,
773  const std::complex<double> e4,
774  std::complex<double> &x1,
775  std::complex<double> &x2,
776  std::complex<double> &x3,
777  std::complex<double> &x4);
778 
779  /// Return a string denoting the type ("quartic_complex_simple")
780  const char *type() { return "quartic_complex_simple"; }
781 
782 #ifndef DOXYGEN_NO_O2NS
783 
784  protected:
785 
786  /// The object to solve for the associated cubic
788 
789 #endif
790 
791  };
792 
793 #ifndef DOXYGEN_NO_O2NS
794 }
795 #endif
796 
797 #endif
o2scl::cubic_complex
Solve a cubic polynomial with complex coefficients and complex roots [abstract base].
Definition: poly.h:186
o2scl::quadratic_real_coeff::solve_r
virtual int solve_r(const double a2, const double b2, const double c2, double &x1, double &x2)
Solves the polynomial giving the two solutions and .
o2scl::poly_real_coeff::solve_rc
virtual int solve_rc(int n, const double co[], std::complex< double > ro[])=0
Solve the n-th order polynomial.
o2scl::cubic_complex::type
const char * type()
Return a string denoting the type ("cubic_complex")
Definition: poly.h:221
o2scl::cubic_real::type
const char * type()
Return a string denoting the type ("cubic_real")
Definition: poly.h:151
o2scl::poly_complex::type
const char * type()
Return a string denoting the type ("poly_complex")
Definition: poly.h:386
o2scl::quadratic_complex::solve_c
virtual int solve_c(const std::complex< double > a2, const std::complex< double > b2, const std::complex< double > c2, std::complex< double > &x1, std::complex< double > &x2)=0
Solves the complex polynomial giving the two complex solutions and .
o2scl::quartic_real_coeff_cern::rrteq4
virtual int rrteq4(double a, double b, double c, double d, std::complex< double > z[], double &dc, int &mt)
The CERNLIB-like interface.
o2scl::poly_real_coeff
Solve a general polynomial with real coefficients and complex roots [abstract base].
Definition: poly.h:342
o2scl::quartic_complex::type
const char * type()
Return a string denoting the type ("quartic_complex")
Definition: poly.h:336
o2scl::quartic_real::type
const char * type()
Return a string denoting the type ("quartic_real")
Definition: poly.h:256
o2scl::cubic_real_coeff_cern::type
const char * type()
Return a string denoting the type ("cubic_real_coeff_cern")
Definition: poly.h:450
o2scl::cubic_real_coeff_cern
Solve a cubic with real coefficients and complex roots (CERNLIB)
Definition: poly.h:396
o2scl::poly_real_coeff_gsl::solve_rc
virtual int solve_rc(int n, const double co[], std::complex< double > ro[])
Solve a generic polynomial given n+1 coefficients.
o2scl::quadratic_real_coeff::solve_rc
virtual int solve_rc(const double a2, const double b2, const double c2, std::complex< double > &x1, std::complex< double > &x2)=0
Solves the polynomial giving the two complex solutions and .
o2scl::quartic_real_simple::solve_r
virtual int solve_r(const double a4, const double b4, const double c4, const double d4, const double e4, double &x1, double &x2, double &x3, double &x4)
Solves the polynomial giving the four solutions , , , and .
o2scl::cubic_real_coeff
Solve a cubic polynomial with real coefficients and complex roots [abstract base].
Definition: poly.h:157
o2scl::quartic_complex_simple::cub_obj
cubic_complex_std cub_obj
The object to solve for the associated cubic.
Definition: poly.h:787
o2scl::poly_real_coeff_gsl::w2
gsl_poly_complex_workspace * w2
Workspace for quadratic polynomials.
Definition: poly.h:669
o2scl::cubic_real_coeff_gsl::solve_rc
virtual int solve_rc(const double a3, const double b3, const double c3, const double d3, double &x1, std::complex< double > &x2, std::complex< double > &x3)
Solves the polynomial giving the real solution and two complex solutions and .
o2scl::quartic_complex_simple::type
const char * type()
Return a string denoting the type ("quartic_complex_simple")
Definition: poly.h:780
o2scl::quadratic_complex
Solve a quadratic polynomial with complex coefficients and complex roots [abstract base].
Definition: poly.h:103
o2scl::cubic_real_coeff_gsl
Solve a cubic with real coefficients and complex roots (GSL)
Definition: poly.h:511
o2scl::poly_complex
Solve a general polynomial with complex coefficients [abstract base].
Definition: poly.h:365
o2scl::quartic_real_gsl
Solve a quartic with real coefficients and real roots (GSL)
Definition: poly.h:555
o2scl::cubic_real_coeff_cern::improve_scale
bool improve_scale
Improve algorithm for poorly-scaled roots (default true)
Definition: poly.h:413
o2scl::cubic_real_coeff_cern::rrteq3
virtual int rrteq3(double r, double s, double t, double x[], double &d)
The CERNLIB-like interface.
o2scl::poly_complex::solve_c
virtual int solve_c(int n, const std::complex< double > co[], std::complex< double > ro[])=0
Solve the n-th order polynomial.
o2scl
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
Definition: anneal.h:42
o2scl::quartic_real_coeff_cern::solve_rc
virtual int solve_rc(const double a4, const double b4, const double c4, const double d4, const double e4, std::complex< double > &x1, std::complex< double > &x2, std::complex< double > &x3, std::complex< double > &x4)
Solves the polynomial giving the four complex solutions , , , and .
o2scl::cubic_real_coeff_cern::solve_rc
virtual int solve_rc(const double a3, const double b3, const double c3, const double d3, double &x1, std::complex< double > &x2, std::complex< double > &x3)
Solves the polynomial giving the real solution and two complex solutions , and .
o2scl::quartic_real_coeff_cern::type
const char * type()
Return a string denoting the type ("quartic_real_coeff_cern")
Definition: poly.h:477
o2scl::quadratic_real_coeff_gsl::type
const char * type()
Return a string denoting the type ("quadratic_real_coeff_gsl")
Definition: poly.h:505
o2scl_inte_qng_coeffs::x4
static const double x4[22]
Definition: inte_qng_gsl.h:139
o2scl::cubic_real_coeff_gsl::type
const char * type()
Return a string denoting the type ("cubic_real_coeff_gsl")
Definition: poly.h:527
o2scl::quartic_real_simple::cube_root_tol
double cube_root_tol
A tolerance for determining the proper cube root (default )
Definition: poly.h:753
o2scl::quadratic_complex::type
const char * type()
Return a string denoting the type ("quadratic_complex")
Definition: poly.h:129
o2scl::quartic_real_coeff_cern
Solve a quartic with real coefficients and complex roots (CERNLIB)
Definition: poly.h:456
o2scl::quadratic_complex::solve_rc
virtual int solve_rc(const double a2, const double b2, const double c2, std::complex< double > &x1, std::complex< double > &x2)
Solves the polynomial giving the two complex solutions and .
o2scl::quadratic_real_coeff_gsl
Solve a quadratic with real coefficients and complex roots (GSL)
Definition: poly.h:492
o2scl::quartic_real_gsl::cube_root_tol
double cube_root_tol
A tolerance for determining the proper cube root (default )
Definition: poly.h:568
o2scl::quartic_complex::solve_rc
virtual int solve_rc(const double a4, const double b4, const double c4, const double d4, const double e4, std::complex< double > &x1, std::complex< double > &x2, std::complex< double > &x3, std::complex< double > &x4)
Solves the polynomial giving the four complex solutions , , , and .
o2scl::quadratic_real_coeff_gsl::solve_rc
virtual int solve_rc(const double a2, const double b2, const double c2, std::complex< double > &x1, std::complex< double > &x2)
Solves the polynomial giving the two complex solutions and .
o2scl_inte_qng_coeffs::x2
static const double x2[5]
Definition: inte_qng_gsl.h:66
o2scl::quartic_real_coeff::solve_rc
virtual int solve_rc(const double a4, const double b4, const double c4, const double d4, const double e4, std::complex< double > &x1, std::complex< double > &x2, std::complex< double > &x3, std::complex< double > &x4)=0
Solves the polynomial giving the four complex solutions , , , and .
o2scl::quartic_complex
Solve a quartic polynomial with complex coefficients and complex roots [abstract base].
Definition: poly.h:295
o2scl::quartic_real_simple
Solve a quartic with real coefficients and real roots.
Definition: poly.h:733
o2scl::quadratic_real_coeff::type
const char * type()
Return a string denoting the type ("quadratic_real_coeff")
Definition: poly.h:97
o2scl::cubic_real::solve_r
virtual int solve_r(const double a3, const double b3, const double c3, const double d3, double &x1, double &x2, double &x3)=0
Solves the polynomial giving the three solutions , , and .
o2scl::quadratic_real::solve_r
virtual int solve_r(const double a2, const double b2, const double c2, double &x1, double &x2)=0
Solves the polynomial giving the two solutions and .
o2scl::quartic_complex::solve_r
virtual int solve_r(const double a4, const double b4, const double c4, const double d4, const double e4, double &x1, double &x2, double &x3, double &x4)
Solves the polynomial giving the four solutions , , , and .
o2scl::cubic_complex_std::solve_c
virtual int solve_c(const std::complex< double > a3, const std::complex< double > b3, const std::complex< double > c3, const std::complex< double > d3, std::complex< double > &x1, std::complex< double > &x2, std::complex< double > &x3)
Solves the complex polynomial giving the three complex solutions , , and .
o2scl::quartic_real_gsl2::solve_r
virtual int solve_r(const double a4, const double b4, const double c4, const double d4, const double e4, double &x1, double &x2, double &x3, double &x4)
Solves the polynomial giving the four real solutions , , , and .
o2scl_inte_qng_coeffs::x1
static const double x1[5]
Definition: inte_qng_gsl.h:48
o2scl::poly_real_coeff_gsl::w4
gsl_poly_complex_workspace * w4
Workspace for quartic polynomials.
Definition: poly.h:675
o2scl::quartic_complex::solve_c
virtual int solve_c(const std::complex< double > a4, const std::complex< double > b4, const std::complex< double > c4, const std::complex< double > d4, const std::complex< double > e4, std::complex< double > &x1, std::complex< double > &x2, std::complex< double > &x3, std::complex< double > &x4)=0
Solves the complex polynomial giving the four complex solutions , , , and .
o2scl::quartic_real_gsl::solve_r
virtual int solve_r(const double a4, const double b4, const double c4, const double d4, const double e4, double &x1, double &x2, double &x3, double &x4)
Solves the polynomial giving the four real solutions , , , and .
o2scl::quartic_real_coeff_cern::cub_obj
cubic_real_coeff_cern cub_obj
The object to solve for the associated cubic.
Definition: poly.h:484
o2scl::quartic_real_simple::type
const char * type()
Return a string denoting the type ("quartic_real_simple")
Definition: poly.h:748
o2scl::quartic_real_coeff::solve_r
virtual int solve_r(const double a4, const double b4, const double c4, const double d4, const double e4, double &x1, double &x2, double &x3, double &x4)
Solves the polynomial giving the four solutions , , , and .
o2scl::quartic_real
Solve a quartic polynomial with real coefficients and real roots [abstract base].
Definition: poly.h:227
o2scl::poly_real_coeff_gsl::wgen
gsl_poly_complex_workspace * wgen
Workspace for general polynomials.
Definition: poly.h:678
o2scl::quadratic_complex_std::solve_c
virtual int solve_c(const std::complex< double > a2, const std::complex< double > b2, const std::complex< double > c2, std::complex< double > &x1, std::complex< double > &x2)
Solves the complex polynomial giving the two complex solutions and .
o2scl::quadratic_complex_std::type
const char * type()
Return a string denoting the type ("quadratic_complex_std")
Definition: poly.h:704
o2scl::cubic_real
Solve a cubic polynomial with real coefficients and real roots [abstract base].
Definition: poly.h:137
o2scl::quartic_complex_simple
Solve a quartic with complex coefficients and complex roots.
Definition: poly.h:758
o2scl::quadratic_complex_std
Solve a quadratic with complex coefficients and complex roots.
Definition: poly.h:689
o2scl::poly_real_coeff_gsl::type
const char * type()
Return a string denoting the type ("poly_real_coeff_gsl")
Definition: poly.h:662
o2scl::quadratic_real::type
const char * type()
Return a string denoting the type ("quadratic_real")
Definition: poly.h:71
o2scl::poly_complex::polish_c
virtual int polish_c(int n, const std::complex< double > co[], std::complex< double > *ro)=0
Polish the roots.
o2scl::quadratic_real
Solve a quadratic polynomial with real coefficients and real roots [abstract base].
Definition: poly.h:58
o2scl::cubic_real_coeff_cern::eps
double eps
Numerical tolerance (default )
Definition: poly.h:407
o2scl::cubic_complex::solve_c
virtual int solve_c(const std::complex< double > a3, const std::complex< double > b3, const std::complex< double > c3, const std::complex< double > d3, std::complex< double > &x1, std::complex< double > &x2, std::complex< double > &x3)=0
Solves the complex polynomial giving the three complex solutions , , and .
o2scl::cubic_real_coeff::solve_rc
virtual int solve_rc(const double a3, const double b3, const double c3, const double d3, double &x1, std::complex< double > &x2, std::complex< double > &x3)=0
Solves the polynomial giving the real solution and two complex solutions and .
o2scl::poly_real_coeff_gsl
Solve a general polynomial with real coefficients (GSL)
Definition: poly.h:622
o2scl::quartic_real::disc_r
virtual double disc_r(const double a, const double b, const double c, const double d, const double e)
Compute the discriminant.
o2scl::cubic_real_coeff::type
const char * type()
Return a string denoting the type ("cubic_real_coeff")
Definition: poly.h:180
o2scl::quartic_real_gsl::type
const char * type()
Return a string denoting the type ("quartic_real_gsl")
Definition: poly.h:579
o2scl::quartic_real_gsl2::cube_root_tol
double cube_root_tol
A tolerance for determining the proper cube root (default )
Definition: poly.h:605
o2scl::cubic_complex_std::type
const char * type()
Return a string denoting the type ("cubic_complex_std")
Definition: poly.h:728
o2scl::quartic_real_gsl2::type
const char * type()
Return a string denoting the type ("quartic_real_gsl2")
Definition: poly.h:617
o2scl::cubic_real_coeff::solve_r
virtual int solve_r(const double a3, const double b3, const double c3, const double d3, double &x1, double &x2, double &x3)
Solves the polynomial giving the three solutions , , and .
o2scl::poly_real_coeff::type
const char * type()
Return a string denoting the type ("poly_real_coeff")
Definition: poly.h:359
o2scl::cubic_real_coeff_cern::delta
double delta
Numerical tolerance (default )
Definition: poly.h:410
o2scl::quartic_real_coeff
Solve a quartic polynomial with real coefficients and complex roots [abstract base].
Definition: poly.h:262
o2scl::cubic_complex_std
Solve a cubic with complex coefficients and complex roots.
Definition: poly.h:709
o2scl::quadratic_complex::solve_r
virtual int solve_r(const double a2, const double b2, const double c2, double &x1, double &x2)
Solves the polynomial giving the two solutions and .
o2scl::quartic_real_coeff::type
const char * type()
Return a string denoting the type ("quartic_real_coeff")
Definition: poly.h:289
o2scl::cubic_complex::solve_rc
virtual int solve_rc(const double a3, const double b3, const double c3, const double d3, double &x1, std::complex< double > &x2, std::complex< double > &x3)
Solves the polynomial giving the real solution and two complex solutions and .
o2scl::quartic_real_gsl2
Solve a quartic with real coefficients and real roots (GSL)
Definition: poly.h:592
o2scl::quadratic_real_coeff
Solve a quadratic polynomial with real coefficients and complex roots [abstract base].
Definition: poly.h:77
o2scl::quartic_real::solve_r
virtual int solve_r(const double a4, const double b4, const double c4, const double d4, const double e4, double &x1, double &x2, double &x3, double &x4)=0
Solves the polynomial giving the four solutions , , , and .
o2scl::cubic_real_coeff_gsl::gsl_poly_complex_solve_cubic2
int gsl_poly_complex_solve_cubic2(double a, double b, double c, gsl_complex *z0, gsl_complex *z1, gsl_complex *z2)
An alternative to gsl_poly_complex_solve_cubic()
o2scl::cubic_complex::solve_r
virtual int solve_r(const double a3, const double b3, const double c3, const double d3, double &x1, double &x2, double &x3)
Solves the polynomial giving the three solutions , , and .
o2scl::quartic_complex_simple::solve_c
virtual int solve_c(const std::complex< double > a4, const std::complex< double > b4, const std::complex< double > c4, const std::complex< double > d4, const std::complex< double > e4, std::complex< double > &x1, std::complex< double > &x2, std::complex< double > &x3, std::complex< double > &x4)
Solves the complex polynomial giving the four complex solutions , , , and .
o2scl::poly_real_coeff_gsl::w3
gsl_poly_complex_workspace * w3
Workspace for cubic polynomials.
Definition: poly.h:672
o2scl::poly_real_coeff_gsl::gen_size
int gen_size
The size of the workspace wgen.
Definition: poly.h:681
o2scl_inte_qng_coeffs::x3
static const double x3[11]
Definition: inte_qng_gsl.h:94

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).