All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatTSMatAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATTSMATADDEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DMATTSMATADDEXPR_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 DMATTSMATADDEXPR
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 class DMatTSMatAddExpr : public DenseMatrix< DMatTSMatAddExpr<MT1,MT2>, false >
74  , private Expression
75  , private Computation
76 {
77  private:
78  //**Type definitions****************************************************************************
79  typedef typename MT1::ResultType RT1;
80  typedef typename MT2::ResultType RT2;
81  typedef typename MT1::ReturnType RN1;
82  typedef typename MT2::ReturnType RN2;
83  //**********************************************************************************************
84 
85  //**Return type evaluation**********************************************************************
87 
92  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
93 
96  //**********************************************************************************************
97 
98  public:
99  //**Type definitions****************************************************************************
102  typedef typename ResultType::OppositeType OppositeType;
103  typedef typename ResultType::TransposeType TransposeType;
104  typedef typename ResultType::ElementType ElementType;
105 
108 
110  typedef const ResultType CompositeType;
111 
113  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
114 
116  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
117  //**********************************************************************************************
118 
119  //**Compilation flags***************************************************************************
121  enum { vectorizable = 0 };
122  //**********************************************************************************************
123 
124  //**Constructor*********************************************************************************
130  explicit inline DMatTSMatAddExpr( const MT1& lhs, const MT2& rhs )
131  : lhs_( lhs ) // Left-hand side dense matrix of the addition expression
132  , rhs_( rhs ) // Right-hand side sparse matrix of the addition expression
133  {
134  BLAZE_INTERNAL_ASSERT( lhs.rows() == rhs.rows() , "Invalid number of rows" );
135  BLAZE_INTERNAL_ASSERT( lhs.columns() == rhs.columns(), "Invalid number of columns" );
136  }
137  //**********************************************************************************************
138 
139  //**Access operator*****************************************************************************
146  inline ReturnType operator()( size_t i, size_t j ) const {
147  BLAZE_INTERNAL_ASSERT( i < lhs_.rows() , "Invalid row access index" );
148  BLAZE_INTERNAL_ASSERT( j < lhs_.columns(), "Invalid column access index" );
149  return lhs_(i,j) + rhs_(i,j);
150  }
151  //**********************************************************************************************
152 
153  //**Rows function*******************************************************************************
158  inline size_t rows() const {
159  return lhs_.rows();
160  }
161  //**********************************************************************************************
162 
163  //**Columns function****************************************************************************
168  inline size_t columns() const {
169  return lhs_.columns();
170  }
171  //**********************************************************************************************
172 
173  //**Left operand access*************************************************************************
178  inline LeftOperand leftOperand() const {
179  return lhs_;
180  }
181  //**********************************************************************************************
182 
183  //**Right operand access************************************************************************
188  inline RightOperand rightOperand() const {
189  return rhs_;
190  }
191  //**********************************************************************************************
192 
193  //**********************************************************************************************
199  template< typename T >
200  inline bool canAlias( const T* alias ) const {
201  return ( IsExpression<MT1>::value && lhs_.canAlias( alias ) ) ||
202  ( rhs_.canAlias( alias ) );
203  }
204  //**********************************************************************************************
205 
206  //**********************************************************************************************
212  template< typename T >
213  inline bool isAliased( const T* alias ) const {
214  return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
215  }
216  //**********************************************************************************************
217 
218  private:
219  //**Member variables****************************************************************************
222  //**********************************************************************************************
223 
224  //**Assignment to dense matrices****************************************************************
236  template< typename MT // Type of the target dense matrix
237  , bool SO2 > // Storage order of the target dense matrix
238  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const DMatTSMatAddExpr& rhs )
239  {
241 
242  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
243  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
244 
245  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
246  addAssign( ~lhs, rhs.rhs_ );
247  }
248  else {
249  assign ( ~lhs, rhs.lhs_ );
250  addAssign( ~lhs, rhs.rhs_ );
251  }
252  }
254  //**********************************************************************************************
255 
256  //**Assignment to sparse matrices***************************************************************
268  template< typename MT // Type of the target sparse matrix
269  , bool SO2 > // Storage order of the target sparse matrix
270  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const DMatTSMatAddExpr& rhs )
271  {
273 
274  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
275 
281  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
282 
283  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
284  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
285 
286  const TmpType tmp( rhs );
287  assign( ~lhs, tmp );
288  }
290  //**********************************************************************************************
291 
292  //**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 DMatTSMatAddExpr& 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****************************************************
337  template< typename MT // Type of the target dense matrix
338  , bool SO2 > // Storage order of the target dense matrix
339  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const DMatTSMatAddExpr& rhs )
340  {
342 
343  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
344  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
345 
346  subAssign( ~lhs, rhs.lhs_ );
347  subAssign( ~lhs, rhs.rhs_ );
348  }
350  //**********************************************************************************************
351 
352  //**Subtraction assignment to sparse matrices***************************************************
353  // No special implementation for the subtraction assignment to sparse matrices.
354  //**********************************************************************************************
355 
356  //**Multiplication assignment to dense matrices*************************************************
357  // No special implementation for the multiplication assignment to dense matrices.
358  //**********************************************************************************************
359 
360  //**Multiplication assignment to sparse matrices************************************************
361  // No special implementation for the multiplication assignment to sparse matrices.
362  //**********************************************************************************************
363 
364  //**Compile time checks*************************************************************************
371  //**********************************************************************************************
372 };
373 //*************************************************************************************************
374 
375 
376 
377 
378 //=================================================================================================
379 //
380 // GLOBAL BINARY ARITHMETIC OPERATORS
381 //
382 //=================================================================================================
383 
384 //*************************************************************************************************
414 template< typename T1 // Type of the left-hand side dense matrix
415  , typename T2 > // Type of the right-hand side sparse matrix
416 const DMatTSMatAddExpr<T1,T2>
418 {
420 
421  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
422  throw std::invalid_argument( "Matrix sizes do not match" );
423 
424  return DMatTSMatAddExpr<T1,T2>( ~lhs, ~rhs );
425 }
426 //*************************************************************************************************
427 
428 
429 //*************************************************************************************************
459 template< typename T1 // Type of the left-hand side dense matrix
460  , typename T2 > // Type of the right-hand side sparse matrix
461 const DMatTSMatAddExpr<T1,T2>
463 {
465 
466  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
467  throw std::invalid_argument( "Matrix sizes do not match" );
468 
469  return DMatTSMatAddExpr<T1,T2>( ~rhs, ~lhs );
470 }
471 //*************************************************************************************************
472 
473 
474 
475 
476 //=================================================================================================
477 //
478 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
479 //
480 //=================================================================================================
481 
482 //*************************************************************************************************
495 template< typename T1 // Type of the dense matrix of the left-hand side expression
496  , typename T2 // Type of the sparse matrix of the left-hand side expression
497  , typename T3 // Type of the right-hand side dense matrix
498  , bool SO > // Storage order of the right-hand side dense matrix
499 inline const typename AddExprTrait< DMatTSMatAddExpr<T1,T2>, T3 >::Type
500  operator+( const DMatTSMatAddExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
501 {
503 
504  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
505 }
507 //*************************************************************************************************
508 
509 
510 //*************************************************************************************************
523 template< typename T1 // Type of the dense matrix of the left-hand side expression
524  , typename T2 // Type of the sparse matrix of the left-hand side expression
525  , typename T3 // Type of the right-hand side dense matrix
526  , bool SO > // Storage order of the right-hand side dense matrix
527 inline const typename SubExprTrait< DMatTSMatAddExpr<T1,T2>, T3 >::Type
528  operator-( const DMatTSMatAddExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
529 {
531 
532  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
533 }
535 //*************************************************************************************************
536 
537 
538 
539 
540 //=================================================================================================
541 //
542 // GLOBAL OPERATORS
543 //
544 //=================================================================================================
545 
546 //*************************************************************************************************
559 template< typename MT1 // Type of the left-hand side dense matrix
560  , typename MT2 > // Type of the right-hand side sparse matrix
561 inline typename RowExprTrait< DMatTSMatAddExpr<MT1,MT2> >::Type
562  row( const DMatTSMatAddExpr<MT1,MT2>& dm, size_t index )
563 {
565 
566  return row( dm.leftOperand(), index ) + row( dm.rightOperand(), index );
567 }
569 //*************************************************************************************************
570 
571 
572 //*************************************************************************************************
585 template< typename MT1 // Type of the left-hand side dense matrix
586  , typename MT2 > // Type of the right-hand side sparse matrix
587 inline typename ColumnExprTrait< DMatTSMatAddExpr<MT1,MT2> >::Type
588  column( const DMatTSMatAddExpr<MT1,MT2>& dm, size_t index )
589 {
591 
592  return column( dm.leftOperand(), index ) + column( dm.rightOperand(), index );
593 }
595 //*************************************************************************************************
596 
597 
598 
599 
600 //=================================================================================================
601 //
602 // EXPRESSION TRAIT SPECIALIZATIONS
603 //
604 //=================================================================================================
605 
606 //*************************************************************************************************
608 template< typename MT1, typename MT2, typename MT3 >
609 struct DMatDMatAddExprTrait< DMatTSMatAddExpr<MT1,MT2>, MT3 >
610 {
611  public:
612  //**********************************************************************************************
614  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
615  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
616  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
617  , typename DMatTSMatAddExprTrait< typename DMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
618  , INVALID_TYPE >::Type Type;
620  //**********************************************************************************************
621 };
623 //*************************************************************************************************
624 
625 
626 //*************************************************************************************************
628 template< typename MT1, typename MT2, typename MT3 >
629 struct DMatTDMatAddExprTrait< DMatTSMatAddExpr<MT1,MT2>, MT3 >
630 {
631  public:
632  //**********************************************************************************************
634  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
635  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
636  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
637  , typename DMatTSMatAddExprTrait< typename DMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
638  , INVALID_TYPE >::Type Type;
640  //**********************************************************************************************
641 };
643 //*************************************************************************************************
644 
645 
646 //*************************************************************************************************
648 template< typename MT1, typename MT2, typename MT3 >
649 struct DMatDMatSubExprTrait< DMatTSMatAddExpr<MT1,MT2>, MT3 >
650 {
651  public:
652  //**********************************************************************************************
654  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
655  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
656  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
657  , typename DMatTSMatAddExprTrait< typename DMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
658  , INVALID_TYPE >::Type Type;
660  //**********************************************************************************************
661 };
663 //*************************************************************************************************
664 
665 
666 //*************************************************************************************************
668 template< typename MT1, typename MT2, typename MT3 >
669 struct DMatTDMatSubExprTrait< DMatTSMatAddExpr<MT1,MT2>, MT3 >
670 {
671  public:
672  //**********************************************************************************************
674  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
675  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
676  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
677  , typename DMatTSMatAddExprTrait< typename DMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
678  , INVALID_TYPE >::Type Type;
680  //**********************************************************************************************
681 };
683 //*************************************************************************************************
684 
685 
686 //*************************************************************************************************
688 template< typename MT1, typename MT2 >
689 struct RowExprTrait< DMatTSMatAddExpr<MT1,MT2> >
690 {
691  public:
692  //**********************************************************************************************
693  typedef typename AddExprTrait< typename RowExprTrait<const MT1>::Type
694  , typename RowExprTrait<const MT2>::Type >::Type Type;
695  //**********************************************************************************************
696 };
698 //*************************************************************************************************
699 
700 
701 //*************************************************************************************************
703 template< typename MT1, typename MT2 >
704 struct ColumnExprTrait< DMatTSMatAddExpr<MT1,MT2> >
705 {
706  public:
707  //**********************************************************************************************
708  typedef typename AddExprTrait< typename ColumnExprTrait<const MT1>::Type
709  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
710  //**********************************************************************************************
711 };
713 //*************************************************************************************************
714 
715 } // namespace blaze
716 
717 #endif