00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _LIB_NUMERICS_ROOTSOLVE_ROOTSOLVE_H_
00018 #define _LIB_NUMERICS_ROOTSOLVE_ROOTSOLVE_H_ 1
00019
00020 #include <lib/numerics/num_math.h>
00021 #include <lib/salloc.h>
00022
00023
00024 namespace NUM
00025 {
00026
00034 class PolyRootSolver
00035 {
00036 private:
00037 public:
00038 PolyRootSolver() {}
00039 virtual ~PolyRootSolver() {}
00040
00053 virtual int solve(int order,const dbl *c,dbl *r);
00054 };
00055
00056
00068 class PolyRootSolver_Direct : public PolyRootSolver
00069 {
00070 public:
00072 struct Params
00073 {
00075 dbl nearly_zero;
00076 };
00077 Params par;
00078
00080 static const Params defaults;
00081
00082 public:
00083 PolyRootSolver_Direct();
00084 ~PolyRootSolver_Direct();
00085
00087 int solve(int order,const dbl *c,dbl *r);
00088
00094 int Solve_1(const dbl *c,dbl *r);
00095 int Solve_2(const dbl *c,dbl *r);
00096 int Solve_3(const dbl *c,dbl *r);
00097 int Solve_4(const dbl *c,dbl *r);
00098 };
00099
00100
00113 class PolyRootSolver_Sturm : public PolyRootSolver
00114 {
00115 public:
00117 struct Params
00118 {
00120 dbl nearly_zero;
00122 dbl relerror;
00124 int sbisect_maxit;
00126 int modrf_maxit;
00127 };
00128 Params par;
00129
00131 static const Params defaults;
00132
00133 private:
00139 #define PolyRootSolver_Sturm_BufOnStack 1
00140
00142 struct poly
00143 {
00144 int ord;
00145 dbl *coef;
00146
00147 inline poly() : ord(0),coef(NULL) {}
00148
00149 #if !PolyRootSolver_Sturm_BufOnStack
00150 inline void AllocCoeffs(int order)
00151 { coef=REALLOC(coef,order+1); }
00152 inline ~poly() { coef=FREE(coef); }
00153 #else
00154 inline ~poly() {}
00155 #endif
00156 };
00157
00159 int modrf(int ord,dbl *coef,dbl a,dbl b,dbl *val);
00161 int modp(poly *u,poly *v,poly *r);
00163 int buildsturm(int ord,poly *sseq);
00165 int numroots(int np,poly *sseq,int *atneg,int *atpos);
00167 int numchanges(int np,poly *sseq,dbl a);
00169 int sbisect(int np,poly *sseq,dbl min,dbl max,int atmin,
00170 int atmax,dbl *roots);
00171 public:
00172 PolyRootSolver_Sturm();
00173 ~PolyRootSolver_Sturm();
00174
00183 int solve(int order,const dbl *c,dbl *r);
00184 };
00185
00186
00198 class PolyRootSolver_Bairstow : public PolyRootSolver
00199 {
00200 public:
00202 struct Params
00203 {
00205 dbl nearly_zero;
00207 int maxiter;
00209 int iter_chg;
00211 dbl initial_epsilon;
00213 dbl disc_epsilon;
00214 };
00215 Params par;
00216
00218 static const Params defaults;
00219
00220 private:
00221 int _QuadricRealRoots(dbl *a,int n,dbl *wr);
00222 int _QuadricRoots(dbl *a,int n,dbl *wr,dbl *wi);
00223 int _FindQuadFact(dbl *a,int n,dbl *b,dbl *quad,
00224 int red_fact,dbl *err_est);
00225 void _GetQuads(dbl *a,int n,dbl *quad,dbl *x);
00226 int _Recurse(dbl *a,int n,dbl *b,int m,dbl *quad,dbl *err);
00227 void _DiffPoly(dbl *a,int n,dbl *b);
00228 dbl _Deflate(dbl *a,int n,dbl *b,dbl *quad);
00229
00230 public:
00231 PolyRootSolver_Bairstow();
00232 ~PolyRootSolver_Bairstow();
00233
00235 int solve(int order,const dbl *c,dbl *r);
00236 };
00237
00238 }
00239
00240 #endif