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 MatMatSubExpr
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 // EXPRESSION TRAIT SPECIALIZATIONS
489 //
490 //=================================================================================================
491 
492 //*************************************************************************************************
494 template< typename MT1, typename MT2, typename MT3 >
495 struct DMatDMatAddExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
496 {
497  public:
498  //**********************************************************************************************
500  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
501  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
502  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
503  , typename DMatTSMatAddExprTrait< typename DMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
504  , INVALID_TYPE >::Type Type;
506  //**********************************************************************************************
507 };
509 //*************************************************************************************************
510 
511 
512 //*************************************************************************************************
514 template< typename MT1, typename MT2, typename MT3 >
515 struct DMatTDMatAddExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
516 {
517  public:
518  //**********************************************************************************************
520  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
521  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
522  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
523  , typename DMatTSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
524  , INVALID_TYPE >::Type Type;
526  //**********************************************************************************************
527 };
529 //*************************************************************************************************
530 
531 
532 //*************************************************************************************************
534 template< typename MT1, typename MT2, typename MT3 >
535 struct DMatDMatSubExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
536 {
537  public:
538  //**********************************************************************************************
540  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
541  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
542  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
543  , typename TSMatDMatSubExprTrait< MT1, typename DMatDMatAddExprTrait<MT2,MT3>::Type >::Type
544  , INVALID_TYPE >::Type Type;
546  //**********************************************************************************************
547 };
549 //*************************************************************************************************
550 
551 
552 //*************************************************************************************************
554 template< typename MT1, typename MT2, typename MT3 >
555 struct DMatTDMatSubExprTrait< 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 && IsColumnMajorMatrix<MT3>::value
563  , typename TSMatDMatSubExprTrait< MT1, typename DMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
564  , INVALID_TYPE >::Type Type;
566  //**********************************************************************************************
567 };
569 //*************************************************************************************************
570 
571 
572 //*************************************************************************************************
574 template< typename MT1, typename MT2 >
575 struct RowExprTrait< TSMatDMatSubExpr<MT1,MT2> >
576 {
577  public:
578  //**********************************************************************************************
579  typedef typename SubExprTrait< typename RowExprTrait<const MT1>::Type
580  , typename RowExprTrait<const MT2>::Type >::Type Type;
581  //**********************************************************************************************
582 };
584 //*************************************************************************************************
585 
586 
587 //*************************************************************************************************
589 template< typename MT1, typename MT2 >
590 struct ColumnExprTrait< TSMatDMatSubExpr<MT1,MT2> >
591 {
592  public:
593  //**********************************************************************************************
594  typedef typename SubExprTrait< typename ColumnExprTrait<const MT1>::Type
595  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
596  //**********************************************************************************************
597 };
599 //*************************************************************************************************
600 
601 } // namespace blaze
602 
603 #endif