All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TDMatSMatSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_TDMATSMATSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_TDMATSMATSUBEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
45 #include <blaze/util/Assert.h>
48 #include <blaze/util/SelectType.h>
49 #include <blaze/util/Types.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // CLASS TDMATSMATSUBEXPR
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
67 template< typename MT1 // Type of the left-hand side dense matrix
68  , typename MT2 > // Type of the right-hand side sparse matrix
69 class TDMatSMatSubExpr : public DenseMatrix< TDMatSMatSubExpr<MT1,MT2>, false >
70  , private Expression
71  , private Computation
72 {
73  private:
74  //**Type definitions****************************************************************************
75  typedef typename MT1::ResultType RT1;
76  typedef typename MT2::ResultType RT2;
77  typedef typename MT1::ReturnType RN1;
78  typedef typename MT2::ReturnType RN2;
79  //**********************************************************************************************
80 
81  //**Return type evaluation**********************************************************************
83 
88  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
89 
92  //**********************************************************************************************
93 
94  public:
95  //**Type definitions****************************************************************************
98  typedef typename ResultType::OppositeType OppositeType;
99  typedef typename ResultType::TransposeType TransposeType;
100  typedef typename ResultType::ElementType ElementType;
101 
104 
106  typedef const ResultType CompositeType;
107 
109  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
110 
112  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
113  //**********************************************************************************************
114 
115  //**Compilation flags***************************************************************************
117  enum { vectorizable = 0 };
118  //**********************************************************************************************
119 
120  //**Constructor*********************************************************************************
126  explicit inline TDMatSMatSubExpr( const MT1& lhs, const MT2& rhs )
127  : lhs_( lhs ) // Left-hand side dense matrix of the subtraction expression
128  , rhs_( rhs ) // Right-hand side sparse matrix of the subtraction expression
129  {
130  BLAZE_INTERNAL_ASSERT( lhs.rows() == rhs.rows() , "Invalid number of rows" );
131  BLAZE_INTERNAL_ASSERT( lhs.columns() == rhs.columns(), "Invalid number of columns" );
132  }
133  //**********************************************************************************************
134 
135  //**Access operator*****************************************************************************
142  inline ReturnType operator()( size_t i, size_t j ) const {
143  BLAZE_INTERNAL_ASSERT( i < lhs_.rows() , "Invalid row access index" );
144  BLAZE_INTERNAL_ASSERT( j < lhs_.columns(), "Invalid column access index" );
145  return lhs_(i,j) - rhs_(i,j);
146  }
147  //**********************************************************************************************
148 
149  //**Rows function*******************************************************************************
154  inline size_t rows() const {
155  return lhs_.rows();
156  }
157  //**********************************************************************************************
158 
159  //**Columns function****************************************************************************
164  inline size_t columns() const {
165  return lhs_.columns();
166  }
167  //**********************************************************************************************
168 
169  //**Left operand access*************************************************************************
174  inline LeftOperand leftOperand() const {
175  return lhs_;
176  }
177  //**********************************************************************************************
178 
179  //**Right operand access************************************************************************
184  inline RightOperand rightOperand() const {
185  return rhs_;
186  }
187  //**********************************************************************************************
188 
189  //**********************************************************************************************
195  template< typename T >
196  inline bool canAlias( const T* alias ) const {
197  return ( IsExpression<MT1>::value && lhs_.canAlias( alias ) ) ||
198  ( rhs_.canAlias( alias ) );
199  }
200  //**********************************************************************************************
201 
202  //**********************************************************************************************
208  template< typename T >
209  inline bool isAliased( const T* alias ) const {
210  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
211  }
212  //**********************************************************************************************
213 
214  private:
215  //**Member variables****************************************************************************
218  //**********************************************************************************************
219 
220  //**Assignment to dense matrices****************************************************************
232  template< typename MT // Type of the target dense matrix
233  , bool SO2 > // Storage order of the target dense matrix
234  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const TDMatSMatSubExpr& rhs )
235  {
237 
238  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
239  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
240 
241  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
242  subAssign( ~lhs, rhs.rhs_ );
243  }
244  else {
245  assign ( ~lhs, rhs.lhs_ );
246  subAssign( ~lhs, rhs.rhs_ );
247  }
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 TDMatSMatSubExpr& rhs )
267  {
269 
270  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
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*******************************************************
301  template< typename MT // Type of the target dense matrix
302  , bool SO2 > // Storage order of the target dense matrix
303  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const TDMatSMatSubExpr& rhs )
304  {
306 
307  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
308  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
309 
310  addAssign( ~lhs, rhs.lhs_ );
311  subAssign( ~lhs, rhs.rhs_ );
312  }
314  //**********************************************************************************************
315 
316  //**Addition assignment to sparse matrices******************************************************
317  // No special implementation for the addition assignment to sparse matrices.
318  //**********************************************************************************************
319 
320  //**Subtraction assignment to dense matrices****************************************************
333  template< typename MT // Type of the target dense matrix
334  , bool SO2 > // Storage order of the target dense matrix
335  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const TDMatSMatSubExpr& rhs )
336  {
338 
339  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
340  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
341 
342  subAssign( ~lhs, rhs.lhs_ );
343  addAssign( ~lhs, rhs.rhs_ );
344  }
346  //**********************************************************************************************
347 
348  //**Subtraction assignment to sparse matrices***************************************************
349  // No special implementation for the subtraction assignment to sparse matrices.
350  //**********************************************************************************************
351 
352  //**Multiplication assignment to dense matrices*************************************************
353  // No special implementation for the multiplication assignment to dense matrices.
354  //**********************************************************************************************
355 
356  //**Multiplication assignment to sparse matrices************************************************
357  // No special implementation for the multiplication assignment to sparse matrices.
358  //**********************************************************************************************
359 
360  //**Compile time checks*************************************************************************
367  //**********************************************************************************************
368 };
369 //*************************************************************************************************
370 
371 
372 
373 
374 //=================================================================================================
375 //
376 // GLOBAL BINARY ARITHMETIC OPERATORS
377 //
378 //=================================================================================================
379 
380 //*************************************************************************************************
411 template< typename T1 // Type of the left-hand side dense matrix
412  , typename T2 > // Type of the right-hand side sparse matrix
413 inline const TDMatSMatSubExpr<T1,T2>
415 {
417 
418  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
419  throw std::invalid_argument( "Matrix sizes do not match" );
420 
421  return TDMatSMatSubExpr<T1,T2>( ~lhs, ~rhs );
422 }
423 //*************************************************************************************************
424 
425 
426 
427 
428 //=================================================================================================
429 //
430 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
431 //
432 //=================================================================================================
433 
434 //*************************************************************************************************
447 template< typename T1 // Type of the dense matrix of the left-hand side expression
448  , typename T2 // Type of the sparse matrix of the left-hand side expression
449  , typename T3 // Type of the right-hand side dense matrix
450  , bool SO > // Storage order of the right-hand side dense matrix
451 inline const typename AddExprTrait< TDMatSMatSubExpr<T1,T2>, T3 >::Type
452  operator+( const TDMatSMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
453 {
455 
456  return ( lhs.leftOperand() + (~rhs) ) - lhs.rightOperand();
457 }
459 //*************************************************************************************************
460 
461 
462 //*************************************************************************************************
475 template< typename T1 // Type of the dense matrix of the left-hand side expression
476  , typename T2 // Type of the sparse matrix of the left-hand side expression
477  , typename T3 // Type of the right-hand side dense matrix
478  , bool SO > // Storage order of the right-hand side dense matrix
479 inline const typename SubExprTrait< TDMatSMatSubExpr<T1,T2>, T3 >::Type
480  operator-( const TDMatSMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
481 {
483 
484  return ( lhs.leftOperand() - (~rhs) ) - lhs.rightOperand();
485 }
487 //*************************************************************************************************
488 
489 
490 
491 
492 //=================================================================================================
493 //
494 // GLOBAL OPERATORS
495 //
496 //=================================================================================================
497 
498 //*************************************************************************************************
511 template< typename MT1 // Type of the left-hand side dense matrix
512  , typename MT2 > // Type of the right-hand side sparse matrix
513 inline typename RowExprTrait< TDMatSMatSubExpr<MT1,MT2> >::Type
514  row( const TDMatSMatSubExpr<MT1,MT2>& dm, size_t index )
515 {
517 
518  return row( dm.leftOperand(), index ) - row( dm.rightOperand(), index );
519 }
521 //*************************************************************************************************
522 
523 
524 //*************************************************************************************************
537 template< typename MT1 // Type of the left-hand side dense matrix
538  , typename MT2 > // Type of the right-hand side sparse matrix
539 inline typename ColumnExprTrait< TDMatSMatSubExpr<MT1,MT2> >::Type
540  column( const TDMatSMatSubExpr<MT1,MT2>& dm, size_t index )
541 {
543 
544  return column( dm.leftOperand(), index ) - column( dm.rightOperand(), index );
545 }
547 //*************************************************************************************************
548 
549 
550 
551 
552 //=================================================================================================
553 //
554 // EXPRESSION TRAIT SPECIALIZATIONS
555 //
556 //=================================================================================================
557 
558 //*************************************************************************************************
560 template< typename MT1, typename MT2, typename MT3 >
561 struct DMatDMatAddExprTrait< TDMatSMatSubExpr<MT1,MT2>, MT3 >
562 {
563  public:
564  //**********************************************************************************************
566  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
567  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
568  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
569  , typename DMatSMatSubExprTrait< typename TDMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
570  , INVALID_TYPE >::Type Type;
572  //**********************************************************************************************
573 };
575 //*************************************************************************************************
576 
577 
578 //*************************************************************************************************
580 template< typename MT1, typename MT2, typename MT3 >
581 struct DMatTDMatAddExprTrait< TDMatSMatSubExpr<MT1,MT2>, MT3 >
582 {
583  public:
584  //**********************************************************************************************
586  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
587  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
588  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
589  , typename TDMatSMatSubExprTrait< typename TDMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
590  , INVALID_TYPE >::Type Type;
592  //**********************************************************************************************
593 };
595 //*************************************************************************************************
596 
597 
598 //*************************************************************************************************
600 template< typename MT1, typename MT2, typename MT3 >
601 struct DMatDMatSubExprTrait< TDMatSMatSubExpr<MT1,MT2>, MT3 >
602 {
603  public:
604  //**********************************************************************************************
606  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
607  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
608  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
609  , typename DMatSMatSubExprTrait< typename TDMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
610  , INVALID_TYPE >::Type Type;
612  //**********************************************************************************************
613 };
615 //*************************************************************************************************
616 
617 
618 //*************************************************************************************************
620 template< typename MT1, typename MT2, typename MT3 >
621 struct DMatTDMatSubExprTrait< TDMatSMatSubExpr<MT1,MT2>, MT3 >
622 {
623  public:
624  //**********************************************************************************************
626  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
627  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
628  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
629  , typename TDMatSMatSubExprTrait< typename TDMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
630  , INVALID_TYPE >::Type Type;
632  //**********************************************************************************************
633 };
635 //*************************************************************************************************
636 
637 
638 //*************************************************************************************************
640 template< typename MT1, typename MT2 >
641 struct RowExprTrait< TDMatSMatSubExpr<MT1,MT2> >
642 {
643  public:
644  //**********************************************************************************************
645  typedef typename SubExprTrait< typename RowExprTrait<const MT1>::Type
646  , typename RowExprTrait<const MT2>::Type >::Type Type;
647  //**********************************************************************************************
648 };
650 //*************************************************************************************************
651 
652 
653 //*************************************************************************************************
655 template< typename MT1, typename MT2 >
656 struct ColumnExprTrait< TDMatSMatSubExpr<MT1,MT2> >
657 {
658  public:
659  //**********************************************************************************************
660  typedef typename SubExprTrait< typename ColumnExprTrait<const MT1>::Type
661  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
662  //**********************************************************************************************
663 };
665 //*************************************************************************************************
666 
667 } // namespace blaze
668 
669 #endif