Support Forum G3D Web Page |
N x M matrix.
More...
Classes | |
class | Impl |
Used internally by Matrix. More... | |
Public Types | |
typedef float | T |
Internal precision. More... | |
Public Member Functions | |
Matrix () | |
Matrix (const Matrix3 &M) | |
Matrix (const Matrix4 &M) | |
Matrix (int R, int C) | |
Returns a new matrix that is all zero. More... | |
Matrix | abs () const |
void | abs (Matrix &out) const |
Matrix | adjoint () const |
bool | allNonZero () const |
Returns true if all elements are non-zero. More... | |
bool | allZero () const |
bool | anyNonZero () const |
Returns true if any element is non-zero. More... | |
bool | anyZero () const |
Matrix | arrayCos () const |
void | arrayCos (Matrix &out) const |
void | arrayDivInPlace (const Matrix &B) |
Mutates this. More... | |
void | arrayExp (Matrix &out) const |
Matrix | arrayExp () const |
Matrix | arrayLog () const |
void | arrayLog (Matrix &out) const |
Matrix | arrayMul (const Matrix &B) const |
void | arrayMulInPlace (const Matrix &B) |
Mutates this. More... | |
Matrix | arraySin () const |
void | arraySin (Matrix &out) const |
Matrix | arraySqrt () const |
void | arraySqrt (Matrix &out) const |
Matrix | col (int c) const |
int | cols () const |
Number of columns. More... | |
T | determinant () const |
Matrix | gaussJordanPseudoInverse () const |
(ATA)-1AT) computed using Gauss-Jordan elimination. More... | |
T | get (int r, int c) const |
Matrix | inverse () const |
A-1 computed using the Gauss-Jordan algorithm, for square matrices. More... | |
Matrix | lsub (const T &B) const |
scalar B - this More... | |
void | mulRow (int r, const T &v) |
void | negate (Matrix &out) const |
Matrix | negate () const |
double | norm () const |
2-norm (sqrt(sum(squares)) More... | |
double | normSquared () const |
2-norm squared: sum(squares). More... | |
int | numElements () const |
Matrix | operator!= (const T &scalar) const |
Matrix | operator* (const Matrix &B) const |
Matrix multiplication. More... | |
Matrix | operator* (const T &B) const |
See also A *= B, which is more efficient in many cases. More... | |
Matrix & | operator*= (const T &B) |
Generally more efficient than A * B. More... | |
Matrix & | operator*= (const Matrix &B) |
No performance advantage over A * B because matrix multiplication requires intermediate storage. More... | |
Matrix | operator+ (const Matrix &B) const |
See also A += B, which is more efficient in many cases. More... | |
Matrix | operator+ (const T &v) const |
See also A += B, which is more efficient in many cases. More... | |
Matrix & | operator+= (const T &B) |
Generally more efficient than A + B. More... | |
Matrix & | operator+= (const Matrix &B) |
Generally more efficient than A + B. More... | |
Matrix | operator- (const Matrix &B) const |
See also A -= B, which is more efficient in many cases. More... | |
Matrix | operator- (const T &v) const |
See also A -= B, which is more efficient in many cases. More... | |
Matrix | operator- () const |
Matrix & | operator-= (const T &B) |
Generally more efficient than A - B. More... | |
Matrix & | operator-= (const Matrix &B) |
Generally more efficient than A - B. More... | |
Matrix & | operator/= (const T &B) |
Generally more efficient than A / B. More... | |
Matrix | operator< (const T &scalar) const |
Matrix | operator<= (const T &scalar) const |
Matrix | operator== (const T &scalar) const |
Matrix | operator> (const T &scalar) const |
Matrix | operator>= (const T &scalar) const |
Matrix | pseudoInverse (float tolerance=-1) const |
Computes the Moore-Penrose pseudo inverse, equivalent to (ATA)-1AT). More... | |
Matrix | row (int r) const |
int | rows () const |
The number of rows. More... | |
void | serialize (TextOutput &t) const |
Serializes in Matlab source format. More... | |
void | set (int r, int c, T v) |
void | setCol (int c, const Matrix &vec) |
void | setRow (int r, const Matrix &vec) |
Vector2int16 | size () const |
Matrix | subMatrix (int r1, int r2, int c1, int c2) const |
Returns a new matrix that is a subset of this one, from r1:r2 to c1:c2, inclusive. More... | |
void | svd (Matrix &U, Array< T > &d, Matrix &V, bool sort=true) const |
Singular value decomposition. More... | |
Matrix | svdPseudoInverse (float tolerance=-1) const |
Called from pseudoInverse when the matrix has size > 4 along some dimension. More... | |
void | swapAndNegateCols (int c0, int c1) |
Swaps columns c0 and c1 and negates both. More... | |
void | swapRows (int r0, int r1) |
Matrix3 | toMatrix3 () const |
Matrix4 | toMatrix4 () const |
String | toString (const String &name) const |
String | toString () const |
Vector2 | toVector2 () const |
Vector3 | toVector3 () const |
Vector4 | toVector4 () const |
Matrix | transpose () const |
AT More... | |
void | transpose (Matrix &out) const |
Transpose in place; more efficient than transpose. More... | |
Static Public Member Functions | |
template<class S > | |
static Matrix | fromDiagonal (const Array< S > &d) |
static Matrix | fromDiagonal (const Matrix &d) |
static Matrix | identity (int N) |
Returns a new identity matrix. More... | |
static Matrix | one (int R, int C) |
Returns a new matrix that is all one. More... | |
static Matrix | random (int R, int C) |
Uniformly distributed values between zero and one. More... | |
static const char * | svdCore (float **U, int rows, int cols, float *D, float **V) |
Low-level SVD functionality. More... | |
static Matrix | zero (int R, int C) |
Returns a new matrix that is all zero. More... | |
Static Public Attributes | |
static int | debugNumAllocOps |
Incremented every time a new matrix object is allocated. More... | |
static int | debugNumCopyOps |
Incremented every time the elements of a matrix are copied. More... | |
N x M matrix.
The actual data is tracked internally by a reference counted pointer; it is efficient to pass and assign Matrix objects because no data is actually copied. This avoids the headache of pointers and allows natural math notation:
Matrix A, B, C; // ...
C = A * f(B); C = C.inverse();
A = Matrix::identity(4); C = A; C.set(0, 0, 2.0); // Triggers a copy of the data so that A remains unchanged.
// etc.
The Matrix::debugNumCopyOps and Matrix::debugNumAllocOps counters increment every time an operation forces the copy and allocation of matrices. You can use these to detect slow operations when efficiency is a major concern.
Some methods accept an output argument instead of returning a value. For example, A = B.transpose()
can also be invoked as B.transpose(A)
. The latter may be more efficient, since Matrix may be able to re-use the storage of A (if it has approximatly the right size and isn't currently shared with another matrix).
typedef float G3D::Matrix::T |
Internal precision.
Currently float, but this may become a templated class in the future to allow operations like Matrix<double> and Matrix<ComplexFloat>.
Not necessarily a plain-old-data type (e.g., could ComplexFloat), but must be something with no constructor, that can be safely memcpyd, and that has a bit pattern of all zeros when zero.
|
inline |
Referenced by adjoint(), inverse(), and transpose().
|
inline |
|
inline |
|
inline |
Returns a new matrix that is all zero.
void G3D::Matrix::abs | ( | Matrix & | out | ) | const |
|
inline |
bool G3D::Matrix::allNonZero | ( | ) | const |
Returns true if all elements are non-zero.
Referenced by anyZero().
|
inline |
bool G3D::Matrix::anyNonZero | ( | ) | const |
Returns true if any element is non-zero.
Referenced by allZero().
|
inline |
|
inline |
void G3D::Matrix::arrayCos | ( | Matrix & | out | ) | const |
void G3D::Matrix::arrayDivInPlace | ( | const Matrix & | B | ) |
Mutates this.
void G3D::Matrix::arrayExp | ( | Matrix & | out | ) | const |
|
inline |
|
inline |
void G3D::Matrix::arrayLog | ( | Matrix & | out | ) | const |
void G3D::Matrix::arrayMulInPlace | ( | const Matrix & | B | ) |
Mutates this.
void G3D::Matrix::arraySin | ( | Matrix & | out | ) | const |
|
inline |
void G3D::Matrix::arraySqrt | ( | Matrix & | out | ) | const |
|
inline |
Matrix G3D::Matrix::col | ( | int | c | ) | const |
|
inline |
Number of columns.
Referenced by adjoint(), numElements(), size(), and transpose().
|
inline |
|
inline |
(ATA)-1AT) computed using Gauss-Jordan elimination.
T G3D::Matrix::get | ( | int | r, |
int | c | ||
) | const |
|
static |
Returns a new identity matrix.
|
inline |
A-1 computed using the Gauss-Jordan algorithm, for square matrices.
Run time is O(R3), where R is the number of rows.
scalar B - this
Referenced by operator-().
void G3D::Matrix::mulRow | ( | int | r, |
const T & | v | ||
) |
|
inline |
Referenced by operator-().
void G3D::Matrix::negate | ( | Matrix & | out | ) | const |
double G3D::Matrix::norm | ( | ) | const |
2-norm (sqrt(sum(squares))
double G3D::Matrix::normSquared | ( | ) | const |
2-norm squared: sum(squares).
(i.e., dot product with itself)
|
inline |
|
static |
Returns a new matrix that is all one.
Matrix multiplication.
To perform element-by-element multiplication, see arrayMul.
See also A *= B, which is more efficient in many cases.
No performance advantage over A * B because matrix multiplication requires intermediate storage.
See also A += B, which is more efficient in many cases.
See also A += B, which is more efficient in many cases.
See also A -= B, which is more efficient in many cases.
See also A -= B, which is more efficient in many cases.
|
inline |
Matrix G3D::Matrix::pseudoInverse | ( | float | tolerance = -1 | ) | const |
Computes the Moore-Penrose pseudo inverse, equivalent to (ATA)-1AT).
The SVD method is used for performance when the matrix has more than four rows or columns
tolerance | Use -1 for automatic tolerance. |
|
static |
Uniformly distributed values between zero and one.
Matrix G3D::Matrix::row | ( | int | r | ) | const |
|
inline |
The number of rows.
Referenced by adjoint(), numElements(), size(), and transpose().
void G3D::Matrix::serialize | ( | TextOutput & | t | ) | const |
Serializes in Matlab source format.
void G3D::Matrix::set | ( | int | r, |
int | c, | ||
T | v | ||
) |
Referenced by fromDiagonal().
void G3D::Matrix::setCol | ( | int | c, |
const Matrix & | vec | ||
) |
void G3D::Matrix::setRow | ( | int | r, |
const Matrix & | vec | ||
) |
|
inline |
Referenced by G3D::Matrix::Impl::operator new().
Matrix G3D::Matrix::subMatrix | ( | int | r1, |
int | r2, | ||
int | c1, | ||
int | c2 | ||
) | const |
Returns a new matrix that is a subset of this one, from r1:r2 to c1:c2, inclusive.
Singular value decomposition.
Factors into three matrices such that this = U * fromDiagonal(d) * V.transpose().
The matrix must have at least as many rows as columns.
Run time is O(C2*R).
sort | If true (default), the singular values are arranged so that D is sorted from largest to smallest. |
|
static |
Low-level SVD functionality.
Useful for applications that do not want to construct a Matrix but need to perform the SVD operation.
this = U * D * V'
Assumes that rows >= cols
U | rows x cols matrix to be decomposed, gets overwritten with U, a rows x cols matrix with orthogonal columns. |
D | vector of singular values of a (diagonal of the D matrix). Length cols. |
V | returns the right orthonormal transformation matrix, size cols x cols |
Matrix G3D::Matrix::svdPseudoInverse | ( | float | tolerance = -1 | ) | const |
Called from pseudoInverse when the matrix has size > 4 along some dimension.
void G3D::Matrix::swapAndNegateCols | ( | int | c0, |
int | c1 | ||
) |
Swaps columns c0 and c1 and negates both.
void G3D::Matrix::swapRows | ( | int | r0, |
int | r1 | ||
) |
Matrix3 G3D::Matrix::toMatrix3 | ( | ) | const |
Matrix4 G3D::Matrix::toMatrix4 | ( | ) | const |
|
inline |
Vector2 G3D::Matrix::toVector2 | ( | ) | const |
Vector3 G3D::Matrix::toVector3 | ( | ) | const |
Vector4 G3D::Matrix::toVector4 | ( | ) | const |
|
inline |
AT
Referenced by gaussJordanPseudoInverse().
void G3D::Matrix::transpose | ( | Matrix & | out | ) | const |
Transpose in place; more efficient than transpose.
|
static |
Returns a new matrix that is all zero.
Referenced by fromDiagonal().
|
static |
Incremented every time a new matrix object is allocated.
Useful for profiling your own code that uses Matrix to determine when it is slow due to allocation.
|
static |
Incremented every time the elements of a matrix are copied.
Useful for profiling your own code that uses Matrix to determine when it is slow due to copying.