All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatTSMatSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATTSMATSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DMATTSMATSUBEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
47 #include <blaze/util/Assert.h>
49 #include <blaze/util/SelectType.h>
50 #include <blaze/util/Types.h>
51 
52 
53 namespace blaze {
54 
55 //=================================================================================================
56 //
57 // CLASS DMATTSMATSUBEXPR
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
68 template< typename MT1 // Type of the left-hand side dense matrix
69  , typename MT2 > // Type of the right-hand side sparse matrix
70 class DMatTSMatSubExpr : public DenseMatrix< DMatTSMatSubExpr<MT1,MT2>, false >
71  , private Expression
72  , private Computation
73 {
74  private:
75  //**Type definitions****************************************************************************
76  typedef typename MT1::ResultType RT1;
77  typedef typename MT2::ResultType RT2;
78  typedef typename MT1::ReturnType RN1;
79  typedef typename MT2::ReturnType RN2;
80  //**********************************************************************************************
81 
82  //**Return type evaluation**********************************************************************
84 
89  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
90 
93  //**********************************************************************************************
94 
95  public:
96  //**Type definitions****************************************************************************
99  typedef typename ResultType::OppositeType OppositeType;
100  typedef typename ResultType::TransposeType TransposeType;
101  typedef typename ResultType::ElementType ElementType;
102 
105 
107  typedef const ResultType CompositeType;
108 
110  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
111 
113  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
114  //**********************************************************************************************
115 
116  //**Compilation flags***************************************************************************
118  enum { vectorizable = 0 };
119 
121  enum { canAlias = IsExpression<MT1>::value };
122  //**********************************************************************************************
123 
124  //**Constructor*********************************************************************************
130  explicit inline DMatTSMatSubExpr( const MT1& lhs, const MT2& rhs )
131  : lhs_( lhs ) // Left-hand side dense matrix of the subtraction expression
132  , rhs_( rhs ) // Right-hand side sparse matrix of the subtraction expression
133  {
134  BLAZE_INTERNAL_ASSERT( lhs.rows() == rhs.rows() , "Invalid number of rows" );
135  BLAZE_INTERNAL_ASSERT( lhs.columns() == rhs.columns(), "Invalid number of columns" );
136  }
137  //**********************************************************************************************
138 
139  //**Access operator*****************************************************************************
146  inline ReturnType operator()( size_t i, size_t j ) const {
147  BLAZE_INTERNAL_ASSERT( i < lhs_.rows() , "Invalid row access index" );
148  BLAZE_INTERNAL_ASSERT( j < lhs_.columns(), "Invalid column access index" );
149  return lhs_(i,j) - rhs_(i,j);
150  }
151  //**********************************************************************************************
152 
153  //**Rows function*******************************************************************************
158  inline size_t rows() const {
159  return lhs_.rows();
160  }
161  //**********************************************************************************************
162 
163  //**Columns function****************************************************************************
168  inline size_t columns() const {
169  return lhs_.columns();
170  }
171  //**********************************************************************************************
172 
173  //**Left operand access*************************************************************************
178  inline LeftOperand leftOperand() const {
179  return lhs_;
180  }
181  //**********************************************************************************************
182 
183  //**Right operand access************************************************************************
188  inline RightOperand rightOperand() const {
189  return rhs_;
190  }
191  //**********************************************************************************************
192 
193  //**********************************************************************************************
199  template< typename T >
200  inline bool isAliased( const T* alias ) const {
201  return IsExpression<MT1>::value && lhs_.isAliased( alias );
202  }
203  //**********************************************************************************************
204 
205  private:
206  //**Member variables****************************************************************************
209  //**********************************************************************************************
210 
211  //**Assignment to dense matrices****************************************************************
223  template< typename MT // Type of the target dense matrix
224  , bool SO2 > // Storage order of the target dense matrix
225  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const DMatTSMatSubExpr& rhs )
226  {
227  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
228  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
229 
230  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
231  subAssign( ~lhs, rhs.rhs_ );
232  }
233  else {
234  assign ( ~lhs, rhs.lhs_ );
235  subAssign( ~lhs, rhs.rhs_ );
236  }
237  }
239  //**********************************************************************************************
240 
241  //**Assignment to sparse matrices***************************************************************
253  template< typename MT // Type of the target sparse matrix
254  , bool SO2 > // Storage order of the target sparse matrix
255  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const DMatTSMatSubExpr& rhs )
256  {
257  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
258 
264  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
265 
266  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
267  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
268 
269  const TmpType tmp( rhs );
270  assign( ~lhs, tmp );
271  }
273  //**********************************************************************************************
274 
275  //**Addition assignment to dense matrices*******************************************************
288  template< typename MT // Type of the target dense matrix
289  , bool SO2 > // Storage order of the target dense matrix
290  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const DMatTSMatSubExpr& rhs )
291  {
292  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
293  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
294 
295  addAssign( ~lhs, rhs.lhs_ );
296  subAssign( ~lhs, rhs.rhs_ );
297  }
299  //**********************************************************************************************
300 
301  //**Addition assignment to sparse matrices******************************************************
302  // No special implementation for the addition assignment to sparse matrices.
303  //**********************************************************************************************
304 
305  //**Subtraction assignment to dense matrices****************************************************
318  template< typename MT // Type of the target dense matrix
319  , bool SO2 > // Storage order of the target dense matrix
320  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const DMatTSMatSubExpr& rhs )
321  {
322  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
323  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
324 
325  subAssign( ~lhs, rhs.lhs_ );
326  addAssign( ~lhs, rhs.rhs_ );
327  }
329  //**********************************************************************************************
330 
331  //**Subtraction assignment to sparse matrices***************************************************
332  // No special implementation for the subtraction assignment to sparse matrices.
333  //**********************************************************************************************
334 
335  //**Multiplication assignment to dense matrices*************************************************
336  // No special implementation for the multiplication assignment to dense matrices.
337  //**********************************************************************************************
338 
339  //**Multiplication assignment to sparse matrices************************************************
340  // No special implementation for the multiplication assignment to sparse matrices.
341  //**********************************************************************************************
342 
343  //**Compile time checks*************************************************************************
350  //**********************************************************************************************
351 };
352 //*************************************************************************************************
353 
354 
355 
356 
357 //=================================================================================================
358 //
359 // GLOBAL BINARY ARITHMETIC OPERATORS
360 //
361 //=================================================================================================
362 
363 //*************************************************************************************************
393 template< typename T1 // Type of the left-hand side dense matrix
394  , typename T2 > // Type of the right-hand side sparse matrix
395 inline const DMatTSMatSubExpr<T1,T2>
397 {
398  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
399  throw std::invalid_argument( "Matrix sizes do not match" );
400 
401  return DMatTSMatSubExpr<T1,T2>( ~lhs, ~rhs );
402 }
403 //*************************************************************************************************
404 
405 
406 
407 
408 //=================================================================================================
409 //
410 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
411 //
412 //=================================================================================================
413 
414 //*************************************************************************************************
427 template< typename T1 // Type of the dense matrix of the left-hand side expression
428  , typename T2 // Type of the sparse matrix of the left-hand side expression
429  , typename T3 // Type of the right-hand side dense matrix
430  , bool SO > // Storage order of the right-hand side dense matrix
431 inline const typename AddExprTrait< DMatTSMatSubExpr<T1,T2>, T3 >::Type
432  operator+( const DMatTSMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
433 {
434  return ( lhs.leftOperand() + (~rhs) ) - lhs.rightOperand();
435 }
437 //*************************************************************************************************
438 
439 
440 //*************************************************************************************************
453 template< typename T1 // Type of the dense matrix of the left-hand side expression
454  , typename T2 // Type of the sparse matrix of the left-hand side expression
455  , typename T3 // Type of the right-hand side dense matrix
456  , bool SO > // Storage order of the right-hand side dense matrix
457 inline const typename SubExprTrait< DMatTSMatSubExpr<T1,T2>, T3 >::Type
458  operator-( const DMatTSMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
459 {
460  return ( lhs.leftOperand() - (~rhs) ) - lhs.rightOperand();
461 }
463 //*************************************************************************************************
464 
465 
466 
467 
468 //=================================================================================================
469 //
470 // EXPRESSION TRAIT SPECIALIZATIONS
471 //
472 //=================================================================================================
473 
474 //*************************************************************************************************
476 template< typename MT1, typename MT2, typename MT3 >
477 struct DMatDMatAddExprTrait< DMatTSMatSubExpr<MT1,MT2>, MT3 >
478 {
479  public:
480  //**********************************************************************************************
482  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
483  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
484  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
485  , typename DMatTSMatSubExprTrait< typename DMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
486  , INVALID_TYPE >::Type Type;
488  //**********************************************************************************************
489 };
491 //*************************************************************************************************
492 
493 
494 //*************************************************************************************************
496 template< typename MT1, typename MT2, typename MT3 >
497 struct DMatTDMatAddExprTrait< DMatTSMatSubExpr<MT1,MT2>, MT3 >
498 {
499  public:
500  //**********************************************************************************************
502  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
503  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
504  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
505  , typename DMatTSMatSubExprTrait< typename DMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
506  , INVALID_TYPE >::Type Type;
508  //**********************************************************************************************
509 };
511 //*************************************************************************************************
512 
513 
514 //*************************************************************************************************
516 template< typename MT1, typename MT2, typename MT3 >
517 struct DMatDMatSubExprTrait< DMatTSMatSubExpr<MT1,MT2>, MT3 >
518 {
519  public:
520  //**********************************************************************************************
522  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
523  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
524  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
525  , typename DMatTSMatSubExprTrait< typename DMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
526  , INVALID_TYPE >::Type Type;
528  //**********************************************************************************************
529 };
531 //*************************************************************************************************
532 
533 
534 //*************************************************************************************************
536 template< typename MT1, typename MT2, typename MT3 >
537 struct DMatTDMatSubExprTrait< DMatTSMatSubExpr<MT1,MT2>, MT3 >
538 {
539  public:
540  //**********************************************************************************************
542  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
543  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
544  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
545  , typename DMatTSMatSubExprTrait< typename DMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
546  , INVALID_TYPE >::Type Type;
548  //**********************************************************************************************
549 };
551 //*************************************************************************************************
552 
553 } // namespace blaze
554 
555 #endif