All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatSMatAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATSMATADDEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DMATSMATADDEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
47 #include <blaze/util/Assert.h>
49 #include <blaze/util/SelectType.h>
50 #include <blaze/util/Types.h>
51 
52 
53 namespace blaze {
54 
55 //=================================================================================================
56 //
57 // CLASS DMATSMATADDEXPR
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
68 template< typename MT1 // Type of the left-hand side dense matrix
69  , typename MT2 // Type of the right-hand side sparse matrix
70  , bool SO > // Storage order
71 class DMatSMatAddExpr : public DenseMatrix< DMatSMatAddExpr<MT1,MT2,SO>, SO >
72  , private Expression
73  , private Computation
74 {
75  private:
76  //**Type definitions****************************************************************************
77  typedef typename MT1::ResultType RT1;
78  typedef typename MT2::ResultType RT2;
79  typedef typename MT1::ReturnType RN1;
80  typedef typename MT2::ReturnType RN2;
81  //**********************************************************************************************
82 
83  //**Return type evaluation**********************************************************************
85 
90  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
91 
94  //**********************************************************************************************
95 
96  public:
97  //**Type definitions****************************************************************************
100  typedef typename ResultType::OppositeType OppositeType;
101  typedef typename ResultType::TransposeType TransposeType;
102  typedef typename ResultType::ElementType ElementType;
103 
106 
108  typedef const ResultType CompositeType;
109 
111  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
112 
114  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
115  //**********************************************************************************************
116 
117  //**Compilation flags***************************************************************************
119  enum { vectorizable = 0 };
120 
122  enum { canAlias = IsExpression<MT1>::value };
123  //**********************************************************************************************
124 
125  //**Constructor*********************************************************************************
131  explicit inline DMatSMatAddExpr( const MT1& lhs, const MT2& rhs )
132  : lhs_( lhs ) // Left-hand side dense matrix of the addition expression
133  , rhs_( rhs ) // Right-hand side sparse matrix of the addition expression
134  {
135  BLAZE_INTERNAL_ASSERT( lhs.rows() == rhs.rows() , "Invalid number of rows" );
136  BLAZE_INTERNAL_ASSERT( lhs.columns() == rhs.columns(), "Invalid number of columns" );
137  }
138  //**********************************************************************************************
139 
140  //**Access operator*****************************************************************************
147  inline ReturnType operator()( size_t i, size_t j ) const {
148  BLAZE_INTERNAL_ASSERT( i < lhs_.rows() , "Invalid row access index" );
149  BLAZE_INTERNAL_ASSERT( j < lhs_.columns(), "Invalid column access index" );
150  return lhs_(i,j) + rhs_(i,j);
151  }
152  //**********************************************************************************************
153 
154  //**Rows function*******************************************************************************
159  inline size_t rows() const {
160  return lhs_.rows();
161  }
162  //**********************************************************************************************
163 
164  //**Columns function****************************************************************************
169  inline size_t columns() const {
170  return lhs_.columns();
171  }
172  //**********************************************************************************************
173 
174  //**Left operand access*************************************************************************
179  inline LeftOperand leftOperand() const {
180  return lhs_;
181  }
182  //**********************************************************************************************
183 
184  //**Right operand access************************************************************************
189  inline RightOperand rightOperand() const {
190  return rhs_;
191  }
192  //**********************************************************************************************
193 
194  //**********************************************************************************************
200  template< typename T >
201  inline bool isAliased( const T* alias ) const {
202  return IsExpression<MT1>::value && lhs_.isAliased( alias );
203  }
204  //**********************************************************************************************
205 
206  private:
207  //**Member variables****************************************************************************
210  //**********************************************************************************************
211 
212  //**Assignment to dense matrices****************************************************************
224  template< typename MT // Type of the target dense matrix
225  , bool SO2 > // Storage order of the target dense matrix
226  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const DMatSMatAddExpr& rhs )
227  {
228  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
229  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
230 
231  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
232  addAssign( ~lhs, rhs.rhs_ );
233  }
234  else {
235  assign ( ~lhs, rhs.lhs_ );
236  addAssign( ~lhs, rhs.rhs_ );
237  }
238  }
240  //**********************************************************************************************
241 
242  //**Assignment to sparse matrices***************************************************************
254  template< typename MT // Type of the target sparse matrix
255  , bool SO2 > // Storage order of the target sparse matrix
256  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const DMatSMatAddExpr& rhs )
257  {
259 
265  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
266 
267  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
268  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
269 
270  const TmpType tmp( rhs );
271  assign( ~lhs, tmp );
272  }
274  //**********************************************************************************************
275 
276  //**Addition assignment to dense matrices*******************************************************
288  template< typename MT // Type of the target dense matrix
289  , bool SO2 > // Storage order of the target dense matrix
290  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const DMatSMatAddExpr& rhs )
291  {
292  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
293  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
294 
295  addAssign( ~lhs, rhs.lhs_ );
296  addAssign( ~lhs, rhs.rhs_ );
297  }
299  //**********************************************************************************************
300 
301  //**Addition assignment to sparse matrices******************************************************
302  // No special implementation for the addition assignment to sparse matrices.
303  //**********************************************************************************************
304 
305  //**Subtraction assignment to dense matrices****************************************************
317  template< typename MT // Type of the target dense matrix
318  , bool SO2 > // Storage order of the target dense matrix
319  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const DMatSMatAddExpr& rhs )
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  subAssign( ~lhs, rhs.lhs_ );
325  subAssign( ~lhs, rhs.rhs_ );
326  }
328  //**********************************************************************************************
329 
330  //**Subtraction assignment to sparse matrices***************************************************
331  // No special implementation for the subtraction assignment to sparse matrices.
332  //**********************************************************************************************
333 
334  //**Multiplication assignment to dense matrices*************************************************
335  // No special implementation for the multiplication assignment to dense matrices.
336  //**********************************************************************************************
337 
338  //**Multiplication assignment to sparse matrices************************************************
339  // No special implementation for the multiplication assignment to sparse matrices.
340  //**********************************************************************************************
341 
342  //**Compile time checks*************************************************************************
348  //**********************************************************************************************
349 };
350 //*************************************************************************************************
351 
352 
353 
354 
355 //=================================================================================================
356 //
357 // GLOBAL BINARY ARITHMETIC OPERATORS
358 //
359 //=================================================================================================
360 
361 //*************************************************************************************************
387 template< typename T1 // Type of the left-hand side dense matrix
388  , typename T2 // Type of the right-hand side sparse matrix
389  , bool SO > // Storage order
390 inline const DMatSMatAddExpr<T1,T2,SO>
392 {
393  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
394  throw std::invalid_argument( "Matrix sizes do not match" );
395 
396  return DMatSMatAddExpr<T1,T2,SO>( ~lhs, ~rhs );
397 }
398 //*************************************************************************************************
399 
400 
401 //*************************************************************************************************
427 template< typename T1 // Type of the left-hand side sparse matrix
428  , typename T2 // Type of the right-hand side dense matrix
429  , bool SO > // Storage order
430 inline const DMatSMatAddExpr<T2,T1,SO>
432 {
433  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
434  throw std::invalid_argument( "Matrix sizes do not match" );
435 
436  return DMatSMatAddExpr<T2,T1,SO>( ~rhs, ~lhs );
437 }
438 //*************************************************************************************************
439 
440 
441 
442 
443 //=================================================================================================
444 //
445 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
446 //
447 //=================================================================================================
448 
449 //*************************************************************************************************
462 template< typename T1 // Type of the dense matrix of the left-hand side expression
463  , typename T2 // Type of the sparse matrix of the left-hand side expression
464  , bool SO1 // Storage order of the left-hand side expression
465  , typename T3 // Type of the right-hand side dense matrix
466  , bool SO2 > // Storage order of the right-hand side dense matrix
467 inline const typename AddExprTrait< DMatSMatAddExpr<T1,T2,SO1>, T3 >::Type
468  operator+( const DMatSMatAddExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
469 {
470  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
471 }
473 //*************************************************************************************************
474 
475 
476 //*************************************************************************************************
489 template< typename T1 // Type of the dense matrix of the left-hand side expression
490  , typename T2 // Type of the sparse matrix of the left-hand side expression
491  , bool SO1 // Storage order of the left-hand side expression
492  , typename T3 // Type of the right-hand side dense matrix
493  , bool SO2 > // Storage order of the right-hand side dense matrix
494 inline const typename SubExprTrait< DMatSMatAddExpr<T1,T2,SO1>, T3 >::Type
495  operator-( const DMatSMatAddExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
496 {
497  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
498 }
500 //*************************************************************************************************
501 
502 
503 
504 
505 //=================================================================================================
506 //
507 // EXPRESSION TRAIT SPECIALIZATIONS
508 //
509 //=================================================================================================
510 
511 //*************************************************************************************************
513 template< typename MT1, typename MT2, typename MT3 >
514 struct DMatDMatAddExprTrait< DMatSMatAddExpr<MT1,MT2,false>, MT3 >
515 {
516  public:
517  //**********************************************************************************************
519  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
520  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
521  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
522  , typename DMatSMatAddExprTrait< typename DMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
523  , INVALID_TYPE >::Type Type;
525  //**********************************************************************************************
526 };
528 //*************************************************************************************************
529 
530 
531 //*************************************************************************************************
533 template< typename MT1, typename MT2, typename MT3 >
534 struct DMatTDMatAddExprTrait< DMatSMatAddExpr<MT1,MT2,false>, MT3 >
535 {
536  public:
537  //**********************************************************************************************
539  typedef typename SelectType< IsDenseMatrix <MT1>::value && IsRowMajorMatrix<MT1>::value &&
540  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
541  IsDenseMatrix <MT3>::value && IsColumnMajorMatrix<MT3>::value
542  , typename DMatSMatAddExprTrait< typename DMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
543  , INVALID_TYPE >::Type Type;
545  //**********************************************************************************************
546 };
548 //*************************************************************************************************
549 
550 
551 //*************************************************************************************************
553 template< typename MT1, typename MT2, typename MT3 >
554 struct TDMatDMatAddExprTrait< DMatSMatAddExpr<MT1,MT2,true>, MT3 >
555 {
556  public:
557  //**********************************************************************************************
559  typedef typename SelectType< IsDenseMatrix <MT1>::value && IsColumnMajorMatrix<MT1>::value &&
560  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
561  IsDenseMatrix <MT3>::value && IsRowMajorMatrix<MT3>::value
562  , typename DMatTSMatAddExprTrait< typename TDMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
563  , INVALID_TYPE >::Type Type;
565  //**********************************************************************************************
566 };
568 //*************************************************************************************************
569 
570 
571 //*************************************************************************************************
573 template< typename MT1, typename MT2, typename MT3 >
574 struct TDMatTDMatAddExprTrait< DMatSMatAddExpr<MT1,MT2,true>, MT3 >
575 {
576  public:
577  //**********************************************************************************************
579  typedef typename SelectType< IsDenseMatrix <MT1>::value && IsColumnMajorMatrix<MT1>::value &&
580  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
581  IsDenseMatrix <MT3>::value && IsColumnMajorMatrix<MT3>::value
582  , typename TDMatTSMatAddExprTrait< typename TDMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
583  , INVALID_TYPE >::Type Type;
585  //**********************************************************************************************
586 };
588 //*************************************************************************************************
589 
590 
591 //*************************************************************************************************
593 template< typename MT1, typename MT2, typename MT3 >
594 struct DMatDMatSubExprTrait< DMatSMatAddExpr<MT1,MT2,false>, MT3 >
595 {
596  public:
597  //**********************************************************************************************
599  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
600  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
601  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
602  , typename DMatSMatAddExprTrait< typename DMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
603  , INVALID_TYPE >::Type Type;
605  //**********************************************************************************************
606 };
608 //*************************************************************************************************
609 
610 
611 //*************************************************************************************************
613 template< typename MT1, typename MT2, typename MT3 >
614 struct DMatTDMatSubExprTrait< DMatSMatAddExpr<MT1,MT2,false>, MT3 >
615 {
616  public:
617  //**********************************************************************************************
619  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
620  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
621  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
622  , typename DMatSMatAddExprTrait< typename DMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
623  , INVALID_TYPE >::Type Type;
625  //**********************************************************************************************
626 };
628 //*************************************************************************************************
629 
630 
631 //*************************************************************************************************
633 template< typename MT1, typename MT2, typename MT3 >
634 struct TDMatDMatSubExprTrait< DMatSMatAddExpr<MT1,MT2,true>, MT3 >
635 {
636  public:
637  //**********************************************************************************************
639  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
640  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
641  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
642  , typename DMatTSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
643  , INVALID_TYPE >::Type Type;
645  //**********************************************************************************************
646 };
648 //*************************************************************************************************
649 
650 
651 //*************************************************************************************************
653 template< typename MT1, typename MT2, typename MT3 >
654 struct TDMatTDMatSubExprTrait< DMatSMatAddExpr<MT1,MT2,true>, MT3 >
655 {
656  public:
657  //**********************************************************************************************
659  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
660  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
661  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
662  , typename TDMatTSMatAddExprTrait< typename TDMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
663  , INVALID_TYPE >::Type Type;
665  //**********************************************************************************************
666 };
668 //*************************************************************************************************
669 
670 } // namespace blaze
671 
672 #endif