All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TDMatSMatAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_TDMATSMATADDEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_TDMATSMATADDEXPR_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 TDMATSMATADDEXPR
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 TDMatSMatAddExpr : public DenseMatrix< TDMatSMatAddExpr<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 TDMatSMatAddExpr( const MT1& lhs, const MT2& rhs )
127  : lhs_( lhs ) // Left-hand side dense matrix of the addition expression
128  , rhs_( rhs ) // Right-hand side sparse matrix of the addition 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 TDMatSMatAddExpr& 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  addAssign( ~lhs, rhs.rhs_ );
228  }
229  else {
230  assign ( ~lhs, rhs.lhs_ );
231  addAssign( ~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 TDMatSMatAddExpr& 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 TDMatSMatAddExpr& 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  addAssign( ~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 TDMatSMatAddExpr& 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  subAssign( ~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
394 {
395  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
396  throw std::invalid_argument( "Matrix sizes do not match" );
397 
398  return TDMatSMatAddExpr<T1,T2>( ~lhs, ~rhs );
399 }
400 //*************************************************************************************************
401 
402 
403 //*************************************************************************************************
434 template< typename T1 // Type of the left-hand side dense matrix
435  , typename T2 > // Type of the right-hand side sparse matrix
438 {
439  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
440  throw std::invalid_argument( "Matrix sizes do not match" );
441 
442  return TDMatSMatAddExpr<T1,T2>( ~rhs, ~lhs );
443 }
444 //*************************************************************************************************
445 
446 
447 
448 
449 //=================================================================================================
450 //
451 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
452 //
453 //=================================================================================================
454 
455 //*************************************************************************************************
468 template< typename T1 // Type of the dense matrix of the left-hand side expression
469  , typename T2 // Type of the sparse matrix of the left-hand side expression
470  , typename T3 // Type of the right-hand side dense matrix
471  , bool SO > // Storage order of the right-hand side dense matrix
472 inline const typename AddExprTrait< TDMatSMatAddExpr<T1,T2>, T3 >::Type
473  operator+( const TDMatSMatAddExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
474 {
475  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
476 }
478 //*************************************************************************************************
479 
480 
481 //*************************************************************************************************
494 template< typename T1 // Type of the dense matrix of the left-hand side expression
495  , typename T2 // Type of the sparse matrix of the left-hand side expression
496  , typename T3 // Type of the right-hand side dense matrix
497  , bool SO > // Storage order of the right-hand side dense matrix
498 inline const typename SubExprTrait< TDMatSMatAddExpr<T1,T2>, T3 >::Type
499  operator-( const TDMatSMatAddExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
500 {
501  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
502 }
504 //*************************************************************************************************
505 
506 
507 
508 
509 //=================================================================================================
510 //
511 // EXPRESSION TRAIT SPECIALIZATIONS
512 //
513 //=================================================================================================
514 
515 //*************************************************************************************************
517 template< typename MT1, typename MT2, typename MT3 >
518 struct DMatDMatAddExprTrait< TDMatSMatAddExpr<MT1,MT2>, MT3 >
519 {
520  public:
521  //**********************************************************************************************
523  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
524  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
525  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
526  , typename DMatSMatAddExprTrait< typename TDMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
527  , INVALID_TYPE >::Type Type;
529  //**********************************************************************************************
530 };
532 //*************************************************************************************************
533 
534 
535 //*************************************************************************************************
537 template< typename MT1, typename MT2, typename MT3 >
538 struct DMatTDMatAddExprTrait< TDMatSMatAddExpr<MT1,MT2>, MT3 >
539 {
540  public:
541  //**********************************************************************************************
543  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
544  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
545  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
546  , typename TDMatSMatAddExprTrait< typename TDMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
547  , INVALID_TYPE >::Type Type;
549  //**********************************************************************************************
550 };
552 //*************************************************************************************************
553 
554 
555 //*************************************************************************************************
557 template< typename MT1, typename MT2, typename MT3 >
558 struct DMatDMatSubExprTrait< TDMatSMatAddExpr<MT1,MT2>, MT3 >
559 {
560  public:
561  //**********************************************************************************************
563  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
564  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
565  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
566  , typename DMatSMatAddExprTrait< typename TDMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
567  , INVALID_TYPE >::Type Type;
569  //**********************************************************************************************
570 };
572 //*************************************************************************************************
573 
574 
575 //*************************************************************************************************
577 template< typename MT1, typename MT2, typename MT3 >
578 struct DMatTDMatSubExprTrait< TDMatSMatAddExpr<MT1,MT2>, MT3 >
579 {
580  public:
581  //**********************************************************************************************
583  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsColumnMajorMatrix<MT1>::value &&
584  IsSparseMatrix<MT2>::value && IsRowMajorMatrix<MT2>::value &&
585  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
586  , typename TDMatSMatAddExprTrait< typename TDMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
587  , INVALID_TYPE >::Type Type;
589  //**********************************************************************************************
590 };
592 //*************************************************************************************************
593 
594 } // namespace blaze
595 
596 #endif