All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SMatDMatSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATDMATSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_SMATDMATSUBEXPR_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 SMATDMATSUBEXPR
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
68 template< typename MT1 // Type of the left-hand side sparse matrix
69  , typename MT2 // Type of the right-hand side dense matrix
70  , bool SO > // Storage order
71 class SMatDMatSubExpr : public DenseMatrix< SMatDMatSubExpr<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<MT2>::value };
123  //**********************************************************************************************
124 
125  //**Constructor*********************************************************************************
131  explicit inline SMatDMatSubExpr( const MT1& lhs, const MT2& rhs )
132  : lhs_( lhs ) // Left-hand side sparse matrix of the subtraction expression
133  , rhs_( rhs ) // Right-hand side dense 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<MT2>::value && rhs_.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 SMatDMatSubExpr& 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  assign ( ~lhs, -rhs.rhs_ );
232  addAssign( ~lhs, rhs.lhs_ );
233  }
235  //**********************************************************************************************
236 
237  //**Assignment to sparse matrices***************************************************************
249  template< typename MT // Type of the target sparse matrix
250  , bool SO2 > // Storage order of the target sparse matrix
251  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const SMatDMatSubExpr& rhs )
252  {
254 
260  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
261 
262  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
263  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
264 
265  const TmpType tmp( rhs );
266  assign( ~lhs, tmp );
267  }
269  //**********************************************************************************************
270 
271  //**Addition assignment to dense matrices*******************************************************
283  template< typename MT // Type of the target dense matrix
284  , bool SO2 > // Storage order of the target dense matrix
285  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const SMatDMatSubExpr& rhs )
286  {
287  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
288  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
289 
290  addAssign( ~lhs, rhs.lhs_ );
291  subAssign( ~lhs, rhs.rhs_ );
292  }
294  //**********************************************************************************************
295 
296  //**Addition assignment to sparse matrices******************************************************
297  // No special implementation for the addition assignment to sparse matrices.
298  //**********************************************************************************************
299 
300  //**Subtraction assignment to dense matrices****************************************************
312  template< typename MT // Type of the target dense matrix
313  , bool SO2 > // Storage order of the target dense matrix
314  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const SMatDMatSubExpr& rhs )
315  {
316  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
317  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
318 
319  subAssign( ~lhs, rhs.lhs_ );
320  addAssign( ~lhs, rhs.rhs_ );
321  }
323  //**********************************************************************************************
324 
325  //**Subtraction assignment to sparse matrices***************************************************
326  // No special implementation for the subtraction assignment to sparse matrices.
327  //**********************************************************************************************
328 
329  //**Multiplication assignment to dense matrices*************************************************
330  // No special implementation for the multiplication assignment to dense matrices.
331  //**********************************************************************************************
332 
333  //**Multiplication assignment to sparse matrices************************************************
334  // No special implementation for the multiplication assignment to sparse matrices.
335  //**********************************************************************************************
336 
337  //**Compile time checks*************************************************************************
343  //**********************************************************************************************
344 };
345 //*************************************************************************************************
346 
347 
348 
349 
350 //=================================================================================================
351 //
352 // GLOBAL BINARY ARITHMETIC OPERATORS
353 //
354 //=================================================================================================
355 
356 //*************************************************************************************************
385 template< typename T1 // Type of the left-hand side sparse matrix
386  , typename T2 // Type of the right-hand side dense matrix
387  , bool SO > // Storage order
388 inline const SMatDMatSubExpr<T1,T2,SO>
390 {
391  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
392  throw std::invalid_argument( "Matrix sizes do not match" );
393 
394  return SMatDMatSubExpr<T1,T2,SO>( ~lhs, ~rhs );
395 }
396 //*************************************************************************************************
397 
398 
399 
400 
401 //=================================================================================================
402 //
403 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
404 //
405 //=================================================================================================
406 
407 //*************************************************************************************************
420 template< typename T1 // Type of the sparse matrix of the left-hand side expression
421  , typename T2 // Type of the dense matrix of the left-hand side expression
422  , bool SO1 // Storage order of the left-hand side expression
423  , typename T3 // Type of the right-hand side dense matrix
424  , bool SO2 > // Storage order of the right-hand side dense matrix
425 inline const typename AddExprTrait< SMatDMatSubExpr<T1,T2,SO1>, T3 >::Type
426  operator+( const SMatDMatSubExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
427 {
428  return ( (~rhs) - lhs.rightOperand() ) + lhs.leftOperand();
429 }
431 //*************************************************************************************************
432 
433 
434 //*************************************************************************************************
447 template< typename T1 // Type of the sparse matrix of the left-hand side expression
448  , typename T2 // Type of the dense matrix of the left-hand side expression
449  , bool SO1 // Storage order of the left-hand side expression
450  , typename T3 // Type of the right-hand side dense matrix
451  , bool SO2 > // Storage order of the right-hand side dense matrix
452 inline const typename SubExprTrait< SMatDMatSubExpr<T1,T2,SO1>, T3 >::Type
453  operator-( const SMatDMatSubExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
454 {
455  return lhs.leftOperand() - ( lhs.rightOperand() + (~rhs) );
456 }
458 //*************************************************************************************************
459 
460 
461 
462 
463 //=================================================================================================
464 //
465 // EXPRESSION TRAIT SPECIALIZATIONS
466 //
467 //=================================================================================================
468 
469 //*************************************************************************************************
471 template< typename MT1, typename MT2, typename MT3 >
472 struct DMatDMatAddExprTrait< SMatDMatSubExpr<MT1,MT2,false>, MT3 >
473 {
474  public:
475  //**********************************************************************************************
477  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
478  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
479  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
480  , typename DMatSMatAddExprTrait< typename DMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
481  , INVALID_TYPE >::Type Type;
483  //**********************************************************************************************
484 };
486 //*************************************************************************************************
487 
488 
489 //*************************************************************************************************
491 template< typename MT1, typename MT2, typename MT3 >
492 struct DMatTDMatAddExprTrait< SMatDMatSubExpr<MT1,MT2,false>, MT3 >
493 {
494  public:
495  //**********************************************************************************************
497  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
498  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
499  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
500  , typename DMatSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
501  , INVALID_TYPE >::Type Type;
503  //**********************************************************************************************
504 };
506 //*************************************************************************************************
507 
508 
509 //*************************************************************************************************
511 template< typename MT1, typename MT2, typename MT3 >
512 struct TDMatDMatAddExprTrait< SMatDMatSubExpr<MT1,MT2,true>, MT3 >
513 {
514  public:
515  //**********************************************************************************************
517  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
518  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
519  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
520  , typename DMatTSMatAddExprTrait< typename DMatTDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
521  , INVALID_TYPE >::Type Type;
523  //**********************************************************************************************
524 };
526 //*************************************************************************************************
527 
528 
529 //*************************************************************************************************
531 template< typename MT1, typename MT2, typename MT3 >
532 struct TDMatTDMatAddExprTrait< SMatDMatSubExpr<MT1,MT2,true>, MT3 >
533 {
534  public:
535  //**********************************************************************************************
537  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
538  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
539  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
540  , typename TDMatTSMatAddExprTrait< typename TDMatTDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
541  , INVALID_TYPE >::Type Type;
543  //**********************************************************************************************
544 };
546 //*************************************************************************************************
547 
548 
549 //*************************************************************************************************
551 template< typename MT1, typename MT2, typename MT3 >
552 struct DMatDMatSubExprTrait< SMatDMatSubExpr<MT1,MT2,false>, MT3 >
553 {
554  public:
555  //**********************************************************************************************
557  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
558  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
559  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
560  , typename SMatDMatSubExprTrait< MT1, typename DMatDMatAddExprTrait<MT2,MT3>::Type >::Type
561  , INVALID_TYPE >::Type Type;
563  //**********************************************************************************************
564 };
566 //*************************************************************************************************
567 
568 
569 //*************************************************************************************************
571 template< typename MT1, typename MT2, typename MT3 >
572 struct DMatTDMatSubExprTrait< SMatDMatSubExpr<MT1,MT2,false>, MT3 >
573 {
574  public:
575  //**********************************************************************************************
577  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
578  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
579  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
580  , typename SMatDMatSubExprTrait< MT1, typename DMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
581  , INVALID_TYPE >::Type Type;
583  //**********************************************************************************************
584 };
586 //*************************************************************************************************
587 
588 
589 //*************************************************************************************************
591 template< typename MT1, typename MT2, typename MT3 >
592 struct TDMatDMatSubExprTrait< SMatDMatSubExpr<MT1,MT2,true>, MT3 >
593 {
594  public:
595  //**********************************************************************************************
597  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
598  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
599  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
600  , typename TSMatDMatSubExprTrait< MT1, typename TDMatDMatAddExprTrait<MT2,MT3>::Type >::Type
601  , INVALID_TYPE >::Type Type;
603  //**********************************************************************************************
604 };
606 //*************************************************************************************************
607 
608 
609 //*************************************************************************************************
611 template< typename MT1, typename MT2, typename MT3 >
612 struct TDMatTDMatSubExprTrait< SMatDMatSubExpr<MT1,MT2,true>, MT3 >
613 {
614  public:
615  //**********************************************************************************************
617  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
618  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
619  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
620  , typename TSMatTDMatSubExprTrait< MT1, typename TDMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
621  , INVALID_TYPE >::Type Type;
623  //**********************************************************************************************
624 };
626 //*************************************************************************************************
627 
628 } // namespace blaze
629 
630 #endif