Libpolycomp  1.0
A compression/decompression library that implements the polynomial compression and other simple compression schemes
Least-squares polynomial fits

Functions

pcomp_poly_fit_data_tpcomp_init_poly_fit (size_t num_of_samples, size_t num_of_coeffs)
 Allocate a new instance of the pcomp_poly_fit_data_t structure on the heap. More...
 
void pcomp_free_poly_fit (pcomp_poly_fit_data_t *poly_fit)
 Free an instance of the pcomp_poly_fit_data_t that has been allocated via a call to pcomp_init_poly_fit. More...
 
size_t pcomp_poly_fit_num_of_samples (const pcomp_poly_fit_data_t *poly_fit)
 Return the number of samples to be used in a polynomial fit. More...
 
size_t pcomp_poly_fit_num_of_coeffs (const pcomp_poly_fit_data_t *poly_fit)
 Return the number of coefficients of the least-squares fitting polynomial. More...
 
int pcomp_run_poly_fit (pcomp_poly_fit_data_t *poly_fit, double *coeffs, const double *points)
 Calculates a polynomial least-squares fit. More...
 

Detailed Description

This set of Libpolycomp routines allows to compute the least squares best fit between a set of floating-point numbers $x_i$ (with $i=1\ldots N$) and a polynomial, through the points $(i, x_i)$. It is used internally by the polynomial compression functions (Polynomial compression functions), but the API is exposed to the library user.

Function Documentation

void pcomp_free_poly_fit ( pcomp_poly_fit_data_t poly_fit)

Free an instance of the pcomp_poly_fit_data_t that has been allocated via a call to pcomp_init_poly_fit.

Parameters
[in]poly_fitPointer to the structure to be freed

Definition at line 210 of file poly.c.

pcomp_poly_fit_data_t* pcomp_init_poly_fit ( size_t  num_of_samples,
size_t  num_of_coeffs 
)

Allocate a new instance of the pcomp_poly_fit_data_t structure on the heap.

Parameters
[in]num_of_samplesNumber of floating-point numbers that must fit the polynomial
[in]num_of_coeffsNumber of coefficients of the least-squares fitting polynomial $p(x)$. This is equal to $\deg p(x) + 1$, where $\deg p(x)$ is the degree of the polynomial. Thus, for a parabolic polynomial of the form $p(x) = a x^2 + b x + c$, num_of_coeffs = 3.
Returns
A newly created instance of pcomp_poly_fit_data_t structure. This must be freed using pcomp_free_poly_fit, once it is no longer used.

Definition at line 173 of file poly.c.

size_t pcomp_poly_fit_num_of_coeffs ( const pcomp_poly_fit_data_t poly_fit)

Return the number of coefficients of the least-squares fitting polynomial.

Parameters
[in]poly_fitPointer to the structure detailing the fit
Returns
The number of coefficients for the fitting polynomial (one plus the polynomial degree)

Definition at line 253 of file poly.c.

size_t pcomp_poly_fit_num_of_samples ( const pcomp_poly_fit_data_t poly_fit)

Return the number of samples to be used in a polynomial fit.

Parameters
[in]poly_fitPointer to the structure detailing the fit
Returns
The number of samples that should be passed to a call to pcomp_run_poly_fit.

Definition at line 234 of file poly.c.

int pcomp_run_poly_fit ( pcomp_poly_fit_data_t poly_fit,
double *  coeffs,
const double *  points 
)

Calculates a polynomial least-squares fit.

Compute a least-squares fit between the numbers $x_i$ (with $i = 1 \ldots N$) and the polynomial $p(x)$ through the points $(i, x_i)_{i=1}^N$. The coefficients of $p(x)$ are saved in coeffs, from the least to the greatest degree.

Here is an example of the usage of this function:

double points[] = { 1.0, 3.0, 5.0 };
double coeffs[2];
const size_t num_of_points = sizeof(points) / sizeof(points[0]);
const size_t num_of_coeffs = sizeof(coeffs) / sizeof(coeffs[0]);
poly_fit = pcomp_init_poly_fit(num_of_points, num_of_coeffs);
pcomp_run_poly_fit(poly_fit, coeffs, points);
printf("The data are fitted by the polynomial y = %f + %f x\n",
coeffs[0], coeffs[1]);
Parameters
[in]poly_fitPointer to a pcomp_poly_fit_data_t structure, created using the pcomp_init_poly_fit function.
[out]coeffsPointer to an array where the coefficients of the polynomial will be stored on exit. The array must have room for a number of elements greater or equal than the value returned by pcomp_poly_fit_num_of_coeffs.
[in]pointsArray of numbers $x_i$ to use in the fit. The number of elements considered in the fit is equal to the return value of pcomp_poly_fit_num_of_samples.
Returns
PCOMP_STAT_SUCCESS if the fit was computed successfully, PCOMP_STAT_INVALID_FIT if the data are incorrect (e.g., there are fewer samples than unknowns).

Definition at line 301 of file poly.c.