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>
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 TDMATSMATSUBEXPR
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
64 template< typename MT1 // Type of the left-hand side dense matrix
65  , typename MT2 > // Type of the right-hand side sparse matrix
66 class TDMatSMatSubExpr : public DenseMatrix< TDMatSMatSubExpr<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<MT1>::value };
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 isAliased( const T* alias ) const {
197  return IsExpression<MT1>::value && lhs_.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 TDMatSMatSubExpr& 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  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
227  subAssign( ~lhs, rhs.rhs_ );
228  }
229  else {
230  assign ( ~lhs, rhs.lhs_ );
231  subAssign( ~lhs, rhs.rhs_ );
232  }
233  }
235  //**********************************************************************************************
236 
237  //**Assignment to sparse matrices***************************************************************
249  template< typename MT // Type of the target sparse matrix
250  , bool SO2 > // Storage order of the target sparse matrix
251  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const TDMatSMatSubExpr& rhs )
252  {
253  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
254 
260  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
261 
262  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
263  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
264 
265  const TmpType tmp( rhs );
266  assign( ~lhs, tmp );
267  }
269  //**********************************************************************************************
270 
271  //**Addition assignment to dense matrices*******************************************************
284  template< typename MT // Type of the target dense matrix
285  , bool SO2 > // Storage order of the target dense matrix
286  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const TDMatSMatSubExpr& rhs )
287  {
288  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
289  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
290 
291  addAssign( ~lhs, rhs.lhs_ );
292  subAssign( ~lhs, rhs.rhs_ );
293  }
295  //**********************************************************************************************
296 
297  //**Addition assignment to sparse matrices******************************************************
298  // No special implementation for the addition assignment to sparse matrices.
299  //**********************************************************************************************
300 
301  //**Subtraction assignment to dense matrices****************************************************
314  template< typename MT // Type of the target dense matrix
315  , bool SO2 > // Storage order of the target dense matrix
316  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const TDMatSMatSubExpr& rhs )
317  {
318  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
319  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
320 
321  subAssign( ~lhs, rhs.lhs_ );
322  addAssign( ~lhs, rhs.rhs_ );
323  }
325  //**********************************************************************************************
326 
327  //**Subtraction assignment to sparse matrices***************************************************
328  // No special implementation for the subtraction assignment to sparse matrices.
329  //**********************************************************************************************
330 
331  //**Multiplication assignment to dense matrices*************************************************
332  // No special implementation for the multiplication assignment to dense matrices.
333  //**********************************************************************************************
334 
335  //**Multiplication assignment to sparse matrices************************************************
336  // No special implementation for the multiplication assignment to sparse matrices.
337  //**********************************************************************************************
338 
339  //**Compile time checks*************************************************************************
346  //**********************************************************************************************
347 };
348 //*************************************************************************************************
349 
350 
351 
352 
353 //=================================================================================================
354 //
355 // GLOBAL BINARY ARITHMETIC OPERATORS
356 //
357 //=================================================================================================
358 
359 //*************************************************************************************************
390 template< typename T1 // Type of the left-hand side dense matrix
391  , typename T2 > // Type of the right-hand side sparse matrix
392 inline const TDMatSMatSubExpr<T1,T2>
394 {
395  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
396  throw std::invalid_argument( "Matrix sizes do not match" );
397 
398  return TDMatSMatSubExpr<T1,T2>( ~lhs, ~rhs );
399 }
400 //*************************************************************************************************
401 
402 
403 
404 
405 //=================================================================================================
406 //
407 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
408 //
409 //=================================================================================================
410 
411 //*************************************************************************************************
424 template< typename T1 // Type of the dense matrix of the left-hand side expression
425  , typename T2 // Type of the sparse matrix of the left-hand side expression
426  , typename T3 // Type of the right-hand side dense matrix
427  , bool SO > // Storage order of the right-hand side dense matrix
428 inline const typename AddExprTrait< TDMatSMatSubExpr<T1,T2>, T3 >::Type
429  operator+( const TDMatSMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
430 {
431  return ( lhs.leftOperand() + (~rhs) ) - lhs.rightOperand();
432 }
434 //*************************************************************************************************
435 
436 
437 //*************************************************************************************************
450 template< typename T1 // Type of the dense matrix of the left-hand side expression
451  , typename T2 // Type of the sparse matrix of the left-hand side expression
452  , typename T3 // Type of the right-hand side dense matrix
453  , bool SO > // Storage order of the right-hand side dense matrix
454 inline const typename SubExprTrait< TDMatSMatSubExpr<T1,T2>, T3 >::Type
455  operator-( const TDMatSMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
456 {
457  return ( lhs.leftOperand() - (~rhs) ) - lhs.rightOperand();
458 }
460 //*************************************************************************************************
461 
462 
463 
464 
465 //=================================================================================================
466 //
467 // EXPRESSION TRAIT SPECIALIZATIONS
468 //
469 //=================================================================================================
470 
471 //*************************************************************************************************
473 template< typename MT1, typename MT2, typename MT3 >
474 struct DMatDMatAddExprTrait< TDMatSMatSubExpr<MT1,MT2>, MT3 >
475 {
476  public:
477  //**********************************************************************************************
479  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
480  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
481  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
482  , typename DMatSMatSubExprTrait< typename TDMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
483  , INVALID_TYPE >::Type Type;
485  //**********************************************************************************************
486 };
488 //*************************************************************************************************
489 
490 
491 //*************************************************************************************************
493 template< typename MT1, typename MT2, typename MT3 >
494 struct DMatTDMatAddExprTrait< TDMatSMatSubExpr<MT1,MT2>, MT3 >
495 {
496  public:
497  //**********************************************************************************************
499  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
500  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
501  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
502  , typename TDMatSMatSubExprTrait< typename TDMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
503  , INVALID_TYPE >::Type Type;
505  //**********************************************************************************************
506 };
508 //*************************************************************************************************
509 
510 
511 //*************************************************************************************************
513 template< typename MT1, typename MT2, typename MT3 >
514 struct DMatDMatSubExprTrait< TDMatSMatSubExpr<MT1,MT2>, MT3 >
515 {
516  public:
517  //**********************************************************************************************
519  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
520  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
521  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
522  , typename DMatSMatSubExprTrait< typename TDMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
523  , INVALID_TYPE >::Type Type;
525  //**********************************************************************************************
526 };
528 //*************************************************************************************************
529 
530 
531 //*************************************************************************************************
533 template< typename MT1, typename MT2, typename MT3 >
534 struct DMatTDMatSubExprTrait< TDMatSMatSubExpr<MT1,MT2>, MT3 >
535 {
536  public:
537  //**********************************************************************************************
539  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
540  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
541  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
542  , typename TDMatSMatSubExprTrait< typename TDMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
543  , INVALID_TYPE >::Type Type;
545  //**********************************************************************************************
546 };
548 //*************************************************************************************************
549 
550 } // namespace blaze
551 
552 #endif