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>
49 #include <blaze/util/Assert.h>
52 #include <blaze/util/SelectType.h>
53 #include <blaze/util/Types.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // CLASS DMATSMATADDEXPR
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename MT1 // Type of the left-hand side dense matrix
72  , typename MT2 // Type of the right-hand side sparse matrix
73  , bool SO > // Storage order
74 class DMatSMatAddExpr : public DenseMatrix< DMatSMatAddExpr<MT1,MT2,SO>, SO >
75  , private Expression
76  , private Computation
77 {
78  private:
79  //**Type definitions****************************************************************************
80  typedef typename MT1::ResultType RT1;
81  typedef typename MT2::ResultType RT2;
82  typedef typename MT1::ReturnType RN1;
83  typedef typename MT2::ReturnType RN2;
84  //**********************************************************************************************
85 
86  //**Return type evaluation**********************************************************************
88 
93  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
94 
97  //**********************************************************************************************
98 
99  public:
100  //**Type definitions****************************************************************************
103  typedef typename ResultType::OppositeType OppositeType;
104  typedef typename ResultType::TransposeType TransposeType;
105  typedef typename ResultType::ElementType ElementType;
106 
109 
111  typedef const ResultType CompositeType;
112 
114  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
115 
117  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
118  //**********************************************************************************************
119 
120  //**Compilation flags***************************************************************************
122  enum { vectorizable = 0 };
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 canAlias( const T* alias ) const {
202  return ( IsExpression<MT1>::value && lhs_.canAlias( alias ) ) ||
203  ( rhs_.canAlias( alias ) );
204  }
205  //**********************************************************************************************
206 
207  //**********************************************************************************************
213  template< typename T >
214  inline bool isAliased( const T* alias ) const {
215  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
216  }
217  //**********************************************************************************************
218 
219  private:
220  //**Member variables****************************************************************************
223  //**********************************************************************************************
224 
225  //**Assignment to dense matrices****************************************************************
237  template< typename MT // Type of the target dense matrix
238  , bool SO2 > // Storage order of the target dense matrix
239  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const DMatSMatAddExpr& rhs )
240  {
242 
243  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
244  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
245 
246  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
247  addAssign( ~lhs, rhs.rhs_ );
248  }
249  else {
250  assign ( ~lhs, rhs.lhs_ );
251  addAssign( ~lhs, rhs.rhs_ );
252  }
253  }
255  //**********************************************************************************************
256 
257  //**Assignment to sparse matrices***************************************************************
269  template< typename MT // Type of the target sparse matrix
270  , bool SO2 > // Storage order of the target sparse matrix
271  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const DMatSMatAddExpr& rhs )
272  {
274 
276 
282  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
283 
284  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
285  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
286 
287  const TmpType tmp( rhs );
288  assign( ~lhs, tmp );
289  }
291  //**********************************************************************************************
292 
293  //**Addition assignment to dense matrices*******************************************************
305  template< typename MT // Type of the target dense matrix
306  , bool SO2 > // Storage order of the target dense matrix
307  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const DMatSMatAddExpr& rhs )
308  {
310 
311  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
312  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
313 
314  addAssign( ~lhs, rhs.lhs_ );
315  addAssign( ~lhs, rhs.rhs_ );
316  }
318  //**********************************************************************************************
319 
320  //**Addition assignment to sparse matrices******************************************************
321  // No special implementation for the addition assignment to sparse matrices.
322  //**********************************************************************************************
323 
324  //**Subtraction assignment to dense matrices****************************************************
336  template< typename MT // Type of the target dense matrix
337  , bool SO2 > // Storage order of the target dense matrix
338  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const DMatSMatAddExpr& rhs )
339  {
341 
342  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
343  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
344 
345  subAssign( ~lhs, rhs.lhs_ );
346  subAssign( ~lhs, rhs.rhs_ );
347  }
349  //**********************************************************************************************
350 
351  //**Subtraction assignment to sparse matrices***************************************************
352  // No special implementation for the subtraction assignment to sparse matrices.
353  //**********************************************************************************************
354 
355  //**Multiplication assignment to dense matrices*************************************************
356  // No special implementation for the multiplication assignment to dense matrices.
357  //**********************************************************************************************
358 
359  //**Multiplication assignment to sparse matrices************************************************
360  // No special implementation for the multiplication assignment to sparse matrices.
361  //**********************************************************************************************
362 
363  //**Compile time checks*************************************************************************
369  //**********************************************************************************************
370 };
371 //*************************************************************************************************
372 
373 
374 
375 
376 //=================================================================================================
377 //
378 // GLOBAL BINARY ARITHMETIC OPERATORS
379 //
380 //=================================================================================================
381 
382 //*************************************************************************************************
408 template< typename T1 // Type of the left-hand side dense matrix
409  , typename T2 // Type of the right-hand side sparse matrix
410  , bool SO > // Storage order
411 inline const DMatSMatAddExpr<T1,T2,SO>
413 {
415 
416  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
417  throw std::invalid_argument( "Matrix sizes do not match" );
418 
419  return DMatSMatAddExpr<T1,T2,SO>( ~lhs, ~rhs );
420 }
421 //*************************************************************************************************
422 
423 
424 //*************************************************************************************************
450 template< typename T1 // Type of the left-hand side sparse matrix
451  , typename T2 // Type of the right-hand side dense matrix
452  , bool SO > // Storage order
453 inline const DMatSMatAddExpr<T2,T1,SO>
455 {
457 
458  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
459  throw std::invalid_argument( "Matrix sizes do not match" );
460 
461  return DMatSMatAddExpr<T2,T1,SO>( ~rhs, ~lhs );
462 }
463 //*************************************************************************************************
464 
465 
466 
467 
468 //=================================================================================================
469 //
470 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
471 //
472 //=================================================================================================
473 
474 //*************************************************************************************************
487 template< typename T1 // Type of the dense matrix of the left-hand side expression
488  , typename T2 // Type of the sparse matrix of the left-hand side expression
489  , bool SO1 // Storage order of the left-hand side expression
490  , typename T3 // Type of the right-hand side dense matrix
491  , bool SO2 > // Storage order of the right-hand side dense matrix
492 inline const typename AddExprTrait< DMatSMatAddExpr<T1,T2,SO1>, T3 >::Type
493  operator+( const DMatSMatAddExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
494 {
496 
497  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
498 }
500 //*************************************************************************************************
501 
502 
503 //*************************************************************************************************
516 template< typename T1 // Type of the dense matrix of the left-hand side expression
517  , typename T2 // Type of the sparse matrix of the left-hand side expression
518  , bool SO1 // Storage order of the left-hand side expression
519  , typename T3 // Type of the right-hand side dense matrix
520  , bool SO2 > // Storage order of the right-hand side dense matrix
521 inline const typename SubExprTrait< DMatSMatAddExpr<T1,T2,SO1>, T3 >::Type
522  operator-( const DMatSMatAddExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
523 {
525 
526  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
527 }
529 //*************************************************************************************************
530 
531 
532 
533 
534 //=================================================================================================
535 //
536 // GLOBAL OPERATORS
537 //
538 //=================================================================================================
539 
540 //*************************************************************************************************
552 template< typename MT1 // Type of the left-hand side dense matrix
553  , typename MT2 // Type of the right-hand side sparse matrix
554  , bool SO > // Storage order
555 inline typename RowExprTrait< DMatSMatAddExpr<MT1,MT2,SO> >::Type
556  row( const DMatSMatAddExpr<MT1,MT2,SO>& dm, size_t index )
557 {
559 
560  return row( dm.leftOperand(), index ) + row( dm.rightOperand(), index );
561 }
563 //*************************************************************************************************
564 
565 
566 //*************************************************************************************************
578 template< typename MT1 // Type of the left-hand side dense matrix
579  , typename MT2 // Type of the right-hand side sparse matrix
580  , bool SO > // Storage order
581 inline typename ColumnExprTrait< DMatDMatAddExpr<MT1,MT2,SO> >::Type
582  column( const DMatSMatAddExpr<MT1,MT2,SO>& dm, size_t index )
583 {
585 
586  return column( dm.leftOperand(), index ) + column( dm.rightOperand(), index );
587 }
589 //*************************************************************************************************
590 
591 
592 
593 
594 //=================================================================================================
595 //
596 // EXPRESSION TRAIT SPECIALIZATIONS
597 //
598 //=================================================================================================
599 
600 //*************************************************************************************************
602 template< typename MT1, typename MT2, typename MT3 >
603 struct DMatDMatAddExprTrait< DMatSMatAddExpr<MT1,MT2,false>, MT3 >
604 {
605  public:
606  //**********************************************************************************************
608  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
609  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
610  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
611  , typename DMatSMatAddExprTrait< typename DMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
612  , INVALID_TYPE >::Type Type;
614  //**********************************************************************************************
615 };
617 //*************************************************************************************************
618 
619 
620 //*************************************************************************************************
622 template< typename MT1, typename MT2, typename MT3 >
623 struct DMatTDMatAddExprTrait< DMatSMatAddExpr<MT1,MT2,false>, MT3 >
624 {
625  public:
626  //**********************************************************************************************
628  typedef typename SelectType< IsDenseMatrix <MT1>::value && IsRowMajorMatrix<MT1>::value &&
629  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
630  IsDenseMatrix <MT3>::value && IsColumnMajorMatrix<MT3>::value
631  , typename DMatSMatAddExprTrait< typename DMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
632  , INVALID_TYPE >::Type Type;
634  //**********************************************************************************************
635 };
637 //*************************************************************************************************
638 
639 
640 //*************************************************************************************************
642 template< typename MT1, typename MT2, typename MT3 >
643 struct TDMatDMatAddExprTrait< DMatSMatAddExpr<MT1,MT2,true>, MT3 >
644 {
645  public:
646  //**********************************************************************************************
648  typedef typename SelectType< IsDenseMatrix <MT1>::value && IsColumnMajorMatrix<MT1>::value &&
649  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
650  IsDenseMatrix <MT3>::value && IsRowMajorMatrix<MT3>::value
651  , typename DMatTSMatAddExprTrait< typename TDMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
652  , INVALID_TYPE >::Type Type;
654  //**********************************************************************************************
655 };
657 //*************************************************************************************************
658 
659 
660 //*************************************************************************************************
662 template< typename MT1, typename MT2, typename MT3 >
663 struct TDMatTDMatAddExprTrait< DMatSMatAddExpr<MT1,MT2,true>, MT3 >
664 {
665  public:
666  //**********************************************************************************************
668  typedef typename SelectType< IsDenseMatrix <MT1>::value && IsColumnMajorMatrix<MT1>::value &&
669  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
670  IsDenseMatrix <MT3>::value && IsColumnMajorMatrix<MT3>::value
671  , typename TDMatTSMatAddExprTrait< typename TDMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
672  , INVALID_TYPE >::Type Type;
674  //**********************************************************************************************
675 };
677 //*************************************************************************************************
678 
679 
680 //*************************************************************************************************
682 template< typename MT1, typename MT2, typename MT3 >
683 struct DMatDMatSubExprTrait< DMatSMatAddExpr<MT1,MT2,false>, MT3 >
684 {
685  public:
686  //**********************************************************************************************
688  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
689  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
690  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
691  , typename DMatSMatAddExprTrait< typename DMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
692  , INVALID_TYPE >::Type Type;
694  //**********************************************************************************************
695 };
697 //*************************************************************************************************
698 
699 
700 //*************************************************************************************************
702 template< typename MT1, typename MT2, typename MT3 >
703 struct DMatTDMatSubExprTrait< DMatSMatAddExpr<MT1,MT2,false>, MT3 >
704 {
705  public:
706  //**********************************************************************************************
708  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
709  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
710  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
711  , typename DMatSMatAddExprTrait< typename DMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
712  , INVALID_TYPE >::Type Type;
714  //**********************************************************************************************
715 };
717 //*************************************************************************************************
718 
719 
720 //*************************************************************************************************
722 template< typename MT1, typename MT2, typename MT3 >
723 struct TDMatDMatSubExprTrait< DMatSMatAddExpr<MT1,MT2,true>, MT3 >
724 {
725  public:
726  //**********************************************************************************************
728  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
729  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
730  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
731  , typename DMatTSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
732  , INVALID_TYPE >::Type Type;
734  //**********************************************************************************************
735 };
737 //*************************************************************************************************
738 
739 
740 //*************************************************************************************************
742 template< typename MT1, typename MT2, typename MT3 >
743 struct TDMatTDMatSubExprTrait< DMatSMatAddExpr<MT1,MT2,true>, MT3 >
744 {
745  public:
746  //**********************************************************************************************
748  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
749  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
750  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
751  , typename TDMatTSMatAddExprTrait< typename TDMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
752  , INVALID_TYPE >::Type Type;
754  //**********************************************************************************************
755 };
757 //*************************************************************************************************
758 
759 
760 //*************************************************************************************************
762 template< typename MT1, typename MT2, bool SO >
763 struct RowExprTrait< DMatSMatAddExpr<MT1,MT2,SO> >
764 {
765  public:
766  //**********************************************************************************************
767  typedef typename AddExprTrait< typename RowExprTrait<const MT1>::Type
768  , typename RowExprTrait<const MT2>::Type >::Type Type;
769  //**********************************************************************************************
770 };
772 //*************************************************************************************************
773 
774 
775 //*************************************************************************************************
777 template< typename MT1, typename MT2, bool SO >
778 struct ColumnExprTrait< DMatSMatAddExpr<MT1,MT2,SO> >
779 {
780  public:
781  //**********************************************************************************************
782  typedef typename AddExprTrait< typename ColumnExprTrait<const MT1>::Type
783  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
784  //**********************************************************************************************
785 };
787 //*************************************************************************************************
788 
789 } // namespace blaze
790 
791 #endif