18 #ifndef PIC_UTIL_coeffNOMIAL_HPP 19 #define PIC_UTIL_coeffNOMIAL_HPP 23 #include "../base.hpp" 25 #ifndef PIC_DISABLE_EIGEN 26 #ifndef PIC_EIGEN_NOT_BUNDLED 27 #include "../externals/Eigen/QR" 28 #include "../externals/Eigen/Eigenvalues" 57 for(
int i = 0; i < nCoeff; i++) {
58 coeff.push_back(0.0f);
71 if(nCoeff < 1 ||
coeff == NULL) {
91 for (
const float &c :
coeff) {
110 std::string ret =
"";
115 auto nCoeff =
coeff.size();
119 std::string sep =
"+ ";
120 if(
coeff[1] < 0.0f) {
126 for(
auto i = 1; i < (nCoeff - 1); i++) {
128 if(
coeff[i + 1] > 0.0f) {
151 ret =
new float[
coeff.size()];
154 for(
int i = 0; i <
coeff.size(); i++) {
170 for (
const float &c :
coeff) {
184 auto nCoeff =
coeff.size();
190 float ret =
coeff[nCoeff - 1] * float(nCoeff - 1);
192 for(
auto i = (nCoeff - 2); i > 0 ; i--) {
193 ret = (ret * x) +
coeff[i] *
float(i);
204 void fit(std::vector<float> &x, std::vector<float> &y,
int n)
206 #ifndef PIC_DISABLE_EIGEN 207 if(n < 1 || (x.size() != y.size())) {
215 int s = int(x.size());
216 Eigen::MatrixXf A(s, np1);
217 Eigen::VectorXf b(s);
219 for(
int i = 0; i < s; i++) {
224 for(
int j = (n - 1); j >= 0; j--) {
225 for(
int i = 0; i < s; i++) {
226 A(i, j) = x[i] * A(i, j + 1);
230 Eigen::VectorXf _x = A.colPivHouseholderQr().solve(b);
232 for(
int i = n; i >= 0; i--) {
233 coeff.push_back(_x(i));
243 auto last =
coeff.size() - 1;
245 if(fabsf(
coeff[last]) > 0.0f) {
247 for(
int i = 0; i < last; i++) {
263 int nCoeff = int(
coeff.size());
266 p.coeff[nCoeff - 2] =
coeff[nCoeff - 1];
268 for(
int i = (nCoeff - 3); i >= 0 ; i--) {
269 p.coeff[i] = (p.coeff[i + 1] * d +
coeff[i + 1]);
272 p.computeAllCoeffPositive();
274 remainder =
coeff[0] + p.coeff[0] * d;
286 auto nCoeff =
coeff.size();
294 if(
coeff[1] > 0.0f) {
311 float max_coeff0 = -1.0f;
313 for(
int i = 1; i <
coeff.size(); i++) {
314 float tmp = fabsf(
coeff[i]);
315 if(tmp > max_coeff0) {
320 float tmp = fabsf(
coeff[0]);
322 if(max_coeff0 < tmp) {
325 max_coeff = max_coeff0;
328 float lower_bound = fabsf(
coeff[0]) / (fabsf(
coeff[0]) + max_coeff0);
331 float upper_bound = 1.0f + max_coeff / fabsf(
coeff[0]);
332 printf(
"Upper bound: %f\n", upper_bound);
333 printf(
"Lower bound: %f\n", lower_bound);
336 float x_p = lower_bound;
338 bool notConverged =
true;
341 while(notConverged) {
343 x_n = x_p - E_x_p /
dEval(x_p);
347 notConverged = (fabsf(E_x_p) > 1e-6f) && (counter < 500);
365 for(
int i = 0; i <
coeff.size() - 1; i++) {
379 int nCoeff = int(
coeff.size());
380 for(
int i = 1; i < (nCoeff - 2); i++) {
402 #ifndef PIC_DISABLE_EIGEN 405 m << -p[3], -p[2], -p[1], -p[0],
406 1.0f, 0.0f, 0.0f, 0.0f,
407 0.0f, 1.0f, 0.0f, 0.0f,
408 0.0f, 0.0f, 1.0f, 0.0f;
410 Eigen::EigenSolver<Eigen::Matrix4d> es(m);
411 Eigen::Vector4cd e = es.eigenvalues();
415 for (
int i = 0; i < 4; i++) {
416 if (fabs(e(i).imag()) <= 0.0) {
417 x[i] = float(e(i).real());
439 float delta = b * b - 4.0f * a * c;
442 float dnum = 2.0f * a;
443 delta = sqrtf(delta);
444 *x0 = (-b + delta) / dnum;
445 *x1 = (-b - delta) / dnum;
463 float delta = b * b - a * c;
465 delta = sqrtf(delta);
466 *x0 = (-b + delta) / a;
467 *x1 = (-b - delta) / a;
479 #endif //PIC_UTIL_coeffNOMIAL_HPP float eval(float x)
eval
Definition: polynomial.hpp:166
std::string fromNumberToString(T num)
fromNumberToString converts a number into a string.
Definition: string.hpp:102
static bool getQuarticRoots(float *p, float *x)
getQuarticRoots –> MANDATORY p[5] == 1.0
Definition: polynomial.hpp:400
void normalForm()
normalForm
Definition: polynomial.hpp:241
static bool getSecondOrderRoots(float a, float b, float c, float *x0, float *x1)
getSecondOrderRoots solves second order equations, ax^2 + b x + c = 0
Definition: polynomial.hpp:437
float * getArray(float *ret)
getArray
Definition: polynomial.hpp:148
void print()
print
Definition: polynomial.hpp:103
Definition: polynomial.hpp:37
Polynomial()
Polynomial.
Definition: polynomial.hpp:46
float dEval(float x)
dEval
Definition: polynomial.hpp:182
bool all_coeff_positive
Definition: polynomial.hpp:41
void computeAllCoeffPositive()
computeAllCoeffPositive
Definition: polynomial.hpp:88
std::string toString()
Definition: polynomial.hpp:108
Polynomial(float *coeff, int nCoeff)
coeffnomial
Definition: polynomial.hpp:69
bool getAllRoots(float *x)
getAllRoots
Definition: polynomial.hpp:363
Definition: bilateral_separation.hpp:25
static bool getSecondOrderRootsS(float a, float b, float c, float *x0, float *x1)
getSecondOrderRootsS solves second order equations, ax^2 + 2 b x + c = 0; i.e., 2 b is even! ...
Definition: polynomial.hpp:461
Polynomial(int nCoeff)
Polynomial.
Definition: polynomial.hpp:55
std::vector< float > coeff
Definition: polynomial.hpp:40
void fit(std::vector< float > &x, std::vector< float > &y, int n)
fit
Definition: polynomial.hpp:204
bool getRoots(float *x)
getRoots
Definition: polynomial.hpp:284
~Polynomial()
Definition: polynomial.hpp:80
Polynomial horner(float d, float &remainder)
horner
Definition: polynomial.hpp:261