Common Pipeline Library Reference Manual  5.3.1
Defines | Functions
Polynomials

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.
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_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.

Detailed Description

This module provides functions to handle uni- and multivariate polynomials.

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
 * 

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.


Define Documentation

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 */       \
                }

Compare the coefficients of two polynomials.

Parameters:
p1the 1st polynomial
p2the 2nd polynomial
tolThe absolute (non-negative) tolerance
Returns:
0 when equal, positive when different, negative on error.

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_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_ILLEGAL_INPUT if tol is negative

Function Documentation

cpl_error_code cpl_polynomial_copy ( cpl_polynomial *  out,
const cpl_polynomial *  in 
)

This function copies contents of a polynomial into another one.

Parameters:
outPre-allocated output cpl_polynomial
inInput cpl_polynomial
Returns:
CPL_ERROR_NONE or the relevant _cpl_error_code_

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:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_INCOMPATIBLE_INPUT if in and out point to the same polynomial
void cpl_polynomial_delete ( cpl_polynomial *  p)

Delete a cpl_polynomial.

Parameters:
pcpl_polynomial to delete
Returns:
void

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,
int  dim 
)

Compute a first order partial derivative.

Parameters:
selfThe polynomial to be modified in place
dimThe dimension to differentiate (zero for first dimension)
Returns:
CPL_ERROR_NONE or the relevant _cpl_error_code_

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_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_ILLEGAL_INPUT if dim is negative.
  • CPL_ERROR_ACCESS_OUT_OF_RANGE if dim exceeds the dimension of self.
cpl_error_code cpl_polynomial_dump ( const cpl_polynomial *  p,
FILE *  stream 
)

Dump a cpl_polynomial as ASCII to a stream.

Parameters:
pInput cpl_polynomial to dump
streamOutput stream, accepts stdout or stderr
Returns:
CPL_ERROR_NONE or the relevant _cpl_error_code_

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_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_FILE_IO if the write operation fails
cpl_polynomial* cpl_polynomial_duplicate ( const cpl_polynomial *  p)

This function duplicates an existing polynomial.

Parameters:
pThe input cpl_polynomial
Returns:
A newly allocated cpl_polynomial or NULL on error

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:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
double cpl_polynomial_eval ( const cpl_polynomial *  p,
const cpl_vector *  x 
)

Evaluate the polynomial at the given point.

Parameters:
pThe polynomial
xPoint of evaluation
Returns:
The computed value or undefined on error.

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:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_INCOMPATIBLE_INPUT if the length of x differs from the dimension of the polynomial
double cpl_polynomial_eval_1d ( const cpl_polynomial *  self,
double  x,
double *  pd 
)

Evaluate a univariate polynomial using Horners rule.

Parameters:
selfThe 1D-polynomial
xThe point of evaluation
pdIff pd is non-NULL, the derivative evaluated at x
Returns:
The result or undefined on error.

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:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_INVALID_TYPE if the polynomial is not univariate
double cpl_polynomial_eval_1d_diff ( const cpl_polynomial *  self,
double  a,
double  b,
double *  ppa 
)

Evaluate p(a) - p(b) using Horners rule.

Parameters:
selfThe 1D-polynomial
aThe evaluation point of the minuend
bThe evaluation point of the subtrahend
ppaIff ppa is not NULL, p(a)
Returns:
The difference or undefined on error

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_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_INVALID_TYPE if the polynomial has the wrong dimension
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.

Parameters:
selfThe multi-variate polynomial
dimThe dimension to collapse (zero for first dimension)
otherThe polynomial to replace dimension dim of self
Returns:
The collapsed polynomial or NULL on error

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_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_INVALID_TYPE if the polynomial is uni-variate.
  • CPL_ERROR_ILLEGAL_INPUT if dim is negative.
  • CPL_ERROR_ACCESS_OUT_OF_RANGE if dim exceeds the dimension of self.
  • CPL_ERROR_INCOMPATIBLE_INPUT if other has the wrong dimension.
  • CPL_ERROR_UNSUPPORTED_MODE if other is not of degree 0.
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.

Parameters:
selfPolynomial of dimension d to hold the coefficients
sampposMatrix of p sample positions, with d rows and p columns
sampsymNULL, or d booleans, true iff the sampling is symmetric
fitvalsVector of the p values to fit
fitsigmUncertainties of the sampled values, or NULL for all ones
dimdegTrue iff there is a fitting degree per dimension
mindegPointer to 1 or d minimum fitting degree(s), or NULL
maxdegPointer to 1 or d maximum fitting degree(s), at least mindeg
Returns:
CPL_ERROR_NONE on success, else the relevant _cpl_error_code_
Note:
Currently only uni- and bi-variate polynomials are supported, fitsigm must be NULL. For all but uni-variate polynomials mindeg must be zero.
See also:
cpl_vector_fill_polynomial_fit_residual()

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 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:

    cpl_polynomial  * fit1d     = cpl_polynomial_new(1);
    cpl_matrix      * samppos1d = my_sampling_points_1d();
    cpl_vector      * fitvals   = my_sampling_values();
    const cpl_boolean sampsym   = CPL_TRUE;
    const int         maxdeg1d  = 4; // Fit 5 coefficients
    cpl_error_code    error1d
        = cpl_polynomial_fit(fit1d, samppos1d, &sampsym, fitvals, NULL,
                             CPL_FALSE, NULL, &maxdeg1d);
    cpl_polynomial  * fit2d      = cpl_polynomial_new(2);
    cpl_matrix      * samppos2d  = my_sampling_points_2d();
    cpl_vector      * fitvals    = my_sampling_values();
    const int         maxdeg2d[] = {2, 1}; // Fit 6 coefficients
    cpl_error_code    error2d
        = cpl_polynomial_fit(fit2d, samppos2d, NULL, fitvals, NULL, CPL_FALSE,
                             NULL, maxdeg2d);

Possible _cpl_error_code_ set in this function:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_ILLEGAL_INPUT if a mindeg value is negative, or if a maxdeg value is less than the corresponding mindeg value.
  • CPL_ERROR_DATA_NOT_FOUND if the number of columns in samppos is less than the number of coefficients to be determined.
  • CPL_ERROR_INCOMPATIBLE_INPUT if samppos, fitvals or fitsigm have incompatible sizes, or if samppos, self or sampsym have incompatible sizes.
  • CPL_ERROR_SINGULAR_MATRIX if samppos contains too few distinct values
  • CPL_ERROR_DIVISION_BY_ZERO if an element in fitsigm is zero
  • CPL_ERROR_UNSUPPORTED_MODE if the polynomial dimension exceeds two, or if there is a non-zero value in the mindeg array.
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.

Parameters:
x_posVector of positions of the signal to fit.
valuesVector of values of the signal to fit.
degreeNon-negative polynomial degree.
mseIff mse is not null, the mean squared error on success
Returns:
The fitted polynomial or NULL on error
See also:
cpl_polynomial_fit()
Deprecated:
Replace this call with cpl_polynomial_fit() and optionally cpl_vector_fill_polynomial_fit_residual().
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.

Parameters:
xy_posBivector positions of the surface to fit.
valuesVector of values of the surface to fit.
degreeNon-negative polynomial degree.
mseIff mse is not null, the mean squared error on success
Returns:
The fitted polynomial or NULL on error
See also:
cpl_polynomial_fit()
Deprecated:
Replace this call with cpl_polynomial_fit() and optionally cpl_vector_fill_polynomial_fit_residual().
double cpl_polynomial_get_coeff ( const cpl_polynomial *  in,
const int *  pows 
)

Get a coefficient of the polynomial.

Parameters:
inthe input polynomial
powsthe powers of the different variables
Returns:
The coefficient or undefined on error

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.

Possible _cpl_error_code_ set in this function:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_ILLEGAL_INPUT if pows contains negative values
int cpl_polynomial_get_degree ( const cpl_polynomial *  p)

The degree of the polynomial.

Parameters:
pthe polynomial
Returns:
The degree or negative on error

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_ERROR_NULL_INPUT if an input pointer is NULL
int cpl_polynomial_get_dimension ( const cpl_polynomial *  p)

The dimension of the polynomial.

Parameters:
pthe polynomial
Returns:
The dimension or negative on error

Possible _cpl_error_code_ set in this function:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
cpl_error_code cpl_polynomial_set_coeff ( cpl_polynomial *  in,
const int *  pows,
double  c 
)

Set a coefficient of the polynomial.

Parameters:
inthe input polynomial
powsthe powers of the different variables
cthe coefficient
Returns:
CPL_ERROR_NONE or the relevant _cpl_error_code_

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 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_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_ILLEGAL_INPUT if pows contains negative values
cpl_error_code cpl_polynomial_shift_1d ( cpl_polynomial *  p,
int  i,
double  u 
)

Modify p, p(x0, x1, ..., xi, ...) := (x0, x1, ..., xi+u, ...)

Parameters:
pThe polynomial to be modified in place
iThe dimension to shift (0 for first)
uThe shift
Returns:
CPL_ERROR_NONE or the relevant _cpl_error_code_
Note:
Currently, only dimensions 1 and 2 are supported

Possible _cpl_error_code_ set in this function:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_ILLEGAL_INPUT if i is negative
  • CPL_ERROR_ACCESS_OUT_OF_RANGE if i exceeds the dimension of p
  • CPL_ERROR_UNSUPPORTED_MODE if the polynomial has a dimension larger than 2
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.

Parameters:
pThe 1D-polynomial
x0First guess of the solution
pxThe solution, on error see below
mulThe root multiplicity (or 1 if unknown)
Returns:
CPL_ERROR_NONE or the relevant _cpl_error_code_

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_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_INVALID_TYPE if the polynomial has the wrong dimension
  • CPL_ERROR_ILLEGAL_INPUT if the multiplicity is non-positive
  • CPL_ERROR_DIVISION_BY_ZERO if a division by zero occurs
  • CPL_ERROR_CONTINUE if the algorithm does not converge
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.

Parameters:
vPreallocated vector to contain the result
pThe 1D-polynomial
x0The first point of evaluation
dThe increment between points of evaluation
Returns:
CPL_ERROR_NONE or the relevant _cpl_error_code_
See also:
cpl_vector_fill

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_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_INVALID_TYPE if the polynomial has the wrong dimension
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.

Parameters:
selfVector to hold the fitting residuals, fitvals may be used
fitvalsVector of the p fitted values
fitsigmUncertainties of the sampled values, or NULL for all ones
fitThe fitted polynomial
sampposMatrix of p sample positions, with d rows and p columns
rechisqIf non-NULL, the reduced chi square of the fit
Returns:
CPL_ERROR_NONE on success, else the relevant _cpl_error_code_
Note:
If necessary, self is resized to the length of fitvals.
See also:
cpl_polynomial_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:

    const double mse = cpl_vector_product(fitresidual, fitresidual)
                     / cpl_vector_get_size(fitresidual);

Possible _cpl_error_code_ set in this function:

  • CPL_ERROR_NULL_INPUT if an input pointer is NULL
  • CPL_ERROR_INCOMPATIBLE_INPUT if self, samppos, fitvals, fitsigm or fit have incompatible sizes
  • CPL_ERROR_DIVISION_BY_ZERO if an element in fitsigm is zero
  • CPL_ERROR_DATA_NOT_FOUND if the number of columns in samppos is less than the number of coefficients in the fitted polynomial.