All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatTDMatAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATTDMATADDEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_DMATTDMATADDEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
58 #include <blaze/util/Assert.h>
61 #include <blaze/util/SelectType.h>
62 #include <blaze/util/Types.h>
63 
64 
65 namespace blaze {
66 
67 //=================================================================================================
68 //
69 // CLASS DMATTDMATADDEXPR
70 //
71 //=================================================================================================
72 
73 //*************************************************************************************************
80 template< typename MT1 // Type of the left-hand side dense matrix
81  , typename MT2 > // Type of the right-hand side dense matrix
82 class DMatTDMatAddExpr : public DenseMatrix< DMatTDMatAddExpr<MT1,MT2>, false >
83  , private MatMatAddExpr
84  , private Computation
85 {
86  private:
87  //**Type definitions****************************************************************************
88  typedef typename MT1::ResultType RT1;
89  typedef typename MT2::ResultType RT2;
90  typedef typename MT1::ReturnType RN1;
91  typedef typename MT2::ReturnType RN2;
92  typedef typename MT1::CompositeType CT1;
93  typedef typename MT2::CompositeType CT2;
94  //**********************************************************************************************
95 
96  //**Return type evaluation**********************************************************************
98 
103  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
104 
107  //**********************************************************************************************
108 
109  public:
110  //**Type definitions****************************************************************************
116 
119 
121  typedef const ResultType CompositeType;
122 
124  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
125 
127  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
128  //**********************************************************************************************
129 
130  //**Compilation flags***************************************************************************
132  enum { vectorizable = 0 };
133  //**********************************************************************************************
134 
135  //**Constructor*********************************************************************************
141  explicit inline DMatTDMatAddExpr( const MT1& lhs, const MT2& rhs )
142  : lhs_( lhs ) // Left-hand side dense matrix of the addition expression
143  , rhs_( rhs ) // Right-hand side dense matrix of the addition expression
144  {
145  BLAZE_INTERNAL_ASSERT( lhs.rows() == rhs.rows() , "Invalid number of rows" );
146  BLAZE_INTERNAL_ASSERT( lhs.columns() == rhs.columns(), "Invalid number of columns" );
147  }
148  //**********************************************************************************************
149 
150  //**Access operator*****************************************************************************
157  inline ReturnType operator()( size_t i, size_t j ) const {
158  BLAZE_INTERNAL_ASSERT( i < lhs_.rows() , "Invalid row access index" );
159  BLAZE_INTERNAL_ASSERT( j < lhs_.columns(), "Invalid column access index" );
160  return lhs_(i,j) + rhs_(i,j);
161  }
162  //**********************************************************************************************
163 
164  //**Rows function*******************************************************************************
169  inline size_t rows() const {
170  return lhs_.rows();
171  }
172  //**********************************************************************************************
173 
174  //**Columns function****************************************************************************
179  inline size_t columns() const {
180  return lhs_.columns();
181  }
182  //**********************************************************************************************
183 
184  //**Left operand access*************************************************************************
189  inline LeftOperand leftOperand() const {
190  return lhs_;
191  }
192  //**********************************************************************************************
193 
194  //**Right operand access************************************************************************
199  inline RightOperand rightOperand() const {
200  return rhs_;
201  }
202  //**********************************************************************************************
203 
204  //**********************************************************************************************
210  template< typename T >
211  inline bool canAlias( const T* alias ) const {
212  return ( IsExpression<MT1>::value && ( RequiresEvaluation<MT1>::value ? lhs_.isAliased( alias ) : lhs_.canAlias( alias ) ) ) ||
213  ( IsExpression<MT2>::value && ( RequiresEvaluation<MT2>::value ? rhs_.isAliased( alias ) : rhs_.canAlias( alias ) ) );
214  }
215  //**********************************************************************************************
216 
217  //**********************************************************************************************
223  template< typename T >
224  inline bool isAliased( const T* alias ) const {
225  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
226  }
227  //**********************************************************************************************
228 
229  private:
230  //**Member variables****************************************************************************
233  //**********************************************************************************************
234 
235  //**Assignment to dense matrices****************************************************************
247  template< typename MT // Type of the target dense matrix
248  , bool SO2 > // Storage order of the target dense matrix
249  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const DMatTDMatAddExpr& rhs )
250  {
252 
253  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
254  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
255 
256  // In case non of the two dense operands requires an intermediate evaluation, the
257  // addition expression is assigned directly in a cache-efficient manner.
259  {
260  const size_t m( rhs.rows() );
261  const size_t n( rhs.columns() );
262  const size_t block( 16UL );
263 
264  for( size_t ii=0UL; ii<m; ii+=block ) {
265  const size_t iend( ( m < ii+block )?( m ):( ii+block ) );
266  for( size_t jj=0UL; jj<n; jj+=block ) {
267  const size_t jend( ( n < jj+block )?( n ):( jj+block ) );
268  for( size_t i=ii; i<iend; ++i ) {
269  for( size_t j=jj; j<jend; ++j ) {
270  (~lhs)(i,j) = rhs.lhs_(i,j) + rhs.rhs_(i,j);
271  }
272  }
273  }
274  }
275  }
276 
277  // In case either of the two dense operands requires an intermediate evaluation, the
278  // expression is evaluated in a one- or two-step approach (depending on whether any
279  // of the operands is aliased with the target matrix).
280  else if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
281  addAssign( ~lhs, rhs.rhs_ );
282  }
283  else if( !IsExpression<MT2>::value && (~lhs).isAliased( &rhs.rhs_ ) ) {
284  addAssign( ~lhs, rhs.lhs_ );
285  }
286  else {
287  assign ( ~lhs, rhs.lhs_ );
288  addAssign( ~lhs, rhs.rhs_ );
289  }
290  }
292  //**********************************************************************************************
293 
294  //**Assignment to sparse matrices***************************************************************
306  template< typename MT // Type of the target sparse matrix
307  , bool SO2 > // Storage order of the target sparse matrix
308  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const DMatTDMatAddExpr& rhs )
309  {
311 
312  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
313 
320 
321  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
322  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
323 
324  const TmpType tmp( rhs );
325  assign( ~lhs, tmp );
326  }
328  //**********************************************************************************************
329 
330  //**Addition assignment to dense matrices*******************************************************
342  template< typename MT // Type of the target dense matrix
343  , bool SO2 > // Storage order of the target dense matrix
344  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const DMatTDMatAddExpr& rhs )
345  {
347 
348  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
349  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
350 
351  // In case non of the two dense operands requires an intermediate evaluation, the
352  // addition expression is assigned directly in a cache-efficient manner.
353  if( !RequiresEvaluation<MT1>::value && !RequiresEvaluation<MT2>::value )
354  {
355  const size_t m( rhs.rows() );
356  const size_t n( rhs.columns() );
357  const size_t block( 16UL );
358 
359  for( size_t ii=0UL; ii<m; ii+=block ) {
360  const size_t iend( ( m < ii+block )?( m ):( ii+block ) );
361  for( size_t jj=0UL; jj<n; jj+=block ) {
362  const size_t jend( ( n < jj+block )?( n ):( jj+block ) );
363  for( size_t i=ii; i<iend; ++i ) {
364  for( size_t j=jj; j<jend; ++j ) {
365  (~lhs)(i,j) += rhs.lhs_(i,j) + rhs.rhs_(i,j);
366  }
367  }
368  }
369  }
370  }
371 
372  // In case either of the two dense operands requires an intermediate evaluation, the
373  // expression is evaluated in a two-step approach.
374  else
375  {
376  addAssign( ~lhs, rhs.lhs_ );
377  addAssign( ~lhs, rhs.rhs_ );
378  }
379  }
381  //**********************************************************************************************
382 
383  //**Addition assignment to sparse matrices******************************************************
384  // No special implementation for the addition assignment to sparse matrices.
385  //**********************************************************************************************
386 
387  //**Subtraction assignment to dense matrices****************************************************
399  template< typename MT // Type of the target dense matrix
400  , bool SO2 > // Storage order of the target dense matrix
401  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const DMatTDMatAddExpr& rhs )
402  {
404 
405  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
406  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
407 
408  // In case non of the two dense operands requires an intermediate evaluation, the
409  // addition expression is assigned directly in a cache-efficient manner.
410  if( !RequiresEvaluation<MT1>::value && !RequiresEvaluation<MT2>::value )
411  {
412  const size_t m( rhs.rows() );
413  const size_t n( rhs.columns() );
414  const size_t block( 16UL );
415 
416  for( size_t ii=0UL; ii<m; ii+=block ) {
417  const size_t iend( ( m < ii+block )?( m ):( ii+block ) );
418  for( size_t jj=0UL; jj<n; jj+=block ) {
419  const size_t jend( ( n < jj+block )?( n ):( jj+block ) );
420  for( size_t i=ii; i<iend; ++i ) {
421  for( size_t j=jj; j<jend; ++j ) {
422  (~lhs)(i,j) -= rhs.lhs_(i,j) + rhs.rhs_(i,j);
423  }
424  }
425  }
426  }
427  }
428 
429  // In case either of the two dense operands requires an intermediate evaluation, the
430  // expression is evaluated in a two-step approach.
431  else
432  {
433  subAssign( ~lhs, rhs.lhs_ );
434  subAssign( ~lhs, rhs.rhs_ );
435  }
436  }
438  //**********************************************************************************************
439 
440  //**Subtraction assignment to sparse matrices***************************************************
441  // No special implementation for the subtraction assignment to sparse matrices.
442  //**********************************************************************************************
443 
444  //**Multiplication assignment to dense matrices*************************************************
445  // No special implementation for the multiplication assignment to dense matrices.
446  //**********************************************************************************************
447 
448  //**Multiplication assignment to sparse matrices************************************************
449  // No special implementation for the multiplication assignment to sparse matrices.
450  //**********************************************************************************************
451 
452  //**Compile time checks*************************************************************************
459  //**********************************************************************************************
460 };
461 //*************************************************************************************************
462 
463 
464 
465 
466 //=================================================================================================
467 //
468 // GLOBAL BINARY ARITHMETIC OPERATORS
469 //
470 //=================================================================================================
471 
472 //*************************************************************************************************
501 template< typename T1 // Type of the left-hand side dense matrix
502  , typename T2 > // Type of the right-hand side dense matrix
503 inline const DMatTDMatAddExpr<T1,T2>
505 {
507 
508  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
509  throw std::invalid_argument( "Matrix sizes do not match" );
510 
511  return DMatTDMatAddExpr<T1,T2>( ~lhs, ~rhs );
512 }
513 //*************************************************************************************************
514 
515 
516 //*************************************************************************************************
545 template< typename T1 // Type of the left-hand side dense matrix
546  , typename T2 > // Type of the right-hand side dense matrix
547 inline const DMatTDMatAddExpr<T2,T1>
549 {
551 
552  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
553  throw std::invalid_argument( "Matrix sizes do not match" );
554 
555  return DMatTDMatAddExpr<T2,T1>( ~rhs, ~lhs );
556 }
557 //*************************************************************************************************
558 
559 
560 
561 
562 //=================================================================================================
563 //
564 // EXPRESSION TRAIT SPECIALIZATIONS
565 //
566 //=================================================================================================
567 
568 //*************************************************************************************************
570 template< typename MT1, typename MT2 >
571 struct SubmatrixExprTrait< DMatTDMatAddExpr<MT1,MT2> >
572 {
573  public:
574  //**********************************************************************************************
575  typedef typename AddExprTrait< typename SubmatrixExprTrait<const MT1>::Type
576  , typename SubmatrixExprTrait<const MT2>::Type >::Type Type;
577  //**********************************************************************************************
578 };
580 //*************************************************************************************************
581 
582 
583 //*************************************************************************************************
585 template< typename MT1, typename MT2 >
586 struct RowExprTrait< DMatTDMatAddExpr<MT1,MT2> >
587 {
588  public:
589  //**********************************************************************************************
590  typedef typename AddExprTrait< typename RowExprTrait<const MT1>::Type
591  , typename RowExprTrait<const MT2>::Type >::Type Type;
592  //**********************************************************************************************
593 };
595 //*************************************************************************************************
596 
597 
598 //*************************************************************************************************
600 template< typename MT1, typename MT2 >
601 struct ColumnExprTrait< DMatTDMatAddExpr<MT1,MT2> >
602 {
603  public:
604  //**********************************************************************************************
605  typedef typename AddExprTrait< typename ColumnExprTrait<const MT1>::Type
606  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
607  //**********************************************************************************************
608 };
610 //*************************************************************************************************
611 
612 } // namespace blaze
613 
614 #endif
Evaluation of the return type of an addition expression.Via this type trait it is possible to evaluat...
Definition: AddExprTrait.h:103
Compile time check whether the given type is a temporary vector or matrix type.This type trait class ...
Definition: IsTemporary.h:87
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:196
#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:79
Header file for the ColumnExprTrait class template.
ResultType::OppositeType OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: DMatTDMatAddExpr.h:113
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: DMatTDMatAddExpr.h:114
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2375
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:248
AddTrait< RT1, RT2 >::Type ResultType
Result type for expression template evaluations.
Definition: DMatTDMatAddExpr.h:112
Header file for the AddExprTrait class template.
Header file for the Computation base class.
bool canAlias(const T *alias) const
Returns whether the expression can alias with the given address alias.
Definition: DMatTDMatAddExpr.h:211
Header file for the RequiresEvaluation type trait.
MT1::CompositeType CT1
Composite type of the left-hand side dense matrix expression.
Definition: DMatTDMatAddExpr.h:92
Base class for dense matrices.The DenseMatrix class is a base class for all dense matrix classes...
Definition: DenseMatrix.h:70
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes...
Definition: Forward.h:104
Constraint on the data type.
DMatTDMatAddExpr(const MT1 &lhs, const MT2 &rhs)
Constructor for the DMatTDMatAddExpr class.
Definition: DMatTDMatAddExpr.h:141
Constraint on the data type.
Compile time check to query the requirement to evaluate an expression.Via this type trait it is possi...
Definition: RequiresEvaluation.h:90
MT1::ReturnType RN1
Return type of the left-hand side dense matrix expression.
Definition: DMatTDMatAddExpr.h:90
size_t rows() const
Returns the current number of rows of the matrix.
Definition: DMatTDMatAddExpr.h:169
Compile time type selection.The SelectType class template selects one of the two given types T1 and T...
Definition: SelectType.h:59
Header file for the IsTemporary type trait class.
const ResultType CompositeType
Data type for composite expression templates.
Definition: DMatTDMatAddExpr.h:121
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a column-major dense or sparse matri...
Definition: StorageOrder.h:161
const SelectType< returnExpr, ExprReturnType, ElementType >::Type ReturnType
Return type for expression template evaluations.
Definition: DMatTDMatAddExpr.h:118
Header file for the DenseMatrix base class.
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:179
Base class for all matrix/matrix addition expression templates.The MatMatAddExpr class serves as a ta...
Definition: MatMatAddExpr.h:65
const DenseIterator< Type > operator+(const DenseIterator< Type > &it, ptrdiff_t inc)
Addition between a DenseIterator and an integral value.
Definition: DenseIterator.h:556
AddExprTrait< RN1, RN2 >::Type ExprReturnType
Expression return type for the subscript operator.
Definition: DMatTDMatAddExpr.h:106
#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:78
size_t columns() const
Returns the current number of columns of the matrix.
Definition: DMatTDMatAddExpr.h:179
Constraints on the storage order of matrix types.
Header file for the SelectType class template.
Header file for the RowExprTrait class template.
Header file for all forward declarations for expression class templates.
bool isAliased(const T *alias) const
Returns whether the expression is aliased with the given address alias.
Definition: DMatTDMatAddExpr.h:224
DMatTDMatAddExpr< MT1, MT2 > This
Type of this DMatTDMatAdd instance.
Definition: DMatTDMatAddExpr.h:111
MT2::ResultType RT2
Result type of the right-hand side dense matrix expression.
Definition: DMatTDMatAddExpr.h:89
Header file for the SubmatrixExprTrait class template.
SelectType< IsExpression< MT2 >::value, const MT2, const MT2 & >::Type RightOperand
Composite type of the right-hand side dense matrix expression.
Definition: DMatTDMatAddExpr.h:127
#define BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a row-major dense or sparse matrix t...
Definition: StorageOrder.h:81
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2374
LeftOperand lhs_
Left-hand side dense matrix of the addition expression.
Definition: DMatTDMatAddExpr.h:231
Header file for run time assertion macros.
Base template for the AddTrait class.
Definition: AddTrait.h:141
Header file for the addition trait.
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:209
ResultType::ElementType ElementType
Resulting element type.
Definition: DMatTDMatAddExpr.h:115
RightOperand rhs_
Right-hand side dense matrix of the addition expression.
Definition: DMatTDMatAddExpr.h:232
RightOperand rightOperand() const
Returns the right-hand side transpose dense matrix operand.
Definition: DMatTDMatAddExpr.h:199
LeftOperand leftOperand() const
Returns the left-hand side dense matrix operand.
Definition: DMatTDMatAddExpr.h:189
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:239
MT2::ReturnType RN2
Return type of the right-hand side dense matrix expression.
Definition: DMatTDMatAddExpr.h:91
Header file for the MatMatAddExpr base class.
#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:283
SelectType< IsExpression< MT1 >::value, const MT1, const MT1 & >::Type LeftOperand
Composite type of the left-hand side dense matrix expression.
Definition: DMatTDMatAddExpr.h:124
MT1::ResultType RT1
Result type of the left-hand side dense matrix expression.
Definition: DMatTDMatAddExpr.h:88
ReturnType operator()(size_t i, size_t j) const
2D-access to the matrix elements.
Definition: DMatTDMatAddExpr.h:157
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
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2370
size_t columns(const Matrix< MT, SO > &m)
Returns the current number of columns of the matrix.
Definition: Matrix.h:154
Header file for basic type definitions.
MT2::CompositeType CT2
Composite type of the right-hand side dense matrix expression.
Definition: DMatTDMatAddExpr.h:93
size_t rows(const Matrix< MT, SO > &m)
Returns the current number of rows of the matrix.
Definition: Matrix.h:138
#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
Compile time check whether the given type is an expression template.This type trait class tests wheth...
Definition: IsExpression.h:87
Header file for the IsExpression type trait class.
Header file for the FunctionTrace class.
Expression object for dense matrix-transpose dense matrix additions.The DMatTDMatAddExpr class repres...
Definition: DMatTDMatAddExpr.h:82