All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TSMatDMatSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_TSMATDMATSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_TSMATDMATSUBEXPR_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 SMATTDMATSUBEXPR
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
67 template< typename MT1 // Type of the left-hand side sparse matrix
68  , typename MT2 > // Type of the right-hand side dense matrix
69 class TSMatDMatSubExpr : public DenseMatrix< TSMatDMatSubExpr<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 TSMatDMatSubExpr( const MT1& lhs, const MT2& rhs )
127  : lhs_( lhs ) // Left-hand side sparse matrix of the subtraction expression
128  , rhs_( rhs ) // Right-hand side dense 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 ( lhs_.canAlias( alias ) ) ||
198  ( IsExpression<MT2>::value && 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 TSMatDMatSubExpr& 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  assign ( ~lhs, -rhs.rhs_ );
242  addAssign( ~lhs, rhs.lhs_ );
243  }
245  //**********************************************************************************************
246 
247  //**Assignment to sparse matrices***************************************************************
259  template< typename MT // Type of the target sparse matrix
260  , bool SO2 > // Storage order of the target sparse matrix
261  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const TSMatDMatSubExpr& rhs )
262  {
264 
265  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
266 
272  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
273 
274  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
275  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
276 
277  const TmpType tmp( rhs );
278  assign( ~lhs, tmp );
279  }
281  //**********************************************************************************************
282 
283  //**Addition assignment to dense matrices*******************************************************
296  template< typename MT // Type of the target dense matrix
297  , bool SO2 > // Storage order of the target dense matrix
298  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const TSMatDMatSubExpr& rhs )
299  {
301 
302  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
303  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
304 
305  subAssign( ~lhs, rhs.rhs_ );
306  addAssign( ~lhs, rhs.lhs_ );
307  }
309  //**********************************************************************************************
310 
311  //**Addition assignment to sparse matrices******************************************************
312  // No special implementation for the addition assignment to sparse matrices.
313  //**********************************************************************************************
314 
315  //**Subtraction assignment to dense matrices****************************************************
328  template< typename MT // Type of the target dense matrix
329  , bool SO2 > // Storage order of the target dense matrix
330  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const TSMatDMatSubExpr& rhs )
331  {
333 
334  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
335  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
336 
337  addAssign( ~lhs, rhs.rhs_ );
338  subAssign( ~lhs, rhs.lhs_ );
339  }
341  //**********************************************************************************************
342 
343  //**Subtraction assignment to sparse matrices***************************************************
344  // No special implementation for the subtraction assignment to sparse matrices.
345  //**********************************************************************************************
346 
347  //**Multiplication assignment to dense matrices*************************************************
348  // No special implementation for the multiplication assignment to dense matrices.
349  //**********************************************************************************************
350 
351  //**Multiplication assignment to sparse matrices************************************************
352  // No special implementation for the multiplication assignment to sparse matrices.
353  //**********************************************************************************************
354 
355  //**Compile time checks*************************************************************************
362  //**********************************************************************************************
363 };
364 //*************************************************************************************************
365 
366 
367 
368 
369 //=================================================================================================
370 //
371 // GLOBAL BINARY ARITHMETIC OPERATORS
372 //
373 //=================================================================================================
374 
375 //*************************************************************************************************
405 template< typename T1 // Type of the left-hand side sparse matrix
406  , typename T2 > // Type of the right-hand side dense matrix
407 inline const TSMatDMatSubExpr<T1,T2>
409 {
411 
412  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
413  throw std::invalid_argument( "Matrix sizes do not match" );
414 
415  return TSMatDMatSubExpr<T1,T2>( ~lhs, ~rhs );
416 }
417 //*************************************************************************************************
418 
419 
420 
421 
422 //=================================================================================================
423 //
424 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
425 //
426 //=================================================================================================
427 
428 //*************************************************************************************************
441 template< typename T1 // Type of the sparse matrix of the left-hand side expression
442  , typename T2 // Type of the dense matrix of the left-hand side expression
443  , typename T3 // Type of the right-hand side dense matrix
444  , bool SO > // Storage order of the right-hand side dense matrix
445 inline const typename AddExprTrait< TSMatDMatSubExpr<T1,T2>, T3 >::Type
446  operator+( const TSMatDMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
447 {
449 
450  return ( (~rhs) - lhs.rightOperand() ) + lhs.leftOperand();
451 }
453 //*************************************************************************************************
454 
455 
456 //*************************************************************************************************
469 template< typename T1 // Type of the sparse matrix of the left-hand side expression
470  , typename T2 // Type of the dense matrix of the left-hand side expression
471  , typename T3 // Type of the right-hand side dense matrix
472  , bool SO > // Storage order of the right-hand side dense matrix
473 inline const typename SubExprTrait< TSMatDMatSubExpr<T1,T2>, T3 >::Type
474  operator-( const TSMatDMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
475 {
477 
478  return lhs.leftOperand() - ( lhs.rightOperand() + (~rhs) );
479 }
481 //*************************************************************************************************
482 
483 
484 
485 
486 //=================================================================================================
487 //
488 // GLOBAL OPERATORS
489 //
490 //=================================================================================================
491 
492 //*************************************************************************************************
505 template< typename MT1 // Type of the left-hand side sparse matrix
506  , typename MT2 > // Type of the right-hand side dense matrix
507 inline typename RowExprTrait< TSMatDMatSubExpr<MT1,MT2> >::Type
508  row( const TSMatDMatSubExpr<MT1,MT2>& dm, size_t index )
509 {
511 
512  return row( dm.leftOperand(), index ) - row( dm.rightOperand(), index );
513 }
515 //*************************************************************************************************
516 
517 
518 //*************************************************************************************************
531 template< typename MT1 // Type of the left-hand side sparse matrix
532  , typename MT2 > // Type of the right-hand side dense matrix
533 inline typename ColumnExprTrait< TSMatDMatSubExpr<MT1,MT2> >::Type
534  column( const TSMatDMatSubExpr<MT1,MT2>& dm, size_t index )
535 {
537 
538  return column( dm.leftOperand(), index ) - column( dm.rightOperand(), index );
539 }
541 //*************************************************************************************************
542 
543 
544 
545 
546 //=================================================================================================
547 //
548 // EXPRESSION TRAIT SPECIALIZATIONS
549 //
550 //=================================================================================================
551 
552 //*************************************************************************************************
554 template< typename MT1, typename MT2, typename MT3 >
555 struct DMatDMatAddExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
556 {
557  public:
558  //**********************************************************************************************
560  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
561  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
562  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
563  , typename DMatTSMatAddExprTrait< typename DMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
564  , INVALID_TYPE >::Type Type;
566  //**********************************************************************************************
567 };
569 //*************************************************************************************************
570 
571 
572 //*************************************************************************************************
574 template< typename MT1, typename MT2, typename MT3 >
575 struct DMatTDMatAddExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
576 {
577  public:
578  //**********************************************************************************************
580  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
581  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
582  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
583  , typename DMatTSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
584  , INVALID_TYPE >::Type Type;
586  //**********************************************************************************************
587 };
589 //*************************************************************************************************
590 
591 
592 //*************************************************************************************************
594 template< typename MT1, typename MT2, typename MT3 >
595 struct DMatDMatSubExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
596 {
597  public:
598  //**********************************************************************************************
600  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
601  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
602  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
603  , typename TSMatDMatSubExprTrait< MT1, typename DMatDMatAddExprTrait<MT2,MT3>::Type >::Type
604  , INVALID_TYPE >::Type Type;
606  //**********************************************************************************************
607 };
609 //*************************************************************************************************
610 
611 
612 //*************************************************************************************************
614 template< typename MT1, typename MT2, typename MT3 >
615 struct DMatTDMatSubExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
616 {
617  public:
618  //**********************************************************************************************
620  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
621  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
622  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
623  , typename TSMatDMatSubExprTrait< MT1, typename DMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
624  , INVALID_TYPE >::Type Type;
626  //**********************************************************************************************
627 };
629 //*************************************************************************************************
630 
631 
632 //*************************************************************************************************
634 template< typename MT1, typename MT2 >
635 struct RowExprTrait< TSMatDMatSubExpr<MT1,MT2> >
636 {
637  public:
638  //**********************************************************************************************
639  typedef typename SubExprTrait< typename RowExprTrait<const MT1>::Type
640  , typename RowExprTrait<const MT2>::Type >::Type Type;
641  //**********************************************************************************************
642 };
644 //*************************************************************************************************
645 
646 
647 //*************************************************************************************************
649 template< typename MT1, typename MT2 >
650 struct ColumnExprTrait< TSMatDMatSubExpr<MT1,MT2> >
651 {
652  public:
653  //**********************************************************************************************
654  typedef typename SubExprTrait< typename ColumnExprTrait<const MT1>::Type
655  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
656  //**********************************************************************************************
657 };
659 //*************************************************************************************************
660 
661 } // namespace blaze
662 
663 #endif