All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TDMatSMatAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_TDMATSMATADDEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_TDMATSMATADDEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
45 #include <blaze/util/Assert.h>
48 #include <blaze/util/SelectType.h>
49 #include <blaze/util/Types.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // CLASS TDMATSMATADDEXPR
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
67 template< typename MT1 // Type of the left-hand side dense matrix
68  , typename MT2 > // Type of the right-hand side sparse matrix
69 class TDMatSMatAddExpr : public DenseMatrix< TDMatSMatAddExpr<MT1,MT2>, false >
70  , private MatMatAddExpr
71  , private Computation
72 {
73  private:
74  //**Type definitions****************************************************************************
75  typedef typename MT1::ResultType RT1;
76  typedef typename MT2::ResultType RT2;
77  typedef typename MT1::ReturnType RN1;
78  typedef typename MT2::ReturnType RN2;
79  //**********************************************************************************************
80 
81  //**Return type evaluation**********************************************************************
83 
88  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
89 
92  //**********************************************************************************************
93 
94  public:
95  //**Type definitions****************************************************************************
98  typedef typename ResultType::OppositeType OppositeType;
99  typedef typename ResultType::TransposeType TransposeType;
100  typedef typename ResultType::ElementType ElementType;
101 
104 
106  typedef const ResultType CompositeType;
107 
109  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
110 
112  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
113  //**********************************************************************************************
114 
115  //**Compilation flags***************************************************************************
117  enum { vectorizable = 0 };
118  //**********************************************************************************************
119 
120  //**Constructor*********************************************************************************
126  explicit inline TDMatSMatAddExpr( const MT1& lhs, const MT2& rhs )
127  : lhs_( lhs ) // Left-hand side dense matrix of the addition expression
128  , rhs_( rhs ) // Right-hand side sparse matrix of the addition expression
129  {
130  BLAZE_INTERNAL_ASSERT( lhs.rows() == rhs.rows() , "Invalid number of rows" );
131  BLAZE_INTERNAL_ASSERT( lhs.columns() == rhs.columns(), "Invalid number of columns" );
132  }
133  //**********************************************************************************************
134 
135  //**Access operator*****************************************************************************
142  inline ReturnType operator()( size_t i, size_t j ) const {
143  BLAZE_INTERNAL_ASSERT( i < lhs_.rows() , "Invalid row access index" );
144  BLAZE_INTERNAL_ASSERT( j < lhs_.columns(), "Invalid column access index" );
145  return lhs_(i,j) + rhs_(i,j);
146  }
147  //**********************************************************************************************
148 
149  //**Rows function*******************************************************************************
154  inline size_t rows() const {
155  return lhs_.rows();
156  }
157  //**********************************************************************************************
158 
159  //**Columns function****************************************************************************
164  inline size_t columns() const {
165  return lhs_.columns();
166  }
167  //**********************************************************************************************
168 
169  //**Left operand access*************************************************************************
174  inline LeftOperand leftOperand() const {
175  return lhs_;
176  }
177  //**********************************************************************************************
178 
179  //**Right operand access************************************************************************
184  inline RightOperand rightOperand() const {
185  return rhs_;
186  }
187  //**********************************************************************************************
188 
189  //**********************************************************************************************
195  template< typename T >
196  inline bool canAlias( const T* alias ) const {
197  return ( IsExpression<MT1>::value && lhs_.canAlias( alias ) ) ||
198  ( rhs_.canAlias( alias ) );
199  }
200  //**********************************************************************************************
201 
202  //**********************************************************************************************
208  template< typename T >
209  inline bool isAliased( const T* alias ) const {
210  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
211  }
212  //**********************************************************************************************
213 
214  private:
215  //**Member variables****************************************************************************
218  //**********************************************************************************************
219 
220  //**Assignment to dense matrices****************************************************************
232  template< typename MT // Type of the target dense matrix
233  , bool SO2 > // Storage order of the target dense matrix
234  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const TDMatSMatAddExpr& rhs )
235  {
237 
238  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
239  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
240 
241  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
242  addAssign( ~lhs, rhs.rhs_ );
243  }
244  else {
245  assign ( ~lhs, rhs.lhs_ );
246  addAssign( ~lhs, rhs.rhs_ );
247  }
248  }
250  //**********************************************************************************************
251 
252  //**Assignment to sparse matrices***************************************************************
264  template< typename MT // Type of the target sparse matrix
265  , bool SO2 > // Storage order of the target sparse matrix
266  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const TDMatSMatAddExpr& rhs )
267  {
269 
270  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
271 
277  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
278 
279  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
280  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
281 
282  const TmpType tmp( rhs );
283  assign( ~lhs, tmp );
284  }
286  //**********************************************************************************************
287 
288  //**Addition assignment to dense matrices*******************************************************
301  template< typename MT // Type of the target dense matrix
302  , bool SO2 > // Storage order of the target dense matrix
303  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const TDMatSMatAddExpr& rhs )
304  {
306 
307  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
308  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
309 
310  addAssign( ~lhs, rhs.lhs_ );
311  addAssign( ~lhs, rhs.rhs_ );
312  }
314  //**********************************************************************************************
315 
316  //**Addition assignment to sparse matrices******************************************************
317  // No special implementation for the addition assignment to sparse matrices.
318  //**********************************************************************************************
319 
320  //**Subtraction assignment to dense matrices****************************************************
333  template< typename MT // Type of the target dense matrix
334  , bool SO2 > // Storage order of the target dense matrix
335  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const TDMatSMatAddExpr& rhs )
336  {
338 
339  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
340  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
341 
342  subAssign( ~lhs, rhs.lhs_ );
343  subAssign( ~lhs, rhs.rhs_ );
344  }
346  //**********************************************************************************************
347 
348  //**Subtraction assignment to sparse matrices***************************************************
349  // No special implementation for the subtraction assignment to sparse matrices.
350  //**********************************************************************************************
351 
352  //**Multiplication assignment to dense matrices*************************************************
353  // No special implementation for the multiplication assignment to dense matrices.
354  //**********************************************************************************************
355 
356  //**Multiplication assignment to sparse matrices************************************************
357  // No special implementation for the multiplication assignment to sparse matrices.
358  //**********************************************************************************************
359 
360  //**Compile time checks*************************************************************************
367  //**********************************************************************************************
368 };
369 //*************************************************************************************************
370 
371 
372 
373 
374 //=================================================================================================
375 //
376 // GLOBAL BINARY ARITHMETIC OPERATORS
377 //
378 //=================================================================================================
379 
380 //*************************************************************************************************
411 template< typename T1 // Type of the left-hand side dense matrix
412  , typename T2 > // Type of the right-hand side sparse matrix
413 const TDMatSMatAddExpr<T1,T2>
415 {
417 
418  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
419  throw std::invalid_argument( "Matrix sizes do not match" );
420 
421  return TDMatSMatAddExpr<T1,T2>( ~lhs, ~rhs );
422 }
423 //*************************************************************************************************
424 
425 
426 //*************************************************************************************************
457 template< typename T1 // Type of the left-hand side dense matrix
458  , typename T2 > // Type of the right-hand side sparse matrix
459 const TDMatSMatAddExpr<T1,T2>
461 {
463 
464  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
465  throw std::invalid_argument( "Matrix sizes do not match" );
466 
467  return TDMatSMatAddExpr<T1,T2>( ~rhs, ~lhs );
468 }
469 //*************************************************************************************************
470 
471 
472 
473 
474 //=================================================================================================
475 //
476 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
477 //
478 //=================================================================================================
479 
480 //*************************************************************************************************
493 template< typename T1 // Type of the dense matrix of the left-hand side expression
494  , typename T2 // Type of the sparse matrix of the left-hand side expression
495  , typename T3 // Type of the right-hand side dense matrix
496  , bool SO > // Storage order of the right-hand side dense matrix
497 inline const typename AddExprTrait< TDMatSMatAddExpr<T1,T2>, T3 >::Type
498  operator+( const TDMatSMatAddExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
499 {
501 
502  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
503 }
505 //*************************************************************************************************
506 
507 
508 //*************************************************************************************************
521 template< typename T1 // Type of the dense matrix of the left-hand side expression
522  , typename T2 // Type of the sparse matrix of the left-hand side expression
523  , typename T3 // Type of the right-hand side dense matrix
524  , bool SO > // Storage order of the right-hand side dense matrix
525 inline const typename SubExprTrait< TDMatSMatAddExpr<T1,T2>, T3 >::Type
526  operator-( const TDMatSMatAddExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
527 {
529 
530  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
531 }
533 //*************************************************************************************************
534 
535 
536 
537 
538 //=================================================================================================
539 //
540 // EXPRESSION TRAIT SPECIALIZATIONS
541 //
542 //=================================================================================================
543 
544 //*************************************************************************************************
546 template< typename MT1, typename MT2, typename MT3 >
547 struct DMatDMatAddExprTrait< TDMatSMatAddExpr<MT1,MT2>, MT3 >
548 {
549  public:
550  //**********************************************************************************************
552  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
553  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
554  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
555  , typename DMatSMatAddExprTrait< typename TDMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
556  , INVALID_TYPE >::Type Type;
558  //**********************************************************************************************
559 };
561 //*************************************************************************************************
562 
563 
564 //*************************************************************************************************
566 template< typename MT1, typename MT2, typename MT3 >
567 struct DMatTDMatAddExprTrait< TDMatSMatAddExpr<MT1,MT2>, MT3 >
568 {
569  public:
570  //**********************************************************************************************
572  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
573  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
574  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
575  , typename TDMatSMatAddExprTrait< typename TDMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
576  , INVALID_TYPE >::Type Type;
578  //**********************************************************************************************
579 };
581 //*************************************************************************************************
582 
583 
584 //*************************************************************************************************
586 template< typename MT1, typename MT2, typename MT3 >
587 struct DMatDMatSubExprTrait< TDMatSMatAddExpr<MT1,MT2>, MT3 >
588 {
589  public:
590  //**********************************************************************************************
592  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
593  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
594  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
595  , typename DMatSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
596  , INVALID_TYPE >::Type Type;
598  //**********************************************************************************************
599 };
601 //*************************************************************************************************
602 
603 
604 //*************************************************************************************************
606 template< typename MT1, typename MT2, typename MT3 >
607 struct DMatTDMatSubExprTrait< TDMatSMatAddExpr<MT1,MT2>, MT3 >
608 {
609  public:
610  //**********************************************************************************************
612  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
613  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
614  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
615  , typename TDMatSMatAddExprTrait< typename TDMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
616  , INVALID_TYPE >::Type Type;
618  //**********************************************************************************************
619 };
621 //*************************************************************************************************
622 
623 
624 //*************************************************************************************************
626 template< typename MT1, typename MT2 >
627 struct RowExprTrait< TDMatSMatAddExpr<MT1,MT2> >
628 {
629  public:
630  //**********************************************************************************************
631  typedef typename AddExprTrait< typename RowExprTrait<const MT1>::Type
632  , typename RowExprTrait<const MT2>::Type >::Type Type;
633  //**********************************************************************************************
634 };
636 //*************************************************************************************************
637 
638 
639 //*************************************************************************************************
641 template< typename MT1, typename MT2 >
642 struct ColumnExprTrait< TDMatSMatAddExpr<MT1,MT2> >
643 {
644  public:
645  //**********************************************************************************************
646  typedef typename AddExprTrait< typename ColumnExprTrait<const MT1>::Type
647  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
648  //**********************************************************************************************
649 };
651 //*************************************************************************************************
652 
653 } // namespace blaze
654 
655 #endif