SquareMatrix.h

Classes

SquareMatrix -- Fast Square Matrix class with fixed (templated) size (full description)
Global Functions -- Various global math and IO functions. (full description)

template <class T, Int n> class SquareMatrix

Types

enum

General
Diagonal
ScalarId

Interface

friend RigidVector<Complex,4> operator*(const SquareMatrix<Complex,4>& m, const RigidVector<Float,4>& v)
friend SquareMatrix<Complex,4>& directProduct(SquareMatrix<Complex,4>& result, const SquareMatrix<Complex,2>& left, const SquareMatrix<Complex,2>& right)
Public Members
~SquareMatrix()
SquareMatrix() : type_p(ScalarId)
SquareMatrix(int itype) : type_p(itype)
SquareMatrix(const SquareMatrix<T,n>& m)
SquareMatrix(const T a[n][n])
SquareMatrix(const Matrix<T>& mat)
SquareMatrix(const T vec[n])
SquareMatrix(const Vector<T>& vec)
SquareMatrix(const T& scalar) : type_p(ScalarId)
SquareMatrix<T,n>& operator=(const SquareMatrix<T,n>& m)
SquareMatrix<T,n>& operator=(const T a[n][n])
SquareMatrix<T,n>& operator=(const n<T>& m)
SquareMatrix<T,n>& operator=(const T vec[n])
SquareMatrix<T,n>& operator=(const n<T>& v)
SquareMatrix<T,n>& operator=(T val)
SquareMatrix<T,n>& operator+=(const SquareMatrix<T,n>& other)
SquareMatrix<T,n>& operator*=(const SquareMatrix<T,n>& other)
SquareMatrix<T,n>& operator*=(Float f)
T operator()(Int i, Int j) const
T& operator()(Int i, Int j)
SquareMatrix<T,n>& conj()
SquareMatrix<T,n>& adjoint()
SquareMatrix<T,n>& conj(SquareMatrix<T,n>& result)
SquareMatrix<T,n>& adjoint(SquareMatrix<T,n>& result)
SquareMatrix<T,n>& inverse(SquareMatrix<T,n>& result) const
SquareMatrix<T,n> inverse() const
Matrix<T>& matrix(Matrix<T>& result) const
Matrix<T> matrix() const
Private Members
T& throwInvAccess()

Description

Review Status

Date Reviewed:
yyyy/mm/dd

Prerequisite

Etymology

SquareMatrix is a specialized class for small (<5x5) square matrices.

Synopsis

SquareMatrix provides operations similar to the Matrix class, but it is much faster for small arrays. One important difference is that operators *= and * do matrix products for SquareMatrices instead of element by element multiplication. SquareMatrices also optimize operations internally for scalar identity matrices (diagonal matrix with all elements equal) and diagonal matrices. The different types of SquareMatrix are created by constructors and operator= taking either a scalar, a vector or a full matrix.

Example

    // create two SquareMatrices
    SquareMatrix<Float,2> sm1(3.0); // a scalar identity matrix
    Vector<Float> vec(2); vec(0)=2.0; vec(1)=3.0;
    SquareMatrix<Float,2> sm2(vec); // a diagonal matrix
    // multiply the matrices
    // Note: A*=B is equivalent to A=A*B where '*' is matrix multiplication
    sm1*=sm2; // sm1 now diagonal
    

Motivation

The basic Matrix classes are rather inefficient for small sizes, new and delete tend to dominate the execution time for computationally intensive code. The SquareMatrix classes circumvent this by having a compile-time fixed size c-array internally. The SquareMatrix class have fixed zero origin and no increments, this allows fast indexing, copying and math operations. As mentioned in the synopsis, the SquareMatrix classes also avoid unnecessary operations for simple matrices (scalar-identity and diagonal).

Template Type Argument Requirements (T)

Thrown Exceptions

To Do

Member Description

friend RigidVector<Complex,4> operator*(const SquareMatrix<Complex,4>& m, const RigidVector<Float,4>& v)

friend class SquareMatrix;// Sun native does not accept this friend class SquareMatrix; // for directProduct of 2x2 Global friend function for product of Complex matrix and Float 4-vector

friend SquareMatrix<Complex,4>& directProduct(SquareMatrix<Complex,4>& result, const SquareMatrix<Complex,2>& left, const SquareMatrix<Complex,2>& right)

Global friend function to calculate direct product

enum

Enum used internally to optimize operations.

~SquareMatrix()

Destructor

SquareMatrix() : type_p(ScalarId)

Default constructor - creates a unity matrix at present, this may not be what we want (non-intuitive?)

SquareMatrix(int itype) : type_p(itype)

Create a matrix of a given type, no initialization

SquareMatrix(const SquareMatrix<T,n>& m)

Copy construct a SquareMatrix, a true copy is made.

SquareMatrix(const T a[n][n])

Construct from c-style matrix (by copying elements).

SquareMatrix(const Matrix<T>& mat)

Construct from Matrix.

SquareMatrix(const T vec[n])

Construct from c-style vector, creates a diagonal matrix.

SquareMatrix(const Vector<T>& vec)

Construct from Vector, creates a diagonal matrix.

SquareMatrix(const T& scalar) : type_p(ScalarId)

Construct from scalar, creates a scalar-identity matrix

SquareMatrix<T,n>& operator=(const SquareMatrix<T,n>& m)

Assignment, uses copy semantics.

SquareMatrix<T,n>& operator=(const T a[n][n])

Assign a c-style matrix, creates a general matrix.

SquareMatrix<T,n>& operator=(const n<T>& m)

Assign a Matrix, creates a general matrix.

SquareMatrix<T,n>& operator=(const T vec[n])

Assign a c-style vector, creates a diagonal matrix

SquareMatrix<T,n>& operator=(const n<T>& v)

Assign a Vector, creates a diagonal matrix

SquareMatrix<T,n>& operator=(T val)

Assign a scalar, creates a scalar-identity matrix

SquareMatrix<T,n>& operator+=(const SquareMatrix<T,n>& other)

Add two SquareMatrices, element by element.

SquareMatrix<T,n>& operator*=(const SquareMatrix<T,n>& other)

Matrix product of 'this' SquareMatrix with other, i.e., A*=B; is equivalent with A=A*B where '*' is matrix multiplication.

SquareMatrix<T,n>& operator*=(Float f)

Scalar multiplication

T operator()(Int i, Int j) const

Indexing, only const indexing is allowed. You cannot change the matrix via indexing. No bounds checking.

T& operator()(Int i, Int j)

Non const indexing, throws exception if you try to change an element which would require a type change of the matrix

SquareMatrix<T,n>& conj()

For a SquareMatrix<Complex,n>: set the argument result to the real part of the matrix (and return result by reference to allow use in expressions without creating temporary). For a SquareMatrix<Complex,n>: return the real part of the matrix. Conjugate the matrix in place(!).

SquareMatrix<T,n>& adjoint()

Tranpose and conjugate the matrix in place(!).

SquareMatrix<T,n>& conj(SquareMatrix<T,n>& result)

Conjugate the matrix, return it in result (and by ref)

SquareMatrix<T,n>& adjoint(SquareMatrix<T,n>& result)

Tranpose and conjugate the matrix, return it in result (and by ref)

SquareMatrix<T,n>& inverse(SquareMatrix<T,n>& result) const

Compute the inverse of the matrix and return it in result (also returns result by reference).

SquareMatrix<T,n> inverse() const

Return the inverse of the matrix by value.

Matrix<T>& matrix(Matrix<T>& result) const

Assign 'this' to the Matrix result, also return result by reference.

Matrix<T> matrix() const

Convert the SquareMatrix to a Matrix.

T& throwInvAccess()


Various global math and IO functions. (source)

Interface

SquareMatrix<Complex,4> directProduct(const SquareMatrix<Complex,2>& left, const SquareMatrix<Complex,2>& right)
SquareMatrix<Complex,2> conj(const SquareMatrix<Complex,2>& m)
SquareMatrix<Complex,4> conj(const SquareMatrix<Complex,4>& m)
SquareMatrix<Complex,2> adjoint(const SquareMatrix<Complex,2>& m)
SquareMatrix<Complex,4> adjoint(const SquareMatrix<Complex,4>& m)
ostream& operator<<(ostream& os, const SquareMatrix<Complex,2>& m)
ostream& operator<<(ostream& os, const SquareMatrix<Complex,4>& m)
ostream& operator<<(ostream& os, const SquareMatrix<Float,2>& m)
ostream& operator<<(ostream& os, const SquareMatrix<Float,4>& m)

Description

Member Description

SquareMatrix<Complex,4> directProduct(const SquareMatrix<Complex,2>& left, const SquareMatrix<Complex,2>& right)

Calculate direct product of two SquareMatrices.

SquareMatrix<Complex,2> conj(const SquareMatrix<Complex,2>& m)

Return conjugate of SquareMatrix.

SquareMatrix<Complex,4> conj(const SquareMatrix<Complex,4>& m)

Return conjugate of SquareMatrix.

SquareMatrix<Complex,2> adjoint(const SquareMatrix<Complex,2>& m)

Return adjoint of SquareMatrix.

SquareMatrix<Complex,4> adjoint(const SquareMatrix<Complex,4>& m)

Return adjoint of SquareMatrix.

ostream& operator<<(ostream& os, const SquareMatrix<Complex,2>& m)

Write SquareMatrix to output, uses Matrix to do the work.

ostream& operator<<(ostream& os, const SquareMatrix<Complex,4>& m)

ostream& operator<<(ostream& os, const SquareMatrix<Float,2>& m)

ostream& operator<<(ostream& os, const SquareMatrix<Float,4>& m)