STK++ 0.9.13
|
The Algebra project provides structures, tools and methods of the usual algebra techniques on matrices and vectors. If lapack is installed on your system, most of the tools presented here use it in background. Otherwise built-in (but maybe less efficient) methods are used.
Main operations that can be performed are:
All methods for matrix decomposition are enclosed in classes. Computations are launched using the run method.
QR decomposition (also called a QR factorization) of a matrix is a decomposition of a matrix A into a product A = QR of an orthogonal matrix Q and an upper triangular matrix R. Given a matrix A
of size (m,n), QR decomposition (also called a QR factorization) of
A
is a decomposition into a product A=QR
of an orthogonal matrix Q
of size (m,m) and an upper triangular matrix
R
of size (m,n).
QR decomposition can be achieved using either the class
or if lapack is available
In the later case, your code have to be compiled using -DSTKUSELAPACK
flag and linked using -llapack
(for GNU-like compilers).
Example | Output |
---|---|
#include <STKpp.h>
using namespace STK;
{
Array2D<Real> a(5,4);
a << 0, 1, 2, 3,
2, 3, 4, 5,
2, 1, 6, 7,
0, 3,-1, 2,
3,-1, 1, 1;
Qr q(a); q.run();
stk_cout << q.R();
applyLeftHouseholderArray(QR, q.Q());
return 0;
}
This file include all the header files of the STK++ project. The MultidimRegression class allows to regress a multidimensional output variable among a multivariat... Definition STK_MultidimRegression.h:52 The namespace STK is the main domain space of the Statistical ToolKit project. | STK++ QR decomposition: ----------------------- R matrix: 4.12311 1.21268 5.57832 6.54846 0 4.41921 2.08981 4.99158 0 0 4.745 4.22321 0 0 0 -1.53827 0 0 0 0 QR matrix: 0 1 2 3 2 3 4 5 2 1 6 7 0 3 -1 2 3 -1 1 1 lapack QR decomposition: -------------------------- R matrix: -4.12311 -1.21268 -5.57832 -6.54846 0 -4.41921 -2.08981 -4.99158 0 0 -4.745 -4.22321 0 0 0 -1.53827 0 0 0 0 QR matrix: 0 1 2 3 2 3 4 5 2 1 6 7 0 3 -1 2 3 -1 1 1 |
Q
matrix (of size
(m,m)) by using the compQ()
method. Q
will be overwritten.It is possible to update a QR decomposition when a column is added or removed to the original matrix. In the following example, we remove the second column of matrix and then insert a column with value 1 on the second column.
Example | Output |
---|---|
#include <STKpp.h>
using namespace STK;
{
a << 0, 1, 2, 3,
2, 3, 4, 5,
2, 1, 6, 7,
0, 3,-1, 2,
3,-1, 1, 1;
stk_cout << "lapack QR decomposition:\n";
// remove column
lq.eraseCol(2);
stk_cout << "R matrix:\n";
stk_cout << "QR matrix:\n";
// insert constant column with value 1 in column 2
lq.insertCol(Const::Vector<Real>(5), 2);
stk_cout << "\nR matrix:\n";
stk_cout << "QR matrix:\n";
return 0;
}
| lapack QR decomposition: R matrix: -4.12311 -1.21268 -6.54846 0 -4.41921 -4.99158 0 0 4.49464 0 0 0 0 0 0 QR matrix: 0 1 3 2 3 5 2 1 7 0 3 2 3 -1 1 R matrix: -4.12311 -1.21268 -1.69775 -6.54846 0 -4.41921 -1.11811 -4.99158 0 0 0.931381 1.39707 0 0 0 -4.272 0 0 0 0 QR matrix: 0 1 1 3 2 3 1 5 2 1 1 7 0 3 1 2 3 -1 1 1 |
The singular-value decomposition of an (m,n) real (or complex) matrix M is a factorization of the form
(m,m) real (or complex) unitary matrix,
(n,n) real (or complex) unitary matrix.
The diagonal entries
Example | Output |
---|---|
#include <STKpp.h>
using namespace STK;
{
a << 0, 1, 2, 3,
2, 3, 4, 5,
2, 1, 6, 7,
0, 3,-1, 2,
3,-1, 1, 1;
Svd<ArrayXX> s(a); s.run();
stk_cout << s.D();
stk_cout << s.U()*s.D()*s.V().transpose();
return 0;
}
| STK++ Svd decomposition: ------------------------ Singular values: 12.6179 0 0 0 0 4.17637 0 0 0 0 2.51817 0 0 0 0 1.00223 UDV^T matrix: 2.5327e-16 1 2 3 2 3 4 5 2 1 6 7 5.13478e-16 3 -1 2 3 -1 1 1 lapack Svd decomposition: --------------------------- Singular values: 12.6179 4.17637 2.51817 1.00223 UDV^T matrix: 1.07206e-15 1 2 3 2 3 4 5 2 1 6 7 -2.35922e-16 3 -1 2 3 -1 1 1 |
Let A be a square (N,N) matrix with N linearly independent eigenvectors,
where Q is the square (N,N) matrix whose ith column is the eigenvector
If A is a symmetric matrix then Q is an orthogonal matrix.
STK provide native and Lapack interface classes allowing to compute the eigenvalue decomposition of a symmetric square matrix.
Example | Output |
---|---|
#include <STKpp.h>
using namespace STK;
{
CSquareX a(5);
a << 0, 1, 2, 3, 4,
2, 3, 4, 5, 6,
2, 1, 6, 7, 8,
0, 3,-1, 2, 3,
3,-1, 1, 1, 0;
stk_cout << "STK++ Eigen decomposition:\n";
stk_cout << "--------------------------\n";
SymEigen<CSquareX> s(a.upperSymmetrize()); s.run();
stk_cout << "D matrix:\n";
stk_cout << s.eigenValues();
stk_cout << "QDQ^T matrix:\n";
stk_cout << "\nlapack Eigen decomposition:\n";
stk_cout << "--------------------------\n";
stk_cout << "D matrix:\n";
stk_cout << "QDQ^T matrix:\n";
return 0;
}
| STK++ Eigen decomposition: -------------------------- D matrix: 20.8996 0.340126 -0.39673 -1.64329 -8.19967 QDQ^T matrix: -2.33147e-15 1 2 3 4 1 3 4 5 6 2 4 6 7 8 3 5 7 2 3 4 6 8 3 1.64313e-14 lapack Eigen decomposition: -------------------------- D matrix: -8.19967 -1.64329 -0.39673 0.340126 20.8996 QDQ^T matrix: -6.88338e-15 1 2 3 4 1 3 4 5 6 2 4 6 7 8 3 5 7 2 3 4 6 8 3 -7.10543e-15 |
In linear regression, the observations are assumed to be the result of random deviations from an underlying relationship between the dependent variables y and independent variable x.
Given a data set
of n statistical units, a linear regression model assumes that the relationship between the dependent variable y and the regressors x is linear. This relationship is modeled through a disturbance term that adds "noise" to the linear relationship between the dependent variable and regressors. Thus the model takes the form
Often these n equations are stacked together and written in matrix notation as
STK++ provide native and Lapack interface classes allowing to solve the least square regression problem.
Example | Output |
---|---|
#include <STKpp.h>
using namespace STK;
{
Law::Normal l(0,1);
x.rand(l);
beta << 0, 1, 2,
2, 3, 4,
2, 1, 6,
0, 3,-1,
3,-1, 1;
stk_cout << "STK++ MultiLeastSquare:\n";
stk_cout << "-----------------------\n";
stk_cout << "beta matrix:\n";
stk_cout << "\nlapack MultiLeastSquare:\n";
stk_cout << "------------------------\n";
stk_cout << "beta matrix:\n";
return 0;
}
Derived & rand(Law::IUnivLaw< Type > const &law) set random values to this using a distribution law given by the user. Definition STK_ArrayBaseApplier.h:122 The class MultiLeastSQquare solve the least square problem when the response b is multidimensional. Definition STK_lapack_MultiLeastSquare.h:102 | STK++ MultiLeastSquare: ----------------------- beta matrix: 0.0247875 1.02139 1.99183 1.96609 3.01792 4.0079 2.04481 1.03293 6.03462 -0.00848336 2.99758 -1.05867 3.02074 -0.996975 1.00954 lapack MultiLeastSquare: ------------------------ beta matrix: 0.0247875 1.02139 1.99183 1.96609 3.01792 4.0079 2.04481 1.03293 6.03462 -0.00848336 2.99758 -1.05867 3.02074 -0.996975 1.00954 |
Matrices can be inverted using either the templated functor STK::InvertMatrix or the templated function STK::invert. The first example below deals with general square and/or symmetric matrices.
Example | Output |
---|---|
#include <STKpp.h>
using namespace STK;
{
CArray<Real, 4, 4> a(4,4);
a << 0, 1, 2, 3,
2, 3, 4, 5,
2, 1, 6, 7,
0, 3,-1, 2;
stk_cout << "Inverse general matrix:\n";
stk_cout << "-----------------------\n";
stk_cout << a*invert(a);
stk_cout << "\nInverse upper-symmetric matrix:\n";
stk_cout << "-------------------------------\n";
stk_cout << a.upperSymmetrize();
stk_cout << a.upperSymmetrize()*invert(a.upperSymmetrize());
stk_cout << "\nInverse lower-symmetric matrix:\n";
stk_cout << "-------------------------------\n";
stk_cout << a.lowerSymmetrize();
stk_cout << a.lowerSymmetrize()*invert(a.lowerSymmetrize());
return 0;
}
| Inverse general matrix: ----------------------- 1 0 0 0 0 1 0 0 -8.88178e-16 0 1 0 0 0 0 1 Inverse upper-symmetric matrix: ------------------------------- 0 1 2 3 1 3 4 5 2 4 6 7 3 5 7 2 1 0 0 0 0 1 -2.22045e-16 -2.22045e-16 6.66134e-16 0 1 -2.22045e-16 -2.10942e-15 0 1.11022e-16 1 Inverse lower-symmetric matrix: ------------------------------- 0 2 2 0 2 3 1 3 2 1 6 -1 0 3 -1 2 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 |
The second example deals with lower and upper triangular arrays.
Example | Output |
---|---|
#include <STKpp.h>
using namespace STK;
{
Array2DLowerTriangular<Real> a(5,5);
a << 1,
1, 2,
3, 4, 3,
4, 5, 6, 6,
7, 8, 2, 3, 2;
stk_cout << "Inverse lower-triangular matrix:\n";
stk_cout << "--------------------------------\n";
stk_cout << a*invert(a);
stk_cout << "\nInverse upper-triangular matrix:\n";
stk_cout << "--------------------------------\n";
stk_cout << a.transpose()*invert(a.transpose());
return 0;
}
| Inverse lower-triangular matrix: -------------------------------- 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 Inverse upper-triangular matrix: -------------------------------- 1 0 0 -1.11022e-16 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 |