Quartic roots
Utilities for C++ programming
Loading...
Searching...
No Matches
PolynomialRoots::QuadraticT< T_real, T_complex > Class Template Reference

#include <PolynomialRoots.hh>

Public Types

using value_type = T_real
using complex_type = T_complex

Public Member Functions

 QuadraticT ()=default
 Build an empty quadratic solver instance.
 QuadraticT (T_real const &a, T_real const &b, T_real const &c)
void setup (T_real const &a, T_real const &b, T_real const &c)
integer num_roots () const
integer numRoots () const
 Alias of num_roots().
bool complex_root () const
bool complexRoot () const
 Alias of complex_root().
bool double_root () const
bool doubleRoot () const
 Alias of double_root().
integer get_real_roots (T_real r[]) const
integer getRealRoots (T_real r[]) const
 Alias of get_real_roots().
integer get_positive_roots (T_real r[]) const
integer getPositiveRoots (T_real r[]) const
 Alias of get_positive_roots().
integer get_negative_roots (T_real r[]) const
integer getNegativeRoots (T_real r[]) const
 Alias of get_negative_roots().
integer get_roots_in_range (T_real const &a, T_real const &b, T_real r[]) const
integer getRootsInRange (T_real const &a, T_real const &b, T_real r[]) const
 Alias of get_roots_in_range().
integer get_roots_in_open_range (T_real const &a, T_real const &b, T_real r[]) const
integer getRootsInOpenRange (T_real const &a, T_real const &b, T_real r[]) const
 Alias of get_roots_in_open_range().
T_real real_root0 () const
T_real real_root1 () const
T_complex root0 () const
 Return the first root as a complex value.
T_complex root1 () const
 Return the second root as a complex value.
void get_root0 (T_real &re, T_real &im) const
void getRoot0 (T_real &re, T_real &im) const
 Alias of get_root0().
void get_root0 (T_complex &r) const
void getRoot0 (T_complex &r) const
 Alias of get_root0().
void get_root1 (T_real &re, T_real &im) const
void getRoot1 (T_real &re, T_real &im) const
 Alias of get_root1().
void get_root1 (T_complex &r) const
void getRoot1 (T_complex &r) const
 Alias of get_root1().
T_complex root (integer const i) const
void get_root (integer const i, T_real &re, T_real &im) const
T_real eval (T_real const &x) const
T_complex eval (T_complex const &x) const
void eval (T_real const &x, T_real &p, T_real &dp) const
void info (ostream_type &s) const
bool check (ostream_type &s) const

Detailed Description

template<typename T_real, typename T_complex>
class PolynomialRoots::QuadraticT< T_real, T_complex >

Quadratic polynomial class

Constructor

double a = 1;
double b = 2;
double c = 3;
Quadratic q(a,b,c); // build an solve `a x^2 + b x + c = 0`
q.setup(a,b,c); // build an solve `a x^2 + b x + c = 0`
void setup(T_real const &a, T_real const &b, T_real const &c)
Definition PolynomialRoots.hh:303
QuadraticT< real_type, real_complex > Quadratic
Definition PolynomialRoots.hh:552

Get kind of solution

int nroots = q.num_roots();
bool has_complex_root = q.complex_root();
bool has_a_double_root = q.double_root();
bool complex_root() const
Definition PolynomialRoots.hh:329
integer num_roots() const
Definition PolynomialRoots.hh:321
bool double_root() const
Definition PolynomialRoots.hh:337

Get real roots

double r_min = 0;
double r_max = 2;
double r[2];
int nroots;
nroots = p.getRealRoots( r );
nroots = p.getPositiveRoots( r );
nroots = p.getNegativeRoots( r );
nroots = p.getRootsInRange( r_min, r_max, r );
nroots = p.getRootsInOpenRange( r_min, r_max, r );

Get roots

real_type r0 = p.real_root0();
real_type r1 = p.real_root1();
real_complex r0 = p.root0();
real_complex r1 = p.root1();
double re, im;
p.getRoot0( re, im );
p.getRoot0( r );
p.getRoot1( re, im );
p.getRoot1( r );
Lightweight complex number type used by the standard-precision solvers.
Definition PolynomialRoots-complex.hxx:30
double real_type
Scalar type used by the standard-precision API.
Definition PolynomialRoots.hh:66

Evaluate polynomial

{double or complex} v, x;
v = p.eval( x );
p.eval( x, p, dp );

Information

p.info( cout );
bool ok = p.check( cout );

Member Typedef Documentation

◆ complex_type

template<typename T_real, typename T_complex>
using PolynomialRoots::QuadraticT< T_real, T_complex >::complex_type = T_complex

◆ value_type

template<typename T_real, typename T_complex>
using PolynomialRoots::QuadraticT< T_real, T_complex >::value_type = T_real

Constructor & Destructor Documentation

◆ QuadraticT() [1/2]

template<typename T_real, typename T_complex>
PolynomialRoots::QuadraticT< T_real, T_complex >::QuadraticT ( )
default

Build an empty quadratic solver instance.

◆ QuadraticT() [2/2]

template<typename T_real, typename T_complex>
PolynomialRoots::QuadraticT< T_real, T_complex >::QuadraticT ( T_real const & a,
T_real const & b,
T_real const & c )
inline

Build the object that store the roots of the quadratic polynomial

\[ q(x) = a x^2 + b x + c \]

Parameters
[in]aleading coefficient of \( q(x) \)
[in]bcoefficient of \( x \)
[in]ccoefficient of \( x^0 \)

Member Function Documentation

◆ check()

template<typename T_real, typename T_complex>
bool PolynomialRoots::QuadraticT< T_real, T_complex >::check ( ostream_type & s) const
nodiscard

Check tolerance and quality of the computed roots

◆ complex_root()

template<typename T_real, typename T_complex>
bool PolynomialRoots::QuadraticT< T_real, T_complex >::complex_root ( ) const
inline

true if roots are complex conjugated

◆ complexRoot()

template<typename T_real, typename T_complex>
bool PolynomialRoots::QuadraticT< T_real, T_complex >::complexRoot ( ) const
inline

Alias of complex_root().

◆ double_root()

template<typename T_real, typename T_complex>
bool PolynomialRoots::QuadraticT< T_real, T_complex >::double_root ( ) const
inline

true if \( p(x) = a x^2 + b x + c = (x-r)^2 \)

◆ doubleRoot()

template<typename T_real, typename T_complex>
bool PolynomialRoots::QuadraticT< T_real, T_complex >::doubleRoot ( ) const
inline

Alias of double_root().

◆ eval() [1/3]

template<typename T_real, typename T_complex>
T_complex PolynomialRoots::QuadraticT< T_real, T_complex >::eval ( T_complex const & x) const
inline

Evaluate the quadratic polynomial at a complex point.

Parameters
[in]xcomplex value where compute \( p(x) \)
Returns
the value \( p(x) \)

◆ eval() [2/3]

template<typename T_real, typename T_complex>
T_real PolynomialRoots::QuadraticT< T_real, T_complex >::eval ( T_real const & x) const
inline

Evaluate the quadratic polynomial at a real point.

Parameters
[in]xvalue where compute \( p(x) \)
Returns
the value \( p(x) \)

◆ eval() [3/3]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::eval ( T_real const & x,
T_real & p,
T_real & dp ) const
inline

Evaluate the polynomial with its derivative

Parameters
[in]xvalue where compute \( p(x) \)
[out]pvalue \( p(x) \)
[out]dpvalue \( p'(x) \)

◆ get_negative_roots()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::get_negative_roots ( T_real r[]) const

Get negative real roots

Parameters
[out]rvector that will be filled with the real roots
Returns
the total number of negative real roots, 0, 1 or 2

◆ get_positive_roots()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::get_positive_roots ( T_real r[]) const

Get positive real roots

Parameters
[out]rvector that will be filled with the real roots
Returns
the total number of positive real roots, 0, 1 or 2

◆ get_real_roots()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::get_real_roots ( T_real r[]) const

Get the real roots

Parameters
[out]rvector that will be filled with the real roots
Returns
the total number of real roots, 0, 1 or 2

◆ get_root()

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::get_root ( integer const i,
T_real & re,
T_real & im ) const
inline

Store the i-th root in split real/imaginary form.

Parameters
[in]iroot index in [0,1]
[out]rereal part of the selected root
[out]imimaginary part of the selected root

◆ get_root0() [1/2]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::get_root0 ( T_complex & r) const
inline

Get the first root (complex or real)

Parameters
[out]rthe first complex root

◆ get_root0() [2/2]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::get_root0 ( T_real & re,
T_real & im ) const
inline

Get the first root (complex or real)

Parameters
[out]rethe first complex root, real part
[out]imthe first complex root, imaginary part

◆ get_root1() [1/2]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::get_root1 ( T_complex & r) const
inline

Get the second root (complex or real)

Parameters
[out]rthe second complex root

◆ get_root1() [2/2]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::get_root1 ( T_real & re,
T_real & im ) const
inline

Get the second root (complex or real)

Parameters
[out]rethe second complex root, real part
[out]imthe second complex root, imaginary part

◆ get_roots_in_open_range()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::get_roots_in_open_range ( T_real const & a,
T_real const & b,
T_real r[] ) const

Get real roots in an open range

Parameters
[in]aleft side of the range
[in]bright side of the range
[out]rvector that will be filled with the real roots
Returns
the total number of real roots in the open range \( (a,b) \)

◆ get_roots_in_range()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::get_roots_in_range ( T_real const & a,
T_real const & b,
T_real r[] ) const

Get real roots in a closed range

Parameters
[in]aleft side of the range
[in]bright side of the range
[out]rvector that will be filled with the real roots
Returns
the total number of real roots in the range \( [a,b] \)

◆ getNegativeRoots()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::getNegativeRoots ( T_real r[]) const
inline

◆ getPositiveRoots()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::getPositiveRoots ( T_real r[]) const
inline

◆ getRealRoots()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::getRealRoots ( T_real r[]) const
inline

Alias of get_real_roots().

◆ getRoot0() [1/2]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::getRoot0 ( T_complex & r) const
inline

Alias of get_root0().

◆ getRoot0() [2/2]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::getRoot0 ( T_real & re,
T_real & im ) const
inline

Alias of get_root0().

◆ getRoot1() [1/2]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::getRoot1 ( T_complex & r) const
inline

Alias of get_root1().

◆ getRoot1() [2/2]

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::getRoot1 ( T_real & re,
T_real & im ) const
inline

Alias of get_root1().

◆ getRootsInOpenRange()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::getRootsInOpenRange ( T_real const & a,
T_real const & b,
T_real r[] ) const
inline

◆ getRootsInRange()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::getRootsInRange ( T_real const & a,
T_real const & b,
T_real r[] ) const
inline

◆ info()

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::info ( ostream_type & s) const

Print info of the roots of the polynomial.

◆ num_roots()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::num_roots ( ) const
inline

Return the number of computed roots of

\[ q(x) = a x^2 + b x + c \]

Normally return 2 but if e.g. \( a = 0 \) return 1 or less depending on the values of \( a, b, c \)

Returns
number of computed roots

◆ numRoots()

template<typename T_real, typename T_complex>
integer PolynomialRoots::QuadraticT< T_real, T_complex >::numRoots ( ) const
inline

Alias of num_roots().

◆ real_root0()

template<typename T_real, typename T_complex>
T_real PolynomialRoots::QuadraticT< T_real, T_complex >::real_root0 ( ) const
inline

Return the first stored real component.

When the polynomial has a complex conjugate pair, this is the common real part of the pair returned by root0() and root1().

◆ real_root1()

template<typename T_real, typename T_complex>
T_real PolynomialRoots::QuadraticT< T_real, T_complex >::real_root1 ( ) const
inline

Return the second stored real component.

When the polynomial has a complex conjugate pair, this is the positive imaginary part paired with real_root0(). Otherwise it is the second real root.

◆ root()

template<typename T_real, typename T_complex>
T_complex PolynomialRoots::QuadraticT< T_real, T_complex >::root ( integer const i) const
inline

Return the i-th root as a complex value.

Parameters
[in]iroot index in [0,1]
Returns
requested root, or zero for an invalid index

◆ root0()

template<typename T_real, typename T_complex>
T_complex PolynomialRoots::QuadraticT< T_real, T_complex >::root0 ( ) const
inline

Return the first root as a complex value.

◆ root1()

template<typename T_real, typename T_complex>
T_complex PolynomialRoots::QuadraticT< T_real, T_complex >::root1 ( ) const
inline

Return the second root as a complex value.

◆ setup()

template<typename T_real, typename T_complex>
void PolynomialRoots::QuadraticT< T_real, T_complex >::setup ( T_real const & a,
T_real const & b,
T_real const & c )
inline

Setup the object that store the roots of the quadratic polynomial

\[ q(x) = a x^2 + b x + c \]

Parameters
[in]aleading coefficient of \( q(x) \)
[in]bcoefficient of \( x \)
[in]ccoefficient of \( x^0 \)

The documentation for this class was generated from the following files: