geomc 1.0
A c++ linear algebra template library
Classes | Functions
Matrix

Matrix-related functions and classes. More...

Classes

class  DiagMatrix< T, M, N >
 A matrix with nonzero elements only along the main diagonal. More...
 
class  MatrixHandle< T >
 A generic matrix class which can hold references to all other matrix types. More...
 
class  PermutationMatrix< N >
 A matrix which, by multiplication, permutes the rows or columns of another matrix. More...
 
class  SimpleMatrix< T, M, N, Lyt, P >
 A basic matrix with M x N elements. More...
 

Functions

template<typename Ma , typename Mb >
bool mtx_aliases_storage (const Ma &a, const Mb &b)
 
template<typename T , index_t N>
bool mtx_aliases_storage (const Vec< T, N > &a, const Vec< T, N > &b)
 
template<typename Matrix , typename Matrix1 , typename Matrix2 >
Matrix & mul (Matrix *into, const Matrix1 &a, const Matrix2 &b)
 
template<typename Md , typename Ma , typename Mb >
std::enable_if< detail::MatrixMultipliable< Ma, Mb >::valanddetail::_ImplMtxResult< Ma, Mb, Md >::agreement==detail::MTX_RESULT_MATCH, Md & >::type mul (Md *into, const Ma &a, const Mb &b)
 
template<typename Matrix1 , typename Matrix2 >
Matrix mul (const Matrix1 &a, const Matrix2 &b)
 
template<typename Ma , typename Mb >
std::enable_if< detail::MatrixMultipliable< Ma, Mb >::val, typenamedetail::_ImplMtxMul< Ma, Mb >::return_t >::type mul (const Ma &a, const Mb &b)
 
template<typename T , index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
std::enable_if< M *N==0orM==N, void >::type transpose (SimpleMatrix< T, M, N, Lyt, P > *into, const SimpleMatrix< T, M, N, Lyt, P > &m)
 
template<typename Matrix1 , typename Matrix2 >
void transpose (Matrix1 *into, const Matrix2 &m)
 
template<typename Md , typename Mx >
std::enable_if< detail::IsMatrix< Md >::valanddetail::IsMatrix< Mx >::valand(Md::ROWDIM==Mx::COLDIMorMd::ROWDIM *Mx::COLDIM==0) and(Md::COLDIM==Mx::ROWDIMorMd::COLDIM *Mx::ROWDIM==0), void >::type transpose (Md *into, const Mx &m)
 
template<typename Matrix1 >
Matrix transpose (const Matrix1 &m)
 
template<typename Mx >
std::enable_if< detail::IsMatrix< Mx >::val, typenamedetail::_ImplMtxTxpose< Mx >::return_t >::type transpose (const Mx &m)
 
template<typename Matrix1 , typename Matrix2 >
bool inv (Matrix1 *into, const Matrix2 &src)
 
template<typename Md , typename Mx >
bool inv (Md *into, const Mx &src, M_ENABLE_IF((detail::MatrixDimensionMatch< Md, Mx >::isStaticMatch and(Mx::ROWDIM==Mx::COLDIM or Mx::ROWDIM *Mx::COLDIM==DYNAMIC_DIM))))
 
template<typename Matrix1 >
Matrix inv (const Matrix1 &m, bool *success)
 
template<typename Mx >
detail::_ImplMtxInv< Mx >::return_t inv (const Mx &m, bool *success, M_ENABLE_IF(Mx::ROWDIM==Mx::COLDIM or Mx::ROWDIM *Mx::COLDIM==DYNAMIC_DIM))
 
template<typename Matrix , typename Matrix1 , typename Matrix2 >
void add (Matrix *d, const Matrix1 &a, const Matrix2 &b)
 
template<typename Md , typename Ma , typename Mb >
std::enable_if< detail::MatrixDimensionMatch< Ma, Mb >::isStaticMatchanddetail::MatrixDimensionMatch< Ma, Md >::isStaticMatch, void >::type add (Md *d, const Ma &a, const Mb &b)
 
template<typename Matrix , typename Matrix1 , typename Matrix2 >
void sub (Matrix *d, const Matrix1 &a, const Matrix2 &b)
 
template<typename Md , typename Ma , typename Mb >
std::enable_if< detail::MatrixDimensionMatch< Ma, Mb >::isStaticMatchanddetail::MatrixDimensionMatch< Ma, Md >::isStaticMatch, void >::type sub (Md *d, const Ma &a, const Mb &b)
 
template<typename Matrix1 , typename Matrix2 >
Matrix add (const Matrix1 &a, const Matrix2 &b)
 
template<typename Ma , typename Mb >
std::enable_if< detail::MatrixDimensionMatch< Ma, Mb >::isStaticMatch, typenamedetail::_ImplMatrixAdd< Ma, Mb >::return_t >::type add (const Ma &a, const Mb &b)
 
template<typename Matrix1 , typename Matrix2 >
Matrix sub (const Matrix1 &a, const Matrix2 &b)
 
template<typename Ma , typename Mb >
std::enable_if< detail::MatrixDimensionMatch< Ma, Mb >::isStaticMatch, typenamedetail::_ImplMatrixAdd< Ma, Mb >::return_t >::type sub (const Ma &a, const Mb &b)
 
template<typename U typename Matrix, typename Matrix >
void scale (Matrix1 *d, U k, const Matrix &m)
 
template<typename U , typename Mx , typename Md >
std::enable_if< detail::IsMatrix< Mx >::valandstd::is_scalar< U >::valueanddetail::MatrixDimensionMatch< Mx, Md >::isStaticMatch, void >::type scale (Md *d, U k, const Mx &m)
 
template<typename U , typename Matrix >
Matrix scale (U k, const Matrix &m)
 
template<typename U , typename Mx >
std::enable_if< detail::IsMatrix< Mx >::valandstd::is_scalar< U >::value, typenamedetail::_ImplMatrixScale< Mx >::return_t >::type scale (U k, const Mx &m)
 
template<typename U , typename Matrix >
Matrix operator* (U k, const Matrix &m)
 
template<typename U , typename Mx >
std::enable_if< detail::IsMatrix< Mx >::valandstd::is_scalar< U >::value, typenamedetail::_ImplMatrixScale< Mx >::return_t >::type operator* (U k, const Mx &m)
 
template<typename U , typename Matrix >
Matrix operator* (const Matrix &m, U k)
 
template<typename U , typename Mx >
std::enable_if< detail::IsMatrix< Mx >::valandstd::is_scalar< U >::value, typenamedetail::_ImplMatrixScale< Mx >::return_t >::type operator* (const Mx &m, U k)
 
template<typename Matrix1 , typename Matrix2 >
Matrix operator+ (const Matrix1 &a, const Matrix2 &b)
 
template<typename Ma , typename Mb >
detail::_ImplMatrixAddReturnType< Ma, Mb >::return_t operator+ (const Ma &a, const Mb &b)
 
template<typename Matrix1 , typename Matrix2 >
Matrix operator- (const Matrix1 &a, const Matrix2 &b)
 
template<typename Ma , typename Mb >
detail::_ImplMatrixAddReturnType< Ma, Mb >::return_t operator- (const Ma &a, const Mb &b)
 
template<typename Matrix1 , typename Matrix2 >
Matrix operator* (const Matrix1 &a, const Matrix2 &b)
 
template<typename Ma , typename Mb >
detail::MatrixMultReturnType< Ma, Mb >::return_t operator* (const Ma &a, const Mb &b)
 
template<typename Matix , T , M , N >
Matrix & operator*= (Matrix &m, const DiagMatrix< T, M, N > &d)
 
template<typename Mx , typename T , index_t M, index_t N>
std::enable_if< detail::MatrixMultipliable< Mx, DiagMatrix< T, M, N > >::val, Mx & >::type operator*= (Mx &m, const DiagMatrix< T, M, N > &d)
 
template<typename Matrix1 , typename Matrix2 >
bool operator== (const Matrix1 &a, const Matrix2 &b)
 
template<typename Ma , typename Mb >
std::enable_if< detail::IsMatrix< Ma >::valanddetail::IsMatrix< Mb >::valanddetail::MatrixDimensionMatch< Ma, Mb >::isStaticMatch, bool >::type operator== (const Ma &a, const Mb &b)
 
template<typename Matrix1 , typename Matrix2 >
bool operator!= (const Matrix1 &a, const Matrix2 &b)
 
template<typename Ma , typename Mb >
std::enable_if< detail::IsMatrix< Ma >::valanddetail::IsMatrix< Mb >::val, bool >::type operator!= (const Ma &a, const Mb &b)
 
template<typename Matrix , typename Matrix1 >
void mtxcopy (Matrix *into, const Matrix1 &src)
 
template<typename Md , typename Ms >
void mtxcopy (Md *into, const Ms &src, typename std::enable_if< detail::LinalgDimensionMatch< Md, Ms >::val and(detail::_ImplVecOrient< Md, Ms >::orient !=detail::ORIENT_VEC_UNKNOWN), int >::type dummy=0)
 
template<typename Md , typename Ms >
void mtxcopy (Md *into, const Ms &src, typename std::enable_if< detail::_ImplVecOrient< Md, Ms >::orient==detail::ORIENT_VEC_UNKNOWN, int >::type dummy=0)
 
template<typename T >
det2x2 (T a, T b, T c, T d)
 Compute the 2x2 matrix determinant from individual parameters.
 
template<typename T >
det3x3 (const T m[9])
 Compute the 3x3 matrix determinant.
 
template<typename T >
det3x3 (T a, T b, T c, T d, T e, T f, T g, T h, T i)
 Compute the 3x3 matrix determinant from individual parameters.
 
template<typename T >
det4x4 (const T m[16])
 Compute the 4x4 matrix determinant.
 
template<typename T >
det_destructive (T *m, index_t n)
 Destructively compute the determinant of a square matrix. More...
 
template<typename T >
detNxN (const T *m, index_t n)
 Compute the determinant of a square matrix. More...
 
template<typename T , index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
det (const SimpleMatrix< T, M, N, Lyt, P > &m)
 Compute the determinant of a square matrix. More...
 
template<typename T , MatrixLayout Lyt, StoragePolicy P>
det (const SimpleMatrix< T, 2, 2, Lyt, P > &m)
 
template<typename T , MatrixLayout Lyt, StoragePolicy P>
det (const SimpleMatrix< T, 3, 3, Lyt, P > &m)
 
template<typename T , MatrixLayout Lyt, StoragePolicy P>
det (const SimpleMatrix< T, 4, 4, Lyt, P > &m)
 
template<typename T , index_t N, index_t M>
det (const DiagMatrix< T, N, M > &m)
 Compute the determinant of a diagonal matrix. More...
 
template<index_t N>
index_t det (const PermutationMatrix< N > &m)
 Compute the determinant of a permutation matrix. More...
 
template<typename T >
bool inv2x2 (T *out, const T m[4])
 2 × 2 matrix inversion on a flat array. More...
 
template<typename T >
bool inv3x3 (T *out, const T m[9])
 3 × 3 matrix inversion on a flat array. More...
 
template<typename T >
bool inv4x4 (T *out, const T m[16])
 4 × 4 matrix inversion on a flat array. More...
 
template<typename T >
bool invNxN (T *out, T *m, index_t n)
 N × N matrix inversion on a flat array. More...
 
template<typename T , index_t N>
bool invNxN (T *out, T *m)
 N × N matrix inversion. More...
 

Detailed Description

Matrix-related functions and classes.

Include

#include <geomc/linalg/Matrix.h>

Design

There are currently four distinct matrix template classes. They are all interoperable, and provide iterators which are functionally interchangeable with each other and with pointers (row iterators, column iterators, region iterators, and row-major matrix body iterators).

These iterators are compatible with std::copy(). Some of them are not writeable (or may possibly throw an error upon writing) in the case where a matrix element does not have a corresponding memory location (DiagonalMatrix is one such case, for example; off-diagonals are not stored).

In general, this scheme was designed to satisfy the following requirements:

Use

Matrix dimensions

Most matrices have templated size:

SimpleMatrix<double, 3, 4> mat3x4;

In the example above, we construct a matrix with 3 rows and 4 columns. This matrix has static dimensions, in that its size is chosen (and fixed) at compile-time.

Matrices with dimensions chosen at runtime may be written as:

SimpleMatrix<double, 0, 0> matNxN(nrows, ncols);

The primary advantage of using "static" matrices is twofold:

Because static matrices are backed with static arrays, large static matrices should not be declared on the stack, and instead should created with the new operator to avoid stack overflow.

Backing storage

By default, matrices generally behave as though their underlying array is duplicated whenever they are copied; ensuring two distinct matrixes do not alias the same common storage. Note that a full array copy is not always necessary (ownership transfer is optimized in c++11 with rvalue references), and careful usage can render "heavy" copies quite rare.

This preference to use "copy" semantics can be changed for SimpleMatrix by passing different policies to its StoragePolicy template parameter.

To check if two matrices share any common storage:

if (mtx_aliases_storage(m1, m2)) { ... }

To copy the elements from one matrix to another, without referencing, and regardless of matrix type or static/dynamic setting:

mtxcopy(&dst_mtx, src_mtx);

This method is often optimized over using std::copy() directly on the matrices' iterators. Some matrices (e.g. DiagMatrix) may not support std::copy(), but do support mtxcopy() with sensible arguments.

Unless otherwise noted, all operations on matrices should be expected to return correct results, even if the destination matrix aliases storage of an operand (though calling operations in this way may incur a performance and/or memory overhead).

Dimension checking

Matrix operations often place restrictions on the dimensions of their matrix operands. For example, matrix addition requires that both operands have the same dimensions (in other words, a 3 x 4 matrix can only be added to another 3 x 4 matrix).

When matrices with static dimensions are involved, the library can often prove at compile time that an argument's dimensions are correct or incorrect. If proven correct, run-time dimension checks can be skipped (by relying on compiler elimination of constructs like if (false) {...}), resulting in moderately faster code. If incorrect, the compiler will error, catching program correctness problems early. Compile-time dimension mismatches / requirement failures will generally manifest as "template argument deduction/substitution failed" errors (a more informative message would be desirable, but c++ does not provide the functionality to produce one).

This code will prove its dimension-correctness at compile time:

SimpleMatrix<double, 4, 4> mat4x4;
DiagMatrix<double, 4, 4>  dmat4x4;
SimpleMatrix<double, 3, 2> mat3x2;
mat4x4 + dmat4x4  // statically proven correct; no runtime dimension checking
mat4x4 + mat3x2   // COMPILE ERROR: "template argument deduction/substitution failed"

The last line will produce a compiler error because the matrix dimensions don't meet the operator requirements (i.e., the dimensions don't match).

This code will defer to a runtime check, since the dimensions of some arguments cannot be deduced from their type:

SimpleMatrix<double, 4, 4> mat4x4;
SimpleMatrix<double, 0, 0> mat_a_NxN(4,4); // 4x4 matrix
SimpleMatrix<double, 0, 0> mat_b_NxN(3,2); // 3x2 matrix

mat4x4 + mat_a_NxN; // runtime dimension check; will succeed.
mat4x4 + mat_b_NxN; // runtime dimension check; will throw an exception.

Runtime dimension checks will throw either a DimensionMismatchException or NonsquareMatrixException on failure. Runtime checks can be disabled completely (at the hazard of introducing memory access violations and other bugs) by un-defining GEOMC_MTX_CHECK_DIMS in geomc_defs.h. Otherwise, a runtime dimension check will occurr if any checked dimension is dynamic.

Operators

Matrices support most basic arithmetic operators. We'll demonstrate with these example objects:

SimpleMatrix<double,3,3> m1;
SimpleMatrix<double,3,4> m2;
Vec<double,3> v;

Inter-matrix mult:

m1 * m2

Matrix-scalar mult:

m1 * 3
1.618 * m1

Matrix-vector mult:

m1 * v
v * m2

Matrix addition / subtraction:

m1 + m1
m1 - m1

Equality test:

m1 == m1
m1 != m2

Indexing:

double x = m1[1][2];
double y = m(2, 2);
m[0][1]  = 2.718;
m(0, 2)  = 1.618;

Accessing elements

Indexing:

// equivalent:
float f1 = m(2, 3);
float f2 = m[2][3];

Assignment:

// all equivalent:    
m.set(2, 3, val);
m[2][3] = val;
m(2, 3) = val;

Matrix body iterators:

typedef SimpleMatrix<double,3,3> mat3;
mat3 m;
// iterate over the matrix body in row-major order:
for (mat3::iterator i = m.begin(); i != m.end(); i++) {
    *i = ... ;
}

Matrix region iterators:

typedef SimpleMatrix<double,3,3> mat3;
mat3 m;
Rect2i r = Rect2i(Vec2i(1,1), Vec2i(3,2));
// iterate over the elements in region `r` in row-major order:
for (mat3::region_iterator i = m.region_begin(r); i != m.region_end(r); i++) {
    Vec2i c = i.point();
    *i = f(c, ...);
}

Function Documentation

◆ add() [1/2]

Matrix geom::add ( const Matrix1 &  a,
const Matrix2 &  b 
)

Matrix addition. Add the corresponding elements of a and b, whose dimensions must match.

Parameters
[in]aA matrix object
[in]bA matrix object
Returns
A new matrix containing a + b, usually a SimpleMatrix.

◆ add() [2/2]

void geom::add ( Matrix *  d,
const Matrix1 &  a,
const Matrix2 &  b 
)

Matrix addition. Add the corresponding elements of a and b, whose dimensions must match.

Parameters
[out]dA writeable matrix, whose dimensions must match a and b.
[in]aA matrix object
[in]bA matrix object

◆ det() [1/3]

T geom::det ( const DiagMatrix< T, N, M > &  m)

Compute the determinant of a diagonal matrix.

If the dimension of the matrix can be determined at compile time to be nonsquare, a static assertion is raised. Otherwise, nonsquare matrices will have determinant 0.

Parameters
mThe matrix whose determinant is to be computed.

◆ det() [2/3]

index_t geom::det ( const PermutationMatrix< N > &  m)
inline

Compute the determinant of a permutation matrix.

If the dimension of the matrix is dynamic and larger than 32, heap memory may be allocated as buffer space for the computation.

If it is desired that no heap memory be allocated, consider using permutation_sign(), which is equal to the permutation's determinant, and uses the permutation map (destructively) as a buffer for the calculation.

Parameters
mThe permutation matrix whose determinant is to be computed.

◆ det() [3/3]

T geom::det ( const SimpleMatrix< T, M, N, Lyt, P > &  m)
inline

Compute the determinant of a square matrix.

If the dimension of the matrix is larger than 6, heap memory may be allocated as buffer space for the computation.

If the dimension of the matrix can be determined at compile time to be nonsquare, a static assertion is raised. Otherwise, nonsquare matrices will have determinant 0.

Parameters
mThe square matrix whose determinant is to be computed.

◆ det_destructive()

T geom::det_destructive ( T *  m,
index_t  n 
)

Destructively compute the determinant of a square matrix.

The provided matrix will be used as a buffer for the computation, and no additional memory will be allocated. The contents of m after the function returns are undefined.

Note that it is generally preferable to use one of the detNxN methods if the dimension is small and known at compile time.

Parameters
mThe square matrix whose determinant is to be computed. m may be either row- or column- major layout. The contents of m will be overwritten.
nThe number of rows/columns in the matrix.

◆ detNxN()

T geom::detNxN ( const T *  m,
index_t  n 
)

Compute the determinant of a square matrix.

If n is larger than 8, heap memory may be allocated as buffer space for the computation. (To avoid heap allocation, use det_destructive() instead).

Parameters
mThe square matrix whose determinant is to be computed. m may be either row- or column- major layout.
nThe number of rows/columns in the matrix.

◆ inv() [1/2]

Matrix geom::inv ( const Matrix1 &  m,
bool *  success 
)

Matrix inversion.

Parameters
[in]mA square matrix.
[out]successWill be set to false if the matrix was singular and could not be inverted, otherwise will be set to true.
Returns
A new matrix containing the inverse of m, or undefined data if m could not be inverted.

◆ inv() [2/2]

bool geom::inv ( Matrix1 *  into,
const Matrix2 &  src 
)

Matrix inversion. src and into must be square matrices of the same dimension. If a runtime check for square dimensions fails, a NonsquareMatrixException is raised.

Parameters
[out]intoA writeable matrix with dimensions equal to src.
[in]srcA square matrix.
Returns
false if the matrix is singular and could not be inverted, true otherwise.

◆ inv2x2()

bool geom::inv2x2 ( T *  out,
const T  m[4] 
)

2 × 2 matrix inversion on a flat array.

out and m may alias each other. It is assumed that m and out have the same layout.

Parameters
outAn array with space for four elements to receive the computed inverse.
mThe 2 × 2 matrix to invert.
Returns
true if the matrix is invertible, false otherwise. If the matrix is not invertible, then out is unchanged.

◆ inv3x3()

bool geom::inv3x3 ( T *  out,
const T  m[9] 
)

3 × 3 matrix inversion on a flat array.

out and m may alias each other. It is assumed that m and out have the same layout.

Parameters
outAn array with space for nine elements to receive the computed inverse.
mThe 3 × 3 matrix to invert.
Returns
true if the matrix is invertible, false otherwise. If the matrix is not invertible, then out is unchanged.

◆ inv4x4()

bool geom::inv4x4 ( T *  out,
const T  m[16] 
)

4 × 4 matrix inversion on a flat array.

out and m may alias each other. It is assumed that m and out have the same layout.

Parameters
outAn array with space for 16 elements to receive the computed inverse.
mThe 4 × 4 matrix to invert.
Returns
true if the matrix is invertible, false otherwise. If the matrix is not invertible, then out is unchanged.

◆ invNxN() [1/2]

bool geom::invNxN ( T *  out,
T *  m 
)

N × N matrix inversion.

After the inversion, m will have undefined contents, regardless of whether it was invertible.

It is assumed that m and out have the same layout. m must not alias out.

This function uses a general inversion algorithm, and the specialized dimension-specific algorithms will be preferable when the dimension is known.

Template Parameters
NThe dimension N of the matrix.
Parameters
outAn array with space for N2 elements to receive the computed inverse.
mThe N × N matrix to invert.
Returns
true if the matrix is invertible, false otherwise. If the matrix is not invertible, then out is unchanged.

◆ invNxN() [2/2]

bool geom::invNxN ( T *  out,
T *  m,
index_t  n 
)

N × N matrix inversion on a flat array.

After the inversion, m will have undefined contents, regardless of whether it was invertible.

It is assumed that m and out have the same layout. m must not alias out.

This function uses a general inversion algorithm, and the specialized dimension-specific algorithms will be preferable when the dimension is known.

Parameters
outAn array with space for N2 elements to receive the computed inverse.
mThe N × N matrix to invert.
nThe dimension N of the matrix.
Returns
true if the matrix is invertible, false otherwise. If the matrix is not invertible, then out is unchanged.

◆ mtx_aliases_storage()

bool geom::mtx_aliases_storage ( const Ma &  a,
const Mb &  b 
)
inline

Do two matrices / vectors share storage?

In other words, might writing to one object change the contents of the other? Matrix1 and Matrix2 must be matrix or vector types.

Parameters
[in]aA matrix or vector object.
[in]bA matrix or vector object.
Returns
true if writing to a may alter b or vice versa; false otherwise.

◆ mtxcopy()

void geom::mtxcopy ( Matrix *  into,
const Matrix1 &  src 
)

Copy the contents of src into into. This function may be optimized to perform better than running std::copy() on some matrix types' iterators. It will also succeed in certain situations where std::copy() would fail (for example when copying the contents of a diagonal matrix to another, any attempt to write off the diagonal will cause an error).

Matrix and Matrix1 must be matrix or vector types whose dimensions match. If the dimensions can be determined to mismatch at compile-time, the program is considered invalid and the compilation will fail. If either object has dynamic size, the check will be deferred to runtime, throwing a DimensionMismatchException if the check fails.

Parameters
[out]intoA writeable matrix with dimensions matching src's.
[in]srcA matrix object.

◆ mul() [1/2]

Matrix geom::mul ( const Matrix1 &  a,
const Matrix2 &  b 
)

Matrix multiplication.

Matrix1 and Matrix2 may either be matrices or vectors. Left operands are, if vectors, assumed to be rows, while right operands will be treated as columns. The dimensions of a and b must satisfy (a x b) * (b x c). If the dimensions can be determined to mismatch at compile time, the program is considered invalid and will not compile. If either argument has dynamic size, the dimension check will be performed at runtime, raising a DimensionMismatchException if it fails.

The return type will be chosen appropriately based on the arguments, and will have dimension (a x c).

Parameters
[in]aA matrix object or vector with dimension (N x b.rows())
[in]bA matrix object or vector with dimension (a.cols() x N)
Returns
A new matrix or vector object containing the result of a * b.

◆ mul() [2/2]

Matrix & geom::mul ( Matrix *  into,
const Matrix1 &  a,
const Matrix2 &  b 
)

matrix * matrix and matrix * vector multiplication

This function handles all multiplication between any two types of matrix, or between any matrix and a vector. Thus, Ma, Mb, (the operands) and Md (the destination), may each be either a matrix or a vector. If the left operand is a vector, it is assumed to be a row vector, whereas right vector operands are column vectors.

This function ensures that all compile-time checkable dimensions agree– that is to say, that (a x b) * (b x c) = (a x c) holds. If any object has a dynamic size, then the check will be deferred to runtime, and a DimensionMismatchException thrown if the check fails. A compile-time dimension mismatch implies the program is invalid and compilation will error.

Parameters
[out]intoA writeable matrix or vector with dimensions (a.rows() x b.cols())
[in]aA matrix object or vector with dimension (N x b.rows())
[in]bA matrix object or vector with dimension (a.cols() x N)

◆ operator!=()

bool geom::operator!= ( const Matrix1 &  a,
const Matrix2 &  b 
)

matrix != matrix.

Matrices a and b are unequal unless a and b have the same dimension and all corresponding elements are equal.

◆ operator*() [1/3]

Matrix geom::operator* ( const Matrix &  m,
k 
)

matrix * scalar

All elements of m are multiplied by k. Matrix must be a matrix type, and U must satisfy std::is_scalar<U>.

◆ operator*() [2/3]

Matrix geom::operator* ( const Matrix1 &  a,
const Matrix2 &  b 
)

matrix * matrix

Matrix multiplication is performed on a and b.

Matrix1 and Matrix2 may either be matrices or vectors (but may not both be vectors; this is handled by a different multiplication operator). Left operands are, if vectors, assumed to be rows, while right operands will be treated as columns. The dimensions of a and b must satisfy (a x b) * (b x c). If the dimensions can be determined to mismatch at compile time, the program is considered invalid and will not compile. If either argument has dynamic size, the dimension check will be performed at runtime, raising a DimensionMismatchException if it fails.

The return type will be chosen appropriately based on the arguments, and will have dimension (a x c).

◆ operator*() [3/3]

Matrix geom::operator* ( k,
const Matrix &  m 
)

scalar * matrix

All elements of m are multiplied by k. Matrix must be a matrix type, and U must satisfy std::is_scalar<U>.

◆ operator*=()

Matrix & geom::operator*= ( Matrix &  m,
const DiagMatrix< T, M, N > &  d 
)

matrix *= diagonal matrix

A matrix m is multiplied by diagonal matrix d.

In this case, multiplication is efficient and memory allocation is avoided, even in the case where m and d alias.

◆ operator+()

Matrix geom::operator+ ( const Matrix1 &  a,
const Matrix2 &  b 
)

matrix + matrix

Add the elements of a and b. The return type will be chosen appropriately based on the arguments (usually a SimpleMatrix). Matrix1 and Matrix2 must both be matrix types.

◆ operator-()

Matrix geom::operator- ( const Matrix1 &  a,
const Matrix2 &  b 
)

matrix - matrix.

Subtract the elements of b from a. The return type will be chosen appropriately based on the arguments (usually a SimpleMatrix). Matrix1 and Matrix2 must both be matrix types.

◆ operator==()

bool geom::operator== ( const Matrix1 &  a,
const Matrix2 &  b 
)

matrix == matrix.

Matrices a and b are equal if and only if a and b have the same dimension and all corresponding elements are equal. Matrix1 and Matrix2 must both be matrix types.

◆ scale() [1/2]

void geom::scale ( Matrix1 *  d,
k,
const Matrix &  m 
)

Scalar muliplication on matrices. In other words, multiply all the elements of m by scalar value k.

Parameters
[out]dA writeable matrix, whose dimensions must match those of m.
[in]kScalar constant (whose type satisfies std::is_scalar<U>).
[in]mMatrix object to be scaled.

◆ scale() [2/2]

Matrix geom::scale ( k,
const Matrix &  m 
)

Scalar muliplication on matrices. In other words, multiply all the elements of m by scalar value k.

Parameters
[in]kScalar constant (whose type satisfies std::is_scalar<U>).
[in]mMatrix object to be scaled.
Returns
A scaled copy of m.

◆ sub() [1/2]

Matrix geom::sub ( const Matrix1 &  a,
const Matrix2 &  b 
)

Matrix subtraction. Subtract the corresponding elements of b from a's. The dimensions of a and b must match.

Parameters
[in]aMatrix object
[in]bMatrix object
Returns
A new matrix containing a - b, usually a SimpleMatrix.

◆ sub() [2/2]

void geom::sub ( Matrix *  d,
const Matrix1 &  a,
const Matrix2 &  b 
)

Matrix subtraction. Subtract the corresponding elements of b from a's. The dimensions of a and b must match.

Parameters
[out]dA writeable matrix, whose dimensions must match a and b.
[in]aA matrix object
[in]bA matrix object

◆ transpose() [1/2]

Matrix geom::transpose ( const Matrix1 &  m)

Matrix transpose.

Parameters
[in]mA matrix object.
Returns
A transposed copy of m, of type appropriate for the argument, usually a SimpleMatrix.

◆ transpose() [2/2]

void geom::transpose ( Matrix1 *  into,
const Matrix2 &  m 
)

Matrix transpose.

Parameters
[out]intoA writeable matrix with dimensions (m.cols(), m.rows())
[in]mA matrix object