TPCCLIB
|
Routines needed in the use of QR decomposition when solving least squares problems. More...
#include "libtpcmodel.h"
Go to the source code of this file.
Functions | |
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 | qr_weight (int N, int M, double **A, double *b, double *weight, double *ws) |
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. | |
Routines needed in the use of QR decomposition when solving least squares problems.
These routines are based on the code of Gerard Jungman and Brian Gough provided in the GSL library (https://sources.redhat.com/gsl/).
Definition in file qr.c.
int qr | ( | double ** | A, |
int | m, | ||
int | n, | ||
double * | B, | ||
double * | X, | ||
double * | rnorm, | ||
double * | tau, | ||
double * | res, | ||
double ** | wws, | ||
double * | ws ) |
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 346 of file qr.c.
int qr_decomp | ( | double ** | a, |
int | M, | ||
int | N, | ||
double * | tau, | ||
double ** | cchain, | ||
double * | chain ) |
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 matrix of working space. |
chain | m size array of working space. |
Definition at line 427 of file qr.c.
Referenced by qr().
int qr_solve | ( | double ** | QR, |
int | M, | ||
int | N, | ||
double * | tau, | ||
double * | b, | ||
double * | x, | ||
double * | residual, | ||
double * | resNorm, | ||
double ** | cchain, | ||
double * | chain ) |
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 matrix of the working space. |
chain | 2m length array of the working space. |
Definition at line 492 of file qr.c.
Referenced by qr().
int qr_weight | ( | int | N, |
int | M, | ||
double ** | A, | ||
double * | b, | ||
double * | weight, | ||
double * | ws ) |
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 576 of file qr.c.
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.
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 633 of file qr.c.
Referenced by bvls().
int qrLSQ | ( | double ** | mat, |
double * | rhs, | ||
double * | sol, | ||
const unsigned int | rows, | ||
const unsigned int | cols, | ||
double * | r2 ) |
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 54 of file qr.c.