DMatInvExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATINVEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_DMATINVEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
46 #include <blaze/math/Exception.h>
67 #include <blaze/util/Assert.h>
70 #include <blaze/util/InvalidType.h>
72 #include <blaze/util/mpl/And.h>
73 #include <blaze/util/mpl/If.h>
74 #include <blaze/util/Types.h>
75 
76 
77 namespace blaze {
78 
79 //=================================================================================================
80 //
81 // CLASS DMATINVEXPR
82 //
83 //=================================================================================================
84 
85 //*************************************************************************************************
91 template< typename MT // Type of the dense matrix
92  , bool SO > // Storage order
93 class DMatInvExpr : public DenseMatrix< DMatInvExpr<MT,SO>, SO >
94  , private MatInvExpr
95  , private Computation
96 {
97  private:
98  //**Type definitions****************************************************************************
99  typedef ResultType_<MT> RT;
101  //**********************************************************************************************
102 
103  public:
104  //**Type definitions****************************************************************************
111 
113  typedef const ResultType CompositeType;
114 
116  typedef If_< IsExpression<MT>, const MT, const MT& > Operand;
117  //**********************************************************************************************
118 
119  //**Compilation flags***************************************************************************
121  enum : bool { simdEnabled = false };
122 
124  enum : bool { smpAssignable = false };
125  //**********************************************************************************************
126 
127  //**Constructor*********************************************************************************
132  explicit inline DMatInvExpr( const MT& dm ) noexcept
133  : dm_( dm ) // Dense matrix of the inversion expression
134  {}
135  //**********************************************************************************************
136 
137  //**Rows function*******************************************************************************
142  inline size_t rows() const noexcept {
143  return dm_.columns();
144  }
145  //**********************************************************************************************
146 
147  //**Columns function****************************************************************************
152  inline size_t columns() const noexcept {
153  return dm_.rows();
154  }
155  //**********************************************************************************************
156 
157  //**Operand access******************************************************************************
162  inline Operand operand() const noexcept {
163  return dm_;
164  }
165  //**********************************************************************************************
166 
167  //**********************************************************************************************
173  template< typename T >
174  inline bool canAlias( const T* alias ) const noexcept {
175  return dm_.isAliased( alias );
176  }
177  //**********************************************************************************************
178 
179  //**********************************************************************************************
185  template< typename T >
186  inline bool isAliased( const T* alias ) const noexcept {
187  return dm_.isAliased( alias );
188  }
189  //**********************************************************************************************
190 
191  private:
192  //**Member variables****************************************************************************
193  Operand dm_;
194  //**********************************************************************************************
195 
196  //**********************************************************************************************
202  static constexpr InversionFlag getInversionFlag() noexcept {
203  if ( IsDiagonal<MT>::value ) return asDiagonal;
204  else if( IsUniUpper<MT>::value ) return asUniUpper;
205  else if( IsUpper<MT>::value ) return asUpper;
206  else if( IsUniLower<MT>::value ) return asUniLower;
207  else if( IsLower<MT>::value ) return asLower;
208  else if( IsHermitian<MT>::value ) return asHermitian;
209  else if( IsSymmetric<MT>::value ) return asSymmetric;
210  else return asGeneral;
211  }
213  //**********************************************************************************************
214 
215  //**Assignment to dense matrices****************************************************************
227  template< typename MT2 // Type of the target dense matrix
228  , bool SO2 > // Storage order of the target dense matrix
229  friend inline void assign( DenseMatrix<MT2,SO2>& lhs, const DMatInvExpr& rhs )
230  {
232 
233  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
234  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
235 
236  if( !isSame( ~lhs, rhs.dm_ ) ) {
237  assign( ~lhs, rhs.dm_ );
238  }
239 
240  invert< DMatInvExpr::getInversionFlag() >( ~lhs );
241  }
243  //**********************************************************************************************
244 
245  //**Assignment to sparse matrices***************************************************************
257  template< typename MT2 // Type of the target sparse matrix
258  , bool SO2 > // Storage order of the target sparse matrix
259  friend inline void assign( SparseMatrix<MT2,SO2>& lhs, const DMatInvExpr& rhs )
260  {
262 
263  typedef IfTrue_< SO == SO2, ResultType, OppositeType > TmpType;
264 
270  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( CompositeType_<TmpType> );
271 
272  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
273  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
274 
275  const TmpType tmp( serial( rhs ) );
276  assign( ~lhs, tmp );
277  }
279  //**********************************************************************************************
280 
281  //**Addition assignment to dense matrices*******************************************************
293  template< typename MT2 // Type of the target dense matrix
294  , bool SO2 > // Storage order of the target dense matrix
295  friend inline void addAssign( DenseMatrix<MT2,SO2>& lhs, const DMatInvExpr& rhs )
296  {
298 
299  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
300  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
301 
302  const ResultType tmp( serial( rhs ) );
303  addAssign( ~lhs, tmp );
304  }
306  //**********************************************************************************************
307 
308  //**Addition assignment to sparse matrices******************************************************
309  // No special implementation for the addition assignment to sparse matrices.
310  //**********************************************************************************************
311 
312  //**Subtraction assignment to dense matrices****************************************************
324  template< typename MT2 // Type of the target dense matrix
325  , bool SO2 > // Storage order of the target dense matrix
326  friend inline void subAssign( DenseMatrix<MT2,SO2>& lhs, const DMatInvExpr& rhs )
327  {
329 
330  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
331  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
332 
333  const ResultType tmp( serial( rhs ) );
334  subAssign( ~lhs, tmp );
335  }
337  //**********************************************************************************************
338 
339  //**Subtraction assignment to sparse matrices***************************************************
340  // No special implementation for the subtraction assignment to sparse matrices.
341  //**********************************************************************************************
342 
343  //**Multiplication assignment to dense matrices*************************************************
344  // No special implementation for the multiplication assignment to dense matrices.
345  //**********************************************************************************************
346 
347  //**Multiplication assignment to sparse matrices************************************************
348  // No special implementation for the multiplication assignment to sparse matrices.
349  //**********************************************************************************************
350 
351  //**Compile time checks*************************************************************************
356  //**********************************************************************************************
357 };
358 //*************************************************************************************************
359 
360 
361 
362 
363 //=================================================================================================
364 //
365 // GLOBAL OPERATORS
366 //
367 //=================================================================================================
368 
369 //*************************************************************************************************
399 template< typename MT // Type of the dense matrix
400  , bool SO > // Storage order
401 inline const DMatInvExpr<MT,SO> inv( const DenseMatrix<MT,SO>& dm )
402 {
404 
405  if( !isSquare( ~dm ) ) {
406  BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
407  }
408 
409  return DMatInvExpr<MT,SO>( ~dm );
410 }
411 //*************************************************************************************************
412 
413 
414 
415 
416 //=================================================================================================
417 //
418 // GLOBAL RESTRUCTURING FUNCTIONS
419 //
420 //=================================================================================================
421 
422 //*************************************************************************************************
442 template< typename MT // Type of the dense matrix
443  , bool SO > // Storage order
444 inline typename DMatInvExpr<MT,SO>::Operand inv( const DMatInvExpr<MT,SO>& dm )
445 {
447 
448  return dm.operand();
449 }
451 //*************************************************************************************************
452 
453 
454 
455 
456 //=================================================================================================
457 //
458 // ROWS SPECIALIZATIONS
459 //
460 //=================================================================================================
461 
462 //*************************************************************************************************
464 template< typename MT, bool SO >
465 struct Rows< DMatInvExpr<MT,SO> > : public Rows<MT>
466 {};
468 //*************************************************************************************************
469 
470 
471 
472 
473 //=================================================================================================
474 //
475 // COLUMNS SPECIALIZATIONS
476 //
477 //=================================================================================================
478 
479 //*************************************************************************************************
481 template< typename MT, bool SO >
482 struct Columns< DMatInvExpr<MT,SO> > : public Columns<MT>
483 {};
485 //*************************************************************************************************
486 
487 
488 
489 
490 //=================================================================================================
491 //
492 // ISSYMMETRIC SPECIALIZATIONS
493 //
494 //=================================================================================================
495 
496 //*************************************************************************************************
498 template< typename MT, bool SO >
499 struct IsSymmetric< DMatInvExpr<MT,SO> >
500  : public BoolConstant< IsSymmetric<MT>::value >
501 {};
503 //*************************************************************************************************
504 
505 
506 
507 
508 //=================================================================================================
509 //
510 // ISHERMITIAN SPECIALIZATIONS
511 //
512 //=================================================================================================
513 
514 //*************************************************************************************************
516 template< typename MT, bool SO >
517 struct IsHermitian< DMatInvExpr<MT,SO> >
518  : public BoolConstant< IsHermitian<MT>::value >
519 {};
521 //*************************************************************************************************
522 
523 
524 
525 
526 //=================================================================================================
527 //
528 // ISLOWER SPECIALIZATIONS
529 //
530 //=================================================================================================
531 
532 //*************************************************************************************************
534 template< typename MT, bool SO >
535 struct IsLower< DMatInvExpr<MT,SO> >
536  : public BoolConstant< IsLower<MT>::value >
537 {};
539 //*************************************************************************************************
540 
541 
542 
543 
544 //=================================================================================================
545 //
546 // ISUNILOWER SPECIALIZATIONS
547 //
548 //=================================================================================================
549 
550 //*************************************************************************************************
552 template< typename MT, bool SO >
553 struct IsUniLower< DMatInvExpr<MT,SO> >
554  : public BoolConstant< IsUniLower<MT>::value >
555 {};
557 //*************************************************************************************************
558 
559 
560 
561 
562 //=================================================================================================
563 //
564 // ISUPPER SPECIALIZATIONS
565 //
566 //=================================================================================================
567 
568 //*************************************************************************************************
570 template< typename MT, bool SO >
571 struct IsUpper< DMatInvExpr<MT,SO> >
572  : public BoolConstant< IsUpper<MT>::value >
573 {};
575 //*************************************************************************************************
576 
577 
578 
579 
580 //=================================================================================================
581 //
582 // ISUNIUPPER SPECIALIZATIONS
583 //
584 //=================================================================================================
585 
586 //*************************************************************************************************
588 template< typename MT, bool SO >
589 struct IsUniUpper< DMatInvExpr<MT,SO> >
590  : public BoolConstant< IsUniUpper<MT>::value >
591 {};
593 //*************************************************************************************************
594 
595 
596 
597 
598 //=================================================================================================
599 //
600 // EXPRESSION TRAIT SPECIALIZATIONS
601 //
602 //=================================================================================================
603 
604 //*************************************************************************************************
606 template< typename MT >
607 struct DMatInvExprTrait< DMatInvExpr<MT,false> >
608 {
609  public:
610  //**********************************************************************************************
611  using Type = If_< And< IsDenseMatrix<MT>, IsRowMajorMatrix<MT> >
612  , Operand_< DMatInvExpr<MT,false> >
613  , INVALID_TYPE >;
614  //**********************************************************************************************
615 };
617 //*************************************************************************************************
618 
619 
620 //*************************************************************************************************
622 template< typename MT >
623 struct TDMatInvExprTrait< DMatInvExpr<MT,true> >
624 {
625  public:
626  //**********************************************************************************************
627  using Type = If_< And< IsDenseMatrix<MT>, IsColumnMajorMatrix<MT> >
628  , Operand_< DMatInvExpr<MT,true> >
629  , INVALID_TYPE >;
630  //**********************************************************************************************
631 };
633 //*************************************************************************************************
634 
635 } // namespace blaze
636 
637 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for auxiliary alias declarations.
TransposeType_< MT > TransposeType
Transpose type for expression template evaluations.
Definition: DMatInvExpr.h:108
Operand dm_
Dense matrix of the inversion expression.
Definition: DMatInvExpr.h:193
Header file for the Rows type trait.
Header file for the IsUniUpper type trait.
BLAZE_ALWAYS_INLINE bool isSame(const Matrix< MT1, SO1 > &a, const Matrix< MT2, SO2 > &b) noexcept
Returns whether the two given matrices represent the same observable state.
Definition: Matrix.h:653
Header file for basic type definitions.
ResultType_< MT > ResultType
Result type for expression template evaluations.
Definition: DMatInvExpr.h:106
Header file for the serial shim.
#define BLAZE_CONSTRAINT_MUST_BE_MATRIX_WITH_STORAGE_ORDER(T, SO)
Constraint on the data type.In case the given data type T is not a dense or sparse matrix type and in...
Definition: StorageOrder.h:63
Header file for the IsDiagonal type trait.
Header file for the TDMatInvExprTrait class template.
Flag for the inversion of a Hermitian matrix (same as byLDLH).
Definition: InversionFlag.h:110
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional matrix type...
Definition: DenseMatrix.h:61
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: DMatInvExpr.h:186
Flag for the inversion of a diagonal matrix.
Definition: InversionFlag.h:115
Header file for the IsColumnMajorMatrix type trait.
Flag for the inversion of a general matrix (same as byLU).
Definition: InversionFlag.h:108
size_t columns() const noexcept
Returns the current number of columns of the matrix.
Definition: DMatInvExpr.h:152
Flag for the inversion of a upper unitriangular matrix.
Definition: InversionFlag.h:114
Header file for the And class template.
Compile time check for lower triangular matrices.This type trait tests whether or not the given templ...
Definition: IsLower.h:88
const DMatSerialExpr< MT, SO > serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:723
Header file for the Computation base class.
Compile time check for upper triangular matrices.This type trait tests whether or not the given templ...
Definition: IsUpper.h:88
Flag for the inversion of a upper triangular matrix.
Definition: InversionFlag.h:113
Header file for the MatInvExpr base class.
Header file for the IsUniLower type trait.
typename T::ResultType ResultType_
Alias declaration for nested ResultType type definitions.The ResultType_ alias declaration provides a...
Definition: Aliases.h:323
Base class for dense matrices.The DenseMatrix class is a base class for all dense matrix classes...
Definition: DenseMatrix.h:70
Constraint on the data type.
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:343
If_< IsExpression< MT >, const MT, const MT & > Operand
Composite data type of the dense matrix expression.
Definition: DMatInvExpr.h:116
DMatInvExpr(const MT &dm) noexcept
Constructor for the DMatInvExpr class.
Definition: DMatInvExpr.h:132
Constraint on the data type.
typename T::CompositeType CompositeType_
Alias declaration for nested CompositeType type definitions.The CompositeType_ alias declaration prov...
Definition: Aliases.h:83
Compile time check for upper unitriangular matrices.This type trait tests whether or not the given te...
Definition: IsUniUpper.h:86
Flag for the inversion of a symmetric matrix (same as byLDLT).
Definition: InversionFlag.h:109
Header file for the IsSymmetric type trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the If class template.
OppositeType_< MT > OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: DMatInvExpr.h:107
Header file for the DenseMatrix base class.
Header file for the Columns type trait.
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
Header file for the IsLower type trait.
Compile time check for diagonal matrices.This type trait tests whether or not the given template para...
Definition: IsDiagonal.h:90
ReturnType_< MT > ReturnType
Return type for expression template evaluations.
Definition: DMatInvExpr.h:110
#define BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:60
Operand operand() const noexcept
Returns the dense matrix operand.
Definition: DMatInvExpr.h:162
Constraints on the storage order of matrix types.
Compile time check for symmetric matrices.This type trait tests whether or not the given template par...
Definition: IsSymmetric.h:85
Header file for the exception macros of the math module.
Header file for all forward declarations for expression class templates.
Header file for the IsDenseMatrix type trait.
InversionFlag
Inversion flag.The InversionFlag type enumeration represents the different types of matrix inversion ...
Definition: InversionFlag.h:101
Compile time check for lower unitriangular matrices.This type trait tests whether or not the given te...
Definition: IsUniLower.h:86
Flag for the inversion of a lower triangular matrix.
Definition: InversionFlag.h:111
Header file for the DMatInvExprTrait class template.
Header file for run time assertion macros.
Utility type for generic codes.
typename If< T1, T2, T3 >::Type If_
Auxiliary alias declaration for the If class template.The If_ alias declaration provides a convenient...
Definition: If.h:160
Compile time check for Hermitian matrices.This type trait tests whether or not the given template par...
Definition: IsHermitian.h:85
Base class for all matrix inversion expression templates.The MatInvExpr class serves as a tag for all...
Definition: MatInvExpr.h:65
DMatInvExpr< MT, SO > This
Type of this DMatInvExpr instance.
Definition: DMatInvExpr.h:105
IntegralConstant< bool, B > BoolConstant
Generic wrapper for a compile time constant boolean value.The BoolConstant class template represents ...
Definition: IntegralConstant.h:100
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: DMatInvExpr.h:174
typename T::OppositeType OppositeType_
Alias declaration for nested OppositeType type definitions.The OppositeType_ alias declaration provid...
Definition: Aliases.h:243
#define BLAZE_CONSTRAINT_MATRICES_MUST_HAVE_SAME_STORAGE_ORDER(T1, T2)
Constraint on the data type.In case either of the two given data types T1 or T2 is not a matrix type ...
Definition: StorageOrder.h:84
Expression object for dense matrix inversions.The DMatInvExpr class represents the compile time expre...
Definition: DMatInvExpr.h:93
ElementType_< MT > ElementType
Resulting element type.
Definition: DMatInvExpr.h:109
Header file for the IsRowMajorMatrix type trait.
Base class for all compute expression templates.The Computation class serves as a tag for all computa...
Definition: Computation.h:59
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:157
const ResultType CompositeType
Data type for composite expression templates.
Definition: DMatInvExpr.h:113
Header file for the IntegralConstant class template.
typename T::TransposeType TransposeType_
Alias declaration for nested TransposeType type definitions.The TransposeType_ alias declaration prov...
Definition: Aliases.h:403
Header file for the IsUpper type trait.
Flag for the inversion of a lower unitriangular matrix.
Definition: InversionFlag.h:112
Header file for the IsHermitian type trait.
const DMatInvExpr< MT, SO > inv(const DenseMatrix< MT, SO > &dm)
Calculation of the inverse of the given dense matrix.
Definition: DMatInvExpr.h:401
BLAZE_ALWAYS_INLINE bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:609
ResultType_< MT > RT
Result type of the dense matrix expression.
Definition: DMatInvExpr.h:99
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the IsExpression type trait class.
CompositeType_< MT > CT
Composite type of the dense matrix expression.
Definition: DMatInvExpr.h:100
size_t rows() const noexcept
Returns the current number of rows of the matrix.
Definition: DMatInvExpr.h:142
Header file for the FunctionTrace class.