35 #ifndef _MATRIXSMALL_H_ 36 #define _MATRIXSMALL_H_ 1
38 #include <lifev/core/LifeV.hpp> 40 #include <lifev/core/mesh/MeshVertex.hpp> 41 #include <lifev/core/array/RNM.hpp> 42 #include <lifev/core/array/VectorSmall.hpp> 50 #define MATRIX_SMALL_DIMENSION_CHECK_NO_CHECK 0
51 #define MATRIX_SMALL_DIMENSION_CHECK_ASSERT 1
52 #define MATRIX_SMALL_DIMENSION_CHECK_EXCEPTION 2
77 typedef Real& (MatrixSmall::*DereferenceMethod) (
UInt const& i );
78 typedef const Real& (MatrixSmall::*ConstDereferenceMethod) (
UInt const& i )
const;
83 void copyFrom ( MatrixSmall<Dim1, Dim2>
const& matrix )
85 for (
UInt i = 0; i < Dim1; i++ )
86 for (
UInt j = 0; j < Dim2; j++ )
88 M_coords[i][j] = matrix.M_coords[i][j];
98 return (fabs (r1 - r2) <= 0.1);
109 for (
UInt i = 0; i < Dim1 * Dim2; i++ )
125 bool isDim2Appropriate =
true;
126 for (
unsigned i = 0; i < matrix.size(); i++)
127 if (matrix[i].size() != Dim2)
129 isDim2Appropriate =
false;
132 ASSERT ( matrix.size() == Dim1 && isDim2Appropriate,
133 " Non matching dimension for the construction of a fixed size matrix via vector ");
135 for (
unsigned int i = 0; i < Dim1; ++i)
137 for (
unsigned int j = 0; j < Dim2; ++j)
139 M_coords[i][j] = matrix[i][j];
150 bool operator== ( MatrixSmall<Dim1, Dim2>
const& matrix )
const 152 for (
UInt i = 0; i < Dim1; i++ )
153 for (
UInt j = 0; j < Dim2; j++ )
162 bool operator!= ( MatrixSmall<Dim1, Dim2>
const& matrix )
const 164 return ! (*
this == matrix);
168 MatrixSmall<Dim1, Dim2>&
operator= ( MatrixSmall<Dim1, Dim2>
const& matrix )
177 MatrixSmall<Dim1, Dim2>&
operator+= ( MatrixSmall<Dim1, Dim2>
const& matrix )
179 for (
UInt i = 0; i < Dim1; i++ )
180 for (
UInt j = 0; j < Dim2; j++ )
182 M_coords[ i ][ j ] += matrix.M_coords[ i ][ j ];
188 MatrixSmall<Dim1, Dim2>
operator+ ( MatrixSmall<Dim1, Dim2>
const& matrix )
const 190 MatrixSmall<Dim1, Dim2> tmp ( *
this );
191 return (tmp += matrix);
195 MatrixSmall<Dim1, Dim2>&
operator-= ( MatrixSmall<Dim1, Dim2>
const& matrix )
197 for (
UInt i = 0; i < Dim1; i++ )
198 for (
UInt j = 0; j < Dim2; j++ )
200 M_coords[ i ][ j ] -= matrix.M_coords[ i ][ j ];
206 MatrixSmall<Dim1, Dim2>
operator- ( MatrixSmall<Dim1, Dim2>
const& matrix )
const 208 MatrixSmall<Dim1, Dim2> tmp ( *
this );
209 return (tmp -= matrix);
215 for (
UInt i = 0; i < Dim1; i++ )
216 for (
UInt j = 0; j < Dim2; j++ )
226 ASSERT ( factor != 0. ,
"Division by zero!" );
227 *
this *= 1. / factor;
234 MatrixSmall<Dim1, Dim2> tmp ( *
this );
235 return (tmp /= factor);
241 MatrixSmall<Dim1, Dim2> tmp ( *
this );
242 return (tmp *= factor);
249 for (
UInt i = 0; i < Dim1; i++ )
252 for (
UInt j = 0; j < Dim2; j++ )
254 resultVector[i] += vector[j] * (*
this) [i][j];
257 return (resultVector);
261 MatrixSmall<Dim1, Dim2>
operator* ( MatrixSmall<Dim2, Dim1>
const& matrix )
const 263 MatrixSmall<Dim1, Dim1> resultMatrix;
264 for (
UInt i = 0; i < Dim1; i++ )
266 for (
UInt j = 0; j < Dim1; j++ )
268 resultMatrix[i][j] = 0;
269 for (
UInt k = 0; k < Dim2; k++ )
271 resultMatrix[i][j] +=
M_coords[i][k] * matrix.M_coords[k][j];
275 return (resultMatrix);
283 ASSERT ( i < Dim1,
"trying to access an index that exceeds the first dimension of the matrix" );
290 ASSERT ( i < Dim1,
"trying to access an index that exceeds the first dimension of the matrix" );
297 ASSERT ( i < Dim1 && j < Dim2,
"trying to access an index that exceeds the first dimension of the matrix" );
304 return (
const_cast<
Real&> (
const_cast<
const MatrixSmall<Dim1, Dim2>&> (*
this) (i, j) ) );
317 Real dot ( MatrixSmall<Dim1, Dim2>
const& matrix )
const 319 Real scalarProduct = 0.;
320 for (
UInt i = 0; i < Dim1; i++ )
321 for (
UInt j = 0; j < Dim2; j++ )
323 scalarProduct +=
M_coords[ i ][ j ] * matrix.M_coords[ i ][ j ];
325 return scalarProduct;
334 MatrixSmall<Dim1, Dim2>
emult ( MatrixSmall<Dim1, Dim2>
const& matrix )
const 336 MatrixSmall<Dim1, Dim2> resultantMatrix ;
337 for (
UInt i = 0; i < Dim1; i++ )
338 for (
UInt j = 0; j < Dim2; j++ )
340 resultantMatrix.M_coords[ i ][ j ] =
M_coords[ i ][ j ] * matrix.M_coords[ i ][ j ];
342 return resultantMatrix;
353 MatrixSmall<Dim1, Dim2> resultantMatrix ;
354 for (
UInt i = 0; i < Dim1; i++ )
355 for (
UInt j = 0; j < Dim2; j++ )
357 resultantMatrix.M_coords[ i ][ j ] =
M_coords[ i ][ j ] * vector[ i ];
359 return resultantMatrix;
371 for (
UInt j = 0; j < Dim2; j++ )
386 for (
UInt i = 0; i < Dim1; i++ )
409 MatrixSmall<Dim2, Dim1> resultantMatrix ;
410 for (
UInt j = 0; j < Dim2; ++j)
411 for (
UInt i = 0; i < Dim1; ++i)
413 resultantMatrix.M_coords[ j ][ i ] =
M_coords[ i ][ j ];
416 return (resultantMatrix);
427 ASSERT ( Dim2 == Dim1,
"The determinat is defined only for squared matrices!");
446 ERROR_MSG (
"The determinat for matrices is implemented for Dim1 = Dim2 < 3!");
461 ASSERT ( Dim2 == Dim1,
"The cofactor is defined only for squared matrices!");
465 MatrixSmall<Dim1, Dim2> cofactor (*
this);
470 cofactor[ 0][0 ] = 1.0;
490 ERROR_MSG (
"The cofactor for matrices is implemented for Dim1 = Dim2 < 3!");
503 for (
Int i = 0; i < Dim1; i++ )
505 for (
Int j = 0; j < Dim2; j++ )
507 std::cout <<
"M_coords[ " << i <<
" ][ " << j <<
" ]= " << M_coords[i][j] << std::endl;
523 ASSERT ( Dim2 == Dim1,
"This method is based on the cofactor and determinant methods which are defined only for squared matrices!");
527 MatrixSmall<Dim1, Dim2> minusT (*
this);
531 minusT =
this->cofactor();
532 det =
this->determinant();
550 ASSERT ( Dim2 == Dim1,
"This method is based on the cofactor and determinant methods which are defined only for squared matrices!");
554 MatrixSmall<Dim1, Dim2> minusT (*
this);
556 minusT =
this->minusTransposed();
558 return minusT.transpose( );
564 ASSERT ( Dim2 == Dim1,
"The trace is defined only for squared matrices!");
568 for (
UInt i (0); i < Dim1; i++ )
582 return std::sqrt (
this->dot ( *
this ) );
597 return ( ( *
this ) /
norm () );
616 friend std::ostream& operator<< ( std::ostream& out, MatrixSmall<Dim1, Dim2>
const& matrix )
618 out <<
"(" << std::endl ;
619 for (
UInt i = 0; i < Dim1; i++ )
621 for (
UInt j = 0; j < Dim2; j++ )
623 out << matrix.M_coords[i][j] <<
" ";
640 template <
typename Matrix>
643 MatrixSmall<Dim1, Dim2> tmp;
644 for (
UInt i = 0; i < Dim1; i++ )
645 for (
UInt j = 0; j < Dim2; j++ )
647 tmp.M_coords[ i ][ j ] = coords[ i ][ j ];
669 inline MatrixSmall<Dim1, Dim2>
operator* (
Real const& factor, MatrixSmall<Dim1, Dim2>
const& matrix )
671 MatrixSmall<Dim1, Dim2> tmp ( matrix );
672 return (tmp *= factor);
679 return (matrix * vector);
MatrixSmall< Dim1, Dim2 > & operator+=(MatrixSmall< Dim1, Dim2 > const &matrix)
Operator +=.
void showMe() const
Plot the Matrix.
MatrixSmall< Dim1, Dim2 > operator*(Real const &factor, MatrixSmall< Dim1, Dim2 > const &matrix)
Operator * (multiplication by scalar on the left)
MatrixSmall< Dim1, Dim2 > & operator/=(Real const &factor)
Operator /= (division by scalar)
void normalize()
Normalize matrix.
VectorSmall< Dim1 > operator*(VectorSmall< Dim2 > const &vector) const
Operator * (multiplication by a vector)
MatrixSmall< Dim1, Dim2 > operator-(MatrixSmall< Dim1, Dim2 > const &matrix) const
Operator -.
MatrixSmall< Dim1, Dim2 > operator/(Real const &factor) const
Operator / (division by scalar)
MatrixSmall(MatrixSmall< Dim1, Dim2 > const &matrix)
Copy constructor.
friend MatrixSmall< Dim1, Dim2 > castToMatrixSmall(Matrix const &coords)
Conversion of an array (std::vector, KN, etc. if applicable) to a MatrixSmall.
MatrixSmall< Dim1, Dim2 > emult(MatrixSmall< Dim1, Dim2 > const &matrix) const
Element-wise multiplication between matrices.
OpIndexReturnType operator[](UInt const &i)
Operator [].
Real & operator()(UInt const &i, UInt const &j)
Operator ()
Real dot(MatrixSmall< Dim1, Dim2 > const &matrix) const
Scalar product.
int32_type Int
Generic integer data.
VectorSmall< Dim1 > operator*(VectorSmall< Dim2 > const &vector, MatrixSmall< Dim1, Dim2 > const &matrix)
Operator * (multiplication by vector on the left)
MatrixSmall< Dim1, Dim2 > & operator-=(MatrixSmall< Dim1, Dim2 > const &matrix)
Operator -=.
MatrixSmall< Dim1, Dim2 > operator*(MatrixSmall< Dim2, Dim1 > const &matrix) const
Operator * between two squared matrices (multiplication by a matrix)
void updateInverseJacobian(const UInt &iQuadPt)
#define MATRIX_SMALL_DIMENSION_CHECK_NO_CHECK
Real extract(UInt const &i, UInt const &j) const
Extraction of a component.
MatrixSmall< Dim1, Dim2 > operator+(MatrixSmall< Dim1, Dim2 > const &matrix) const
Operator +.
Real determinant() const
Determinant of a matrix In this class the determinant is computed explicitly for matrices of dimensio...
OpIndexReturnConstType const operator[](UInt const &i) const
Operator [].
Real const * OpIndexReturnConstType
MatrixSmall< Dim1, Dim2 > inverse() const
This method In this method, which is based on cofactor and determinant, given a matrix, its inverse is computed explicitly for matrices of dimensions 1 2 3 This method is mainly used for structural problems.
bool realEquality(const Real &r1, const Real &r2) const
MatrixSmall< Dim1, Dim2 > cofactor() const
Cofactor of a matrix In this class the cofactor is computed explicitly for matrices of dimensions 1 2...
MatrixSmall< Dim1, Dim2 > & operator*=(Real const &factor)
Operator *= (multiplication by scalar)
MatrixSmall< Dim1, Dim2 > operator*(Real const &factor) const
Operator * (division by scalar)
void copyFrom(MatrixSmall< Dim1, Dim2 > const &matrix)
double Real
Generic real data.
VectorSmall< Dim1 > extractColumn(UInt const &j) const
Extraction of a column.
MatrixSmall< Dim1, Dim2 > & operator=(MatrixSmall< Dim1, Dim2 > const &matrix)
Assignment operator.
MatrixSmall< Dim2, Dim1 > transpose() const
Transpose of a matrix.
MatrixSmall< Dim1, Dim2 > emult(VectorSmall< Dim1 > const &vector) const
Element-wise multiplication between a matrix and a vector.
bool operator==(MatrixSmall< Dim1, Dim2 > const &matrix) const
Operator ==.
MatrixSmall< Dim1, Dim2 > minusTransposed() const
This method In this method, which is based on cofactor and determinant, given a matrix, its inverse transposed is computed explicitly for matrices of dimensions 1 2 3 This method is mainly used for structural problems.
MatrixSmall()
Empty constructor (all components are set to zero)
bool operator!=(MatrixSmall< Dim1, Dim2 > const &matrix) const
Operator !=.
MatrixSmall< Dim1, Dim2 > normalized()
Create the versor associated to this MatrixSmall.
Real norm() const
Norm value.
Real const & operator()(UInt const &i, UInt const &j) const
Operator ()
MatrixSmall(const std::vector< std::vector< Real > > &matrix)
Import from a vector.
VectorSmall< Dim2 > extractRow(UInt const &i) const
Extraction of a row.
Real M_coords[Dim1][Dim2]
Data storage.
uint32_type UInt
generic unsigned integer (used mainly for addressing)