Core 1.0
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Attributes | Friends | List of all members
Matrix Class Reference

This class implements 32 matrix. More...

#include <matrix.h>

Public Member Functions

 Matrix ()
 Empty.
 
 Matrix (const double &)
 Creates a diagonal matrix. More...
 
 Matrix (const Vector &)
 Create a diagonal matrix with diagonal terms set to the vector entries. More...
 
 Matrix (const Vector &, const Vector &, const Vector &)
 Creates a matrix given column vectors. More...
 
 Matrix (const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &)
 Creates a matrix with a set of double values. More...
 
constexpr double & operator[] (int)
 Direct access to the array of the matrix.
 
constexpr double operator[] (int) const
 Overloaded.
 
constexpr double & operator() (int, int)
 Get element (i,j) of the matrix. More...
 
constexpr double operator() (int, int) const
 Overloaded.
 
Vector C (int) const
 Get the i-th column vector of the matrix. More...
 
Vector Row (int) const
 Get the i-th row vector of the matrix. More...
 
Matrix operator- () const
 Returns the opposite of a matrix -A.
 
Matrixoperator+= (const Matrix &)
 Destructive addition operator.
 
Matrixoperator-= (const Matrix &)
 Destructive subtraction operator.
 
Matrixoperator*= (const Matrix &)
 Destructive multiplication.
 
Matrixoperator*= (double)
 Destructive multiplication operator.
 
Matrixoperator/= (double)
 Destructive division operator.
 
Vector operator* (const Vector &) const
 Right multiply by a vector. More...
 
Vector GetRotationAngles () const
 Compute the angles of the rotation matrix.
 
Matrix Abs () const
 Compute the absolute value of the matrix.
 
Matrix T () const
 Transpose the matrix. More...
 
Matrix Adjoint () const
 Compute the adjoint, i.e, the comatrix of the matrix. More...
 
double Determinant () const
 Computes the determinant of the matrix.
 
double Trace () const
 Compute the trace (sum of diagonal terms) of a matrix.
 
Cubic Characteristic () const
 Compute the characteristic polynomial.
 
double SpectralNorm () const
 Computes the spectral norm of the matrix.
 
double InfinityNorm () const
 Compute the infinity norm of the matrix.
 
double FrobeniusNorm () const
 Compute the Frobenius norm of the matrix.
 
void SingularValueDecomposition (Matrix &, Vector &, Matrix &) const
 Decompose a matrix into singular values. More...
 
void QDU (Matrix &, Vector &, Vector &) const
 Perform the QDU decomposition of a matrix. More...
 
void ExtractAngleAxis (Vector &, double &) const
 Given a rotation matrix, extract its defining axis and its angle. More...
 
void EigenSolveSymmetric (double[3], Vector[3]) const
 Find the eigen values of the matrix. More...
 
Vector Eigen () const
 Find the eigen values of the matrix. More...
 
void Tridiagonal (double[3], double[2])
 Compute the Householder reduction of a symmetric matrix. More...
 
int QLAlgorithm (double[3], double[3])
 iteration with implicit shifting to reduce matrix from tridiagonal to diagonal More...
 

Static Public Member Functions

static Matrix Rotation (const Vector &)
 Create a rotation matrix given a vector of angles that specifies the rotation around each world coordinate axis. More...
 
static Matrix RotationMaya (const Vector &)
 Create a rotation matrix given a vector of angles that specifies the rotation around each world coordinate axis. More...
 
static Matrix Rotation (const Vector &, const double &)
 Create a rotation matrix about an arbitrary axis. More...
 
static Matrix Rotation (const Vector &, const Vector &)
 Create a rotation matrix that rotates a normalized vector into another one. More...
 
static Matrix RotationCanonical (const Vector &)
 Create a rotation matrix that rotates (0,0,1) to a normalized vector. More...
 
static Matrix RotationX (const double &)
 Create a rotation matrix around the x-axis. More...
 
static Matrix RotationY (const double &)
 Create a rotation matrix around the y-axis. More...
 
static Matrix RotationZ (const double &)
 Create a rotation matrix around the z-axis. More...
 
static Matrix Symmetry (const Vector &)
 Create a planar symmetry matrix. More...
 
static Matrix Covariance (Vector *, int)
 Compute the covariance matrix of a point cloud. More...
 
static Matrix Lerp (const double &, const Matrix &, const Matrix &)
 Spherical interpolation of two rotation matrices. More...
 
static Matrix Rotation (RandomFast &=random239)
 Generate uniformly distributed random rotation matrix. More...
 
static Matrix Frenet (const Vector &, const Vector &=Vector::Z)
 Compute the Frenet frame. More...
 

Static Public Attributes

static const Matrix Null
 Null matrix.
 
static const Matrix Identity
 Identity matrix.
 

Protected Attributes

double r [9] = { 1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0 }
 The array storing the coefficients of the matrix.
 

Friends

Matrix operator+ (const Matrix &, const Matrix &)
 Overloaded.
 
Matrix operator- (const Matrix &, const Matrix &)
 Overloaded.
 
Matrix operator* (const Matrix &, const Matrix &)
 Multiplication. More...
 
Matrix operator* (const Matrix &, const double &)
 Right multiply by a double.
 
Matrix operator* (const double &, const Matrix &)
 Left multiply by a double.
 
Matrix operator/ (const Matrix &, const double &)
 Right divide by a double.
 
Matrix operator/ (const double &, const Matrix &)
 Computes the inverse matrix and scales it by a double. More...
 
std::ostream & operator<< (std::ostream &, const Matrix &)
 Overloaded. More...
 
Matrix Inverse (const Matrix &)
 Compute the inverse of a matrix A-1. More...
 

Detailed Description

This class implements 32 matrix.

Operators have been overloaded so as to behave as expected with the Vector class.

Many functions have been coded inline out of efficiency.

The constructors of this class are used to create matrices in the general case. See the static member functions Matrix::Rotation() to create different kinds of rotation matrices. Canonical symmetry matrices should be created as follows:

Matrix planar=Matrix(Vector(1.0,-1.0,1.0)); // Symmetry, plane y=0
Matrix origin=Matrix(Vector(-1.0,-1.0,-1.0)); // Symmetry around origin
This class implements 32 matrix.
Definition: matrix.h:14
Matrix()
Empty.
Definition: matrix.h:19
Vectors in three dimensions.
Definition: evector.h:21

More general symmetries can be created using static member functions:

Matrix planar=Matrix::Symmetry(Vector(2.0,-3.0,1.0)); // Symmetry, plane with normal vector as argument
static Matrix Symmetry(const Vector &)
Create a planar symmetry matrix.
Definition: matrix.cpp:185

Components are stored in a single dimension array, sorting components by column.

Constructor & Destructor Documentation

◆ Matrix() [1/4]

Matrix::Matrix ( const double &  a)
inlineexplicit

Creates a diagonal matrix.

Parameters
aThe diagonal value.

◆ Matrix() [2/4]

Matrix::Matrix ( const Vector a)
inlineexplicit

Create a diagonal matrix with diagonal terms set to the vector entries.

Parameters
aVector of diagonal values.

◆ Matrix() [3/4]

Matrix::Matrix ( const Vector a,
const Vector b,
const Vector c 
)
inlineexplicit

Creates a matrix given column vectors.

Parameters
a,b,cColumn vectors.

◆ Matrix() [4/4]

Matrix::Matrix ( const double &  a00,
const double &  a01,
const double &  a02,
const double &  a10,
const double &  a11,
const double &  a12,
const double &  a20,
const double &  a21,
const double &  a22 
)
inlineexplicit

Creates a matrix with a set of double values.

Coefficients are given in column order.

Member Function Documentation

◆ Adjoint()

Matrix Matrix::Adjoint ( ) const

Compute the adjoint, i.e, the comatrix of the matrix.

See also
Inverse()

◆ C()

Vector Matrix::C ( int  i) const
inline

Get the i-th column vector of the matrix.

Parameters
iIndex.

◆ Covariance()

Matrix Matrix::Covariance ( Vector p,
int  n 
)
static

Compute the covariance matrix of a point cloud.

This matrix is useful for computing the principal directions of a point of clouds. The principal directions are defined as the eigen vectors of the matrix.

See also
EigenSolveSymmetric
Parameters
pArray of vertices.
nNumber of vertices.

◆ Eigen()

Vector Matrix::Eigen ( ) const

Find the eigen values of the matrix.

Eigen values are sorted and returned as a vector.

◆ EigenSolveSymmetric()

void Matrix::EigenSolveSymmetric ( double  a[3],
Vector  e[3] 
) const

Find the eigen values of the matrix.

Return eigen vectors as well.

Parameters
aAn array of three eigen values.
eThe array containing the three eigen vectors.

◆ ExtractAngleAxis()

void Matrix::ExtractAngleAxis ( Vector v,
double &  angle 
) const

Given a rotation matrix, extract its defining axis and its angle.

This is the same as computing its quaternion representation.

Parameters
angleAngle.
vVector.

Let (x,y,z) the unit-length axis and let a an angle of rotation. The rotation matrix is R=I+sin(a).P+(1-cos(A)).P2 where I is the identity and P a 0 diagonal matrix with (x,y,z) located at theit right index locations.

Some algebra will show that:
cos(a)=(trace(R)-1)/2 and R-Rt=2.sin(a).P
In the event that a=π, R-Rt=0 which prevents us from extracting the axis through P. Instead note that R=I+2.P2 when a=π, so P2=(R-I)/2. The diagonal entries of P2 are x2-1, y2-1, and z2-1. We can solve these for axis (x,y,z). Because the angle is π, it does not matter which sign you choose on the square roots.

◆ Frenet()

Matrix Matrix::Frenet ( const Vector t,
const Vector u = Vector::Z 
)
static

Compute the Frenet frame.

Given tangent t and up vector u, computes the matrix (t,t^u,u)). This function stores the tangent, the binormal and the normal vectors in the matrix. The normalization of the vectors is performed internally.

Parameters
tTangent, which need not be normalized.
uUp vector.

◆ Lerp()

Matrix Matrix::Lerp ( const double &  t,
const Matrix a,
const Matrix b 
)
static

Spherical interpolation of two rotation matrices.

This is an expensive function that first converts rotations to quaternions, then interpolates the quaternions, before finaly reconstructing the corresponding matrix.

See also
Quaternion Lerp(const double&, const Quaternion&, const Quaternion&);
Parameters
tInterpolation parameter.
a,bRotation matrix.

◆ operator()()

constexpr double & Matrix::operator() ( int  i,
int  j 
)
inlineconstexpr

Get element (i,j) of the matrix.

Parameters
iRow.
jColumn.

◆ operator*()

Vector Matrix::operator* ( const Vector v) const
inline

Right multiply by a vector.

Right multiplication by a vector is a member function and not a friend to avoid left/right multiplication ambiguities since matrix multiplication is not commutative.

Parameters
vVector.
static const double Pi
π.
Definition: mathematics.h:174
static Matrix Rotation(const Vector &)
Create a rotation matrix given a vector of angles that specifies the rotation around each world coord...
Definition: matrix.cpp:117

◆ QDU()

void Matrix::QDU ( Matrix a,
Vector D,
Vector U 
) const

Perform the QDU decomposition of a matrix.

Q is orthogonal, D is diagonal, and U is upper triangular with ones on its diagonal.

The algorithm uses Gram-Schmidt orthogonalization (the QR algorithm).

◆ QLAlgorithm()

int Matrix::QLAlgorithm ( double  diag[3],
double  subd[3] 
)

iteration with implicit shifting to reduce matrix from tridiagonal to diagonal

Parameters
diagDiagonal elements.
subdTriangular sub-matrix.

◆ Rotation() [1/4]

Matrix Matrix::Rotation ( const Vector v)
static

Create a rotation matrix given a vector of angles that specifies the rotation around each world coordinate axis.

Rotations are concatenated, starting by rotating around the z axis, then y and eventually x.

Parameters
vVector of angles in radian.

◆ Rotation() [2/4]

Matrix Matrix::Rotation ( const Vector u,
const double &  a 
)
static

Create a rotation matrix about an arbitrary axis.

Matrix R=Matrix::Rotation(Normalized(Vector(1,2,-1)),Math::DegreeToRadian(30.0));
static constexpr double DegreeToRadian(const double &)
Convert degrees to randians.
Definition: mathematics.h:478
Parameters
uRotation axis, which should be unit.
aAngle, in radian.

◆ Rotation() [3/4]

Matrix Matrix::Rotation ( const Vector a,
const Vector b 
)
static

Create a rotation matrix that rotates a normalized vector into another one.

Parameters
a,bInitial and final vector (should be normalized).

◆ Rotation() [4/4]

Matrix Matrix::Rotation ( RandomFast ra = random239)
static

Generate uniformly distributed random rotation matrix.

See Fast Random Rotation Matrices, J. Arvo. Graphics Gems III, 1992.

Parameters
raFast random number generator.

◆ RotationCanonical()

Matrix Matrix::RotationCanonical ( const Vector v)
static

Create a rotation matrix that rotates (0,0,1) to a normalized vector.

This is the same as:

double r[9]
The array storing the coefficients of the matrix.
Definition: matrix.h:16
static const Vector Z
Vector(0,0,1).
Definition: evector.h:142
Parameters
vFinal vector (should be normalized).

◆ RotationMaya()

Matrix Matrix::RotationMaya ( const Vector v)
static

Create a rotation matrix given a vector of angles that specifies the rotation around each world coordinate axis.

Rotations are concatenated, starting by rotating around the z axis, then y and eventually x.

Parameters
vVector of angles in radian.

◆ RotationX()

Matrix Matrix::RotationX ( const double &  a)
static

Create a rotation matrix around the x-axis.

This static member function is provided out of efficiency as it is much faster than any other general Matrix::Rotation() member.

Parameters
aAngle (in radian).

◆ RotationY()

Matrix Matrix::RotationY ( const double &  a)
static

Create a rotation matrix around the y-axis.

See also
Matrix::RotationX(const double&)
Parameters
aAngle (in radian).

◆ RotationZ()

Matrix Matrix::RotationZ ( const double &  a)
static

Create a rotation matrix around the z-axis.

See also
Matrix::RotationX(const double&)
Parameters
aAngle (in radian).

◆ Row()

Vector Matrix::Row ( int  i) const
inline

Get the i-th row vector of the matrix.

Parameters
iIndex.

◆ SingularValueDecomposition()

void Matrix::SingularValueDecomposition ( Matrix L,
Vector S,
Matrix R 
) const

Decompose a matrix into singular values.

The input matrix M will be equal to M = L S R.

Parameters
LLower matrix.
SSingular values.
RMatrix.

◆ Symmetry()

Matrix Matrix::Symmetry ( const Vector n)
static

Create a planar symmetry matrix.

Parameters
nNormal to the plane.

◆ T()

Matrix Matrix::T ( ) const
inline

Transpose the matrix.

Note that if the matrix is a rotation matrix, then the transpose is equal to its inverse.

See also
T(const Matrix&)

◆ Tridiagonal()

void Matrix::Tridiagonal ( double  diag[3],
double  subd[2] 
)

Compute the Householder reduction of a symmetric matrix.

Decompose a matrix A into Qt M Q.

Parameters
diagDiagonal entries of A.
subdSub-diagonal entries of A.

Friends And Related Function Documentation

◆ Inverse

Matrix Inverse ( const Matrix A)
friend

Compute the inverse of a matrix A-1.

Recall that A-1 can be defined as the transposed adjoint matrix divided by the determinant.

This function returns the null matrix if A cannot be inversed.

The threshold value involved in the singular matrix detection is set to 10-18.

Parameters
AMatrix.

◆ operator*

Matrix operator* ( const Matrix u,
const Matrix v 
)
friend

Multiplication.

Parameters
u,vInput matrices.

◆ operator/

Matrix operator/ ( const double &  x,
const Matrix a 
)
friend

Computes the inverse matrix and scales it by a double.

This function calls Matrix::Inverse().

Parameters
xReal.
aMatrix.

◆ operator<<

std::ostream & operator<< ( std::ostream &  s,
const Matrix matrix 
)
friend

Overloaded.

Parameters
sStream.
matrixThe matrix.