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>
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 DMATSMATSUBEXPR
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  , bool SO > // Storage order
74 class DMatSMatSubExpr : public DenseMatrix< DMatSMatSubExpr<MT1,MT2,SO>, SO >
75  , private Expression
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 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 canAlias( const T* alias ) const {
202  return ( IsExpression<MT1>::value && lhs_.canAlias( alias ) ) ||
203  ( 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 DMatSMatSubExpr& 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  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
247  subAssign( ~lhs, rhs.rhs_ );
248  }
249  else {
250  assign ( ~lhs, rhs.lhs_ );
251  subAssign( ~lhs, rhs.rhs_ );
252  }
253  }
255  //**********************************************************************************************
256 
257  //**Assignment to sparse matrices***************************************************************
269  template< typename MT // Type of the target sparse matrix
270  , bool SO2 > // Storage order of the target sparse matrix
271  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const DMatSMatSubExpr& rhs )
272  {
274 
276 
282  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
283 
284  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
285  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
286 
287  const TmpType tmp( rhs );
288  assign( ~lhs, tmp );
289  }
291  //**********************************************************************************************
292 
293  //**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 DMatSMatSubExpr& 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  subAssign( ~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****************************************************
336  template< typename MT // Type of the target dense matrix
337  , bool SO2 > // Storage order of the target dense matrix
338  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const DMatSMatSubExpr& rhs )
339  {
341 
342  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
343  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
344 
345  subAssign( ~lhs, rhs.lhs_ );
346  addAssign( ~lhs, rhs.rhs_ );
347  }
349  //**********************************************************************************************
350 
351  //**Subtraction assignment to sparse matrices***************************************************
352  // No special implementation for the subtraction assignment to sparse matrices.
353  //**********************************************************************************************
354 
355  //**Multiplication assignment to dense matrices*************************************************
356  // No special implementation for the multiplication assignment to dense matrices.
357  //**********************************************************************************************
358 
359  //**Multiplication assignment to sparse matrices************************************************
360  // No special implementation for the multiplication assignment to sparse matrices.
361  //**********************************************************************************************
362 
363  //**Compile time checks*************************************************************************
369  //**********************************************************************************************
370 };
371 //*************************************************************************************************
372 
373 
374 
375 
376 //=================================================================================================
377 //
378 // GLOBAL BINARY ARITHMETIC OPERATORS
379 //
380 //=================================================================================================
381 
382 //*************************************************************************************************
411 template< typename T1 // Type of the left-hand side dense matrix
412  , typename T2 // Type of the right-hand side sparse matrix
413  , bool SO > // Storage order
414 inline const DMatSMatSubExpr<T1,T2,SO>
416 {
418 
419  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
420  throw std::invalid_argument( "Matrix sizes do not match" );
421 
422  return DMatSMatSubExpr<T1,T2,SO>( ~lhs, ~rhs );
423 }
424 //*************************************************************************************************
425 
426 
427 
428 
429 //=================================================================================================
430 //
431 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
432 //
433 //=================================================================================================
434 
435 //*************************************************************************************************
448 template< typename T1 // Type of the dense matrix of the left-hand side expression
449  , typename T2 // Type of the sparse matrix of the left-hand side expression
450  , bool SO1 // Storage order of the left-hand side expression
451  , typename T3 // Type of the right-hand side dense matrix
452  , bool SO2 > // Storage order of the right-hand side dense matrix
453 inline const typename AddExprTrait< DMatSMatSubExpr<T1,T2,SO1>, T3 >::Type
454  operator+( const DMatSMatSubExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
455 {
457 
458  return ( lhs.leftOperand() + (~rhs) ) - lhs.rightOperand();
459 }
461 //*************************************************************************************************
462 
463 
464 //*************************************************************************************************
477 template< typename T1 // Type of the dense matrix of the left-hand side expression
478  , typename T2 // Type of the sparse matrix of the left-hand side expression
479  , bool SO1 // Storage order of the left-hand side expression
480  , typename T3 // Type of the right-hand side dense matrix
481  , bool SO2 > // Storage order of the right-hand side dense matrix
482 inline const typename SubExprTrait< DMatSMatSubExpr<T1,T2,SO1>, T3 >::Type
483  operator-( const DMatSMatSubExpr<T1,T2,SO1>& lhs, const DenseMatrix<T3,SO2>& rhs )
484 {
486 
487  return ( lhs.leftOperand() - (~rhs) ) - lhs.rightOperand();
488 }
490 //*************************************************************************************************
491 
492 
493 
494 
495 //=================================================================================================
496 //
497 // GLOBAL OPERATORS
498 //
499 //=================================================================================================
500 
501 //*************************************************************************************************
513 template< typename MT1 // Type of the left-hand side dense matrix
514  , typename MT2 // Type of the right-hand side sparse matrix
515  , bool SO > // Storage order
516 inline typename RowExprTrait< DMatSMatSubExpr<MT1,MT2,SO> >::Type
517  row( const DMatSMatSubExpr<MT1,MT2,SO>& dm, size_t index )
518 {
520 
521  return row( dm.leftOperand(), index ) - row( dm.rightOperand(), index );
522 }
524 //*************************************************************************************************
525 
526 
527 //*************************************************************************************************
539 template< typename MT1 // Type of the left-hand side dense matrix
540  , typename MT2 // Type of the right-hand side sparse matrix
541  , bool SO > // Storage order
542 inline typename ColumnExprTrait< DMatSMatSubExpr<MT1,MT2,SO> >::Type
543  column( const DMatSMatSubExpr<MT1,MT2,SO>& dm, size_t index )
544 {
546 
547  return column( dm.leftOperand(), index ) - column( dm.rightOperand(), index );
548 }
550 //*************************************************************************************************
551 
552 
553 
554 
555 //=================================================================================================
556 //
557 // EXPRESSION TRAIT SPECIALIZATIONS
558 //
559 //=================================================================================================
560 
561 //*************************************************************************************************
563 template< typename MT1, typename MT2, typename MT3 >
564 struct DMatDMatAddExprTrait< DMatSMatSubExpr<MT1,MT2,false>, MT3 >
565 {
566  public:
567  //**********************************************************************************************
569  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
570  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
571  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
572  , typename DMatSMatSubExprTrait< typename DMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
573  , INVALID_TYPE >::Type Type;
575  //**********************************************************************************************
576 };
578 //*************************************************************************************************
579 
580 
581 //*************************************************************************************************
583 template< typename MT1, typename MT2, typename MT3 >
584 struct DMatTDMatAddExprTrait< DMatSMatSubExpr<MT1,MT2,false>, MT3 >
585 {
586  public:
587  //**********************************************************************************************
589  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
590  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
591  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
592  , typename DMatSMatSubExprTrait< typename DMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
593  , INVALID_TYPE >::Type Type;
595  //**********************************************************************************************
596 };
598 //*************************************************************************************************
599 
600 
601 //*************************************************************************************************
603 template< typename MT1, typename MT2, typename MT3 >
604 struct TDMatDMatAddExprTrait< DMatSMatSubExpr<MT1,MT2,true>, MT3 >
605 {
606  public:
607  //**********************************************************************************************
609  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
610  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
611  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
612  , typename DMatTSMatSubExprTrait< typename TDMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
613  , INVALID_TYPE >::Type Type;
615  //**********************************************************************************************
616 };
618 //*************************************************************************************************
619 
620 
621 //*************************************************************************************************
623 template< typename MT1, typename MT2, typename MT3 >
624 struct TDMatTDMatAddExprTrait< DMatSMatSubExpr<MT1,MT2,true>, MT3 >
625 {
626  public:
627  //**********************************************************************************************
629  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
630  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
631  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
632  , typename TDMatTSMatSubExprTrait< typename TDMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
633  , INVALID_TYPE >::Type Type;
635  //**********************************************************************************************
636 };
638 //*************************************************************************************************
639 
640 
641 //*************************************************************************************************
643 template< typename MT1, typename MT2, typename MT3 >
644 struct DMatDMatSubExprTrait< DMatSMatSubExpr<MT1,MT2,false>, MT3 >
645 {
646  public:
647  //**********************************************************************************************
649  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
650  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
651  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
652  , typename DMatSMatSubExprTrait< typename DMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
653  , INVALID_TYPE >::Type Type;
655  //**********************************************************************************************
656 };
658 //*************************************************************************************************
659 
660 
661 //*************************************************************************************************
663 template< typename MT1, typename MT2, typename MT3 >
664 struct DMatTDMatSubExprTrait< DMatSMatSubExpr<MT1,MT2,false>, MT3 >
665 {
666  public:
667  //**********************************************************************************************
669  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
670  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
671  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
672  , typename DMatSMatSubExprTrait< typename DMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
673  , INVALID_TYPE >::Type Type;
675  //**********************************************************************************************
676 };
678 //*************************************************************************************************
679 
680 
681 //*************************************************************************************************
683 template< typename MT1, typename MT2, typename MT3 >
684 struct TDMatDMatSubExprTrait< DMatSMatSubExpr<MT1,MT2,true>, MT3 >
685 {
686  public:
687  //**********************************************************************************************
689  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
690  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
691  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
692  , typename DMatTSMatSubExprTrait< typename TDMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
693  , INVALID_TYPE >::Type Type;
695  //**********************************************************************************************
696 };
698 //*************************************************************************************************
699 
700 
701 //*************************************************************************************************
703 template< typename MT1, typename MT2, typename MT3 >
704 struct TDMatTDMatSubExprTrait< DMatSMatSubExpr<MT1,MT2,true>, MT3 >
705 {
706  public:
707  //**********************************************************************************************
709  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
710  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
711  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
712  , typename TDMatTSMatSubExprTrait< typename TDMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
713  , INVALID_TYPE >::Type Type;
715  //**********************************************************************************************
716 };
718 //*************************************************************************************************
719 
720 
721 //*************************************************************************************************
723 template< typename MT1, typename MT2, bool SO >
724 struct RowExprTrait< DMatSMatSubExpr<MT1,MT2,SO> >
725 {
726  public:
727  //**********************************************************************************************
728  typedef typename SubExprTrait< typename RowExprTrait<const MT1>::Type
729  , typename RowExprTrait<const MT2>::Type >::Type Type;
730  //**********************************************************************************************
731 };
733 //*************************************************************************************************
734 
735 
736 //*************************************************************************************************
738 template< typename MT1, typename MT2, bool SO >
739 struct ColumnExprTrait< DMatSMatSubExpr<MT1,MT2,SO> >
740 {
741  public:
742  //**********************************************************************************************
743  typedef typename SubExprTrait< typename ColumnExprTrait<const MT1>::Type
744  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
745  //**********************************************************************************************
746 };
748 //*************************************************************************************************
749 
750 } // namespace blaze
751 
752 #endif