|
TPCCLIB
|
Header file for libtpclinopt. More...
#include "tpcclibConfig.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include "tpcextensions.h"Go to the source code of this file.
Data Structures | |
| struct | NNLSQDATA |
Functions | |
| int | fitLine (double *x, double *y, const int n, double *m, double *c) |
| int | fitLinePearson (double *x, double *y, const int n, double *m, double *msd, double *c, double *csd, double *d, double *dsd, double *r, double *ysd) |
| int | highestSlope (double *x, double *y, const int n, const int slope_n, double x_start, double *m, double *yi, double *xi, double *xh) |
| int | rootsCubic (const double a, const double b, const double c, double *x1, double *x2, double *x3) |
| int | rootsQuadratic (const double a, const double b, const double c, double *x1, double *x2) |
| int | qrLSQ (double **mat, double *rhs, double *sol, const unsigned int rows, const unsigned int cols, double *r2) |
| QR least-squares solving routine. | |
| int | qr (double **A, int m, int n, double *B, double *X, double *rnorm, double *tau, double *res, double **wws, double *ws) |
| int | qr_decomp (double **a, int M, int N, double *tau, double **cchain, double *chain) |
| int | qr_solve (double **QR, int M, int N, double *tau, double *b, double *x, double *residual, double *resNorm, double **cchain, double *chain) |
| int | qrWeight (int N, int M, double **A, double *b, double *weight, double *ws) |
| int | qrWeightRm (int N, int M, double **A, double *b, double *weight, double *ws) |
| int | qrSimpleLSQ (double **A, double *B, int M, int N, double *X, double *r2) |
| int | qrLH (const unsigned int m, const unsigned int n, double *a, double *b, double *x, double *r2) |
| Solve over-determined least-squares problem A x ~ b using successive Householder rotations. | |
| double | householder_transform (double *vector, int size) |
| int | householder_hm (double tau, double *vector, double **matrix, int rowNr, int columnNr) |
| int | householder_hv (double tau, int size, double *v, double *w) |
| double | householder_norm (double *v, int size) |
| int | nnls (double **a, int m, int n, double *b, double *x, double *rnorm, double *w, double *zz, int *index) |
| int | nnlsWght (int N, int M, double **A, double *b, double *weight) |
| int | nnlsWghtSquared (int N, int M, double **A, double *b, double *sweight) |
| int | nnlsq (NNLSQDATA *d, int verbose) |
| int | nnlsqWght (NNLSQDATA *d, double *weight) |
| int | nnlsqWghtSquared (NNLSQDATA *d, double *sweight) |
| void | nnlsqDataInit (NNLSQDATA *d) |
| void | nnlsqDataFree (NNLSQDATA *d) |
| int | nnlsqDataAllocate (NNLSQDATA *d, const int n, const int m) |
| int | bvls (int key, const int m, const int n, double *a, double *b, double *bl, double *bu, double *x, double *w, double *act, double *zz, int *istate, int *iter, int verbose) |
| Bounded-value least-squares method to solve the linear problem A x ~ b , subject to limit1 <= x <= limit2. | |
| int | llsqWght (int N, int M, double **A, double *a, double *b, double *weight) |
| int | llsqWghtSquared (int N, int M, double **A, double *a, double *b, double *weight) |
Header file for libtpclinopt.
Header file for template library libtpclinopt.
Definition in file tpclinopt.h.
|
extern |
Bounded-value least-squares method to solve the linear problem A x ~ b , subject to limit1 <= x <= limit2.
This routine is based on the text and Fortran code in C.L. Lawson and R.J. Hanson, Solving Least Squares Problems, Prentice-Hall, Englewood Cliffs, New Jersey, 1974, and Fortran codes by R.L. Parker and P.B. Stark, and by J. Burkardt.
| key | Enter 0 to solve the problem from scratch, or <>0 to initialize the routine using caller's guess about which parameters are active within their bounds, which are at their lower bounds, and which at their upper bounds, as supplied in the array istate ('warm start'). When key <> 0, the routine initially sets the active components to the averages of their upper and lower bounds. |
| m | Number of samples in matrix A and the length of vector b. |
| n | Number of parameters in matrix A and the length of vector x. The n must not be > m unless at least n-m variables are non-active (set to their bounds). |
| a | Pointer to matrix A; matrix must be given as an n*m array, containing n consecutive m-length vectors. Contents of A are modified in this routine. |
| b | Pointer to vector b of length m. Contents of b are modified in this routine. |
| bl | Array BL[0..n-1] of lower bounds for parameters. |
| bu | Array BU[0..n-1] of upper bounds for parameters. |
| x | Pointer to the result vector x of length n. |
| w | Pointer to an array of length n, which will be used as working memory, and the minimum 2-norm || a.x-b ||, (R^2), will be written in w[0]. |
| act | Pointer to an array of length m*(n+2), or m*(m+2) if m<n, which will be used as working memory. |
| zz | Pointer to an array of length m, to be used as working memory. |
| istate | Pointer to an integer array of length n+1. If parameter key <>0, then the last position istate[n] must contain the total number of components at their bounds (nbound, the bound variables'). The absolute values of the first nbound entries of istate[] are the indices of these bound' components of x[]. The sign of istate[0.. nbound-1] entries indicates whether x[|istate[i]|] is at its upper (positive) or lower (negative) bound. Entries istate[nbound..n-1] contain the indices of the active components. |
| iter | Number of performed iterations is returned in this variable. Maximum nr of iterations is set with this variable, or set it to 0 to use the default maximum, 3*n. |
| verbose | Verbose level; if zero, then nothing is printed to stderr or stdout. |
Definition at line 32 of file bvls.c.
|
extern |
Calculate regression line slope (m) and y axis intercept (c).
| x | Pointer to an array of x axis values. NaN's and infinite values are ignored. |
| y | Pointer to an array of y axis values. NaN's and infinite values are ignored. |
| n | The number of samples (length of x[] and y[]). |
| m | Pointer where calculated slope is written; enter NULL if not needed. |
| c | Pointer where calculated y axis intercept is written; enter NULL if not needed. |
Definition at line 23 of file regression.c.
Referenced by highestSlope().
|
extern |
Calculate regression line slope (m), y axis intercept (c), their SDs, and Pearson's correlation coefficient (r).
| x | Pointer to an array of x axis values. NaN's and infinite values are ignored. |
| y | Pointer to an array of y axis values. NaN's and infinite values are ignored. |
| n | The number of samples (length of x[] and y[]). |
| m | Pointer where calculated slope is written; enter NULL if not needed. |
| msd | Pointer where SD of slope is written; enter NULL if not needed. |
| c | Pointer where calculated y axis intercept is written; enter NULL if not needed. |
| csd | Pointer where SD of y axis intercept is written; enter NULL if not needed. |
| d | Pointer where calculated x axis intercept is written; enter NULL if not needed. |
| dsd | Pointer where SD of x axis intercept is written; enter NULL if not needed. |
| r | Pointer where Pearson's correlation coefficient is written; enter NULL if not needed. |
| ysd | Pointer where residual variance of y values is written; enter NULL if not needed. |
Definition at line 72 of file regression.c.
|
extern |
Find the regression line with the highest slope for x,y data.
| x | Pointer to an array of x axis values. NaN's and infinite values are ignored. Data must be sorted by increasing x, and overlapping x values may cause problem. |
| y | Pointer to an array of y axis values. NaN's and infinite values are ignored. Data is not modified. |
| n | The number of samples (length of x[] and y[]). |
| slope_n | The number of samples used to fit the line; must be at least 2. |
| x_start | Estimation start x value, samples with smaller x are ignored; can usually be set to zero. |
| m | Pointer where calculated max slope is written; NULL if not needed. |
| yi | Pointer where calculated y axis intercept is written; NULL if not needed. |
| xi | Pointer where calculated x axis intercept is written; NULL if not needed. This could be used as an estimate of radioactivity appearance time in TAC data, but you must then check that the max slope m is positive. |
| xh | Pointer where the place (x) of the highest slope is written; NULL if not needed. |
Definition at line 179 of file regression.c.
|
extern |
Applies a householder transformation defined by vector "vector" and scalar tau to the left-hand side of the matrix. (I - tau vector vector^T)*matrix The result of the transform is stored in matrix.
| tau | Coefficient defining householder transform. |
| vector | Vector defining householder transform (of size rowNr). |
| matrix | the matrix that is to be transformed. |
| rowNr | Nr of rows in matrix. |
| columnNr | Nr of columns in matrix. |
Definition at line 87 of file hholder.c.
Referenced by qr_decomp().
|
extern |
Applies a householder transformation defined by vector v and coefficient tau to vector w w = (I - tau v v^T) w.
| tau | Coefficient defining householder transform. |
| size | Size of vectors v and w. |
| v | Vector v. |
| w | Vector w. |
Definition at line 126 of file hholder.c.
|
extern |
|
extern |
This function prepares a Householder transformation P = I - tau h h^T which can be used to zero all the elements of the input vector except the first one that will get value beta. On output the elements 1 - size-1 of the vector h are stored in locations vector[1] - vector[size-1] of the input vector and value of beta is stored in location vector[0].
| v | The N-vector to be transformed. |
| N | size of the vector. |
Definition at line 33 of file hholder.c.
Referenced by qr_decomp().
|
extern |
Algorithm for weighting the problem that is given to a LLSQ algorithm.
Square roots of weights are used because in LLSQ algorithms the difference w*A-w*b is squared.
| N | Matrix A dimension N (nr of parameters). |
| M | Matrix A dimension M (nr of samples). |
| A | Pointer to matrix A[N][M]; enter NULL to use following matrix format instead. |
| a | Pointer to matrix A[N*M]; enter NULL to use previous matrix format instead. |
| b | Vector B of length M. |
| weight | Weights for each sample (array of length M). |
Definition at line 425 of file bvls.c.
|
extern |
Algorithm for weighting the problem that is given to a LLSQ algorithm.
Square roots of weights are used because in LLSQ algorithms the difference w*A-w*b is squared.
Here user must give squared weights; this makes calculation faster, when this function needs to be called many times.
| N | Matrix A dimension N (nr of parameters). |
| M | Matrix A dimension M (nr of samples). |
| A | Pointer to matrix A[N][M]; enter NULL to use following matrix format instead. |
| a | Pointer to matrix A[N*M]; enter NULL to use previous matrix format instead. |
| b | Vector B of length M. |
| sweight | Squared weights for each sample (array of length M). |
Definition at line 480 of file bvls.c.
|
extern |
Algorithm NNLS (Non-negative least-squares).
Given an m by n matrix A, and an m-vector B, computes an n-vector X, that solves the least squares problem A * X = B , subject to X>=0
Instead of pointers for working space, NULL can be given to let this function to allocate and free the required memory.
| a | On entry, a[ 0... N ][ 0 ... M ] contains the M by N matrix A. On exit, a[][] contains the product matrix Q*A, where Q is an m by n orthogonal matrix generated implicitly by this function. |
| m | Matrix dimension m. |
| n | Matrix dimension n. |
| b | On entry, b[] must contain the m-vector B. On exit, b[] contains Q*B. |
| x | On exit, x[] will contain the solution vector. |
| rnorm | On exit, rnorm contains the squared Euclidean norm of the residual vector, R^2 (Sum-of-Squares). If NULL is given, no rnorm is calculated. |
| wp | An n-array of working space, wp[]. On exit, wp[] will contain the dual solution vector. wp[i]=0.0 for all i in set p and wp[i]<=0.0 for all i in set z. Can be NULL, which causes this algorithm to allocate memory for it. |
| zzp | An m-array of working space, zz[]. Can be NULL, which causes this algorithm to allocate memory for it. |
| indexp | An n-array of working space, index[]. Can be NULL, which causes this algorithm to allocate memory for it. |
Definition at line 43 of file nnls.c.
Referenced by spectralDExp(), spectralDMSurge(), and tacDelayCMFit().
|
extern |
Algorithm NNLS (Non-negative least-squares).
The same algorithm as nnls(), but this one gets its data in a struct, and max iteration number and dependence factor (previously fixed to 0.01) can be set as function parameters in the struct.
Given an m by n matrix A, and an m-vector B, computes an n-vector X, that solves the least squares problem A * X = B , subject to X>=0
Instead of pointers for working space, NULL can be given to let this function to allocate and free the required memory.
| d | Pointer to structure containing the data and place for the results. |
| verbose | Verbose level; if zero, then nothing is printed to stderr or stdout. |
Definition at line 55 of file nnlsq.c.
|
extern |
Allocate memory for NNLSQDATA (and set data pointers inside the structure). Any previous contents are deleted. Contents are set to NaN or zero.
| d | Pointer to initiated structure; any old contents are deleted. |
| n | Number of parameters (columns of matrix A). |
| m | Number of samples (rows of matrix A). |
Definition at line 411 of file nnlsq.c.
|
extern |
Free memory allocated for NNLSQDATA data. All contents are destroyed.
| d | Pointer to structure. |
Definition at line 378 of file nnlsq.c.
Referenced by nnlsqDataAllocate().
|
extern |
Initiate the NNLSQDATA structure before any use.
| d | Pointer to structure to initiate before use. |
Definition at line 357 of file nnlsq.c.
Referenced by nnlsqDataFree().
|
extern |
Algorithm for weighting the problem that is given to NNLS-algorithm.
Square roots of weights are used because in NNLS the difference w*A-w*b is squared.
| d | Pointer to filled data structure. |
| weight | Weights for each sample (array of length M). |
Definition at line 470 of file nnlsq.c.
|
extern |
Algorithm for weighting the problem that is given to NNLS-algorithm.
Square roots of weights are used because in NNLS the difference w*A-w*b is squared. Here user must give squared weights; this makes calculation faster, when this function needs to be called many times.
| d | Pointer to filled data structure. |
| sweight | Squared weights for each sample (array of length d->m). |
|
extern |
Algorithm for weighting the problem that is given to nnls-algorithm.
Square roots of weights are used because in nnls the difference w*A-w*b is squared.
| N | NNLS dimension N (nr of parameters). |
| M | NNLS dimension M (nr of samples). |
| A | NNLS matrix A. |
| b | NNLS vector B. |
| weight | Weights for each sample (array of length M). |
Definition at line 259 of file nnls.c.
Referenced by spectralDExp(), and spectralDMSurge().
|
extern |
Algorithm for weighting the problem that is given to nnls-algorithm.
Square roots of weights are used because in nnls the difference w*A-w*b is squared. Here user must give squared weights; this makes calculation faster, when this function needs to be called many times.
| N | NNLS dimension N (nr of parameters). |
| M | NNLS dimension M (nr of samples). |
| A | NNLS matrix A. |
| b | NNLS vector B. |
| sweight | Squared weights for each sample (array of length M). |
Definition at line 308 of file nnls.c.
|
extern |
Algorithm QR.
Solves a matrix form least square problem min||A x - b|| => A x =~ b (A is m*n matrix, m>=n) using the QR decomposition for overdetermined systems. Based on GNU Scientific Library, edited by Kaisa Liukko.
Instead of pointers for working space, NULL can be given to let this function to allocate and free the required memory.
| A | On entry, a[m][n] contains the m by n matrix A. On exit, a[][] contains the QR factorization. |
| m | Dimensions of matrix A are a[m][n]. |
| n | Dimensions of matrix A are a[m][n]. |
| B | B[] is an m-size vector containing the right-hand side vector b. |
| X | On exit, x[] will contain the solution vector x (size of n). |
| rnorm | On exit, rnorm (pointer to double) contains the squared Euclidean norm of the residual vector (R^2); enter NULL if not needed. |
| tau | On exit, tau[] will contain the householder coefficients (size of n); enter NULL, if not needed. |
| res | An m-size array of working space, res[]. On output contains residual b - Ax. Enter NULL to let qr() to handle it. |
| wws | m*n array of working space. Enter NULL to let qr() to handle it. |
| ws | 2m-array of working space. Enter NULL to let qr() to handle it. |
Definition at line 411 of file qrlsq.c.
|
extern |
Factorise a general M x N matrix A into A = Q R , where Q is orthogonal (M x M) and R is upper triangular (M x N).
Q is stored as a packed set of Householder vectors in the strict lower triangular part of the input matrix A and a set of coefficients in vector tau.
R is stored in the diagonal and upper triangle of the input matrix.
The full matrix for Q can be obtained as the product Q = Q_1 Q_2 .. Q_k and it's transform as the product Q^T = Q_k .. Q_2 Q_1 , where k = min(M,N) and Q_i = (I - tau_i * h_i * h_i^T) and where h_i is a Householder vector h_i = [1, A(i+1,i), A(i+2,i), ... , A(M,i)]. This storage scheme is the same as in LAPACK.
NOTICE! The calling program must take care that pointer tau is of size N or M, whichever is smaller.
| a | contains coefficient matrix A (m*n) as input and factorisation QR as output. |
| M | nr of rows in matrix A. |
| N | nr of columns in matrix A. |
| tau | Vector for householder coefficients, of length N or M, whichever is smaller. |
| cchain | m*n array of working space. |
| chain | m size array of working space. |
Definition at line 493 of file qrlsq.c.
Referenced by qr().
|
extern |
Find the least squares solution to the overdetermined system
A x = b
for m >= n using the QR factorisation A = Q R. qr_decomp() must be used prior to this function in order to form the QR factorisation of A. Solution is formed in the following order: QR x = b => R x = Q^T b => x = R^-1 (Q^T b)
NOTICE! The calling program must take care that pointers b, x and residual are of the right size.
| QR | m*n matrix containing householder vectors of A. |
| M | nr of rows in matrix A. |
| N | nr of columns in matrix A. |
| tau | vector containing householder coefficients tau; length is M or N, whichever is smaller. |
| b | Contains m-size vector b of A x = b. |
| x | solution vector x of length n. |
| residual | residual vector of length m. |
| resNorm | norm^2 of the residual vector; enter NULL if not needed. |
| cchain | m*n array of the working space. |
| chain | 2m length array of the working space. |
Definition at line 563 of file qrlsq.c.
Referenced by qr().
|
extern |
Solve over-determined least-squares problem A x ~ b using successive Householder rotations.
This routine is based on the text and Fortran code in C.L. Lawson and R.J. Hanson, Solving Least Squares Problems, Prentice-Hall, Englewood Cliffs, New Jersey, 1974, and Fortran code by R.L. Parker and P.B. Stark.
| m | Number of samples in matrix A and the length of vector b. |
| n | Number of parameters in matrix A and the length of vector x. The n must be smaller or equal to m. |
| a | Pointer to matrix A; matrix must be given as an n*m array, containing n consecutive m-length vectors. Contents of A are modified in this routine. |
| b | Pointer to vector b of length n. Contents of b are modified in this routine. |
| x | Pointer to the result vector x of length n. |
| r2 | Pointer to a double value, in where the sum of squared residuals is written. |
Definition at line 946 of file qrlsq.c.
Referenced by bvls().
|
extern |
QR least-squares solving routine.
Solves a matrix form least square problem min||A x - b|| => A x =~ b (A is m*n matrix, m>=n) using the QR decomposition for overdetermined systems
| mat | Pointer to the row-major matrix (2D array), A[rows][cols]; not modified. |
| rhs | Vector b[rows]; modified to contain the computed (fitted) b[], that is, matrix-vector product A*x. |
| sol | Solution vector x[cols]; solution x corresponds to the solution of both the modified and original systems A and B. |
| rows | Number of rows (samples) in matrix A and length of vector B. |
| cols | Number of cols (parameters) in matrix A and length of vector X. |
| r2 | Pointer to value where R^2, the difference between original and computed right hand side (b); enter NULL if not needed. |
Definition at line 72 of file qrlsq.c.
|
extern |
Simplistic QR least-squares solving routine.
Reference: Máté A. Introduction to Numerical Analysis with C programs. Chapter 38. Overdetermined systems of linear equations. Brooklyn College of the City University of New York, 2014.
| A | Matrix A[M][N]. |
| B | Vector B[M]. |
| M | nr of rows in matrix A, must be >=N. |
| N | nr of columns in matrix A, must be <=M. |
| X | Vector X of length N for the results. |
| r2 | Pointer to value where R^2, the difference between original and computed right hand side (b); enter NULL if not needed. |
Definition at line 854 of file qrlsq.c.
|
extern |
Algorithm for weighting the problem that is given to QR algorithm. Square roots of weights are used because in QR the difference w*A-w*b is squared.
| N | Dimensions of matrix A are a[m][n]. |
| M | Dimensions of matrix A are a[m][n]; size of vector B is m. |
| A | Matrix a[m][n] for QR, contents will be weighted here. |
| b | B[] is an m-size vector for QR, contents will be weighted here. |
| weight | Pointer to array of size m, which contains sample weights, used here to weight matrix A and vector b. |
| ws | m-sized vector for working space; enter NULL to allocate locally. |
Definition at line 745 of file qrlsq.c.
|
extern |
Remove the weights from data provided to QR LSQ algorithms.
| N | Dimensions of matrix A are a[m][n]. |
| M | Dimensions of matrix A are a[m][n]; size of vector B is m. |
| A | Matrix a[m][n] for QR, weights will be removed here; enter NULL if not needed. |
| b | B[] is an m-size vector for QR, weights will be removed here; enter NULL if not needed here. |
| weight | Pointer to array of size m, which contains sample weights, used here to weight matrix A and vector b. |
| ws | m-sized vector for working space; enter NULL to allocate locally |
Definition at line 796 of file qrlsq.c.
|
extern |
Find the real roots of cubic equation x^3 + a x^2 + b x + c = 0.
The number of real roots can be 1 or 3, but coincident roots are possible. Root values, excluding duplicates and triplicates, will be placed in pointers x1, x2, and x3 in ascending order, or set to NaN.
Based on GNU Scientific Library function gsl_poly_solve_cubic.
| a | Input: Parameter a of the cubic equation. |
| b | Input: Parameter b of the cubic equation. |
| c | Input: Parameter c of the cubic equation. |
| x1 | Output: Pointer for root 1 value. Will be set to NaN if there are no real roots. |
| x2 | Output: Pointer for root 2 value. Will be set to NaN if there is only one individual real root. |
| x3 | Output: Pointer for root 3 value. Will be set to NaN if there are only two individual real roots. |
Definition at line 30 of file roots.c.
|
extern |
Find the real roots of quadratic equation a x^2 + b x + c = 0.
The number of real roots can be 0, 1, or 2, but coincident roots are possible. Root values, excluding duplicate value, will be placed in pointers x1 and x2 in ascending order, or set to NaN.
Based on GNU Scientific Library function gsl_poly_solve_quadratic.
| a | Input: Parameter a of the quadratic equation. |
| b | Input: Parameter b of the quadratic equation. |
| c | Input: Parameter c of the quadratic equation. |
| x1 | Output: Pointer for root 1 value. Will be set to NaN if there are no real roots. |
| x2 | Output: Pointer for root 2 value. Will be set to NaN if there is only one individual real root. |
Definition at line 111 of file roots.c.