CHROMA
remez_gmp.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! \file
3  * \brief Remez algorithm for finding nth roots
4  */
5 
6 #ifndef __remez_gmp_h__
7 #define __remez_gmp_h__
8 
9 #include "chromabase.h"
12 
13 namespace Chroma
14 {
15 
16  //! Remez algorithm
17  /*! @ingroup monomial
18  *
19  * The Remez algorithm is used for generating the nth root
20  * rational approximation.
21  *
22  * Note this class can only be used if
23  * the gnu multiprecision (GNU MP) library is present.
24  *
25  */
26  class RemezGMP
27  {
28  public:
29  //! Default constructor
30  RemezGMP();
31 
32  //! Constructor
33  RemezGMP(const Real& lower, const Real& upper, long prec);
34 
35  //! Destructor
36  ~RemezGMP() {}
37 
38  //! Reset the bounds of the approximation
39  void setBounds(const Real& lower, const Real& upper);
40 
41  //! Generate the rational approximation x^(pnum/pden)
42  Real generateApprox(int num_degree, int den_degree,
43  unsigned long power_num, unsigned long power_den);
44  Real generateApprox(int degree,
45  unsigned long power_num, unsigned long power_den);
46 
47  //! Return the partial fraction expansion of the approximation x^(pnum/pden)
49 
50  //! Return the partial fraction expansion of the approximation x^(-pnum/pden)
52 
53  //! Given a partial fraction expansion, evaluate it at x
54  Real evalPFE(const Real& x, const RemezCoeff_t& coeff);
55 
56  private:
57  // The approximation parameters
58  multi1d<bigfloat> param, roots, poles;
60 
61  //! The numerator and denominator degree (n=d)
62  int n, d;
63 
64  //! The bounds of the approximation
66 
67  //! the numerator and denominator of the power we are approximating
68  unsigned long power_num;
69  unsigned long power_den;
70 
71  //! Flag to determine whether the arrays have been allocated
72  bool alloc;
73 
74  //! Variables used to calculate the approximation
75  int nd1, iter;
76  multi1d<bigfloat> xx, mm, step;
78 
79  //! The number of equations we must solve at each iteration (n+d+1)
80  int neq;
81 
82  //! The precision of the GNU MP library
83  long prec;
84 
85  //! Initial values of maximal and minmal errors
86  void initialGuess();
87 
88  //! Solve the equations
89  void equations();
90 
91  //! Search for error maxima and minima
92  void search(multi1d<bigfloat>& step);
93 
94  //! Initialise step sizes
95  void stpini(multi1d<bigfloat>& step);
96 
97  //! Calculate the roots of the approximation
98  int root();
99 
100  //! Evaluate the polynomial
101  bigfloat polyEval(const bigfloat& x, const multi1d<bigfloat>& poly, long size);
102 
103  //! Evaluate the differential of the polynomial
104  bigfloat polyDiff(const bigfloat& x, const multi1d<bigfloat>& poly, long size);
105 
106  //! Newton's method to calculate roots
107  bigfloat rtnewt(const multi1d<bigfloat>& poly, long i,
108  const bigfloat& x1, const bigfloat& x2, const bigfloat& xacc);
109 
110  // Evaluate the partial fraction expansion of the rational function
111  // with res roots and poles poles. Result is overwritten on input
112  // arrays.
113  void pfe(multi1d<bigfloat>& res, multi1d<bigfloat>& poles, const bigfloat& norm);
114 
115  // Evaluate the rational form P(x)/Q(x) using coefficients from the
116  // solution std::vector param
117  bigfloat approx(const bigfloat& x);
118 
119  //! Calculate function required for the approximation
120  bigfloat func(const bigfloat& x);
121 
122  //! Compute size and sign of the approximation error at x
123  bigfloat getErr(const bigfloat& x, int& sign);
124 
125  //! Solve the system AX=B
126  int simq(multi1d<bigfloat>& A, multi1d<bigfloat>& B, multi1d<bigfloat>& X, int n);
127 
128  //! Free memory and reallocate as necessary
129  void allocate(int num_degree, int den_degree);
130  };
131 
132 } // namespace Chroma
133 
134 #endif // include guard
135 
136 
137 
Remez algorithm for finding nth roots.
Primary include file for CHROMA library code.
Remez algorithm.
Definition: remez_gmp.h:27
bigfloat polyDiff(const bigfloat &x, const multi1d< bigfloat > &poly, long size)
Evaluate the differential of the polynomial.
Definition: remez_gmp.cc:646
bigfloat delta
Definition: remez_gmp.h:77
bigfloat getErr(const bigfloat &x, int &sign)
Compute size and sign of the approximation error at x.
Definition: remez_gmp.cc:429
bool alloc
Flag to determine whether the arrays have been allocated.
Definition: remez_gmp.h:72
int simq(multi1d< bigfloat > &A, multi1d< bigfloat > &B, multi1d< bigfloat > &X, int n)
Solve the system AX=B.
Definition: remez_gmp.cc:468
bigfloat func(const bigfloat &x)
Calculate function required for the approximation.
Definition: remez_gmp.cc:447
void allocate(int num_degree, int den_degree)
Free memory and reallocate as necessary.
Definition: remez_gmp.cc:54
int neq
The number of equations we must solve at each iteration (n+d+1)
Definition: remez_gmp.h:80
bigfloat approx(const bigfloat &x)
Definition: remez_gmp.cc:410
void initialGuess()
Initial values of maximal and minmal errors.
Definition: remez_gmp.cc:228
multi1d< bigfloat > xx
Definition: remez_gmp.h:76
bigfloat rtnewt(const multi1d< bigfloat > &poly, long i, const bigfloat &x1, const bigfloat &x2, const bigfloat &xacc)
Newton's method to calculate roots.
Definition: remez_gmp.cc:655
RemezCoeff_t getIPFE()
Return the partial fraction expansion of the approximation x^(-pnum/pden)
Definition: remez_gmp.cc:189
multi1d< bigfloat > mm
Definition: remez_gmp.h:76
unsigned long power_den
Definition: remez_gmp.h:69
Real evalPFE(const Real &x, const RemezCoeff_t &coeff)
Given a partial fraction expansion, evaluate it at x.
Definition: remez_gmp.cc:762
bigfloat tolerance
Definition: remez_gmp.h:77
bigfloat apwidt
Definition: remez_gmp.h:65
multi1d< bigfloat > roots
Definition: remez_gmp.h:58
bigfloat norm
Definition: remez_gmp.h:59
int n
The numerator and denominator degree (n=d)
Definition: remez_gmp.h:62
~RemezGMP()
Destructor.
Definition: remez_gmp.h:36
void stpini(multi1d< bigfloat > &step)
Initialise step sizes.
Definition: remez_gmp.cc:260
multi1d< bigfloat > poles
Definition: remez_gmp.h:58
void pfe(multi1d< bigfloat > &res, multi1d< bigfloat > &poles, const bigfloat &norm)
Definition: remez_gmp.cc:683
multi1d< bigfloat > step
Definition: remez_gmp.h:76
bigfloat polyEval(const bigfloat &x, const multi1d< bigfloat > &poly, long size)
Evaluate the polynomial.
Definition: remez_gmp.cc:638
multi1d< bigfloat > param
Definition: remez_gmp.h:58
RemezGMP()
Default constructor.
Definition: remez_gmp.cc:13
RemezCoeff_t getPFE()
Return the partial fraction expansion of the approximation x^(pnum/pden)
Definition: remez_gmp.cc:149
unsigned long power_num
the numerator and denominator of the power we are approximating
Definition: remez_gmp.h:68
bigfloat apstrt
The bounds of the approximation.
Definition: remez_gmp.h:65
bigfloat spread
Definition: remez_gmp.h:77
void search(multi1d< bigfloat > &step)
Search for error maxima and minima.
Definition: remez_gmp.cc:277
void setBounds(const Real &lower, const Real &upper)
Reset the bounds of the approximation.
Definition: remez_gmp.cc:71
long prec
The precision of the GNU MP library.
Definition: remez_gmp.h:83
int root()
Calculate the roots of the approximation.
Definition: remez_gmp.cc:588
Real generateApprox(int num_degree, int den_degree, unsigned long power_num, unsigned long power_den)
Generate the rational approximation x^(pnum/pden)
Definition: remez_gmp.cc:89
int nd1
Variables used to calculate the approximation.
Definition: remez_gmp.h:75
bigfloat apend
Definition: remez_gmp.h:65
void equations()
Solve the equations.
Definition: remez_gmp.cc:367
Bigfloat.
Definition: bigfloat.h:23
int x
Definition: meslate.cc:34
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
int i
Definition: pbg5p_w.cc:55
A(A, psi, r, Ncb, PLUS)
Remez algorithm coefficients.
Convenient structure to package Remez coeffs.
Definition: remez_coeff.h:19