Description
template<class Real = double>
class chrono::ChMatrix< Real >
A base class for matrix objects (tables of NxM numbers). To access elements, the indexes start from zero, and you must indicate first row, then column, that is: m(2,4) means the element at 3rd row, 5th column. This is an abstract class, so you cannot instantiate objects from it: you must rather create matrices using the specialized child classes like ChMatrixDynamic, ChMatrixNM, ChMatrix33 and so on; all of them have this same base class. Warning: for optimization reasons, not all functions will check about boundaries of element indexes and matrix sizes (in some cases, if sizes are wrong, debug asserts are used).
Further info at the Mathematical objects in Chrono manual page.
#include <ChMatrix.h>

Public Member Functions | |
Real & | operator() (const int row, const int col) |
Parenthesis () operator, to access a single element of the matrix, by supplying the row and the column (indexes start from 0). More... | |
const Real & | operator() (const int row, const int col) const |
Real & | operator() (const int el) |
Parenthesis () operator, to access a single element of the matrix, by supplying the ordinal of the element (indexes start from 0). More... | |
const Real & | operator() (const int el) const |
Real * | operator[] (const int row) |
The [] operator returns the address of the n-th row. More... | |
const Real * | operator[] (const int row) const |
ChMatrix< Real > & | operator*= (const Real factor) |
Multiplies this matrix by a factor, in place. | |
template<class RealB > | |
ChMatrix< Real > & | operator+= (const ChMatrix< RealB > &matbis) |
Increments this matrix by another matrix, in place. | |
template<class RealB > | |
ChMatrix< Real > & | operator-= (const ChMatrix< RealB > &matbis) |
Decrements this matrix by another matrix, in place. | |
bool | operator== (const ChMatrix< Real > &other) |
Matrices are equal? | |
bool | operator!= (const ChMatrix< Real > &other) |
Matrices are not equal? | |
virtual ChMatrix< Real > & | operator= (const ChMatrix< Real > &matbis) |
Assignment operator. | |
template<class RealB > | |
ChMatrix< Real > & | operator= (const ChMatrix< RealB > &matbis) |
void | SetElement (int row, int col, Real elem) |
Sets the element at row,col position. Indexes start with zero. | |
Real | GetElement (int row, int col) |
Gets the element at row,col position. More... | |
Real | GetElement (int row, int col) const |
void | SetElementN (int index, Real elem) |
Sets the Nth element, counting row after row. | |
Real | GetElementN (int index) |
Gets the Nth element, counting row after row. | |
const Real | GetElementN (int index) const |
Real & | Element (int row, int col) |
Access a single element of the matrix, by supplying the row and the column (indexes start from 0). More... | |
const Real & | Element (int row, int col) const |
Real & | ElementN (int index) |
Access a single element of the matrix, the Nth element, counting row after row. More... | |
const Real & | ElementN (int index) const |
Real * | GetAddress () |
Access directly the "Real* address" buffer. More... | |
const Real * | GetAddress () const |
int | GetRows () const |
Gets the number of rows. | |
int | GetColumns () const |
Gets the number of columns. | |
virtual void | Resize (int nrows, int ncols) |
Reallocate memory for a new size. VIRTUAL! Must be implemented by child classes! | |
void | SwapColumns (int a, int b) |
Swaps the columns a and b. | |
void | SwapRows (int a, int b) |
Swap the rows a and b. | |
void | FillDiag (Real sample) |
Fill the diagonal elements, given a sample. More... | |
void | FillElem (Real sample) |
Fill the matrix with the same value in all elements. | |
void | FillRandom (Real max, Real min) |
Fill the matrix with random float numbers, falling within the "max"/"min" range. More... | |
virtual void | Reset () |
Resets the matrix to zero (warning: simply sets memory to 0 bytes!) | |
void | Reset (int nrows, int ncols) |
Reset to zeroes and (if needed) changes the size to have row and col. | |
void | SetIdentity () |
Reset to identity matrix (ones on diagonal, zero elsewhere) | |
template<class RealB > | |
void | CopyFromMatrix (const ChMatrix< RealB > &matra) |
Copy a matrix "matra" into this matrix. More... | |
template<class RealB > | |
void | CopyFromMatrixT (const ChMatrix< RealB > &matra) |
Copy the transpose of matrix "matra" into this matrix. More... | |
template<class RealB > | |
void | CopyTUpMatrix (const ChMatrix< RealB > &matra) |
Copy the transposed upper triangular part of "matra" in the lower triangular part of this matrix. More... | |
template<class RealB > | |
void | CopyTLwMatrix (const ChMatrix< RealB > &matra) |
Copy the transposed lower triangulat part of "matra" in the upper triangular part of this matrix. More... | |
virtual void | ArchiveOUT (ChArchiveOut &marchive) |
Method to allow serialization of transient data in archives. | |
virtual void | ArchiveIN (ChArchiveIn &marchive) |
Method to allow de serialization of transient data from archives. | |
void | StreamOUT (ChStreamOutAscii &mstream) |
Method to allow serializing transient data into in ascii as a readable item, for example "chrono::GetLog() << myobject;" ***OBSOLETE***. | |
void | StreamOUTdenseMatlabFormat (ChStreamOutAscii &mstream) |
Method to allow serializing transient data into an ascii stream (ex. More... | |
void | MatrNeg () |
Changes the sign of all the elements of this matrix, in place. | |
template<class RealB , class RealC > | |
void | MatrAdd (const ChMatrix< RealB > &matra, const ChMatrix< RealC > &matrb) |
Sum two matrices, and stores the result in "this" matrix: [this]=[A]+[B]. | |
template<class RealB , class RealC > | |
void | MatrSub (const ChMatrix< RealB > &matra, const ChMatrix< RealC > &matrb) |
Subtract two matrices, and stores the result in "this" matrix: [this]=[A]-[B]. | |
template<class RealB > | |
void | MatrInc (const ChMatrix< RealB > &matra) |
Increments this matrix with another matrix A, as: [this]+=[A]. | |
void | MatrInc (Real val) |
Increments this matrix by val , as [this]+=val. | |
template<class RealB > | |
void | MatrDec (const ChMatrix< RealB > &matra) |
Decrements this matrix with another matrix A, as: [this]-=[A]. | |
void | MatrScale (Real factor) |
Scales a matrix, multiplying all elements by a constant value: [this]*=f. | |
template<class RealB > | |
void | MatrScale (const ChMatrix< RealB > &matra) |
Scales a matrix, multiplying all element by all other elements of matra (it is not the classical matrix multiplication!) | |
void | MatrDivScale (Real factor) |
Scales a matrix, dividing all elements by a constant value: [this]/=f. | |
template<class RealB > | |
void | MatrDivScale (const ChMatrix< RealB > &matra) |
Scales a matrix, dividing all element by all other elements of matra (it is not the classical matrix multiplication!) | |
template<class RealB , class RealC > | |
void | MatrMultiply (const ChMatrix< RealB > &matra, const ChMatrix< RealC > &matrb) |
Multiplies two matrices, and stores the result in "this" matrix: [this]=[A]*[B]. | |
template<class RealB , class RealC > | |
void | MatrMultiplyT (const ChMatrix< RealB > &matra, const ChMatrix< RealC > &matrb) |
Multiplies two matrices (the second is considered transposed): [this]=[A]*[B]' Faster than doing B.MatrTranspose(); result.MatrMultiply(A,B); Note: no check on mistaken size of this! | |
template<class RealB , class RealC > | |
void | MatrTMultiply (const ChMatrix< RealB > &matra, const ChMatrix< RealC > &matrb) |
Multiplies two matrices (the first is considered transposed): [this]=[A]'*[B] Faster than doing A.MatrTranspose(); result.MatrMultiply(A,B);. | |
void | MatrTranspose () |
Transpose this matrix in place. | |
Real | Det () |
Returns the determinant of the matrix. More... | |
void | MatrInverse () |
Returns the inverse of the matrix. More... | |
bool | Equals (const ChMatrix< Real > &other) const |
Returns true if vector is identical to other matrix. | |
bool | Equals (const ChMatrix< Real > &other, Real tol) const |
Returns true if vector equals another vector, within a tolerance 'tol'. | |
template<class RealB > | |
ChVector< Real > | Matr34_x_Quat (const ChQuaternion< RealB > &qua) |
Multiplies this 3x4 matrix by a quaternion, as v=[G]*q The matrix must be 3x4. More... | |
template<class RealB > | |
ChQuaternion< Real > | Matr34T_x_Vect (const ChVector< RealB > &va) |
Multiplies this 3x4 matrix (transposed) by a vector, as q=[G]'*v The matrix must be 3x4. More... | |
template<class RealB > | |
ChQuaternion< Real > | Matr44_x_Quat (const ChQuaternion< RealB > &qua) |
Multiplies this 4x4 matrix (transposed) by a quaternion, The matrix must be 4x4. More... | |
void | MatrXq_SemiTranspose () |
Transposes only the lower-right 3x3 submatrix of a hemisymmetric 4x4 matrix, used when the 4x4 matrix is a "star" matrix [q] coming from a quaternion q: the non commutative quat. More... | |
void | MatrXq_SemiNeg () |
Change the sign of the 2nd, 3rd and 4th columns of a 4x4 matrix, The product between a quaternion q1 and the conjugate of q2 (q2'), is: q1 x q2' = [q1]*q2' = [q1sn]*q2 where [q1sn] is the semi-negation of the 4x4 matrix [q1]. More... | |
Real | NormInf () const |
Gets the norm infinite of the matrix, i.e. More... | |
Real | NormTwo () const |
Gets the norm two of the matrix, i.e. More... | |
Real | Max () const |
Finds max value among the values of the matrix. | |
Real | Min () const |
Finds min value among the values of the matrix. | |
void | LinInterpolate (const ChMatrix< Real > &matra, const ChMatrix< Real > &matrb, Real mx) |
Linear interpolation of two matrices. More... | |
void | RowColInterp (Real vmin, Real vmax, Real umin, Real umax) |
Fills a matrix or a vector with a bilinear interpolation, from corner values (as a u-v patch). More... | |
template<class RealB > | |
void | PasteMatrix (const ChMatrix< RealB > &matra, int insrow, int inscol) |
Paste a matrix "matra" into "this", inserting at location insrow-inscol. More... | |
template<class RealB > | |
void | PasteSumMatrix (const ChMatrix< RealB > &matra, int insrow, int inscol) |
Paste a matrix "matra" into "this", inserting at location insrow-inscol and performing a sum with the preexisting values. More... | |
template<class RealB > | |
void | PasteTranspMatrix (const ChMatrix< RealB > &matra, int insrow, int inscol) |
Paste a matrix "matra", transposed, into "this", inserting at location insrow-inscol. More... | |
template<class RealB > | |
void | PasteSumTranspMatrix (const ChMatrix< RealB > &matra, int insrow, int inscol) |
Paste a matrix "matra", transposed, into "this", inserting at location insrow-inscol and performing a sum with the preexisting values. More... | |
template<class RealB > | |
void | PasteClippedMatrix (const ChMatrix< RealB > &matra, int cliprow, int clipcol, int nrows, int ncolumns, int insrow, int inscol) |
Paste a clipped portion of the matrix "matra" into "this", inserting the clip (of size nrows, ncolumns) at the location insrow-inscol. More... | |
template<class RealB > | |
void | PasteClippedMatrixToVector (const ChMatrix< RealB > &matra, int cliprow, int clipcol, int nrows, int ncolumns, int insindex) |
Paste a clipped portion of the matrix "matra" into "this", where "this" is a vector (of ChMatrix type), inserting the clip (of size nrows, ncolumns) at the location insindex. More... | |
template<class RealB > | |
void | PasteClippedVectorToMatrix (const ChMatrix< RealB > &matra, int cliprow, int clipcol, int nrows, int ncolumns, int insindex) |
Paste a clipped portion of a vector into "this", where "this" is a matrix (of ChMatrix type), inserting the clip (of size nrows, ncolumns) at the location insindex. More... | |
template<class RealB > | |
void | PasteSumClippedMatrix (const ChMatrix< RealB > &matra, int cliprow, int clipcol, int nrows, int ncolumns, int insrow, int inscol) |
Paste a clipped portion of the matrix "matra" into "this", performing a sum with preexisting values, inserting the clip (of size nrows, ncolumns) at the location insrow-inscol. More... | |
template<class RealB > | |
void | PasteVector (const ChVector< RealB > &va, int insrow, int inscol) |
Paste a vector "va" into the matrix. | |
template<class RealB > | |
void | PasteSumVector (const ChVector< RealB > &va, int insrow, int inscol) |
Paste a vector "va" into the matrix, summing it with preexisting values. | |
template<class RealB > | |
void | PasteSubVector (const ChVector< RealB > &va, int insrow, int inscol) |
Paste a vector "va" into the matrix, subtracting it from preexisting values. | |
template<class RealB > | |
void | PasteQuaternion (const ChQuaternion< RealB > &qa, int insrow, int inscol) |
Paste a quaternion into the matrix. | |
template<class RealB > | |
void | PasteSumQuaternion (const ChQuaternion< RealB > &qa, int insrow, int inscol) |
Paste a quaternion into the matrix, summing it with preexisting values. | |
template<class RealB > | |
void | PasteCoordsys (const ChCoordsys< RealB > &cs, int insrow, int inscol) |
Paste a coordsys into the matrix. | |
ChVector< Real > | ClipVector (int insrow, int inscol) const |
Returns the vector clipped from insrow, inscol. | |
ChQuaternion< Real > | ClipQuaternion (int insrow, int inscol) const |
Returns the quaternion clipped from insrow, inscol. | |
ChCoordsys< Real > | ClipCoordsys (int insrow, int inscol) const |
Returns the coordsys clipped from insrow, inscol. | |
template<class RealB > | |
void | Set_Xq_matrix (const ChQuaternion< RealB > &q) |
Fills a 4x4 matrix as the "star" matrix, representing quaternion cross product. More... | |
Static Public Member Functions | |
template<class RealB , class RealC > | |
static Real | MatrDot (const ChMatrix< RealB > &ma, const ChMatrix< RealC > &mb) |
Computes dot product between two column-matrices (vectors) with same size. More... | |
Protected Attributes | |
int | rows = 1 |
int | columns = 1 |
Real * | address |
Member Function Documentation
void chrono::ChMatrix< Real >::CopyFromMatrix | ( | const ChMatrix< RealB > & | matra | ) |
Copy a matrix "matra" into this matrix.
Note that the destination matrix will be resized if necessary.
void chrono::ChMatrix< Real >::CopyFromMatrixT | ( | const ChMatrix< RealB > & | matra | ) |
Copy the transpose of matrix "matra" into this matrix.
Note that the destination matrix will be resized if necessary.
void chrono::ChMatrix< Real >::CopyTLwMatrix | ( | const ChMatrix< RealB > & | matra | ) |
Copy the transposed lower triangulat part of "matra" in the upper triangular part of this matrix.
(matra must be square) Note that the destination matrix will be resized if necessary.
void chrono::ChMatrix< Real >::CopyTUpMatrix | ( | const ChMatrix< RealB > & | matra | ) |
Copy the transposed upper triangular part of "matra" in the lower triangular part of this matrix.
(matra must be square) Note that the destination matrix will be resized if necessary.
Real chrono::ChMatrix< Real >::Det | ( | ) |
Returns the determinant of the matrix.
Note! This method must be used only with max 4x4 matrices, otherwise it throws an exception.
Real& chrono::ChMatrix< Real >::Element | ( | int | row, |
int | col | ||
) |
Access a single element of the matrix, by supplying the row and the column (indexes start from 0).
Value is returned by reference, so it can be modified, like in m.Element(1,2)=10.
Real& chrono::ChMatrix< Real >::ElementN | ( | int | index | ) |
Access a single element of the matrix, the Nth element, counting row after row.
Value is returned by reference, so it can be modified, like in m.Element(5)=10.
void chrono::ChMatrix< Real >::FillDiag | ( | Real | sample | ) |
Fill the diagonal elements, given a sample.
Note that the matrix must already be square (no check for rectangular matrices!), and the extra-diagonal elements are not modified -this function does not set them to 0-
void chrono::ChMatrix< Real >::FillRandom | ( | Real | max, |
Real | min | ||
) |
Fill the matrix with random float numbers, falling within the "max"/"min" range.
Real* chrono::ChMatrix< Real >::GetAddress | ( | ) |
Access directly the "Real* address" buffer.
Warning! this is a low level function, it should be used in rare cases, if really needed!
Real chrono::ChMatrix< Real >::GetElement | ( | int | row, |
int | col | ||
) |
Gets the element at row,col position.
Indexes start with zero. The return value is a copy of original value. Use Element() instead if you want to access directly by reference the original element.
void chrono::ChMatrix< Real >::LinInterpolate | ( | const ChMatrix< Real > & | matra, |
const ChMatrix< Real > & | matrb, | ||
Real | mx | ||
) |
Linear interpolation of two matrices.
Parameter mx must be 0...1. [this] =(1-x)[A]+ (x)[B] Matrices must have the same size!!
ChVector<Real> chrono::ChMatrix< Real >::Matr34_x_Quat | ( | const ChQuaternion< RealB > & | qua | ) |
Multiplies this 3x4 matrix by a quaternion, as v=[G]*q The matrix must be 3x4.
- Returns
- The result of the multiplication, i.e. a vector.
ChQuaternion<Real> chrono::ChMatrix< Real >::Matr34T_x_Vect | ( | const ChVector< RealB > & | va | ) |
Multiplies this 3x4 matrix (transposed) by a vector, as q=[G]'*v The matrix must be 3x4.
- Returns
- The result of the multiplication, i.e. a quaternion.
ChQuaternion<Real> chrono::ChMatrix< Real >::Matr44_x_Quat | ( | const ChQuaternion< RealB > & | qua | ) |
Multiplies this 4x4 matrix (transposed) by a quaternion, The matrix must be 4x4.
- Returns
- The result of the multiplication, i.e. a quaternion.
|
static |
Computes dot product between two column-matrices (vectors) with same size.
Returns a scalar value.
void chrono::ChMatrix< Real >::MatrInverse | ( | ) |
Returns the inverse of the matrix.
Note! This method must be used only with max 4x4 matrices, otherwise it throws an exception.
void chrono::ChMatrix< Real >::MatrXq_SemiNeg | ( | ) |
Change the sign of the 2nd, 3rd and 4th columns of a 4x4 matrix, The product between a quaternion q1 and the conjugate of q2 (q2'), is: q1 x q2' = [q1]*q2' = [q1sn]*q2 where [q1sn] is the semi-negation of the 4x4 matrix [q1].
void chrono::ChMatrix< Real >::MatrXq_SemiTranspose | ( | ) |
Transposes only the lower-right 3x3 submatrix of a hemisymmetric 4x4 matrix, used when the 4x4 matrix is a "star" matrix [q] coming from a quaternion q: the non commutative quat.
product is: q1 x q2 = [q1]*q2 = [q2st]*q1 where [q2st] is the "semi-transpose of [q2].
Real chrono::ChMatrix< Real >::NormInf | ( | ) | const |
Gets the norm infinite of the matrix, i.e.
the max. of its elements in absolute value.
Real chrono::ChMatrix< Real >::NormTwo | ( | ) | const |
Gets the norm two of the matrix, i.e.
the square root of the sum of the elements squared.
Real& chrono::ChMatrix< Real >::operator() | ( | const int | row, |
const int | col | ||
) |
Parenthesis () operator, to access a single element of the matrix, by supplying the row and the column (indexes start from 0).
For example: m(3,5) gets the element at the 4th row, 6th column. Value is returned by reference, so it can be modified, like in m(1,2)=10.
Real& chrono::ChMatrix< Real >::operator() | ( | const int | el | ) |
Parenthesis () operator, to access a single element of the matrix, by supplying the ordinal of the element (indexes start from 0).
For example: m(3) gets the 4th element, counting row by row. Mostly useful if the matrix is Nx1 sized (i.e. a N-element vector). Value is returned by reference, so it can be modified, like in m(1,2)=10.
Real* chrono::ChMatrix< Real >::operator[] | ( | const int | row | ) |
The [] operator returns the address of the n-th row.
This is mostly for compatibility with old matrix programming styles (2d array-like) where to access an element at row i, column j, one can write mymatrix[i][j].
void chrono::ChMatrix< Real >::PasteClippedMatrix | ( | const ChMatrix< RealB > & | matra, |
int | cliprow, | ||
int | clipcol, | ||
int | nrows, | ||
int | ncolumns, | ||
int | insrow, | ||
int | inscol | ||
) |
Paste a clipped portion of the matrix "matra" into "this", inserting the clip (of size nrows, ncolumns) at the location insrow-inscol.
void chrono::ChMatrix< Real >::PasteClippedMatrixToVector | ( | const ChMatrix< RealB > & | matra, |
int | cliprow, | ||
int | clipcol, | ||
int | nrows, | ||
int | ncolumns, | ||
int | insindex | ||
) |
Paste a clipped portion of the matrix "matra" into "this", where "this" is a vector (of ChMatrix type), inserting the clip (of size nrows, ncolumns) at the location insindex.
void chrono::ChMatrix< Real >::PasteClippedVectorToMatrix | ( | const ChMatrix< RealB > & | matra, |
int | cliprow, | ||
int | clipcol, | ||
int | nrows, | ||
int | ncolumns, | ||
int | insindex | ||
) |
Paste a clipped portion of a vector into "this", where "this" is a matrix (of ChMatrix type), inserting the clip (of size nrows, ncolumns) at the location insindex.
void chrono::ChMatrix< Real >::PasteMatrix | ( | const ChMatrix< RealB > & | matra, |
int | insrow, | ||
int | inscol | ||
) |
Paste a matrix "matra" into "this", inserting at location insrow-inscol.
Normal copy for insrow=inscol=0
void chrono::ChMatrix< Real >::PasteSumClippedMatrix | ( | const ChMatrix< RealB > & | matra, |
int | cliprow, | ||
int | clipcol, | ||
int | nrows, | ||
int | ncolumns, | ||
int | insrow, | ||
int | inscol | ||
) |
Paste a clipped portion of the matrix "matra" into "this", performing a sum with preexisting values, inserting the clip (of size nrows, ncolumns) at the location insrow-inscol.
void chrono::ChMatrix< Real >::PasteSumMatrix | ( | const ChMatrix< RealB > & | matra, |
int | insrow, | ||
int | inscol | ||
) |
Paste a matrix "matra" into "this", inserting at location insrow-inscol and performing a sum with the preexisting values.
void chrono::ChMatrix< Real >::PasteSumTranspMatrix | ( | const ChMatrix< RealB > & | matra, |
int | insrow, | ||
int | inscol | ||
) |
Paste a matrix "matra", transposed, into "this", inserting at location insrow-inscol and performing a sum with the preexisting values.
void chrono::ChMatrix< Real >::PasteTranspMatrix | ( | const ChMatrix< RealB > & | matra, |
int | insrow, | ||
int | inscol | ||
) |
Paste a matrix "matra", transposed, into "this", inserting at location insrow-inscol.
Normal copy for insrow=inscol=0
void chrono::ChMatrix< Real >::RowColInterp | ( | Real | vmin, |
Real | vmax, | ||
Real | umin, | ||
Real | umax | ||
) |
Fills a matrix or a vector with a bilinear interpolation, from corner values (as a u-v patch).
void chrono::ChMatrix< Real >::Set_Xq_matrix | ( | const ChQuaternion< RealB > & | q | ) |
Fills a 4x4 matrix as the "star" matrix, representing quaternion cross product.
That is, given two quaternions a and b, aXb= [Astar]*b
void chrono::ChMatrix< Real >::StreamOUTdenseMatlabFormat | ( | ChStreamOutAscii & | mstream | ) |
Method to allow serializing transient data into an ascii stream (ex.
a file) as a Matlab .dat file (all numbers in a row, separated by space, then CR)