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 *self, double x, double *pd) |
Evaluate a univariate polynomial using Horners rule. | |
double | cpl_polynomial_eval_1d_diff (const cpl_polynomial *self, double a, double b, double *ppa) |
Evaluate p(a) - p(b) 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_error_code | cpl_polynomial_fit (cpl_polynomial *self, const cpl_matrix *samppos, const cpl_boolean *sampsym, const cpl_vector *fitvals, const cpl_vector *fitsigm, cpl_boolean dimdeg, const int *mindeg, const int *maxdeg) |
Fit a polynomial to a set of samples 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, int i, double u) |
Modify p, p(x0, x1, ..., xi, ...) := (x0, x1, ..., xi+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. | |
cpl_error_code | cpl_vector_fill_polynomial_fit_residual (cpl_vector *self, const cpl_vector *fitvals, const cpl_vector *fitsigm, const cpl_polynomial *fit, const cpl_matrix *samppos, double *rechisq) |
Compute the residual of a polynomial fit. |
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 |
NULL
, nothing is done, and no error is set.
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 * | self, | |
double | x, | |||
double * | pd | |||
) |
Evaluate a univariate polynomial using Horners rule.
self | The 1D-polynomial | |
x | The point of evaluation | |
pd | Iff pd is non-NULL, the derivative 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.
If the derivative is requested it is computed using a nested Horner rule. This requires 4n FLOPs in total.
Possible _cpl_error_code_ set in this function:
double cpl_polynomial_eval_1d_diff | ( | const cpl_polynomial * | self, | |
double | a, | |||
double | b, | |||
double * | ppa | |||
) |
Evaluate p(a) - p(b) using Horners rule.
self | The 1D-polynomial | |
a | The evaluation point of the minuend | |
b | The evaluation point of the subtrahend | |
ppa | Iff ppa is not NULL, p(a) |
ppa may be NULL. If it is not, *ppa is set to self(a), which is calculated at no extra cost.
The underlying algorithm is the same as that used in cpl_polynomial_eval_1d() when the derivative is also requested.
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_error_code cpl_polynomial_fit | ( | cpl_polynomial * | self, | |
const cpl_matrix * | samppos, | |||
const cpl_boolean * | sampsym, | |||
const cpl_vector * | fitvals, | |||
const cpl_vector * | fitsigm, | |||
cpl_boolean | dimdeg, | |||
const int * | mindeg, | |||
const int * | maxdeg | |||
) |
Fit a polynomial to a set of samples in a least squares sense.
self | Polynomial of dimension d to hold the coefficients | |
samppos | Matrix of p sample positions, with d rows and p columns | |
sampsym | NULL, or d booleans, true iff the sampling is symmetric | |
fitvals | Vector of the p values to fit | |
fitsigm | Uncertainties of the sampled values, or NULL for all ones | |
dimdeg | True iff there is a fitting degree per dimension | |
mindeg | Pointer to 1 or d minimum fitting degree(s), or NULL | |
maxdeg | Pointer to 1 or d maximum fitting degree(s), at least mindeg |
For 1D-polynomials N = 1 + maxdeg - mindeg coefficients are fitted.
For multi-variate polynomials the fit depends on dimdeg:
If dimdeg is false, an n-degree coefficient is fitted iff mindeg <= n <= maxdeg. For a 2D-polynomial this means that N * (N + 1) / 2 coefficients are fitted.
If dimdeg is true, nci = 1 + maxdeg[i] + mindeg[i] coefficients are fitted for dimension i, i.e. for a 2D-polynomial N = nc1 * nc2 coefficients are fitted.
The number of distinct samples should exceed the number of coefficients to fit. The number of distinct samples may also equal the number of coefficients to fit, but in this case the fit has another meaning (any non-zero residual is due to rounding errors, not a fitting error). It is an error to try to fit more coefficients than there are distinct samples.
If the relative uncertainties of the sampled values are known, they may be passed via fitsigm. NULL means that all uncertainties equals one.
The caller may use sampsym to indicate an a priori knowledge that the sampling positions are symmetric. NULL indicates no knowledge of such symmetry. sampsym[i] may be set to true iff the sampling is symmetric around u_i, where u_i is the mean of the sampling positions in dimension i.
In 1D this implies that the sampling points as pairs average u_0 (with an odd number of samples one sample must equal u_0). E.g. both x = (1, 2, 4, 6, 7) and x = (1, 6, 4, 2, 7) have sampling symmetry, while x = (1, 2, 4, 6) does not.
In 2D this implies that the sampling points are symmetric in the 2D-plane. For the first dimension sampling symmetry means that the sampling is line- symmetric around y = u_1, while for the second dimension, sampling symmetry implies line-symmetry around x = u_2. Point symmetry around (x,y) = (u_1, u_2) means that both sampsym[0] and sampsym[1] may be set to true.
Knowledge of symmetric sampling allows the fit to be both faster and eliminates certain round-off errors.
Warning: An increase in the polynomial degree will normally reduce the fitting 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 fitting error to _increase_. In some cases this happens with an increase of the polynomial degree from 8 to 9.
Examples of usage:
cpl_polynomial * fit1d = cpl_polynomial_new(1); cpl_matrix * samppos = my_sampling_points_1d(); cpl_vector * fitvals = my_sampling_values(); const cpl_boolean sampsym = CPL_TRUE; const int mindeg1d = 1; const int maxdeg1d = 4; cpl_error_code error1d = cpl_polynomial_fit(fit1d, samppos1d, &sampsym, fitvals, NULL, CPL_FALSE, &mindeg1d, &maxdeg1d);
cpl_polynomial * fit2d = cpl_polynomial_new(2); cpl_matrix * samppos2d = my_sampling_points_2d(); cpl_vector * fitvals = my_sampling_values(); const int mindeg2d[] = {1, 1}; const int maxdeg2d[] = {4, 4}; cpl_error_code error2d = cpl_polynomial_fit(fit2d, samppos2d, NULL, fitvals, NULL, CPL_FALSE, mindeg2d, maxdeg2d);
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, | |
int | i, | |||
double | u | |||
) |
Modify p, p(x0, x1, ..., xi, ...) := (x0, x1, ..., xi+u, ...).
p | The polynomial to be modified in place | |
i | The dimension to shift (0 for first) | |
u | The shift |
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:
cpl_error_code cpl_vector_fill_polynomial_fit_residual | ( | cpl_vector * | self, | |
const cpl_vector * | fitvals, | |||
const cpl_vector * | fitsigm, | |||
const cpl_polynomial * | fit, | |||
const cpl_matrix * | samppos, | |||
double * | rechisq | |||
) |
Compute the residual of a polynomial fit.
self | Vector to hold the fitting residuals, fitvals may be used | |
fitvals | Vector of the p fitted values | |
fitsigm | Uncertainties of the sampled values, or NULL for all ones | |
fit | The fitted polynomial | |
samppos | Matrix of p sample positions, with d rows and p columns | |
rechisq | If non-NULL, the reduced chi square of the fit |
If the relative uncertainties of the sampled values are known, they may be passed via fitsigm. NULL means that all uncertainties equals one.
If rechisq is non-NULL, the reduced chi square of the fit is computed as well.
The mean square error, which was computed directly by the former CPL functions cpl_polynomial_fit_1d_create() and cpl_polynomial_fit_2d_create() can be computed from the fitting residual like this:
const double mse = cpl_vector_product(fitresidual, fitresidual) / cpl_vector_get_size(fitresidual);
Possible _cpl_error_code_ set in this function: