#include "smlxmatrix.h"
SMLXMatrix class encapsulates resizable matrix object. Matrix elements are floating-point numbers.
Class includes methods and data which allow to form resizable matrix, multiply two matrices, transpose them, add, subtract, copy, initialize, find maximum absolute value among elements, and others.
Default SMLXMatrix object constructor. Creates matrix of size 0X0.
Example:
SMLXMatrix A;
No.
[top]
SMLLIBENTRY SMLXMatrix(const SMLXMatrix& B);
SMLXMatrix object copy-constructor.
Example:
SMLXMatrix A(6, 6); A.Identity(); SMLXMatrix B(A);
B - reference to SMLXMatrix object to copy.
[top]
SMLLIBENTRY SMLXMatrix(short nrow, short ncol);
SMLXMatrix creates matrix with nrow x ncol size.
Example:
SMLXMatrix A(6, 6);
nrow - number of matrix rows.
ncol - number of matrix columns.
[top]
SMLLIBENTRY SMLXMatrix(short nrow, short ncol, const float* initialValues);
SMLXMatrix creates matrix with nrow x ncol size and sets matrix elements to values pointed by initialValues. It is assumed that array initialValues contains matrix elements starting from 1st row and till the last row, i.e. data[i][j] = initialValues[i * ncol + j];
Example:
float src[4] = {1, 2, 3, 4};
SMLXMatrix A(2, 2, src);
nrow - number of matrix rows.
ncol - number of matrix columns.
initialValues - pointer to initial values. Initial values should be stored in row based manner (first row is placed first, etc.).
[top]
SMLLIBENTRY explicit SMLXMatrix(const SMLMatrix3f& m33);
Explicit SMLXMatrixobject constructor. Constructs 3x3 matrix and initializes it with corresponding values of SMLMatrix3f object.
Example:
float src[9] = {1,2,3,4,5,6,7,8,9};
SMLMatrix3f A(src);
SMLXMatrix B(A);
m33 - reference to SMLMatrix3f object.
[top]
SMLLIBENTRY explicit SMLXMatrix(const SMLMatrix4f& m44);
Explicit SMLXMatrixobject constructor. Constructs 4x4 matrix and initializes it with corresponding values of SMLMatrix4f object.
Example:
float src[16] = {1,2,3,4,5,6,7,8,9,2,5,3,7,8,3,9};
SMLMatrix4f A(src);
SMLXMatrix B(A);
m44 - reference to SMLMatrix4f object.
[top]
SMLXMatrix object destructor.
No.
[top]
SMLLIBENTRY float Get(int i, int j) const;
Access to specified matrix element, lying on the intersection of i-th row and j-th column.
Example:
float element;
float src[4] = {1, 2, 3, 4};
SMLXMatrix A(2, 2, src);
element = A.Get(1, 1); // element = 4
i - row index.
j - column index.
Matrix element value.
[top]
SMLLIBENTRY float MaxAbs() const;
Finds maximum absolute value among matrix elements.
Example:
float maEl;
float src[4] = {-1, 2, -5, 4};
SMLXMatrix A(2, 2, src);
maEl = A.MaxAbs(); // maEl = -5
No.
Maximum absolute value of all matrix elements.
[top]
SMLLIBENTRY float* Data();
SMLLIBENTRY const float* Data() const;
Access matrix data.
Example:
float *ptrData;
float src[3] = {-1, 3, 4};
SMLXMatrix A(1, 3, src);
ptrData = A.Data(); // ptrData[1] = 3
No.
Returns a pointer to an array storing matrix elements in row-based manner.
[top]
SMLLIBENTRY float* operator[](int i);
SMLLIBENTRY const float* operator[](int i) const;
Method is an overloaded operator-function. Access to i-th row.
Example:
float *temp;
float src[6] = {1, 0, 2, -1, 3, 4};
SMLXMatrix A(3, 2, src);
temp = A[2]; // temp[0] = 3
i - matrix row.
Returns pointer to an array storing i matrix row.
[top]
SMLLIBENTRY int Index(int i, int j) const;
Calculates an index of element specified by (i , j) in an array storing matrix elements. (See method Data())
Example:
int ind;
float src[6] = {1, 0, 2, -1, 3, 4};
SMLXMatrix A(3, 2, src);
ind = A.Index(2,1); // ind = 5
i - matrix row in which element resides.
j - matrix column in which element resides.
Returns matrix element index in matrix data array.
[top]
SMLLIBENTRY int Size() const;
Return size of this matrix (# of rows * # of columns).
Example:
int sz;
float src[6] = {1, 0, 2, -1, 3, 4};
SMLXMatrix A(3, 2, src);
sz = A.Size(); // sz = 6
No.
Size of this matrix.
[top]
SMLLIBENTRY operator const SMLMatrix3f&() const;
Method is an overloaded operator-function cast to SMLMatrix3f type. It is assumed, that current matrix is 3x3 matrix.
Example:
float src[9] = {1,2,3,4,5,6,7,8,9};
SMLXMatrix A(3, 3, src);
SMLMatrix3f B(A);
No.
Reference to SMLMatrix3f object.
[top]
SMLLIBENTRY operator const SMLMatrix4f&() const;
Method is an overloaded operator-function cast to SMLMatrix4f type. It is assumed, that current matrix is 4x4 matrix.
Example:
float src[16] = {1,2,3,4,5,6,7,8,9,2,5,3,7,8,3,9};
SMLXMatrix A(4, 4, src);
SMLMatrix4f B(A);
No.
Reference to SMLMatrix4f object.
[top]
SMLLIBENTRY short ncols() const;
Returns number of matrix columns.
Example:
short clmn;
float src[6] = {1, 0, 2, -1, 3, 4};
SMLXMatrix A(3, 2, src);
clmn = A.ncols(); // clmn = 2
No.
Number of matrix columns.
[top]
SMLLIBENTRY short nrows() const;
Returns number of matrix rows.
Example:
short rws;
float src[6] = {1, 0, 2, -1, 3, 4};
SMLXMatrix A(3, 2, src);
rws = A.nrows(); // rws = 3
No.
Number of matrix rows.
[top]
SMLLIBENTRY void Export(float* dst) const;
Exports matrix data to an array pointed by dst.
Example:
float dst[6];
float src[6] = {1, 0, 2, -1, 3, 4};
SMLXMatrix A(3, 2, src);
A.Export(dst); // dst[i] = src[i], i=0...5
dst - pointer to destination array.
No.
[top]
SMLLIBENTRY SMLXTmpMatrix& TMat() const;
Creates SMLXTmpMatrix object from this SMLXMatrix object (memory is allocated from the static pool).
Example:
SMLXMatrix A(3, 2);
SMLXTmpMatrix B = A.TMat();
No.
Reference to the created SMLXTmpMatrix object.
[top]
SMLLIBENTRY static SMLXTmpMatrix& TMat(const SMLXMatrix& A);
Creates SMLXTmpMatrix object from object A SMLXMatrix class with sizes of object A (memory is allocated from the static pool).
Example:
SMLXMatrix A(3, 2);
SMLXTmpMatrix B(A);
A - reference to SMLXMatrix object.
Reference to SMLXTmpMatrix object.
[top]
SMLLIBENTRY bool Invert();
Inverts this matrix.
Example:
float src[4] = {2, 0, 0.5, 1};
SMLXMatrix A(2, 2, src);
A.Invert(); // inverse matrix = {0.5, 0, -0.25, 1}
No.
true, if matrix was inverted.
false, otherwise.
[top]
SMLLIBENTRY void Identity();
Sets main diagonal elements of matrix to 1 and all others to 0.
Example:
SMLXMatrix A(2, 2);
A.Identity(); // matrix = {1, 0, 0, 1}
No.
No.
[top]
SMLLIBENTRY void Identity(short nrow);
Resizes matrix to nrow x nrow and sets main diagonal elements of matrix to 1 and all others to 0.
Example:
SMLXMatrix A();
A.Identity(2); // matrix = {1, 0, 0, 1}
nrow - number of matrix rows.
No.
[top]
SMLLIBENTRY void Identity(short nrow, short ncol);
Resizes matrix to nrow x ncol and set main diagonal elements of matrix to 1 and all others to 0.
Example:
SMLXMatrix A();
A.Identity(2, 3); // matrix = {1, 0, 0, 0, 1, 0}
nrow - number of matrix rows.
ncol - number of matrix columns.
No.
[top]
SMLLIBENTRY void IdentityMinus();
This matrix is replaced with matrix (E - *this), where E - is appropriate identity matrix.
Example:
float src[4] = {1, 0, 2, -1};
SMLXMatrix A(2, 2, src);
A.IdentityMinus(); // matrix = {0, 0, -2, 2}
No.
No.
[top]
SMLLIBENTRY void Negate();
Negates all matrix elements.
Example:
float src[4] = {1, 0, 2, -1};
SMLXMatrix A(2, 2, src);
A.Negate(); // matrix = {-1, 0, -2, 1}
No.
No.
[top]
SMLLIBENTRY void Resize(const SMLXMatrix& A);
Resizes this matrix to A sizes.
Example:
SMLXMatrix A(2, 2), B(4, 4);
A.Resize(B); // resize 2x2 to 4x4
A - reference to SMLXMatrix object.
No.
[top]
SMLLIBENTRY void Resize(short nrow, short ncol);
Resizes this matrix to nrow x ncol.
Example:
SMLXMatrix A(2, 2);
A.Resize(4, 4); // resize 2x2 to 4x4
nrow - number of matrix row.
ncol - number of matrix column.
No.
[top]
SMLLIBENTRY void Set(float initialValue);
Set all matrix elements to initialValue.
Example:
SMLXMatrix A(2, 2); A.Set(1.0f); // set all matrix elements to 1.0
initialValue - initialization value.
No.
[top]
SMLLIBENTRY void Swap(const SMLXMatrix& A);
Swap A and this SMLXMatrix objects. After this operation, A will have matrix data of this matrix, and this matrix will have A's data.
Example:
SMLXMatrix A(2, 2); A.Set(1.0f);
SMLXMatrix B(2, 2); A.Set(2.0f);
A.Swap(B);
A - reference to SMLXMatrix object to be swapped.
No.
[top]
SMLLIBENTRY void Zero();
Set all matrix elements to 0.
Example:
SMLXMatrix A(2, 2); A.Zero();
No.
No.
[top]
SMLLIBENTRY void Transpose();
Transpose this matrix.
Example:
float src[4] = {1, 0, 2, -1};
SMLXMatrix A(2, 2, src);
A.Transpose(); // matrix = {1, 2, 0, -1}
No.
No.
[top]
inline SMLXTransposedMatrix & Transpose(const SMLXMatrix& m);
Global method, which casts matrix m to class SMLXTransposedMatrix. Actual transposition is not performed, but next multiplication operator will know, that it is using transposed matrix.
Example:
SMLXMatrixA(6, 6); A.Identity(); SMLXMatrixB(6, 6); B.Set(1.23f); SMLXMatrix C; C = A * Transpose(B);
m - reference to SMLXMatrix object to be transposed.
Reference to SMLXTransposedMatrix representing Transpose(m).
[top]
SMLLIBENTRY void Transpose(const SMLXMatrix& m);
Replaces this matrix with transposed matrix m.
Example:
float src[4] = {1, 0, 2, -1};
SMLXMatrix A(2, 2, src), B(2, 2);
B.Transpose(A); // matrix B = {1, 2, 0, -1}
m - reference to SMLXMatrix object to be transposed.
No.
[top]
SMLLIBENTRY SMLXTmpMatrix& operator*(float scale) const;
Method is an overloaded operator-function. It multiplies all matrix elements on parameter scale.
Example:
float src[4] = {1, 0, 2, -1};
SMLXMatrix A(2, 2, src);
SMLXTmpMatrix TM = A * 2.05f;
scale - multiplier.
Reference to SMLXTmpMatrix object, which is equal to product of this matrix and number scale.
[top]
SMLLIBENTRY SMLXTmpMatrix& operator+(SMLXTmpMatrix& B) const;
Method is an overloaded operator-function. It computes sum of two matrices (this SMLXMatrix object and matrix B) and returns a sum as a temporary matrix object.
Example:
float src1[4] = {1, 0, 2, -1};
float src2[4] = {3, 1, -1, -4};
SMLXMatrix A(2, 2, src1);
SMLXMatrix B(2, 2, src2); SMLXTmpMatrix tb = B.TMat();
SMLXTmpMatrix TM = A + tb;
B - reference to SMLXTmpMatrix to add to this SMLXMatrix object.
Reference to sum SMLXTmpMatrix object.
[top]
SMLLIBENTRY SMLXTmpMatrix& operator-(SMLXTmpMatrix& B) const;
Method is an overloaded operator-function. It computes difference of two matrices: this and matrix B and returns a difference as a temporary matrix object.
Example:
float src1[4] = {1, 0, 2, -1};
float src2[4] = {3, 1, -1, -4};
SMLXMatrix A(2, 2, src1);
SMLXMatrix B(2, 2, src2); SMLXTmpMatrix tb = B.TMat();
SMLXTmpMatrix TM = A - tb;
B - reference to SMLXTmpMatrix, which is subtracted from this SMLXMatrix object.
Reference to difference SMLXTmpMatrix object.
[top]
SMLLIBENTRY void Transform(SMLXSpatialVector const& A, SMLXSpatialVector& B) const;
Transforms spatial vector A (SMLXSpatialVector object) by this matrix (multiplies matrix by vector). Result is assigned to vector B.
Example:
//create and initialize transform matrix A and spatial vector svB, create spatial vector svC
...
A.Transform(svB, svC);
A - reference to SMLXSpatialVector object.
B - reference to SMLXSpatialVector object (transformed vector).
No.
[top]
SMLLIBENTRY void TransformTransposed(SMLXSpatialVector const& A, SMLXSpatialVector& B) const;
Transform spatial vector A (SMLXSpatialVector object) with transpose of this matrix.
Example:
//create and initialize transform matrix A and spatial vector svB, create spatial vector svC
...
A.TransformTransposed(svB, svC);
A - reference to SMLXSpatialVector object.
B - reference to SMLXSpatialVector object (transformed vector).
No.
[top]
SMLLIBENTRY void Mult(const SMLXMatrix& A, const SMLXMatrix& B, short Atranspose, short Btranspose);
Method computes product of matrices of objects A and B. This product depends on the value of parameters Atranspose and Btranspose. If parameter is zero, the corresponding matrix is not transposed. If parameter is not 0, the corresponding matrix is transposed. Result in this.
Example:
//create and initialize matrix A and matrix B
...
C.Mul(A, B, 0, 1); //multiplication matrix A and transposed matrix B
A - reference to SMLXMatrix object.
B - reference to SMLXMatrix object.
Atranspose - flag, which shows if the method uses transposed matrix A (!= 0) or not (0).
Btranspose - flag, which shows if the method uses transposed matrix B (!= 0) or not (0).
No.
[top]
SMLLIBENTRY void NSub(const SMLXMatrix& B);
Subtracts this matrix from matrix B. Result is assigned to this.
Example:
float src1[4] = {1, 0, 2, -1};
float src2[4] = {3, 1, -1, -4};
SMLXMatrix A(2, 2, src1), B(2, 2, src2);
A.NSub(B); // A = B - A
B - reference to SMLXMatrix object.
No.
[top]
SMLLIBENTRY void Sub(const SMLXMatrix& B);
Subtract matrix B from this matrix. Result is assigned to this.
Example:
float src1[4] = {1, 0, 2, -1};
float src2[4] = {3, 1, -1, -4};
SMLXMatrix A(2, 2, src1), B(2, 2, src2);
A.Sub(B); // A = A - B
B - reference to SMLXMatrix object.
No.
[top]
SMLLIBENTRY void Add(const SMLXMatrix& B);
Adds matrix B to this. Result is assigned to this.
Example:
float src1[4] = {1, 0, 2, -1};
float src2[4] = {3, 1, -1, -4};
SMLXMatrix A(2, 2, src1), B(2, 2, src2);
A.Add(B); // A = A + B
B - reference to SMLXMatrix object.
No.
[top]
SMLLIBENTRY void Cross(const SMLVec3f& V);
Creates matrix cross operator for vector V. Result is assigned to this (see SMLMatrix3f::Cross).
Example:
float src[9] = {1, 0, 2, -1, 2, 4, 1, -4, 0}; SMLXMatrix A(3, 3, src);
SMLVec3f B(2, 4, 6);
A.Cross(B);
V -reference to SMLVec3f object.
No.
[top]
SMLLIBENTRY void Scale(float scale);
Computes product of matrix and parameter scale. Result is assigned to this.
Example:
float src[4] = {1, 0, 2, -1}; SMLXMatrix A(2, 2, src);
A.Scale(2.13f); // A = A * 2.13
scale - multiplier.
No.
[top]
SMLLIBENTRY void operator*=(float scale);
Method is an overloaded operator-function. It computes product of matrix and parameter scale. Result is assigned to this.
Example:
float src[4] = {1, 0, 2, -1}; SMLXMatrix A(2, 2, src);
A *= 2.13f; // A = A * 2.13
scale - multiplier.
No.
[top]
SMLLIBENTRY void operator+=(const SMLXMatrix& B);
Method is an overloaded operator-function. It computes sum of two matrices (this with matrix B). Result is assigned to this.
Example:
float src1[4] = {1, 0, 2, -1};
float src2[4] = {3, 1, -1, -4};
SMLXMatrix A(2, 2, src1), SMLXMatrix B(2, 2, src2);
A += B; // A = A + B
B - reference to SMLXMatrix, which is added to this SMLXMatrix object..
No.
[top]
SMLLIBENTRY void operator-=(const SMLXMatrix& B);
Method is an overloaded operator-function. It computes difference of two matrices (this and matrix B). Result is assigned to this.
Example:
float src1[4] = {1, 0, 2, -1};
float src2[4] = {3, 1, -1, -4};
SMLXMatrix A(2, 2, src1), SMLXMatrix B(2, 2, src2);
A -= B; // A = A - B
B - reference to SMLXMatrix, which is subtracted to this SMLXMatrix object.
No.
[top]
SMLLIBENTRY void operator=(const SMLXMatrix& B);
Method is an overloaded operator-function. Initializes this SMLXMatrix object with data and sizes of matrix B.
Example:
float src[4] = {1, 0, 2, -1}; SMLXMatrix A(2, 2, src), B;
B = A;
B - reference to using for initialization SMLXMatrix object.
No.
[top]
SMLLIBENTRY void operator=(const SMLXTmpMatrix& B);
Method is an overloaded operator-function. Initializes this SMLXMatrix object with data and sizes of matrix B. Method is using fast Swap operation.
Example:
float src[4] = {1, 0, 2, -1}; SMLXMatrix A(2, 2, src);
SMLXTmpMatrix B = A;
B - reference to SMLXTmpMatrix object, which is swapped with this.
No.
[top]
SMLLIBENTRY float Pythag(double a, double b) const;
Computes (a2 + b2)1/2.
a - first parameter.
b - second parameter.
(a2 + b2)1/2.
[top]
SMLLIBENTRY bool LUDecomposition(SMLXMatrix& indx, double& d);
LU - decomposition of this SMLXMatrix object. Factors this matrix into LU where L is lower triangular and U is upper triangular. Matrices L and U overwrite this matrix, while the diagonal elements of L (which are unity) are not stored. It is assumed that this is a square matrix.
Note:
Calling of this function is to be followed by (some) LUBackSubstitutions, if this method is used to solve a linear system of equations.
Example:
float src[9] = {1, 1, 2, 2, 1, 0, 3, 0 ,2}; SMLXMatrix A(3, 3, src);
SMLXMatrix xmid;
double ddet; bool per;
per = A.LUDecomposition(xmid, ddet);
d - determinant of this matrix multiplied by +1 or -1 indicating that the number of row interchanges was even or odd respectively.
indx - reference to some auxiliary matrix. It will be used by LUBackSubstitution method.
Function returns true, if determinant is not 0, false otherwise.
[top]
SMLLIBENTRY void LUBackSubstitution(SMLXMatrix& indx, SMLXMatrix& b) const;
Uses the LU decomposition stored in this to solve the system of n linear equations A x = b. It is assumed, that method LUDecomposition was called for matrix A (this matrix).
Example:
float src[9] = {1, 1, 2, 2, 1, 0, 3, 0 ,2}; SMLXMatrix A(3, 3, src);
float bb[3] = {1, 3, 2}; SMLXMatrix xb(3, 1, bb);
SMLXMatrix xmid;
double ddet; bool per;
per = A.LUDecomposition(xmid, ddet);
A.LUBackSubstitution(xmid, xb);
indx- reference to auxiliary matrix, which was used by LUDecomposition.
b - vector (1 x n matrix), representing right part in the linear system. Upon completion of this function, b is overwritten with x (solution).
No.
[top]
SMLLIBENTRY bool SVDecomposition(SMLXMatrix& W, SMLXMatrix& V, SMLXMatrix& tmp);
Singular value decomposition of this SMLXMatrix object. Factors this matrix into A = U W VT , where U and V are orthogonal matrices (for which UT = U-1), W is diagonal matrix. Matrix U overwrites this, matrices W and V are output parameters.
W - reference to output diagonal matrix.
V - reference to output orthogonal matrix.
tmp - reference to some auxiliary matrix.
Function returns true, if decomposition was successful, false otherwise.
[top]
SMLLIBENTRY void SVDBackSubstitution(const SMLXMatrix& U, const SMLXMatrix& W, const SMLXMatrix& V, const SMLXMatrix& b, SMLXMatrix& tmp);
Uses the SVD decomposition represented as A = U W VT to solve the system of linear equations A x = b. It is assumed, that method SVDecomposition was called for matrix A, which is not required to be square. If A is square matrix, and A-1 exists, then x = A-1 b, otherwise solution is sought in some generalized sense.
U, W, V - matrices representing singular value decomposition of the original matrix A(performed by SVDecomposition).
b - vector (1 x n matrix), representing right part in the linear system. Upon completion of this function, b is overwritten with x (solution).
tmp- reference to auxiliary matrix, which was used by SVDecomposition.
No.
[top]
SMLLIBENTRY void SVDInvert(SMLXMatrix const& U, SMLXMatrix const& W, SMLXMatrix& V);
This matrix will be overwritten with V W-1 UT, where U, W, Vare matrices used by SVDecomposition. If some elements on diagonal of W are less then FLT_EPSILON by absolute value, they are replaced by 0 in W-1. Upon completion of this function, matrix V is changed to V W-1. This methods yields A-1, if it exists.
U, W, V - matrices representing singular value decomposition of the original matrix A(performed by SVDecomposition).
No.
[top]
SMLLIBENTRY bool IsDefined() const;
Defines whether current matrix is valid one.
No.
true, if matrix was defined with non-zero sizes,
false, otherwise.
[top]
SMLLIBENTRY void MarkAsDefined();
Sets internal flag. Next call of IsDefined() will return true.
No.
No.
[top]
SMLLIBENTRY void MarkAsUndefined();
Unsets internal flag. Next call of IsDefined() will return false.
No.
No.
[top]
SMLLIBENTRY void Output(const char* caption = NULL) const;
Method will print the data using OutputDebugString function.
caption - text caption printed data.
No.
[top]
friend inline SMLXTmpMatrix& __fastcall operator*(const SMLXMatrix& A, const SMLXMatrix& B);
Function in global scope to compute and return A B.
[top]
friend inline SMLXTmpMatrix& __fastcall operator*(const SMLXMatrix& A, const SMLXTransposedMatrix& B);
Function in global scope to compute and return A BT.
Example:
SMLXMatrix A(6, 6); A.Identity();
SMLXMatrix B(6, 6); B.Set(1.23f);
SMLXMatrix C;
C = A * Transpose(B);
Function Transposeperforms cast to class SMLXTransposedMatrix, derived from SMLXMatrix. As a result, proper operator* will be used, while transposition is never performed "live".
[top]
friend inline SMLXTmpMatrix& __fastcall operator*(const SMLXTransposedMatrix& A, const SMLXMatrix& B);
Function in global scope to compute and return AT B.
[top]
friend inline SMLXTmpMatrix& __fastcall operator*(const SMLXTransposedMatrix& A, const SMLXTransposedMatrix& B);
Function in global scope to compute and return AT BT.
[top]
friend inline SMLXTmpMatrix& __fastcall operator+(const SMLXMatrix& A, const SMLXMatrix& B);
Function in global scope to compute and return A + B.
[top]
friend inline SMLXTmpMatrix& __fastcall operator-(const SMLXMatrix& A, const SMLXMatrix& B);
Function in global scope to compute and return A - B.
[top]
friend inline void Assign(SMLXMatrix& A, float value);
Function in global scope to set all values in matrix A to value.
[top]
friend inline void Assign(SMLXMatrix& A, const SMLXMatrix& B);
Function in global scope to set all values in matrix A to those of B.
[top]