Jive reference manual
Functions
Dense matrix/vector-matrix/vector multiplication

A collection of functions for computing dense matrix/vector-matrix/vector products. More...

Functions

template<class T >
void jem::numeric::matmul (const Array< T, 1 > &r, const Array< T, 2 > &a, const Array< T, 1 > &b)
 Computes the product of a matrix and a column vector. More...
 
template<class T >
Array< T, 1 > jem::numeric::matmul (const Array< T, 2 > &a, const Array< T, 1 > &b)
 Returns the product of a matrix and a column vector. More...
 
template<class T >
void jem::numeric::matmul (const Array< T, 1 > &r, const Array< T, 1 > &a, const Array< T, 2 > &b)
 Computes the product of a row vector and a matrix. More...
 
template<class T >
Array< T, 1 > jem::numeric::matmul (const Array< T, 1 > &a, const Array< T, 2 > &b)
 Returns the product of a row vector and a matrix. More...
 
template<class T >
void jem::numeric::matmul (const Array< T, 2 > &r, const Array< T, 1 > &a, const Array< T, 1 > &b)
 Computes the product of a column vector and a row vector. More...
 
template<class T >
Array< T, 2 > jem::numeric::matmul (const Array< T, 1 > &a, const Array< T, 1 > &b)
 Returns the product of a column vector and a row vector. More...
 
template<class T >
void jem::numeric::matmul (const Array< T, 2 > &r, const Array< T, 2 > &a, const Array< T, 2 > &b)
 Computes the product of two matrices. More...
 
template<class T , int M, int N, int P>
void jem::numeric::matmul (Tuple< T, M, P > &r, const Tuple< T, M, N > &a, const Tuple< T, N, P > &b)
 Computes the product of two matrices. More...
 
template<class T , int M, int P>
void jem::numeric::matmul (Tuple< T, M, P > &r, const Array< T, 2 > &a, const Array< T, 2 > &b)
 Computes the product of two matrices. More...
 
template<class T , int M, int N>
void jem::numeric::matmul (const Array< T, 2 > &r, const Tuple< T, M, N > &a, const Array< T, 2 > &b)
 Computes the product of two matrices. More...
 
template<class T , int N, int P>
void jem::numeric::matmul (const Array< T, 2 > &r, const Array< T, 2 > &a, const Tuple< T, N, P > &b)
 Computes the product of two matrices. More...
 
template<class T >
Array< T, 2 > jem::numeric::matmul (const Array< T, 2 > &a, const Array< T, 2 > &b)
 Returns the product of two matrices. More...
 
template<class T , int M, int N, int P>
Tuple< T, M, P > jem::numeric::matmul (const Tuple< T, M, N > &a, const Tuple< T, N, P > &b)
 Returns the product of two matrices. More...
 
template<class T , int M, int N>
Array< T, 2 > jem::numeric::matmul (const Tuple< T, M, N > &a, const Array< T, 2 > &b)
 Returns the product of two matrices. More...
 
template<class T , int N, int P>
Array< T, 2 > jem::numeric::matmul (const Array< T, 2 > &a, const Tuple< T, N, P > &b)
 Returns the product of two matrices. More...
 

Detailed Description

#include <jem/numeric/algebra/matmul.h>

This header file exports a set of template functions – all named matmul – that compute the following types of matrix-vector products: the product of a matrix and a column vector; the product of a row vector and a matrix; the product of a column vector and a row vector; and the product of two matrices.

Both column and row vectors are represented by one-dimensional Array objects. Matrices are represented by two-dimensional Array objects and by Tuple objects.

The matmul functions come in two flavors: one with three parameters and one with two parameters. The matmul functions with three parameters store their results in their first argument. The matmul functions with two parameter, on the other hand, return a new Array or Tuple object containing the computed results. The three-parameter versions of matmul are faster because they do not have to allocate memory. The two-parameter versions are sometimes more convenient to use because they can be easily embedded into an array expression.

See also
MatmulChain

Function Documentation

template<class T >
void jem::numeric::matmul ( const Array< T, 1 > &  r,
const Array< T, 2 > &  a,
const Array< T, 1 > &  b 
)

Computes the product of the matrix a and the column vector b. The result is stored in the column vector r.

This function is equivalent with:

int i, j;
for ( i = 0; i < r.size(); i++ )
{
r[i] = (T) 0;
for ( j = 0; j < b.size(); j++ )
{
r[i] += a(i,j) * b[j];
}
}

However, if the template parameter T is of type bool, then the matrix-vector product is computed as follows:

int i, j;
for ( i = 0; i < r.size(); i++ )
{
r[i] = false;
for ( j = 0; j < b.size(); j++ )
{
r[i] = r[i] || ( a(i,j) && b[j] );
}
}

Note that the actual implementation of this is function is much more efficient than the code shown above because it takes advantage of the particular way in which the Array class stores its data.

Parameters
r- a column vector that is set to the matrix-vector product of a and b.
a- a matrix.
b- a column vector.
Precondition
r.size() == a.size(0) &&
b.size() == a.size(1)
template<class T >
Array<T,1> jem::numeric::matmul ( const Array< T, 2 > &  a,
const Array< T, 1 > &  b 
)

Returns a column vector containing the product of the matrix a and the column vector b.

This function simply executes:

Array<T> r ( a.size(0) );
matmul ( r, a, b );
return r;
Parameters
a- a matrix.
b- a column vector.
Returns
The product of a and b.
template<class T >
void jem::numeric::matmul ( const Array< T, 1 > &  r,
const Array< T, 1 > &  a,
const Array< T, 2 > &  b 
)

Computes the product of the row vector a and the matrix b. The result is stored in the row vector r.

This function essentially executes the following algorithm:

int i, j;
for ( j = 0; j < r.size(); j++ )
{
r[j] = (T) 0;
for ( i = 0; i < a.size(); i++ )
{
r[j] += a[i] * b(i,j);
}
}

If the template argument T equals the type bool, however, the vector-matrix product is computed as follows:

int i, j;
for ( j = 0; j < r.size(); j++ )
{
r[j] = false;
for ( i = 0; i < a.size(); i++ )
{
r[j] = r[j] || ( a[i] && b(i,j) );
}
}

Note that the actual implementation of this is function is much more efficient than the code shown above because it takes advantage of the particular way in which the Array class stores its data.

Parameters
r- a row vector that is set to the vector-matrix product of a and b.
a- a row vector.
b- a matrix.
Precondition
r.size() == b.size(1) &&
a.size() == a.size(0)
template<class T >
Array<T,1> jem::numeric::matmul ( const Array< T, 1 > &  a,
const Array< T, 2 > &  b 
)

Returns a row vector containing the product of the row vector a and the matrix b.

This function simply executes:

Array<T> r ( b.size(1) );
matmul ( r, a, b );
return r;
Parameters
a- a row vector.
b- a matrix.
Returns
The product of a and b.
template<class T >
void jem::numeric::matmul ( const Array< T, 2 > &  r,
const Array< T, 1 > &  a,
const Array< T, 1 > &  b 
)

Computes the product of the column vector a and the row vector b. The result is stored in the matrix r.

The vector-vector product is computed as follows:

int i, j;
for ( i = 0; i < a.size(); i++ )
{
for ( j = 0; j < b.size(); j++ )
{
r(i,j) = a[i] * b[j];
}
}

If the template arguments T is of type bool, however, then the product is computed as follows:

int i, j;
for ( i = 0; i < a.size(); i++ )
{
for ( j = 0; j < b.size(); j++ )
{
r(i,j) = a[i] && b[j];
}
}

Note that the actual implementation of this is function is much more efficient than the code shown above because it takes advantage of the particular way in which the Array class stores its data.

Parameters
r- a matrix that is set to the vector-vector product of a and b.
a- a column vector.
b- a row vector.
Precondition
r.size(0) == a.size() &&
r.size(1) == b.size()
template<class T >
Array<T,2> jem::numeric::matmul ( const Array< T, 1 > &  a,
const Array< T, 1 > &  b 
)

Returns a matrix containing the product of the column vector a and the row vector b.

This function simply executes:

Array<T,2> r ( a.size(), b.size() );
matmul ( r, a, b );
return r;
Parameters
a- a column vector.
b- a row vector.
Returns
The product of a and b.
template<class T >
void jem::numeric::matmul ( const Array< T, 2 > &  r,
const Array< T, 2 > &  a,
const Array< T, 2 > &  b 
)

Computes the product of the matrices a and b. The resulting matrix is stored in r.

The matrix-matrix product is computed as follows:

int i, j, k;
for ( i = 0; i < r.size(0); i++ )
{
for ( j = 0; j < r.size(1); j++ )
{
r(i,j) = (T) 0;
for ( k = 0; k < a.size(1); k++ )
{
r(i,j) += a(i,k) * b(k,j);
}
}
}

If the template arguments T is of type bool, then the product is computes as follows:

int i, j, k;
for ( i = 0; i < r.size(0); i++ )
{
for ( j = 0; j < r.size(1); j++ )
{
r(i,j) = false;
for ( k = 0; k < a.size(1); k++ )
{
r(i,j) = r(i,j) || ( a(i,k) && b(k,j) );
}
}
}

Note that the actual implementation of this is function is much more efficient than the code shown above because it takes advantage of the particular way in which the Array class stores its data.

Parameters
r- a matrix that is set to the matrix-matrix product of a and b.
a- a matrix.
b- another matrix.
Precondition
r.size(0) == a.size(0) &&
r.size(1) == b.size(1) &&
a.size(1) == b.size(0)
template<class T , int M, int N, int P>
void jem::numeric::matmul ( Tuple< T, M, P > &  r,
const Tuple< T, M, N > &  a,
const Tuple< T, N, P > &  b 
)

Computes the matrix-matrix product of the tuples a and b. The resulting matrix is stored in the tuple r. This function has the same semantics as the equivalent matmul function with three array arguments.

Parameters
r- a tuple that is set to the matrix-matrix product of a and b.
a- a tuple representing a matrix.
b- a tuple representing another matrix.
template<class T , int M, int P>
void jem::numeric::matmul ( Tuple< T, M, P > &  r,
const Array< T, 2 > &  a,
const Array< T, 2 > &  b 
)

Computes the matrix-matrix product of the two-dimensional arrays a and b. The resulting matrix is stored in the tuple r. This function has the same semantics as the equivalent matmul function with three array arguments.

Parameters
r- a tuple that is set to the matrix-matrix product of a and b.
a- an array representing a matrix.
b- another array.
Precondition
a.size(0) == M &&
a.size(1) == b.size(0) &&
b.size(1) == P
template<class T , int M, int N>
void jem::numeric::matmul ( const Array< T, 2 > &  r,
const Tuple< T, M, N > &  a,
const Array< T, 2 > &  b 
)

Computes the matrix-matrix product of the tuple a and the two-dimensional array b. The resulting matrix is stored in the two-dimensional array r. This function has the same semantics as the equivalent matmul function with three array arguments.

Parameters
r- an array that is set to the matrix-matrix product of a and b.
a- a tuple representing a matrix.
b- an array representing another matrix.
Precondition
r.size(0) == M &&
r.size(1) == b.size(1) &&
b.size(0) == N
template<class T , int N, int P>
void jem::numeric::matmul ( const Array< T, 2 > &  r,
const Array< T, 2 > &  a,
const Tuple< T, N, P > &  b 
)

Computes the matrix-matrix product of the two-dimensional array a and the tuple b. The resulting matrix is stored in the two-dimensional array r. This function has the same semantics as the equivalent matmul function with three array arguments.

Parameters
r- an array that is set to the matrix-matrix product of a and b.
a- an array representing a matrix.
b- a tuple representing another matrix.
Precondition
r.size(0) == a.size(0) &&
r.size(1) == P &&
a.size(1) == N
template<class T >
Array<T,2> jem::numeric::matmul ( const Array< T, 2 > &  a,
const Array< T, 2 > &  b 
)

Returns a matrix containing the product of the matrices a and b.

This function simply executes:

Array<T,2> r ( a.size(0), b.size(1) );
matmul ( r, a, b );
return r;
Parameters
a- a matrix.
b- another matrix.
Precondition
a.size(1) == b.size(0)
Returns
The product of a and b.
template<class T , int M, int N, int P>
Tuple<T,M,P> jem::numeric::matmul ( const Tuple< T, M, N > &  a,
const Tuple< T, N, P > &  b 
)

Returns a tuple containing the product of the tuples a and b.

This function simply executes:

Tuple<T,M,P> r;
matmul ( r, a, b );
return r;
Parameters
a- a tuple representing a matrix.
b- a tuple representing another matrix.
Returns
The product of a and b.
template<class T , int M, int N>
Array<T,2> jem::numeric::matmul ( const Tuple< T, M, N > &  a,
const Array< T, 2 > &  b 
)

Returns a two-dimensional array containing the product of the tuple a and the two-dimensional array b.

This function simply executes:

Array<T,2> r ( M, b.size(1) );
matmul ( r, a, b );
return r;
Parameters
a- a tuple representing a matrix.
b- an array representing another matrix.
Precondition
b.size(0) == N
Returns
The product of a and b.
template<class T , int N, int P>
Array<T,2> jem::numeric::matmul ( const Array< T, 2 > &  a,
const Tuple< T, N, P > &  b 
)

Returns a two-dimensional array containing the product of the two-dimensional array a and the tuple b.

This function simply executes:

Array<T,2> r ( a.size(0), P );
matmul ( r, a, b );
return r;
Parameters
a- an array representing a matrix.
b- a tuple representing another matrix.
Precondition
a.size(1) == N
Returns
The product of a and b.