Defines | |
| #define | CPL_POLYNOMIAL_CMP |
| Compare the coefficients of two polynomials. | |
Functions | |
| cpl_error_code | cpl_polynomial_copy (cpl_polynomial *out, const cpl_polynomial *in) |
| This function copies contents of a polynomial into another one. | |
| void | cpl_polynomial_delete (cpl_polynomial *p) |
| Delete a cpl_polynomial. | |
| cpl_error_code | cpl_polynomial_derivative (cpl_polynomial *self, int dim) |
| Compute a first order partial derivative. | |
| cpl_error_code | cpl_polynomial_dump (const cpl_polynomial *p, FILE *stream) |
| Dump a cpl_polynomial as ASCII to a stream. | |
| cpl_polynomial * | cpl_polynomial_duplicate (const cpl_polynomial *p) |
| This function duplicates an existing polynomial. | |
| double | cpl_polynomial_eval (const cpl_polynomial *p, const cpl_vector *x) |
| Evaluate the polynomial at the given point. | |
| double | cpl_polynomial_eval_1d (const cpl_polynomial *p, double x, double *pd) |
| Evaluate a univariate polynomial using Horners rule. | |
| cpl_polynomial * | cpl_polynomial_extract (const cpl_polynomial *self, int dim, const cpl_polynomial *other) |
| Collapse one dimension of a multi-variate polynomial by composition. | |
| cpl_polynomial * | cpl_polynomial_fit_1d_create (const cpl_vector *x_pos, const cpl_vector *values, int degree, double *mse) |
| Fit a 1D-polynomial to a 1D-signal in a least squares sense. | |
| cpl_polynomial * | cpl_polynomial_fit_2d_create (cpl_bivector *xy_pos, cpl_vector *values, int degree, double *mse) |
| Fit a 2D-polynomial to a 2D-surface in a least squares sense. | |
| double | cpl_polynomial_get_coeff (const cpl_polynomial *in, const int *pows) |
| Get a coefficient of the polynomial. | |
| int | cpl_polynomial_get_degree (const cpl_polynomial *p) |
| The degree of the polynomial. | |
| int | cpl_polynomial_get_dimension (const cpl_polynomial *p) |
| The dimension of the polynomial. | |
| cpl_polynomial * | cpl_polynomial_new (int dim) |
| Create a new cpl_polynomial. | |
| cpl_error_code | cpl_polynomial_set_coeff (cpl_polynomial *in, const int *pows, double c) |
| Set a coefficient of the polynomial. | |
| cpl_error_code | cpl_polynomial_shift_1d (cpl_polynomial *p, double u) |
| Given p and u, modify the polynomial to p(x) := p(x+u). | |
| cpl_error_code | cpl_polynomial_solve_1d (const cpl_polynomial *p, double x0, double *px, int mul) |
| A real solution to p(x) = 0 using Newton-Raphsons method. | |
| cpl_error_code | cpl_vector_fill_polynomial (cpl_vector *v, const cpl_polynomial *p, double x0, double d) |
| Evaluate a 1D-polynomial on equidistant points using Horners rule. | |
Univariate polynomials use the Horner rule for evaluation, while multivariate polynomials are evaluated simply as the sum of each term.
This means that of the two polynomials
* P1(x) = p0 + p1.x + p4.x^2 *
* P2(x,y) = p0 + p1.x + p2.y + p3.x.y + p4.x^2 + p5.y^2 *
Note that a polynomial like P3(z) = p0 + p1.z + p2.z^2 + p3.z^3, z=x^4 is preferable to p4(x) = p0 + p1.x^4 + p2.x^8 + p3.x^12.
| #define CPL_POLYNOMIAL_CMP |
Value:
/* Verify that it differs within tolerance */ \ if (fabs(p1->c[i] - p2->c[j]) <= tol) { \ /* Verify that the powers match */ \ for (dim=0; dim < p1->dim; dim++) \ if (p1->pow[p1->dim * i + dim] \ != p2->pow[p1->dim * j + dim]) break; \ if (dim == p1->dim) break; /* - found it */ \ }
| p1 | the 1st polynomial | |
| p2 | the 2nd polynomial | |
| tol | The absolute (non-negative) tolerance |
This means that the following pair of polynomials per definition are considered different: P1(x1,x2) = 3*x1 different from P2(x1) = 3*x1.
If all parameters are valid and p1 and p2 point to the same polynomial the functions returns 0.
Possible _cpl_error_code_ set in this function:
| cpl_error_code cpl_polynomial_copy | ( | cpl_polynomial * | out, | |
| const cpl_polynomial * | in | |||
| ) |
This function copies contents of a polynomial into another one.
| out | Pre-allocated output cpl_polynomial | |
| in | Input cpl_polynomial |
If out already contains coefficients, they are overwritten.
This is the only function that can modify the dimension of a polynomial.
Possible _cpl_error_code_ set in this function:
| void cpl_polynomial_delete | ( | cpl_polynomial * | p | ) |
Delete a cpl_polynomial.
| p | cpl_polynomial to delete |
| cpl_error_code cpl_polynomial_derivative | ( | cpl_polynomial * | self, | |
| int | dim | |||
| ) |
Compute a first order partial derivative.
| self | The polynomial to be modified in place | |
| dim | The dimension to differentiate (zero for first dimension) |
The call requires n FLOPs, where n is the number of (non-zero) polynomial coefficients whose power in dimension dim is at least 1.
Possible _cpl_error_code_ set in this function:
| cpl_error_code cpl_polynomial_dump | ( | const cpl_polynomial * | p, | |
| FILE * | stream | |||
| ) |
Dump a cpl_polynomial as ASCII to a stream.
| p | Input cpl_polynomial to dump | |
| stream | Output stream, accepts stdout or stderr |
Comment lines start with the hash character.
Possible _cpl_error_code_ set in this function:
| cpl_polynomial* cpl_polynomial_duplicate | ( | const cpl_polynomial * | p | ) |
This function duplicates an existing polynomial.
| p | The input cpl_polynomial |
Possible _cpl_error_code_ set in this function:
| double cpl_polynomial_eval | ( | const cpl_polynomial * | p, | |
| const cpl_vector * | x | |||
| ) |
Evaluate the polynomial at the given point.
| p | The polynomial | |
| x | Point of evaluation |
A polynomial with no non-zero coefficents evaluates as 0.
For 1-dimensional polynomials the call requires 2n FLOPs where n+1 is the number of coefficients in p, see also cpl_polynomial_eval_1d(). For multivariate polynomials the call requires n*(1+dim) + d_1 + d_2 + ... + d_dim FLOPs, where dim is the dimenstion, n is the number of coefficients in p and d_i is the highest power used in dimension i, i = 1, 2, ..., dim.
Possible _cpl_error_code_ set in this function:
| double cpl_polynomial_eval_1d | ( | const cpl_polynomial * | p, | |
| double | x, | |||
| double * | pd | |||
| ) |
Evaluate a univariate polynomial using Horners rule.
| p | The 1D-polynomial | |
| x | The point of evaluation | |
| pd | Iff pd is not null, the derivative p`(x) evaluated at x |
The result is computed as p_0 + x * ( p_1 + x * ( p_2 + ... x * p_n )) and requires 2n FLOPs where n+1 is the number of coefficients in p.
If the derivative is requested it is computed along with p(x), using a nested Horner rule. This requires 4n FLOPs in total.
Possible _cpl_error_code_ set in this function:
| cpl_polynomial* cpl_polynomial_extract | ( | const cpl_polynomial * | self, | |
| int | dim, | |||
| const cpl_polynomial * | other | |||
| ) |
Collapse one dimension of a multi-variate polynomial by composition.
| self | The multi-variate polynomial | |
| dim | The dimension to collapse (zero for first dimension) | |
| other | The polynomial to replace dimension dim of self |
The created polynomial thus has a dimension which is one less than the polynomial self and which is equal to that of the other polynomial. Collapsing one dimension of a 1D-polynomial is equivalent to evaluating it, which can be done with cpl_polynomial_eval_1d().
FIXME: The other polynomial must currently have a degree of zero, i.e. it must be a constant.
Currently, the call requires dn + p FLOPs, where d the dimension of the polynomial self, p is the largest power of dimension dim and n the number of (non-zero) coefficients of the polynomial self.
The returned object is a newly allocated cpl_polynomial that must be deallocated by the caller using cpl_polynomial_delete().
Possible _cpl_error_code_ set in this function:
| cpl_polynomial* cpl_polynomial_fit_1d_create | ( | const cpl_vector * | x_pos, | |
| const cpl_vector * | values, | |||
| int | degree, | |||
| double * | mse | |||
| ) |
Fit a 1D-polynomial to a 1D-signal in a least squares sense.
| x_pos | Vector of positions of the signal to fit. | |
| values | Vector of values of the signal to fit. | |
| degree | Non-negative polynomial degree. | |
| mse | Iff mse is not null, the mean squared error on success |
The input signal is given in x_pos and values which must be of the same size.
x_pos must contain more than degree distinct values.
mse may be NULL. If it is not, *mse is set to the mean squared error on success, while it is unchanged on error.
The fit is done in the following steps: 1) x_pos is first transformed into xhat = x_pos - mean(x_pos). 2) The normal equations of the Vandermonde matrix are formed from xhat. 3) The normal equations are solved using Cholesky factorization. 4) The resulting polynomial in xhat is transformed back to x_pos.
The call requires 6MN + N^3/3 + 7/2N^2 + O(M) FLOPs where M is the number of data points and where N is the number of polynomial coefficients, N = degree + 1.
Possible _cpl_error_code_ set in this function:
| cpl_polynomial* cpl_polynomial_fit_2d_create | ( | cpl_bivector * | xy_pos, | |
| cpl_vector * | values, | |||
| int | degree, | |||
| double * | mse | |||
| ) |
Fit a 2D-polynomial to a 2D-surface in a least squares sense.
| xy_pos | Bivector positions of the surface to fit. | |
| values | Vector of values of the surface to fit. | |
| degree | Non-negative polynomial degree. | |
| mse | Iff mse is not null, the mean squared error on success |
The input signal is given in xy_pos and values which must be of the same size.
The size of xy_pos must be at least (degree+1)*(degree+2)/2, which is the number of polynomial coefficients to be determined - and xy_pos must contain at least that many distinct values.
mse may be NULL. If it is not, *mse is set to the mean squared error on success, while it is unchanged on error.
Example: For degree=3, the following terms will be computed:
1 x x^2 x^3 y x.y x^2.y y^2 x.y^2 y^3
The fit is done in the following steps: 1) The x-positions are first transformed into xhat = x - mean(x), and the y-positions are first transformed into yhat = y - mean(y). 2) The Vandermonde matrix is formed from xhat and yhat. 3) The normal equations of the Vandermonde matrix is solved. 4) The resulting polynomial in (xhat, yhat) is transformed to back (x,y).
Warning: An increase in the polynomial degree will normally reduce the mean squared error. However, due to rounding errors and the limited accuracy of the solver of the normal equations, an increase in the polynomial degree may at some point cause the mse to _increase_. In some cases this happens with an increase of the polynomial degree from 8 to 9.
The call requires MN^2 + N^3/3 + O(MN) FLOPs where M is the number of data points and where N is the number of polynomial coefficients, N = (degree + 1)*(degree + 2)/2.
Possible _cpl_error_code_ set in this function:
| double cpl_polynomial_get_coeff | ( | const cpl_polynomial * | in, | |
| const int * | pows | |||
| ) |
Get a coefficient of the polynomial.
| in | the input polynomial | |
| pows | the powers of the different variables |
It is allowed to specify a (set of) power(s) for which no coefficient has previously been set. In this case the function returns zero.
Possible _cpl_error_code_ set in this function:
| int cpl_polynomial_get_degree | ( | const cpl_polynomial * | p | ) |
The degree of the polynomial.
| p | the polynomial |
If there are no non-zero coefficients the degree is zero.
Possible _cpl_error_code_ set in this function:
| int cpl_polynomial_get_dimension | ( | const cpl_polynomial * | p | ) |
The dimension of the polynomial.
| p | the polynomial |
| cpl_polynomial* cpl_polynomial_new | ( | int | dim | ) |
Create a new cpl_polynomial.
| dim | The positive polynomial dimension (number of variables) |
A newly created polynomial has degree 0 and evaluates as 0.
Possible _cpl_error_code_ set in this function:
| cpl_error_code cpl_polynomial_set_coeff | ( | cpl_polynomial * | in, | |
| const int * | pows, | |||
| double | c | |||
| ) |
Set a coefficient of the polynomial.
| in | the input polynomial | |
| pows | the powers of the different variables | |
| c | the coefficient |
If the coefficient is already there, it is overwritten, if not, a new coefficient is added to the polynomial. This may cause the degree of the polynomial to be increased.
Setting the coefficient of x1^4 * x3^2 in the 4 dimensional polynomial p to 12.3 would be performed by:
cpl_polynomial_set_coeff(p, pows, 12.3); where pows is the integer array [4, 0, 2, 0].
For efficiency reasons the coefficients of a 1d-polynomial are best inserted with the leading coefficient first.
Possible _cpl_error_code_ set in this function:
| cpl_error_code cpl_polynomial_shift_1d | ( | cpl_polynomial * | p, | |
| double | u | |||
| ) |
Given p and u, modify the polynomial to p(x) := p(x+u).
| p | The 1D-polynomial to be modified in place | |
| u | The shift |
The transformation p(x) := (x+1)^n will generate the binomial coefficients, p_i = p_{n-i} = ( n ), i =0, 1, ..., n. ( i )
Transformation with u = -p_{n-1}/n/p_n will (in absence of rounding errors) yield a polynomial with p_{n-1} = 0 and roots that have the sum zero. No transformation can modify the leading coefficient.
Possible _cpl_error_code_ set in this function:
| cpl_error_code cpl_polynomial_solve_1d | ( | const cpl_polynomial * | p, | |
| double | x0, | |||
| double * | px, | |||
| int | mul | |||
| ) |
A real solution to p(x) = 0 using Newton-Raphsons method.
| p | The 1D-polynomial | |
| x0 | First guess of the solution | |
| px | The solution or undefined on error | |
| mul | The root multiplicity (or 1 if unknown) |
No solution is found and *px is undefined when the iterative process stops because: 1) It can not proceed because p`(x) = 0 (CPL_ERROR_DIVISION_BY_ZERO). 2) Only a finite number of iterations are allowed. (CPL_ERROR_CONTINUE). Both cases may be due to lack of a real solution or a bad first guess.
The accuracy and robustness deteriorates with increasing multiplicity of the solution. This is also the case with numerical multiplicity, i.e. when multiple solutions are located close together.
mul is assumed to be the multiplicity of the solution. Knowledge of the root multiplicity often improves the robustnes and accuracy. If there is no knowledge of the root multiplicity mul should be 1. Setting mul to a too high value should be avoided.
Reverse order of the coefficients: Given x such that p(x) = 0 (p having non-zero constant and leading coefficient) then q(1/x) = 0, where q is obtained by reversing the order of the coefficients of p.
Possible _cpl_error_code_ set in this function:
| cpl_error_code cpl_vector_fill_polynomial | ( | cpl_vector * | v, | |
| const cpl_polynomial * | p, | |||
| double | x0, | |||
| double | d | |||
| ) |
Evaluate a 1D-polynomial on equidistant points using Horners rule.
| v | Preallocated vector to contain the result | |
| p | The 1D-polynomial | |
| x0 | The first point of evaluation | |
| d | The increment between points of evaluation |
If d is zero it is preferable to simply use cpl_vector_fill(v, cpl_polynomial_eval_1d(p, x0, NULL)).
The call requires about 2nm FLOPs, where m+1 is the number of coefficients in p.
Possible _cpl_error_code_ set in this function:
1.5.1