All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SMatTDMatSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATTDMATSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_SMATTDMATSUBEXPR_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 SMATTDMATSUBEXPR
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 class SMatTDMatSubExpr : public DenseMatrix< SMatTDMatSubExpr<MT1,MT2>, false >
74  , private Expression
75  , private Computation
76 {
77  private:
78  //**Type definitions****************************************************************************
79  typedef typename MT1::ResultType RT1;
80  typedef typename MT2::ResultType RT2;
81  typedef typename MT1::ReturnType RN1;
82  typedef typename MT2::ReturnType RN2;
83  //**********************************************************************************************
84 
85  //**Return type evaluation**********************************************************************
87 
92  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
93 
96  //**********************************************************************************************
97 
98  public:
99  //**Type definitions****************************************************************************
102  typedef typename ResultType::OppositeType OppositeType;
103  typedef typename ResultType::TransposeType TransposeType;
104  typedef typename ResultType::ElementType ElementType;
105 
108 
110  typedef const ResultType CompositeType;
111 
113  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
114 
116  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
117  //**********************************************************************************************
118 
119  //**Compilation flags***************************************************************************
121  enum { vectorizable = 0 };
122  //**********************************************************************************************
123 
124  //**Constructor*********************************************************************************
130  explicit inline SMatTDMatSubExpr( const MT1& lhs, const MT2& rhs )
131  : lhs_( lhs ) // Left-hand side sparse matrix of the subtraction expression
132  , rhs_( rhs ) // Right-hand side dense matrix of the subtraction expression
133  {
134  BLAZE_INTERNAL_ASSERT( lhs.rows() == rhs.rows() , "Invalid number of rows" );
135  BLAZE_INTERNAL_ASSERT( lhs.columns() == rhs.columns(), "Invalid number of columns" );
136  }
137  //**********************************************************************************************
138 
139  //**Access operator*****************************************************************************
146  inline ReturnType operator()( size_t i, size_t j ) const {
147  BLAZE_INTERNAL_ASSERT( i < lhs_.rows() , "Invalid row access index" );
148  BLAZE_INTERNAL_ASSERT( j < lhs_.columns(), "Invalid column access index" );
149  return lhs_(i,j) - rhs_(i,j);
150  }
151  //**********************************************************************************************
152 
153  //**Rows function*******************************************************************************
158  inline size_t rows() const {
159  return lhs_.rows();
160  }
161  //**********************************************************************************************
162 
163  //**Columns function****************************************************************************
168  inline size_t columns() const {
169  return lhs_.columns();
170  }
171  //**********************************************************************************************
172 
173  //**Left operand access*************************************************************************
178  inline LeftOperand leftOperand() const {
179  return lhs_;
180  }
181  //**********************************************************************************************
182 
183  //**Right operand access************************************************************************
188  inline RightOperand rightOperand() const {
189  return rhs_;
190  }
191  //**********************************************************************************************
192 
193  //**********************************************************************************************
199  template< typename T >
200  inline bool canAlias( const T* alias ) const {
201  return ( lhs_.canAlias( alias ) ) ||
202  ( IsExpression<MT2>::value && rhs_.canAlias( alias ) );
203  }
204  //**********************************************************************************************
205 
206  //**********************************************************************************************
212  template< typename T >
213  inline bool isAliased( const T* alias ) const {
214  return lhs_.isAliased( alias ) && rhs_.isAliased( alias );
215  }
216  //**********************************************************************************************
217 
218  private:
219  //**Member variables****************************************************************************
222  //**********************************************************************************************
223 
224  //**Assignment to dense matrices****************************************************************
236  template< typename MT // Type of the target dense matrix
237  , bool SO2 > // Storage order of the target dense matrix
238  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const SMatTDMatSubExpr& rhs )
239  {
241 
242  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
243  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
244 
245  assign ( ~lhs, -rhs.rhs_ );
246  addAssign( ~lhs, rhs.lhs_ );
247  }
249  //**********************************************************************************************
250 
251  //**Assignment to sparse matrices***************************************************************
263  template< typename MT // Type of the target sparse matrix
264  , bool SO2 > // Storage order of the target sparse matrix
265  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const SMatTDMatSubExpr& rhs )
266  {
268 
269  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
270 
276  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
277 
278  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
279  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
280 
281  const TmpType tmp( rhs );
282  assign( ~lhs, tmp );
283  }
285  //**********************************************************************************************
286 
287  //**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 SMatTDMatSubExpr& 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****************************************************
332  template< typename MT // Type of the target dense matrix
333  , bool SO2 > // Storage order of the target dense matrix
334  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const SMatTDMatSubExpr& rhs )
335  {
337 
338  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
339  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
340 
341  addAssign( ~lhs, rhs.rhs_ );
342  subAssign( ~lhs, rhs.lhs_ );
343  }
345  //**********************************************************************************************
346 
347  //**Subtraction assignment to sparse matrices***************************************************
348  // No special implementation for the subtraction assignment to sparse matrices.
349  //**********************************************************************************************
350 
351  //**Multiplication assignment to dense matrices*************************************************
352  // No special implementation for the multiplication assignment to dense matrices.
353  //**********************************************************************************************
354 
355  //**Multiplication assignment to sparse matrices************************************************
356  // No special implementation for the multiplication assignment to sparse matrices.
357  //**********************************************************************************************
358 
359  //**Compile time checks*************************************************************************
366  //**********************************************************************************************
367 };
368 //*************************************************************************************************
369 
370 
371 
372 
373 //=================================================================================================
374 //
375 // GLOBAL BINARY ARITHMETIC OPERATORS
376 //
377 //=================================================================================================
378 
379 //*************************************************************************************************
410 template< typename T1 // Type of the left-hand side sparse matrix
411  , typename T2 > // Type of the right-hand side dense matrix
412 inline const SMatTDMatSubExpr<T1,T2>
414 {
416 
417  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
418  throw std::invalid_argument( "Matrix sizes do not match" );
419 
420  return SMatTDMatSubExpr<T1,T2>( ~lhs, ~rhs );
421 }
422 //*************************************************************************************************
423 
424 
425 
426 
427 //=================================================================================================
428 //
429 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
430 //
431 //=================================================================================================
432 
433 //*************************************************************************************************
446 template< typename T1 // Type of the sparse matrix of the left-hand side expression
447  , typename T2 // Type of the dense matrix of the left-hand side expression
448  , typename T3 // Type of the right-hand side dense matrix
449  , bool SO > // Storage order of the right-hand side dense matrix
450 inline const typename AddExprTrait< SMatTDMatSubExpr<T1,T2>, T3 >::Type
451  operator+( const SMatTDMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
452 {
454 
455  return ( (~rhs) - lhs.rightOperand() ) + lhs.leftOperand();
456 }
458 //*************************************************************************************************
459 
460 
461 //*************************************************************************************************
474 template< typename T1 // Type of the sparse matrix of the left-hand side expression
475  , typename T2 // Type of the dense matrix of the left-hand side expression
476  , typename T3 // Type of the right-hand side dense matrix
477  , bool SO > // Storage order of the right-hand side dense matrix
478 inline const typename SubExprTrait< SMatTDMatSubExpr<T1,T2>, T3 >::Type
479  operator-( const SMatTDMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
480 {
482 
483  return lhs.leftOperand() - ( lhs.rightOperand() + (~rhs) );
484 }
486 //*************************************************************************************************
487 
488 
489 
490 
491 //=================================================================================================
492 //
493 // GLOBAL OPERATORS
494 //
495 //=================================================================================================
496 
497 //*************************************************************************************************
510 template< typename MT1 // Type of the left-hand side sparse matrix
511  , typename MT2 > // Type of the right-hand side dense matrix
512 inline typename RowExprTrait< SMatTDMatSubExpr<MT1,MT2> >::Type
513  row( const SMatTDMatSubExpr<MT1,MT2>& dm, size_t index )
514 {
516 
517  return row( dm.leftOperand(), index ) - row( dm.rightOperand(), index );
518 }
520 //*************************************************************************************************
521 
522 
523 //*************************************************************************************************
536 template< typename MT1 // Type of the left-hand side sparse matrix
537  , typename MT2 > // Type of the right-hand side dense matrix
538 inline typename ColumnExprTrait< SMatTDMatSubExpr<MT1,MT2> >::Type
539  column( const SMatTDMatSubExpr<MT1,MT2>& dm, size_t index )
540 {
542 
543  return column( dm.leftOperand(), index ) - column( dm.rightOperand(), index );
544 }
546 //*************************************************************************************************
547 
548 
549 
550 
551 //=================================================================================================
552 //
553 // EXPRESSION TRAIT SPECIALIZATIONS
554 //
555 //=================================================================================================
556 
557 //*************************************************************************************************
559 template< typename MT1, typename MT2, typename MT3 >
560 struct DMatDMatAddExprTrait< SMatTDMatSubExpr<MT1,MT2>, MT3 >
561 {
562  public:
563  //**********************************************************************************************
565  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
566  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
567  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
568  , typename DMatSMatAddExprTrait< typename DMatTDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
569  , INVALID_TYPE >::Type Type;
571  //**********************************************************************************************
572 };
574 //*************************************************************************************************
575 
576 
577 //*************************************************************************************************
579 template< typename MT1, typename MT2, typename MT3 >
580 struct DMatTDMatAddExprTrait< SMatTDMatSubExpr<MT1,MT2>, MT3 >
581 {
582  public:
583  //**********************************************************************************************
585  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
586  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
587  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
588  , typename TDMatSMatAddExprTrait< typename TDMatTDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
589  , INVALID_TYPE >::Type Type;
591  //**********************************************************************************************
592 };
594 //*************************************************************************************************
595 
596 
597 //*************************************************************************************************
599 template< typename MT1, typename MT2, typename MT3 >
600 struct DMatDMatSubExprTrait< SMatTDMatSubExpr<MT1,MT2>, MT3 >
601 {
602  public:
603  //**********************************************************************************************
605  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
606  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
607  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
608  , typename SMatDMatSubExprTrait< MT1, typename TDMatDMatAddExprTrait<MT2,MT3>::Type >::Type
609  , INVALID_TYPE >::Type Type;
611  //**********************************************************************************************
612 };
614 //*************************************************************************************************
615 
616 
617 //*************************************************************************************************
619 template< typename MT1, typename MT2, typename MT3 >
620 struct DMatTDMatSubExprTrait< SMatTDMatSubExpr<MT1,MT2>, MT3 >
621 {
622  public:
623  //**********************************************************************************************
625  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
626  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
627  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
628  , typename SMatTDMatSubExprTrait< MT1, typename TDMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
629  , INVALID_TYPE >::Type Type;
631  //**********************************************************************************************
632 };
634 //*************************************************************************************************
635 
636 
637 //*************************************************************************************************
639 template< typename MT1, typename MT2 >
640 struct RowExprTrait< SMatTDMatSubExpr<MT1,MT2> >
641 {
642  public:
643  //**********************************************************************************************
644  typedef typename SubExprTrait< typename RowExprTrait<const MT1>::Type
645  , typename RowExprTrait<const MT2>::Type >::Type Type;
646  //**********************************************************************************************
647 };
649 //*************************************************************************************************
650 
651 
652 //*************************************************************************************************
654 template< typename MT1, typename MT2 >
655 struct ColumnExprTrait< SMatTDMatSubExpr<MT1,MT2> >
656 {
657  public:
658  //**********************************************************************************************
659  typedef typename SubExprTrait< typename ColumnExprTrait<const MT1>::Type
660  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
661  //**********************************************************************************************
662 };
664 //*************************************************************************************************
665 
666 } // namespace blaze
667 
668 #endif