Common Pipeline Library Reference Manual
6.1.1
|
Functions | |
cpl_error_code | cpl_polynomial_add (cpl_polynomial *self, const cpl_polynomial *first, const cpl_polynomial *second) |
Add two polynomials of the same dimension. | |
int | cpl_polynomial_compare (const cpl_polynomial *p1, const cpl_polynomial *p2, double tol) |
Compare the coefficients of two polynomials. | |
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, cpl_size 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 (1D) 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, cpl_size 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 cpl_size *mindeg, const cpl_size *maxdeg) |
Fit a polynomial to a set of samples in a least squares sense. | |
cpl_polynomial * | cpl_polynomial_fit_1d_create (const cpl_vector *x_pos, const cpl_vector *values, cpl_size 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, cpl_size 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 cpl_size *pows) |
Get a coefficient of the polynomial. | |
cpl_size | cpl_polynomial_get_degree (const cpl_polynomial *p) |
The degree of the polynomial. | |
cpl_size | cpl_polynomial_get_dimension (const cpl_polynomial *p) |
The dimension of the polynomial. | |
cpl_error_code | cpl_polynomial_multiply_scalar (cpl_polynomial *self, const cpl_polynomial *other, double factor) |
Multiply a polynomial with a scalar. | |
cpl_error_code | cpl_polynomial_set_coeff (cpl_polynomial *in, const cpl_size *pows, double c) |
Set a coefficient of the polynomial. | |
cpl_error_code | cpl_polynomial_shift_1d (cpl_polynomial *p, cpl_size 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, cpl_size mul) |
A real solution to p(x) = 0 using Newton-Raphsons method. | |
cpl_error_code | cpl_polynomial_subtract (cpl_polynomial *self, const cpl_polynomial *first, const cpl_polynomial *second) |
Subtract two polynomials of the same dimension. | |
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. |
This module provides functions to handle uni- and multivariate polynomials.
Univariate (1D) 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 *
and
* P2(x,y) = p0 + p1.x + p2.y + p3.x.y + p4.x^2 + p5.y^2 *
P1(x) may evaluate to more accurate results than P2(x,0), especially around the roots.
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.
cpl_error_code cpl_polynomial_add | ( | cpl_polynomial * | self, |
const cpl_polynomial * | first, | ||
const cpl_polynomial * | second | ||
) |
Add two polynomials of the same dimension.
self | The polynomial to hold the result |
first | The 1st polynomial to add |
second | The 2nd polynomial to add |
Possible CPL error code set in this function:
int cpl_polynomial_compare | ( | const cpl_polynomial * | p1, |
const cpl_polynomial * | p2, | ||
double | tol | ||
) |
Compare the coefficients of two polynomials.
p1 | the 1st polynomial |
p2 | the 2nd polynomial |
tol | The absolute (non-negative) tolerance |
The two polynomials are considered equal iff they have identical dimensions and the absolute difference between their coefficients does not exceed the 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 |
in and out must point to different polynomials.
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 |
The function deallocates the memory used by the polynomial p. If p is NULL
, nothing is done, and no error is set.
cpl_error_code cpl_polynomial_derivative | ( | cpl_polynomial * | self, |
cpl_size | 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 dimension of the polynomial is preserved, even if the operation may cause the polynomial to become independent of the dimension dim of the variable.
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 |
Each coefficient is preceded by its integer power(s) and written on a single line. If the polynomial has non-zero coefficients, only those are printed, otherwise the (zero-valued) constant term is printed.
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 |
Notice that 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:
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 |
The length of x must be the polynomial dimension.
A polynomial with no non-zero coefficients evaluates as 0.
If the classical evaluation method is used, the computational cost is:
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 dimension, n is the number of coefficients in p and d_i is the highest power used in dimension i, i = 1, 2, ..., dim.
If the Horner evaluation method is used the complexity has not been studied yet.
Possible _cpl_error_code_ set in this function:
double cpl_polynomial_eval_1d | ( | const cpl_polynomial * | self, |
double | x, | ||
double * | pd | ||
) |
Evaluate a univariate (1D) polynomial using Horners rule.
self | The 1D-polynomial |
x | The point of evaluation |
pd | Iff pd is non-NULL, the derivative evaluated at x |
A polynomial with no non-zero coefficents evaluates to 0 with a derivative that does likewise.
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) |
The call requires about 4n FLOPs where n is the number of coefficients in self, which is the same as that required for two separate polynomial evaluations. cpl_polynomial_eval_1d_diff() is however more accurate.
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, |
cpl_size | 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 dimension of the polynomial self must be one greater than that of the other polynomial. Given these two polynomials the dimension dim of self is collapsed by creating a new polynomial from self(x0, x1, ..., x{dim-1}, other(x0, x1, ..., x{dim-1}, x{dim+1}, x{dim+2}, ..., x{n-1}), x{dim+1}, x{dim+2}, ..., x{n-1}).
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 cpl_size * | mindeg, | ||
const cpl_size * | 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 |
Any pre-set non-zero coefficients in self are overwritten or reset by the fit.
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.
symsamp is ignored if mindeg is nonzero, otherwise 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.
For higher order fitting the fitting problem known as "Runge's phenomenon" is minimized using the socalled "Chebyshev nodes" as sampling points. For Chebyshev nodes symsamp can be set to CPL_TRUE.
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.
The fit is done in the following steps: 1) If mindeg is zero, the sampling positions are first transformed into Xhat_i = X_i - mean(X_i), i=1, .., dimension. 2) The Vandermonde matrix is formed from Xhat. 3) The normal equations of the Vandermonde matrix is solved. 4) If mindeg is zero, the resulting polynomial in Xhat is transformed back to X.
For a univariate (1D) fit 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 to fit, N = 1 + maxdeg - mindeg.
For a bivariate fit 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 to fit.
Examples of usage:
Possible _cpl_error_code_ set in this function:
cpl_polynomial* cpl_polynomial_fit_1d_create | ( | const cpl_vector * | x_pos, |
const cpl_vector * | values, | ||
cpl_size | 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 |
cpl_polynomial* cpl_polynomial_fit_2d_create | ( | cpl_bivector * | xy_pos, |
cpl_vector * | values, | ||
cpl_size | 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 |
double cpl_polynomial_get_coeff | ( | const cpl_polynomial * | in, |
const cpl_size * | pows | ||
) |
Get a coefficient of the polynomial.
in | the input polynomial |
pows | the powers of the different variables |
The array pows must have the size of the polynomial dimension and have non-negative elements.
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.
Example of usage:
Possible _cpl_error_code_ set in this function:
cpl_size cpl_polynomial_get_degree | ( | const cpl_polynomial * | p | ) |
The degree of the polynomial.
p | the polynomial |
The degree is the highest sum of exponents (with a non-zero coefficient).
If there are no non-zero coefficients the degree is zero.
Possible _cpl_error_code_ set in this function:
cpl_size cpl_polynomial_get_dimension | ( | const cpl_polynomial * | p | ) |
The dimension of the polynomial.
p | the polynomial |
Possible _cpl_error_code_ set in this function:
cpl_error_code cpl_polynomial_multiply_scalar | ( | cpl_polynomial * | self, |
const cpl_polynomial * | other, | ||
double | factor | ||
) |
Multiply a polynomial with a scalar.
self | The polynomial to hold the result |
other | The polynomial to scale, may equal self |
factor | The factor to multiply with |
Possible CPL error code set in this function:
cpl_error_code cpl_polynomial_set_coeff | ( | cpl_polynomial * | in, |
const cpl_size * | pows, | ||
double | c | ||
) |
Set a coefficient of the polynomial.
in | the input polynomial |
pows | the powers of the different variables |
c | the coefficient |
The array pows must have the size of the polynomial dimension and have non-negative elements.
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 poly4d to 12.3 would be performed by:
Setting the coefficient of x^3 in the 1-dimensional polynomial poly1d to 12.3 would be performed by:
For efficiency reasons the coefficients of a 1D-polynomial are best inserted with that of the highest power first.
Possible _cpl_error_code_ set in this function:
cpl_error_code cpl_polynomial_shift_1d | ( | cpl_polynomial * | p, |
cpl_size | 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 |
Possible _cpl_error_code_ set in this function:
cpl_error_code cpl_polynomial_solve_1d | ( | const cpl_polynomial * | p, |
double | x0, | ||
double * | px, | ||
cpl_size | 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, on error see below |
mul | The root multiplicity (or 1 if unknown) |
Even if a real solution exists, it may not be found if the first guess is too far from the solution. But a solution is guaranteed to be found if all roots of p are real. If the constant term is zero, the solution 0 will be returned regardless of the first guess.
No solution is found 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. In these two cases *px is set to the value where the error occurred. In case of other errors *px is unmodified.
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 robustness 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.
Possible _cpl_error_code_ set in this function:
cpl_error_code cpl_polynomial_subtract | ( | cpl_polynomial * | self, |
const cpl_polynomial * | first, | ||
const cpl_polynomial * | second | ||
) |
Subtract two polynomials of the same dimension.
self | The polynomial to hold the result |
first | The polynomial to subtract from |
second | The polynomial to subtract |
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 |
The evaluation points are x_i = x0 + i * d, i=0, 1, ..., n-1, where n is the length of the vector.
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 |
It is allowed to pass the same vector as both fitvals and as self, in which case fitvals is overwritten with the residuals.
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:
Possible _cpl_error_code_ set in this function: