LinAlg.cpp File Reference

Some operations for matrices. More...

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <SharkDefs.h>
#include <Array/ArrayOp.h>
#include <LinAlg/LinAlg.h>

Go to the source code of this file.

Functions

Array< double > mean (const Array< double > &x)
 Calculates the mean vector of array "x".
Array< double > variance (const Array< double > &x)
 Calculates the variance vector of array "x".
double angle (const Array< double > &x, const Array< double > &y)
 Calculates the angle between the vectors "x" and "y".
void meanvar (const Array< double > &x, Array< double > &m, Array< double > &v)
 Calculates the mean and variance values of matrix "x".
void meanvar (const Array< double > &pxA, const Array< double > &xA, double &mA, double &vA, const int startA, const int endA)
 Calculates the mean and variance values of 1d-arrays p(x).
double corrcoef (const Array< double > &x, const Array< double > &y)
 Calculates the coefficient of correlation of the data vectors "x" and "y".
Array< double > corrcoef (const Array< double > &x)
 Calculates the coefficient of correlation matrix of the data vectors stored in matrix "x".
double covariance (const Array< double > &x, const Array< double > &y)
 Calculates the covariance between the data vectors "x" and "y".
Array< double > covariance (const Array< double > &x)
 Calculates the covariance matrix of the data vectors stored in matrix "x".
void matMat (Array2D< double > &A, const Array2D< double > &B, const Array2D< double > &C)
 Computes A = BC , where B and C are an $n\times l$ and a $k\times l$ matrix, respectively.
void matColVec (Array< double > &A, const Array2D< double > &B, const Array< double > &C)
 Computes A = BC , where B is an $n\times n$ matrix and the $n$-dimensional Array C is viewed as a column vector.
void matColVec (ArrayReference< double > A, const Array2D< double > &B, const ArrayReference< double > C)
 Computes A = BC , where B is an $n\times n$ matrix and the $n$-dimensional Array C is viewed as a column vector.
void matColVec (Array< double > &A, const Array2D< double > &B, const Array< double > &C, unsigned int index)
 Computes $ A = B C_i $ , where $ C_i $ is a column of the matrix C.
double vecMatVec (const Array< double > &A, const Array2D< double > &B, const Array< double > &C)
 Computes s = ABC , where A and C are vectors of length $n$ and B is an $n\times n$ matrix.
double vecMatVec (const Array< double > &A, unsigned int i, const Array2D< double > &B, const Array< double > &C, unsigned int j)
 Computes s = $A_iBC_j$ , where $A_i$ and $C_j$ are vectors of length $n$ and $m$, and $B$ is an $n m$ matrix.
void invertSymm (Array2D< double > &I, const Array2D< double > &A)
 Computes I = A^T for a square symmetric matrix A.
void CholeskyDecomposition (const Array2D< double > &M, Array2D< double > &C)
 lower triangular Cholesky decomposition


Detailed Description

Some operations for matrices.

Author:
M. Kreutz
Date:
1995-01-01
Copyright (c) 1999-2000:
Institut fuer Neuroinformatik
Ruhr-Universitaet Bochum
D-44780 Bochum, Germany
Phone: +49-234-32-25558
Fax: +49-234-32-14209
eMail: Shark-admin@neuroinformatik.ruhr-uni-bochum.de
www: http://www.neuroinformatik.ruhr-uni-bochum.de


This file is part of Shark. This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Definition in file LinAlg.cpp.


Function Documentation

double angle ( const Array< double > &  x,
const Array< double > &  y 
)

Calculates the angle between the vectors "x" and "y".

Given the two one-dimensional vectors "x" and "y" with the same no. N of elements, the function calculates:

\[ angle = \frac{\sum_{i=1}^{N} (x_i * y_i)}{\sum_{i=1}^{N} x_i * \sum_{i=1}^{N} y_i} \]

Parameters:
x one-dimensional vector no. 1
y one-dimensional vector no. 2
Returns:
the angle between x and y
Exceptions:
SharkException the type of the exception will be "size mismatch" and indicates that x is not one-dimensional or that x has not the same size than y
Please follow the link to view the source code of the example. The example can be executed in the example directory of package LinAlg. <
Author:
M. Kreutz
Date:
1995
Changes
none
Status
stable
Examples:
linalg_simple_test.cpp.

Definition at line 216 of file LinAlg.cpp.

void CholeskyDecomposition ( const Array2D< double > &  M,
Array2D< double > &  C 
)

lower triangular Cholesky decomposition

Given an $ m \times m $ symmetric positive definite matrix $M$, compute the lower triangular matrix $C$ such that $ M=CC^T $

Parameters:
M $ m \times m $ matrix, which must be symmetric and positive definite
C $ m \times m $ matrix, which stores the Cholesky factor
Returns:
none
Author:
T. Suttorp and C. Igel
Date:
2008
Status
stable

Definition at line 868 of file LinAlg.cpp.

Referenced by invertSymmPositiveDefinite().

Array< double > corrcoef ( const Array< double > &  x  ) 

Calculates the coefficient of correlation matrix of the data vectors stored in matrix "x".

Calculates the coefficient of correlation matrix of the data vectors stored in matrix "x".

Given a matrix $X = (x_{ij})$ of $n$ vectors with length $N$, the function calculates the coefficient of correlation matrix given as

$ r := (r_{kl}) \mbox{,\ } r_{kl} = \frac{c_{kl}}{\Delta x_k \Delta x_l}\mbox{,\ } k,l = 1, \dots, N $

where $c_{kl}$ is the entry of the covariance matrix of $x$ and $y$ (see covariance(const Array<double>& x)) and $\Delta x_k$ and $\Delta x_l$ are the standard deviations of $x_k$ and $x_l$ respectively.

Parameters:
x The $n \times N$ input matrix.
Returns:
The $N \times N$ coefficient of correlation matrix.
Exceptions:
SharkException the type of the exception will be "size mismatch" and indicates that x is only one- or non-dimensional
Please follow the link to view the source code of the example. The example can be executed in the example directory of package LinAlg.

Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable

Definition at line 483 of file LinAlg.cpp.

References covariance().

double corrcoef ( const Array< double > &  x,
const Array< double > &  y 
)

Calculates the coefficient of correlation of the data vectors "x" and "y".

Calculates the coefficient of correlation of the data vectors "x" and "y".

Given two data vectors $x$ and $y$ of length $n$, the function calculates the coefficient of correlation given as

$ r := \frac{cov(x, y)}{\Delta x \Delta y} $

where $cov(x, y)$ is the covariance between the two vectors (see also covariance(const Array< double >&, const Array< double >&)) and $\Delta x$ and $\Delta y$ are the standard deviations of $x$ and $y$ respectively.
The coefficient of correlation is used to show the dependence between $x$ and $y$. It always holds $-1 \leq r \leq 1$ and the greater the value of $r^2$ is, the greater is the dependence between $x$ and $y$.

Parameters:
x first data vector.
y second data vector.
Returns:
the coefficient of correlation.
Exceptions:
SharkException the type of the exception will be "size mismatch" and indicates that x is not one-dimensional or x has not the same size than y
Please follow the link to view the source code of the example. The example can be executed in the example directory of package LinAlg.

Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable
Examples:
covar_corrcoef_test.cpp.

Definition at line 408 of file LinAlg.cpp.

Array< double > covariance ( const Array< double > &  x  ) 

Calculates the covariance matrix of the data vectors stored in matrix "x".

Calculates the covariance matrix of the data vectors stored in matrix "x".

Given a matrix $X = (x_{ij})$ of $n$ vectors with length $N$, the function calculates the covariance matrix given as

$ Cov = (c_{kl}) \mbox{,\ } c_{kl} = \frac{1}{n - 1} \sum_{i=1}^n (x_{ik} - \overline{x_k})(x_{il} - \overline{x_l})\mbox{,\ } k,l = 1, \dots, N $

where $\overline{x_j} = \frac{1}{n} \sum_{i = 1}^n x_{ij}$ is the mean value of $x_j \mbox{,\ }j = 1, \dots, N$.

Parameters:
x The $n \times N$ input matrix.
Returns:
$N \times N$ matrix of covariance values.
Exceptions:
SharkException the type of the exception will be "type mismatch" and indicates that x is not 2-dimensional
Please follow the link to view the source code of the example. The example can be executed in the example directory of package LinAlg.

Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable

Definition at line 602 of file LinAlg.cpp.

References mean().

double covariance ( const Array< double > &  x,
const Array< double > &  y 
)

Calculates the covariance between the data vectors "x" and "y".

Given two data vectors $x$ and $y$ with length $n$, interpreted as $n$ points $(x_i, y_i)$ with $i = 1, \dots, n$, the function calculates the covariance given as

$ cov = \frac{1}{n - 1} \sum_{i = 1}^n (x_i - \overline{x}) (y_i - \overline{y}) $

where $\overline{x}$ and $\overline{y}$ are the mean values of $x$ and $y$ respectively.

Parameters:
x first data vector.
y second data vector.
Returns:
the covariance matrix.
Exceptions:
SharkException the type of the exception will be "type mismatch" and indicates that x is not one-dimensional or x has not the same size than y
Please follow the link to view the source code of the example. The example can be executed in the example directory of package LinAlg.

Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable
Examples:
covar_corrcoef_test.cpp.

Definition at line 542 of file LinAlg.cpp.

Referenced by corrcoef().

void invertSymm ( Array2D< double > &  I,
const Array2D< double > &  A 
)

Computes I = A^T for a square symmetric matrix A.

Inverts a symmetric matrix.

Author:
C. Igel
Date:
2007
Changes
none

Definition at line 824 of file LinAlg.cpp.

References eigensymm(), and transpose().

Referenced by g_inverseCholesky(), and Matrix::inverseSymm().

void matColVec ( Array< double > &  A,
const Array2D< double > &  B,
const Array< double > &  C,
unsigned int  index 
)

Computes $ A = B C_i $ , where $ C_i $ is a column of the matrix C.

Returns $ A = B C_i $ , where $ C_i $ is a column of the matrix C.

Author:
T. Glasmachers
Date:
2007
Changes
none

Definition at line 723 of file LinAlg.cpp.

void matColVec ( ArrayReference< double >  A,
const Array2D< double > &  B,
const ArrayReference< double >  C 
)

Computes A = BC , where B is an $n\times n$ matrix and the $n$-dimensional Array C is viewed as a column vector.

Returns A = BC, where C is viewed as a column vector.

Author:
T. Glasmachers
Date:
2008

Definition at line 692 of file LinAlg.cpp.

void matColVec ( Array< double > &  A,
const Array2D< double > &  B,
const Array< double > &  C 
)

Computes A = BC , where B is an $n\times n$ matrix and the $n$-dimensional Array C is viewed as a column vector.

Returns A = BC, where C is viewed as a column vector.

Author:
C. Igel
Date:
2007

Definition at line 665 of file LinAlg.cpp.

void matMat ( Array2D< double > &  A,
const Array2D< double > &  B,
const Array2D< double > &  C 
)

Computes A = BC , where B and C are an $n\times l$ and a $k\times l$ matrix, respectively.

Multiplies two 2D matrices.

Author:
C. Igel
Date:
2007
Changes
none

Definition at line 641 of file LinAlg.cpp.

Referenced by g_inverseCholesky(), g_inverseMoorePenrose(), and invertSymmPositiveDefinite().

Array< double > mean ( const Array< double > &  x  ) 

Calculates the mean vector of array "x".

Given a d -dimensional array x with size N1 x ... x Nd, this function calculates the mean vector given as:

\[ mean_j = \frac{1}{N1} \sum_{i=1}^{N1} x_{i,j} \]

Example:

\[ \left( \begin{array}{*{4}{c}} 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8\\ 9 & 10 & 11 & 12\\ \end{array} \right) \longrightarrow \frac{1}{3} \left( \begin{array}{*{4}{c}} 1+5+9 & 2+6+10 & 3+7+11 & 4+8+12\\ \end{array} \right) \longrightarrow \left( \begin{array}{*{4}{c}} 5 & 6 & 7 & 8\\ \end{array} \right) \]

Parameters:
x multidimensional array, from which the mean value will be calculated
Returns:
the mean vector of x
Exceptions:
SharkException the type of the exception will be "size mismatch" and indicates that x is only one-dimensional or has no dimensions
Please follow the link to view the source code of the example. The example can be executed in the example directory of package LinAlg.

Author:
M. Kreutz
Date:
1995
Changes
none
Status
stable
Examples:
linalg_simple_test.cpp.

Definition at line 103 of file LinAlg.cpp.

Referenced by covariance().

void meanvar ( const Array< double > &  pxA,
const Array< double > &  xA,
double &  mA,
double &  vA,
const int  startA,
const int  endA 
)

Calculates the mean and variance values of 1d-arrays p(x).

Definition at line 322 of file LinAlg.cpp.

void meanvar ( const Array< double > &  x,
Array< double > &  m,
Array< double > &  v 
)

Calculates the mean and variance values of matrix "x".

Given the input matrix x, the mean and variance values are calculated as in the functions mean and variance. The mean and variance values are stored in the vectors m and v.

Parameters:
x The input matrix.
m Vector of mean values.
v Vector of variances.
Returns:
none.
Exceptions:
SharkException the type of the exception will be "size mismatch" and indicates that x is only one- or non-dimensional
Please follow the link to view the source code of the example. The example can be executed in the example directory of package LinAlg.

Author:
M. Kreutz
Date:
1995
Changes
none
Status
stable
See also:
mean, variance
Examples:
linalg_simple_test.cpp.

Definition at line 271 of file LinAlg.cpp.

Array< double > variance ( const Array< double > &  x  ) 

Calculates the variance vector of array "x".

Given a d -dimensional array x with size N1 x ... x Nd and mean value vector m, this function calculates the variance vector given as:

\[ variance = \frac{1}{N1} \sum_{i=1}^{N1} (x_i - m_i)^2 \]

Parameters:
x multidimensional array, from which the variance will be calculated
Returns:
the variance vector of x
Exceptions:
SharkException the type of the exception will be "size mismatch" and indicates that x is only one-dimensional or has no dimensions or has no values in its first dimension
Please follow the link to view the source code of the example. The example can be executed in the example directory of package LinAlg.

Author:
M. Kreutz
Date:
1995
Changes
none
Status
stable
Examples:
linalg_simple_test.cpp.

Definition at line 151 of file LinAlg.cpp.

double vecMatVec ( const Array< double > &  A,
unsigned int  i,
const Array2D< double > &  B,
const Array< double > &  C,
unsigned int  j 
)

Computes s = $A_iBC_j$ , where $A_i$ and $C_j$ are vectors of length $n$ and $m$, and $B$ is an $n m$ matrix.

Returns the scalar $ A_i B C_j $.

Author:
C. Igel
Date:
2007
Changes
none

Definition at line 789 of file LinAlg.cpp.

double vecMatVec ( const Array< double > &  A,
const Array2D< double > &  B,
const Array< double > &  C 
)

Computes s = ABC , where A and C are vectors of length $n$ and B is an $n\times n$ matrix.

Returns the scalar ABC.

Author:
C. Igel
Date:
2007
Changes
none

Definition at line 755 of file LinAlg.cpp.