34 #include <Base/Common/BIASpragma.hh>
36 #include <MathAlgo/PolynomialSolve.hh>
37 #include <Base/Math/Random.hh>
47 bool operator<(complex<double>& left, complex<double>& right)
49 return (left.real()<right.real());
51 bool operator<(const complex<double>& left,
const complex<double>& right)
53 return (left.real()<right.real());
57 void solout(vector<POLYNOMIALSOLVE_TYPE>& sol)
59 sort(sol.begin(), sol.end());
60 int s=(int)sol.size();
62 for (
int i=0; i<s; i++)
63 if (debug) cout << sol[i] <<
" ";
64 if (debug) cout << endl;
67 void solout(vector<complex<POLYNOMIALSOLVE_TYPE> >& sol)
69 sort(sol.begin(), sol.end());
70 int s=(int)sol.size();
72 for (
int i=0; i<s; i++)
73 if (debug) cout << sol[i] <<
" ";
74 if (debug) cout << endl;
78 void errout(vector<POLYNOMIALSOLVE_TYPE>& sol,
79 vector<POLYNOMIALSOLVE_TYPE>& gtsol)
81 sort(sol.begin(), sol.end());
82 int s=(int)sol.size();
84 if (debug) cout <<
"error: ";
85 for (
int i=0; i<s; i++){
87 if (debug) cout << fabs(sol[i]-gtsol[i]) <<
" ";
88 if (fabs(sol[i]-gtsol[i])>maxerr) {
89 cerr <<
"\n\n\nerror bigger than "<<maxerr<<
"\n" <<endl;
91 cout <<
"ground truth: ";
96 for (
int i=0; i<s; i++) cout << fabs(sol[i]-gtsol[i]) <<
" ";
98 cerr <<
"\n\n\nthis happens if the roots of the polynomial are close together\n\n\n"
103 if (debug) cout << endl << endl;
106 void errout(vector<complex<POLYNOMIALSOLVE_TYPE> >& sol,
107 vector<POLYNOMIALSOLVE_TYPE>& gtsol)
109 sort(sol.begin(), sol.end());
110 int s=(int)sol.size();
112 if (debug) cout <<
"error: ";
113 for (
int i=0; i<s; i++){
114 if (debug) cout << fabs((sol[i]).real()-gtsol[i]) <<
" ";
115 if (fabs((sol[i]).real()-gtsol[i])>maxerr) {
116 cerr <<
"\n\n\nerror bigger than "<<maxerr<<
"\n" <<endl;
118 cout <<
"ground truth: ";
120 cout <<
"computed: ";
123 for (
int i=0; i<s; i++) cout << fabs((sol[i]).real()-gtsol[i]) <<
" ";
125 cerr <<
"\n\n\nthis happens if the roots of the polynomial are close together\n\n\n"
130 if (debug) cout << endl << endl;
193 vector<POLYNOMIALSOLVE_TYPE> coeff, sol, gtsol;
194 vector<complex<POLYNOMIALSOLVE_TYPE> > csol;
196 for (
int n=0; n<num; n++){
200 cout <<
"-----------------"<< setw(3) << n <<
"------------------"<<endl;
201 cout <<
"using a polynom of degree "<<deg<<endl;
206 cout <<
"." << flush;
213 for (
int d=0; d<deg; d++){
217 cout << d <<
"th root: "<<gtsol[d]<<endl;
219 sort(gtsol.begin(), gtsol.end());
224 for (
int i=0; i<deg+1; i++){
225 cout << i <<
"th coefficient: "<< coeff[i]<<endl;
240 cout <<
"polynomial at ground truth roots: "<<endl;
241 for (
int i=0; i<deg; i++){
248 nums=ps.
Solve(coeff, sol);
249 if (debug) cout <<
"Solve (real) found "<<nums<<
" solutions:\n";
250 if (nums>0) { solout(sol); errout(sol, gtsol); }
251 nums=ps.
Solve(coeff, csol);
252 if (debug) cout <<
"Solve (complex) found "<<nums<<
" solutions:\n";
253 if (nums>0) { solout(csol); errout(csol, gtsol); }
256 if (debug)cout<<
"Numeric (real) found "<<nums<<
" solutions:\n";
257 if (nums>0) { solout(sol); errout(sol, gtsol); }
259 if (debug) cout<<
"Numeric (complex) found "<<nums<<
" solutions:\n";
260 if (nums>0) { solout(csol); errout(csol, gtsol); }
264 if (debug) cout <<
"Analytic (real) found "<<nums<<
" solutions:\n";
265 if (nums>0) { solout(sol); errout(sol, gtsol); }
269 if (debug) cout <<
"Analytic (complex) found "<<nums<<
" solutions:\n";
270 if (nums>0) { solout(csol); errout(csol, gtsol); }
int Numeric(const std::vector< POLYNOMIALSOLVE_TYPE > &coeff, std::vector< POLYNOMIALSOLVE_TYPE > &sol)
solves polynomial of arbitrary order n numerically coeff[n]x^n+...+coeff[2]x^2+coeff[1]x+coeff[0]=0 c...
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
base class for solving polynomial equations
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
POLYNOMIALSOLVE_TYPE EvaluatePolynomial(const POLYNOMIALSOLVE_TYPE x, const std::vector< POLYNOMIALSOLVE_TYPE > &coeff) const
numerically robust way to evaluate a polynomial at position x, uses Horner scheme (same principle as ...
void CalCoefficients(const std::vector< POLYNOMIALSOLVE_TYPE > &sol, std::vector< POLYNOMIALSOLVE_TYPE > &coeff)
inverse function, calculates coefficients from the solutions
int Analytic(const std::vector< POLYNOMIALSOLVE_TYPE > &coeff, std::vector< POLYNOMIALSOLVE_TYPE > &sol)
solves polynomial of arbitrary order n<=4 analytically coeff[n]x^n+...+coeff[2]x^2+coeff[1]x+coeff[0]...
int Solve(const std::vector< POLYNOMIALSOLVE_TYPE > &coeff, std::vector< POLYNOMIALSOLVE_TYPE > &sol)
solve polynomial of arbitrary order n coeff[n]x^n+...+coeff[2]x^2+coeff[1]x+coeff[0]=0 coeff[n]!=0...
class for producing random numbers from different distributions