HiRep 0.1
Loading...
Searching...
No Matches
alg_remez.h
1/*
2
3 Mike Clark - 25th May 2005
4
5 alg_remez.h
6
7 AlgRemez is an implementation of the Remez algorithm, which in this
8 case is used for generating the optimal nth root rational
9 approximation.
10
11 Note this class requires the gnu multiprecision (GNU MP) library.
12
13*/
14
15#ifndef INCLUDED_ALG_REMEZ_H
16#define INCLUDED_ALG_REMEZ_H
17
18#include "bigfloat.h"
19
20#define JMAX 10000 //Maximum number of iterations of Newton's approximation
21#define SUM_MAX 10 // Maximum number of terms in exponential
22
23class AlgRemez {
24private:
25 char *cname;
26
27 // The approximation parameters
28 bigfloat *param, *roots, *poles;
29 bigfloat norm;
30
31 // The numerator and denominator degree (n=d)
32 int n, d;
33
34 // The bounds of the approximation
35 bigfloat apstrt, apwidt, apend;
36
37 // the numerator and denominator of the power we are approximating
38 unsigned long power_num;
39 unsigned long power_den;
40
41 // Flag to determine whether the arrays have been allocated
42 int alloc;
43
44 // Flag to determine whether the roots have been found
45 int foundRoots;
46
47 // Variables used to calculate the approximation
48 int nd1, iter;
49 bigfloat *xx, *mm, *step;
50 bigfloat delta, spread, tolerance;
51
52 // The exponential summation coefficients
53 bigfloat *a;
54 int *a_power;
55 int a_length;
56
57 // The number of equations we must solve at each iteration (n+d+1)
58 int neq;
59
60 // The precision of the GNU MP library
61 long prec;
62
63 // Initial values of maximal and minmal errors
64 void initialGuess();
65
66 // Solve the equations
67 void equations();
68
69 // Search for error maxima and minima
70 void search(bigfloat *step);
71
72 // Initialise step sizes
73 void stpini(bigfloat *step);
74
75 // Calculate the roots of the approximation
76 int root();
77
78 // Evaluate the polynomial
79 bigfloat polyEval(bigfloat x, bigfloat *poly, long size);
80 //complex_bf polyEval(complex_bf x, complex_bf *poly, long size);
81
82 // Evaluate the differential of the polynomial
83 bigfloat polyDiff(bigfloat x, bigfloat *poly, long size);
84 //complex_bf polyDiff(complex_bf x, complex_bf *poly, long size);
85
86 // Newton's method to calculate roots
87 bigfloat rtnewt(bigfloat *poly, long i, bigfloat x1, bigfloat x2, bigfloat xacc);
88 //complex_bf rtnewt(complex_bf *poly, long i, bigfloat x1, bigfloat x2, bigfloat xacc);
89
90 // Evaluate the partial fraction expansion of the rational function
91 // with res roots and poles poles. Result is overwritten on input
92 // arrays.
93 void pfe(bigfloat *res, bigfloat *poles, bigfloat norm);
94
95 // Calculate function required for the approximation
96 bigfloat func(bigfloat x);
97
98 // Compute size and sign of the approximation error at x
99 bigfloat getErr(bigfloat x, int *sign);
100
101 // Solve the system AX=B
102 int simq(bigfloat *A, bigfloat *B, bigfloat *X, int n);
103
104 // Free memory and reallocate as necessary
105 void allocate(int num_degree, int den_degree);
106
107 // Evaluate the rational form P(x)/Q(x) using coefficients from the
108 // solution vector param
109 bigfloat approx(bigfloat x);
110
111public:
112 // Constructor
113 AlgRemez(double lower, double upper, long prec);
114
115 // Destructor
116 virtual ~AlgRemez();
117
118 // Reset the bounds of the approximation
119 void setBounds(double lower, double upper);
120
121 // Generate the rational approximation x^(pnum/pden)
122 double generateApprox(int num_degree, int den_degree, unsigned long power_num, unsigned long power_den, bigfloat *dmm,
123 int a_len, double *a_param, int *a_pow);
124 double generateApprox(int num_degree, int den_degree, unsigned long power_num, unsigned long power_den, bigfloat *dmm = 0);
125 double generateApprox(int degree, unsigned long power_num, unsigned long power_den, bigfloat *dmm = 0);
126
127 void getMM(bigfloat *dmm);
128
129 // Return the partial fraction expansion of the approximation x^(pnum/pden)
130 int getPFE(double *Res, double *Pole, double *Norm);
131
132 // Return the partial fraction expansion of the approximation x^(-pnum/pden)
133 int getIPFE(double *Res, double *Pole, double *Norm);
134
135 // Return the poles and roots of the approximation x^(pnum/pden)
136 int getPR(double *Root, double *Pole, double *Norm);
137
138 // Evaluate the rational form P(x)/Q(x) using coefficients from the
139 // solution vector param
140 double evaluateApprox(double x);
141
142 // Evaluate the rational form Q(x)/P(x) using coefficients from the
143 // solution vector param
144 double evaluateInverseApprox(double x);
145
146 // Calculate function required for the approximation
147 double evaluateFunc(double x);
148
149 // Calculate inverse function required for the approximation
150 double evaluateInverseFunc(double x);
151};
152
153#endif // Include guard
Definition alg_remez.h:23
Definition bigfloat.h:19