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>
43 #include <blaze/util/Assert.h>
45 #include <blaze/util/SelectType.h>
46 #include <blaze/util/Types.h>
47 
48 
49 namespace blaze {
50 
51 //=================================================================================================
52 //
53 // CLASS SMATTDMATSUBEXPR
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
64 template< typename MT1 // Type of the left-hand side sparse matrix
65  , typename MT2 > // Type of the right-hand side dense matrix
66 class TSMatDMatSubExpr : public DenseMatrix< TSMatDMatSubExpr<MT1,MT2>, false >
67  , private Expression
68  , private Computation
69 {
70  private:
71  //**Type definitions****************************************************************************
72  typedef typename MT1::ResultType RT1;
73  typedef typename MT2::ResultType RT2;
74  typedef typename MT1::ReturnType RN1;
75  typedef typename MT2::ReturnType RN2;
76  //**********************************************************************************************
77 
78  //**Return type evaluation**********************************************************************
80 
85  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
86 
89  //**********************************************************************************************
90 
91  public:
92  //**Type definitions****************************************************************************
95  typedef typename ResultType::OppositeType OppositeType;
96  typedef typename ResultType::TransposeType TransposeType;
97  typedef typename ResultType::ElementType ElementType;
98 
101 
103  typedef const ResultType CompositeType;
104 
106  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
107 
109  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
110  //**********************************************************************************************
111 
112  //**Compilation flags***************************************************************************
114  enum { vectorizable = 0 };
115 
117  enum { canAlias = IsExpression<MT2>::value };
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 isAliased( const T* alias ) const {
197  return IsExpression<MT2>::value && rhs_.isAliased( alias );
198  }
199  //**********************************************************************************************
200 
201  private:
202  //**Member variables****************************************************************************
205  //**********************************************************************************************
206 
207  //**Assignment to dense matrices****************************************************************
219  template< typename MT // Type of the target dense matrix
220  , bool SO2 > // Storage order of the target dense matrix
221  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const TSMatDMatSubExpr& rhs )
222  {
223  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
224  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
225 
226  assign ( ~lhs, -rhs.rhs_ );
227  addAssign( ~lhs, rhs.lhs_ );
228  }
230  //**********************************************************************************************
231 
232  //**Assignment to sparse matrices***************************************************************
244  template< typename MT // Type of the target sparse matrix
245  , bool SO2 > // Storage order of the target sparse matrix
246  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const TSMatDMatSubExpr& rhs )
247  {
248  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
249 
255  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
256 
257  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
258  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
259 
260  const TmpType tmp( rhs );
261  assign( ~lhs, tmp );
262  }
264  //**********************************************************************************************
265 
266  //**Addition assignment to dense matrices*******************************************************
279  template< typename MT // Type of the target dense matrix
280  , bool SO2 > // Storage order of the target dense matrix
281  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const TSMatDMatSubExpr& rhs )
282  {
283  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
284  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
285 
286  addAssign( ~lhs, rhs.lhs_ );
287  subAssign( ~lhs, rhs.rhs_ );
288  }
290  //**********************************************************************************************
291 
292  //**Addition assignment to sparse matrices******************************************************
293  // No special implementation for the addition assignment to sparse matrices.
294  //**********************************************************************************************
295 
296  //**Subtraction assignment to dense matrices****************************************************
309  template< typename MT // Type of the target dense matrix
310  , bool SO2 > // Storage order of the target dense matrix
311  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const TSMatDMatSubExpr& rhs )
312  {
313  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
314  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
315 
316  subAssign( ~lhs, rhs.lhs_ );
317  addAssign( ~lhs, rhs.rhs_ );
318  }
320  //**********************************************************************************************
321 
322  //**Subtraction assignment to sparse matrices***************************************************
323  // No special implementation for the subtraction assignment to sparse matrices.
324  //**********************************************************************************************
325 
326  //**Multiplication assignment to dense matrices*************************************************
327  // No special implementation for the multiplication assignment to dense matrices.
328  //**********************************************************************************************
329 
330  //**Multiplication assignment to sparse matrices************************************************
331  // No special implementation for the multiplication assignment to sparse matrices.
332  //**********************************************************************************************
333 
334  //**Compile time checks*************************************************************************
341  //**********************************************************************************************
342 };
343 //*************************************************************************************************
344 
345 
346 
347 
348 //=================================================================================================
349 //
350 // GLOBAL BINARY ARITHMETIC OPERATORS
351 //
352 //=================================================================================================
353 
354 //*************************************************************************************************
384 template< typename T1 // Type of the left-hand side sparse matrix
385  , typename T2 > // Type of the right-hand side dense matrix
386 inline const TSMatDMatSubExpr<T1,T2>
388 {
389  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
390  throw std::invalid_argument( "Matrix sizes do not match" );
391 
392  return TSMatDMatSubExpr<T1,T2>( ~lhs, ~rhs );
393 }
394 //*************************************************************************************************
395 
396 
397 
398 
399 //=================================================================================================
400 //
401 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
402 //
403 //=================================================================================================
404 
405 //*************************************************************************************************
418 template< typename T1 // Type of the sparse matrix of the left-hand side expression
419  , typename T2 // Type of the dense matrix of the left-hand side expression
420  , typename T3 // Type of the right-hand side dense matrix
421  , bool SO > // Storage order of the right-hand side dense matrix
422 inline const typename AddExprTrait< TSMatDMatSubExpr<T1,T2>, T3 >::Type
423  operator+( const TSMatDMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
424 {
425  return ( (~rhs) - lhs.rightOperand() ) + lhs.leftOperand();
426 }
428 //*************************************************************************************************
429 
430 
431 //*************************************************************************************************
444 template< typename T1 // Type of the sparse matrix of the left-hand side expression
445  , typename T2 // Type of the dense matrix of the left-hand side expression
446  , typename T3 // Type of the right-hand side dense matrix
447  , bool SO > // Storage order of the right-hand side dense matrix
448 inline const typename SubExprTrait< TSMatDMatSubExpr<T1,T2>, T3 >::Type
449  operator-( const TSMatDMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
450 {
451  return lhs.leftOperand() - ( lhs.rightOperand() + (~rhs) );
452 }
454 //*************************************************************************************************
455 
456 
457 
458 
459 //=================================================================================================
460 //
461 // EXPRESSION TRAIT SPECIALIZATIONS
462 //
463 //=================================================================================================
464 
465 //*************************************************************************************************
467 template< typename MT1, typename MT2, typename MT3 >
468 struct DMatDMatAddExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
469 {
470  public:
471  //**********************************************************************************************
473  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
474  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
475  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
476  , typename DMatTSMatAddExprTrait< typename DMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
477  , INVALID_TYPE >::Type Type;
479  //**********************************************************************************************
480 };
482 //*************************************************************************************************
483 
484 
485 //*************************************************************************************************
487 template< typename MT1, typename MT2, typename MT3 >
488 struct DMatTDMatAddExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
489 {
490  public:
491  //**********************************************************************************************
493  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
494  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
495  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
496  , typename DMatTSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
497  , INVALID_TYPE >::Type Type;
499  //**********************************************************************************************
500 };
502 //*************************************************************************************************
503 
504 
505 //*************************************************************************************************
507 template< typename MT1, typename MT2, typename MT3 >
508 struct DMatDMatSubExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
509 {
510  public:
511  //**********************************************************************************************
513  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
514  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
515  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
516  , typename TSMatDMatSubExprTrait< MT1, typename DMatDMatAddExprTrait<MT2,MT3>::Type >::Type
517  , INVALID_TYPE >::Type Type;
519  //**********************************************************************************************
520 };
522 //*************************************************************************************************
523 
524 
525 //*************************************************************************************************
527 template< typename MT1, typename MT2, typename MT3 >
528 struct DMatTDMatSubExprTrait< TSMatDMatSubExpr<MT1,MT2>, MT3 >
529 {
530  public:
531  //**********************************************************************************************
533  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
534  IsDenseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
535  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
536  , typename TSMatDMatSubExprTrait< MT1, typename DMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
537  , INVALID_TYPE >::Type Type;
539  //**********************************************************************************************
540 };
542 //*************************************************************************************************
543 
544 } // namespace blaze
545 
546 #endif