All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatSMatSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATSMATSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DMATSMATSUBEXPR_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 DMATSMATSUBEXPR
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 DMatSMatSubExpr : public DenseMatrix< DMatSMatSubExpr<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 DMatSMatSubExpr( const MT1& lhs, const MT2& rhs )
132  : lhs_( lhs ) // Left-hand side dense matrix of the subtraction expression
133  , rhs_( rhs ) // Right-hand side sparse matrix of the subtraction 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 DMatSMatSubExpr& 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  subAssign( ~lhs, rhs.rhs_ );
233  }
234  else {
235  assign ( ~lhs, rhs.lhs_ );
236  subAssign( ~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 DMatSMatSubExpr& 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 DMatSMatSubExpr& 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  subAssign( ~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 DMatSMatSubExpr& 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  addAssign( ~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 //*************************************************************************************************
390 template< typename T1 // Type of the left-hand side dense matrix
391  , typename T2 // Type of the right-hand side sparse matrix
392  , bool SO > // Storage order
393 inline const DMatSMatSubExpr<T1,T2,SO>
395 {
396  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
397  throw std::invalid_argument( "Matrix sizes do not match" );
398 
399  return DMatSMatSubExpr<T1,T2,SO>( ~lhs, ~rhs );
400 }
401 //*************************************************************************************************
402 
403 
404 
405 
406 //=================================================================================================
407 //
408 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
409 //
410 //=================================================================================================
411 
412 //*************************************************************************************************
425 template< typename T1 // Type of the dense matrix of the left-hand side expression
426  , typename T2 // Type of the sparse matrix of the left-hand side expression
427  , bool SO1 // Storage order of the left-hand side expression
428  , typename T3 // Type of the right-hand side dense matrix
429  , bool SO2 > // Storage order of the right-hand side dense matrix
430 inline const typename AddExprTrait< DMatSMatSubExpr<T1,T2,SO1>, T3 >::Type
431  operator+( const DMatSMatSubExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
432 {
433  return ( lhs.leftOperand() + (~rhs) ) - lhs.rightOperand();
434 }
436 //*************************************************************************************************
437 
438 
439 //*************************************************************************************************
452 template< typename T1 // Type of the dense matrix of the left-hand side expression
453  , typename T2 // Type of the sparse matrix of the left-hand side expression
454  , bool SO1 // Storage order of the left-hand side expression
455  , typename T3 // Type of the right-hand side dense matrix
456  , bool SO2 > // Storage order of the right-hand side dense matrix
457 inline const typename SubExprTrait< DMatSMatSubExpr<T1,T2,SO1>, T3 >::Type
458  operator-( const DMatSMatSubExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
459 {
460  return ( lhs.leftOperand() - (~rhs) ) - lhs.rightOperand();
461 }
463 //*************************************************************************************************
464 
465 
466 
467 
468 //=================================================================================================
469 //
470 // EXPRESSION TRAIT SPECIALIZATIONS
471 //
472 //=================================================================================================
473 
474 //*************************************************************************************************
476 template< typename MT1, typename MT2, typename MT3 >
477 struct DMatDMatAddExprTrait< DMatSMatSubExpr<MT1,MT2,false>, MT3 >
478 {
479  public:
480  //**********************************************************************************************
482  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
483  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
484  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
485  , typename DMatSMatSubExprTrait< typename DMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
486  , INVALID_TYPE >::Type Type;
488  //**********************************************************************************************
489 };
491 //*************************************************************************************************
492 
493 
494 //*************************************************************************************************
496 template< typename MT1, typename MT2, typename MT3 >
497 struct DMatTDMatAddExprTrait< DMatSMatSubExpr<MT1,MT2,false>, MT3 >
498 {
499  public:
500  //**********************************************************************************************
502  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
503  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
504  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
505  , typename DMatSMatSubExprTrait< typename DMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
506  , INVALID_TYPE >::Type Type;
508  //**********************************************************************************************
509 };
511 //*************************************************************************************************
512 
513 
514 //*************************************************************************************************
516 template< typename MT1, typename MT2, typename MT3 >
517 struct TDMatDMatAddExprTrait< DMatSMatSubExpr<MT1,MT2,true>, MT3 >
518 {
519  public:
520  //**********************************************************************************************
522  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
523  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
524  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
525  , typename DMatTSMatSubExprTrait< typename TDMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
526  , INVALID_TYPE >::Type Type;
528  //**********************************************************************************************
529 };
531 //*************************************************************************************************
532 
533 
534 //*************************************************************************************************
536 template< typename MT1, typename MT2, typename MT3 >
537 struct TDMatTDMatAddExprTrait< DMatSMatSubExpr<MT1,MT2,true>, MT3 >
538 {
539  public:
540  //**********************************************************************************************
542  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
543  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
544  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
545  , typename TDMatTSMatSubExprTrait< typename TDMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
546  , INVALID_TYPE >::Type Type;
548  //**********************************************************************************************
549 };
551 //*************************************************************************************************
552 
553 
554 //*************************************************************************************************
556 template< typename MT1, typename MT2, typename MT3 >
557 struct DMatDMatSubExprTrait< DMatSMatSubExpr<MT1,MT2,false>, MT3 >
558 {
559  public:
560  //**********************************************************************************************
562  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
563  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
564  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
565  , typename DMatSMatSubExprTrait< typename DMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
566  , INVALID_TYPE >::Type Type;
568  //**********************************************************************************************
569 };
571 //*************************************************************************************************
572 
573 
574 //*************************************************************************************************
576 template< typename MT1, typename MT2, typename MT3 >
577 struct DMatTDMatSubExprTrait< DMatSMatSubExpr<MT1,MT2,false>, MT3 >
578 {
579  public:
580  //**********************************************************************************************
582  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
583  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
584  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
585  , typename DMatSMatSubExprTrait< typename DMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
586  , INVALID_TYPE >::Type Type;
588  //**********************************************************************************************
589 };
591 //*************************************************************************************************
592 
593 
594 //*************************************************************************************************
596 template< typename MT1, typename MT2, typename MT3 >
597 struct TDMatDMatSubExprTrait< DMatSMatSubExpr<MT1,MT2,true>, MT3 >
598 {
599  public:
600  //**********************************************************************************************
602  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
603  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
604  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
605  , typename DMatTSMatSubExprTrait< typename TDMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
606  , INVALID_TYPE >::Type Type;
608  //**********************************************************************************************
609 };
611 //*************************************************************************************************
612 
613 
614 //*************************************************************************************************
616 template< typename MT1, typename MT2, typename MT3 >
617 struct TDMatTDMatSubExprTrait< DMatSMatSubExpr<MT1,MT2,true>, MT3 >
618 {
619  public:
620  //**********************************************************************************************
622  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
623  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
624  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
625  , typename TDMatTSMatSubExprTrait< typename TDMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
626  , INVALID_TYPE >::Type Type;
628  //**********************************************************************************************
629 };
631 //*************************************************************************************************
632 
633 } // namespace blaze
634 
635 #endif