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>
49 #include <blaze/util/Assert.h>
52 #include <blaze/util/SelectType.h>
53 #include <blaze/util/Types.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // CLASS DMATTSMATSUBEXPR
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
71 template< typename MT1 // Type of the left-hand side dense matrix
72  , typename MT2 > // Type of the right-hand side sparse matrix
73 class DMatTSMatSubExpr : public DenseMatrix< DMatTSMatSubExpr<MT1,MT2>, false >
74  , private MatMatSubExpr
75  , private Computation
76 {
77  private:
78  //**Type definitions****************************************************************************
79  typedef typename MT1::ResultType RT1;
80  typedef typename MT2::ResultType RT2;
81  typedef typename MT1::ReturnType RN1;
82  typedef typename MT2::ReturnType RN2;
83  //**********************************************************************************************
84 
85  //**Return type evaluation**********************************************************************
87 
92  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
93 
96  //**********************************************************************************************
97 
98  public:
99  //**Type definitions****************************************************************************
102  typedef typename ResultType::OppositeType OppositeType;
103  typedef typename ResultType::TransposeType TransposeType;
104  typedef typename ResultType::ElementType ElementType;
105 
108 
110  typedef const ResultType CompositeType;
111 
113  typedef typename SelectType< IsExpression<MT1>::value, const MT1, const MT1& >::Type LeftOperand;
114 
116  typedef typename SelectType< IsExpression<MT2>::value, const MT2, const MT2& >::Type RightOperand;
117  //**********************************************************************************************
118 
119  //**Compilation flags***************************************************************************
121  enum { vectorizable = 0 };
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 canAlias( const T* alias ) const {
201  return ( IsExpression<MT1>::value && lhs_.canAlias( alias ) ) ||
202  ( rhs_.canAlias( alias ) );
203  }
204  //**********************************************************************************************
205 
206  //**********************************************************************************************
212  template< typename T >
213  inline bool isAliased( const T* alias ) const {
214  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
215  }
216  //**********************************************************************************************
217 
218  private:
219  //**Member variables****************************************************************************
222  //**********************************************************************************************
223 
224  //**Assignment to dense matrices****************************************************************
236  template< typename MT // Type of the target dense matrix
237  , bool SO2 > // Storage order of the target dense matrix
238  friend inline void assign( DenseMatrix<MT,SO2>& lhs, const DMatTSMatSubExpr& rhs )
239  {
241 
242  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
243  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
244 
245  if( !IsExpression<MT1>::value && (~lhs).isAliased( &rhs.lhs_ ) ) {
246  subAssign( ~lhs, rhs.rhs_ );
247  }
248  else {
249  assign ( ~lhs, rhs.lhs_ );
250  subAssign( ~lhs, rhs.rhs_ );
251  }
252  }
254  //**********************************************************************************************
255 
256  //**Assignment to sparse matrices***************************************************************
268  template< typename MT // Type of the target sparse matrix
269  , bool SO2 > // Storage order of the target sparse matrix
270  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const DMatTSMatSubExpr& rhs )
271  {
273 
274  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
275 
281  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
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  const TmpType tmp( rhs );
287  assign( ~lhs, tmp );
288  }
290  //**********************************************************************************************
291 
292  //**Addition assignment to dense matrices*******************************************************
305  template< typename MT // Type of the target dense matrix
306  , bool SO2 > // Storage order of the target dense matrix
307  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const DMatTSMatSubExpr& rhs )
308  {
310 
311  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
312  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
313 
314  addAssign( ~lhs, rhs.lhs_ );
315  subAssign( ~lhs, rhs.rhs_ );
316  }
318  //**********************************************************************************************
319 
320  //**Addition assignment to sparse matrices******************************************************
321  // No special implementation for the addition assignment to sparse matrices.
322  //**********************************************************************************************
323 
324  //**Subtraction assignment to dense matrices****************************************************
337  template< typename MT // Type of the target dense matrix
338  , bool SO2 > // Storage order of the target dense matrix
339  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const DMatTSMatSubExpr& rhs )
340  {
342 
343  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
344  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
345 
346  subAssign( ~lhs, rhs.lhs_ );
347  addAssign( ~lhs, rhs.rhs_ );
348  }
350  //**********************************************************************************************
351 
352  //**Subtraction assignment to sparse matrices***************************************************
353  // No special implementation for the subtraction assignment to sparse matrices.
354  //**********************************************************************************************
355 
356  //**Multiplication assignment to dense matrices*************************************************
357  // No special implementation for the multiplication assignment to dense matrices.
358  //**********************************************************************************************
359 
360  //**Multiplication assignment to sparse matrices************************************************
361  // No special implementation for the multiplication assignment to sparse matrices.
362  //**********************************************************************************************
363 
364  //**Compile time checks*************************************************************************
371  //**********************************************************************************************
372 };
373 //*************************************************************************************************
374 
375 
376 
377 
378 //=================================================================================================
379 //
380 // GLOBAL BINARY ARITHMETIC OPERATORS
381 //
382 //=================================================================================================
383 
384 //*************************************************************************************************
414 template< typename T1 // Type of the left-hand side dense matrix
415  , typename T2 > // Type of the right-hand side sparse matrix
416 inline const DMatTSMatSubExpr<T1,T2>
418 {
420 
421  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
422  throw std::invalid_argument( "Matrix sizes do not match" );
423 
424  return DMatTSMatSubExpr<T1,T2>( ~lhs, ~rhs );
425 }
426 //*************************************************************************************************
427 
428 
429 
430 
431 //=================================================================================================
432 //
433 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
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 AddExprTrait< DMatTSMatSubExpr<T1,T2>, T3 >::Type
455  operator+( const DMatTSMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
456 {
458 
459  return ( lhs.leftOperand() + (~rhs) ) - lhs.rightOperand();
460 }
462 //*************************************************************************************************
463 
464 
465 //*************************************************************************************************
478 template< typename T1 // Type of the dense matrix of the left-hand side expression
479  , typename T2 // Type of the sparse matrix of the left-hand side expression
480  , typename T3 // Type of the right-hand side dense matrix
481  , bool SO > // Storage order of the right-hand side dense matrix
482 inline const typename SubExprTrait< DMatTSMatSubExpr<T1,T2>, T3 >::Type
483  operator-( const DMatTSMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
484 {
486 
487  return ( lhs.leftOperand() - (~rhs) ) - lhs.rightOperand();
488 }
490 //*************************************************************************************************
491 
492 
493 
494 
495 //=================================================================================================
496 //
497 // EXPRESSION TRAIT SPECIALIZATIONS
498 //
499 //=================================================================================================
500 
501 //*************************************************************************************************
503 template< typename MT1, typename MT2, typename MT3 >
504 struct DMatDMatAddExprTrait< DMatTSMatSubExpr<MT1,MT2>, MT3 >
505 {
506  public:
507  //**********************************************************************************************
509  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
510  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
511  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
512  , typename DMatTSMatSubExprTrait< typename DMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
513  , INVALID_TYPE >::Type Type;
515  //**********************************************************************************************
516 };
518 //*************************************************************************************************
519 
520 
521 //*************************************************************************************************
523 template< typename MT1, typename MT2, typename MT3 >
524 struct DMatTDMatAddExprTrait< DMatTSMatSubExpr<MT1,MT2>, MT3 >
525 {
526  public:
527  //**********************************************************************************************
529  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
530  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
531  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
532  , typename DMatTSMatSubExprTrait< typename DMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
533  , INVALID_TYPE >::Type Type;
535  //**********************************************************************************************
536 };
538 //*************************************************************************************************
539 
540 
541 //*************************************************************************************************
543 template< typename MT1, typename MT2, typename MT3 >
544 struct DMatDMatSubExprTrait< DMatTSMatSubExpr<MT1,MT2>, MT3 >
545 {
546  public:
547  //**********************************************************************************************
549  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
550  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
551  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
552  , typename DMatTSMatSubExprTrait< typename DMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
553  , INVALID_TYPE >::Type Type;
555  //**********************************************************************************************
556 };
558 //*************************************************************************************************
559 
560 
561 //*************************************************************************************************
563 template< typename MT1, typename MT2, typename MT3 >
564 struct DMatTDMatSubExprTrait< DMatTSMatSubExpr<MT1,MT2>, MT3 >
565 {
566  public:
567  //**********************************************************************************************
569  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
570  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
571  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
572  , typename DMatTSMatSubExprTrait< typename DMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
573  , INVALID_TYPE >::Type Type;
575  //**********************************************************************************************
576 };
578 //*************************************************************************************************
579 
580 
581 //*************************************************************************************************
583 template< typename MT1, typename MT2 >
584 struct RowExprTrait< DMatTSMatSubExpr<MT1,MT2> >
585 {
586  public:
587  //**********************************************************************************************
588  typedef typename SubExprTrait< typename RowExprTrait<const MT1>::Type
589  , typename RowExprTrait<const MT2>::Type >::Type Type;
590  //**********************************************************************************************
591 };
593 //*************************************************************************************************
594 
595 
596 //*************************************************************************************************
598 template< typename MT1, typename MT2 >
599 struct ColumnExprTrait< DMatTSMatSubExpr<MT1,MT2> >
600 {
601  public:
602  //**********************************************************************************************
603  typedef typename SubExprTrait< typename ColumnExprTrait<const MT1>::Type
604  , typename ColumnExprTrait<const MT2>::Type >::Type Type;
605  //**********************************************************************************************
606 };
608 //*************************************************************************************************
609 
610 } // namespace blaze
611 
612 #endif