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>
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 SMATDMATSUBEXPR
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename MT1 // Type of the left-hand side sparse matrix
72  , typename MT2 // Type of the right-hand side dense matrix
73  , bool SO > // Storage order
74 class SMatDMatSubExpr : public DenseMatrix< SMatDMatSubExpr<MT1,MT2,SO>, SO >
75  , private MatMatSubExpr
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 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 canAlias( const T* alias ) const {
202  return ( lhs_.canAlias( alias ) ) ||
203  ( IsExpression<MT2>::value && 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 SMatDMatSubExpr& 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  assign ( ~lhs, -rhs.rhs_ );
247  addAssign( ~lhs, rhs.lhs_ );
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 SMatDMatSubExpr& rhs )
267  {
269 
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*******************************************************
300  template< typename MT // Type of the target dense matrix
301  , bool SO2 > // Storage order of the target dense matrix
302  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const SMatDMatSubExpr& rhs )
303  {
305 
306  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
307  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
308 
309  subAssign( ~lhs, rhs.rhs_ );
310  addAssign( ~lhs, rhs.lhs_ );
311  }
313  //**********************************************************************************************
314 
315  //**Addition assignment to sparse matrices******************************************************
316  // No special implementation for the addition assignment to sparse matrices.
317  //**********************************************************************************************
318 
319  //**Subtraction assignment to dense matrices****************************************************
331  template< typename MT // Type of the target dense matrix
332  , bool SO2 > // Storage order of the target dense matrix
333  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const SMatDMatSubExpr& rhs )
334  {
336 
337  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
338  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
339 
340  addAssign( ~lhs, rhs.rhs_ );
341  subAssign( ~lhs, rhs.lhs_ );
342  }
344  //**********************************************************************************************
345 
346  //**Subtraction assignment to sparse matrices***************************************************
347  // No special implementation for the subtraction assignment to sparse matrices.
348  //**********************************************************************************************
349 
350  //**Multiplication assignment to dense matrices*************************************************
351  // No special implementation for the multiplication assignment to dense matrices.
352  //**********************************************************************************************
353 
354  //**Multiplication assignment to sparse matrices************************************************
355  // No special implementation for the multiplication assignment to sparse matrices.
356  //**********************************************************************************************
357 
358  //**Compile time checks*************************************************************************
364  //**********************************************************************************************
365 };
366 //*************************************************************************************************
367 
368 
369 
370 
371 //=================================================================================================
372 //
373 // GLOBAL BINARY ARITHMETIC OPERATORS
374 //
375 //=================================================================================================
376 
377 //*************************************************************************************************
406 template< typename T1 // Type of the left-hand side sparse matrix
407  , typename T2 // Type of the right-hand side dense matrix
408  , bool SO > // Storage order
409 inline const SMatDMatSubExpr<T1,T2,SO>
411 {
413 
414  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
415  throw std::invalid_argument( "Matrix sizes do not match" );
416 
417  return SMatDMatSubExpr<T1,T2,SO>( ~lhs, ~rhs );
418 }
419 //*************************************************************************************************
420 
421 
422 
423 
424 //=================================================================================================
425 //
426 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
427 //
428 //=================================================================================================
429 
430 //*************************************************************************************************
443 template< typename T1 // Type of the sparse matrix of the left-hand side expression
444  , typename T2 // Type of the dense matrix of the left-hand side expression
445  , bool SO1 // Storage order of the left-hand side expression
446  , typename T3 // Type of the right-hand side dense matrix
447  , bool SO2 > // Storage order of the right-hand side dense matrix
448 inline const typename AddExprTrait< SMatDMatSubExpr<T1,T2,SO1>, T3 >::Type
449  operator+( const SMatDMatSubExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
450 {
452 
453  return ( (~rhs) - lhs.rightOperand() ) + lhs.leftOperand();
454 }
456 //*************************************************************************************************
457 
458 
459 //*************************************************************************************************
472 template< typename T1 // Type of the sparse matrix of the left-hand side expression
473  , typename T2 // Type of the dense matrix of the left-hand side expression
474  , bool SO1 // Storage order of the left-hand side expression
475  , typename T3 // Type of the right-hand side dense matrix
476  , bool SO2 > // Storage order of the right-hand side dense matrix
477 inline const typename SubExprTrait< SMatDMatSubExpr<T1,T2,SO1>, T3 >::Type
478  operator-( const SMatDMatSubExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
479 {
481 
482  return lhs.leftOperand() - ( lhs.rightOperand() + (~rhs) );
483 }
485 //*************************************************************************************************
486 
487 
488 
489 
490 //=================================================================================================
491 //
492 // EXPRESSION TRAIT SPECIALIZATIONS
493 //
494 //=================================================================================================
495 
496 //*************************************************************************************************
498 template< typename MT1, typename MT2, typename MT3 >
499 struct DMatDMatAddExprTrait< SMatDMatSubExpr<MT1,MT2,false>, MT3 >
500 {
501  public:
502  //**********************************************************************************************
504  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
505  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
506  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
507  , typename DMatSMatAddExprTrait< typename DMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
508  , INVALID_TYPE >::Type Type;
510  //**********************************************************************************************
511 };
513 //*************************************************************************************************
514 
515 
516 //*************************************************************************************************
518 template< typename MT1, typename MT2, typename MT3 >
519 struct DMatTDMatAddExprTrait< SMatDMatSubExpr<MT1,MT2,false>, MT3 >
520 {
521  public:
522  //**********************************************************************************************
524  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
525  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
526  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
527  , typename DMatSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
528  , INVALID_TYPE >::Type Type;
530  //**********************************************************************************************
531 };
533 //*************************************************************************************************
534 
535 
536 //*************************************************************************************************
538 template< typename MT1, typename MT2, typename MT3 >
539 struct TDMatDMatAddExprTrait< SMatDMatSubExpr<MT1,MT2,true>, MT3 >
540 {
541  public:
542  //**********************************************************************************************
544  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
545  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
546  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
547  , typename DMatTSMatAddExprTrait< typename DMatTDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
548  , INVALID_TYPE >::Type Type;
550  //**********************************************************************************************
551 };
553 //*************************************************************************************************
554 
555 
556 //*************************************************************************************************
558 template< typename MT1, typename MT2, typename MT3 >
559 struct TDMatTDMatAddExprTrait< SMatDMatSubExpr<MT1,MT2,true>, MT3 >
560 {
561  public:
562  //**********************************************************************************************
564  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
565  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
566  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
567  , typename TDMatTSMatAddExprTrait< typename TDMatTDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
568  , INVALID_TYPE >::Type Type;
570  //**********************************************************************************************
571 };
573 //*************************************************************************************************
574 
575 
576 //*************************************************************************************************
578 template< typename MT1, typename MT2, typename MT3 >
579 struct DMatDMatSubExprTrait< SMatDMatSubExpr<MT1,MT2,false>, MT3 >
580 {
581  public:
582  //**********************************************************************************************
584  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
585  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
586  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
587  , typename SMatDMatSubExprTrait< MT1, typename DMatDMatAddExprTrait<MT2,MT3>::Type >::Type
588  , INVALID_TYPE >::Type Type;
590  //**********************************************************************************************
591 };
593 //*************************************************************************************************
594 
595 
596 //*************************************************************************************************
598 template< typename MT1, typename MT2, typename MT3 >
599 struct DMatTDMatSubExprTrait< SMatDMatSubExpr<MT1,MT2,false>, MT3 >
600 {
601  public:
602  //**********************************************************************************************
604  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
605  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
606  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
607  , typename SMatDMatSubExprTrait< MT1, typename DMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
608  , INVALID_TYPE >::Type Type;
610  //**********************************************************************************************
611 };
613 //*************************************************************************************************
614 
615 
616 //*************************************************************************************************
618 template< typename MT1, typename MT2, typename MT3 >
619 struct TDMatDMatSubExprTrait< SMatDMatSubExpr<MT1,MT2,true>, MT3 >
620 {
621  public:
622  //**********************************************************************************************
624  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
625  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
626  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
627  , typename TSMatDMatSubExprTrait< MT1, typename TDMatDMatAddExprTrait<MT2,MT3>::Type >::Type
628  , INVALID_TYPE >::Type Type;
630  //**********************************************************************************************
631 };
633 //*************************************************************************************************
634 
635 
636 //*************************************************************************************************
638 template< typename MT1, typename MT2, typename MT3 >
639 struct TDMatTDMatSubExprTrait< SMatDMatSubExpr<MT1,MT2,true>, MT3 >
640 {
641  public:
642  //**********************************************************************************************
644  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
645  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
646  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
647  , typename TSMatTDMatSubExprTrait< MT1, typename TDMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
648  , INVALID_TYPE >::Type Type;
650  //**********************************************************************************************
651 };
653 //*************************************************************************************************
654 
655 
656 //*************************************************************************************************
658 template< typename MT1, typename MT2, bool SO >
659 struct RowExprTrait< SMatDMatSubExpr<MT1,MT2,SO> >
660 {
661  public:
662  //**********************************************************************************************
663  typedef typename SubExprTrait< typename RowExprTrait<const MT1>::Type
664  , typename RowExprTrait<const MT2>::Type >::Type Type;
665  //**********************************************************************************************
666 };
668 //*************************************************************************************************
669 
670 
671 //*************************************************************************************************
673 template< typename MT1, typename MT2, bool SO >
674 struct ColumnExprTrait< SMatDMatSubExpr<MT1,MT2,SO> >
675 {
676  public:
677  //**********************************************************************************************
678  typedef typename SubExprTrait< typename ColumnExprTrait<const MT1>::Type
679  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
680  //**********************************************************************************************
681 };
683 //*************************************************************************************************
684 
685 } // namespace blaze
686 
687 #endif