All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatTSMatAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATTSMATADDEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DMATTSMATADDEXPR_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 DMATTSMATADDEXPR
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 DMatTSMatAddExpr : public DenseMatrix< DMatTSMatAddExpr<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 DMatTSMatAddExpr( const MT1& lhs, const MT2& rhs )
131  : lhs_( lhs ) // Left-hand side dense matrix of the addition expression
132  , rhs_( rhs ) // Right-hand side sparse matrix of the addition 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 DMatTSMatAddExpr& 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  addAssign( ~lhs, rhs.rhs_ );
232  }
233  else {
234  assign ( ~lhs, rhs.lhs_ );
235  addAssign( ~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 DMatTSMatAddExpr& 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 DMatTSMatAddExpr& 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  addAssign( ~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 DMatTSMatAddExpr& 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  subAssign( ~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 const DMatTSMatAddExpr<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 DMatTSMatAddExpr<T1,T2>( ~lhs, ~rhs );
402 }
403 //*************************************************************************************************
404 
405 
406 //*************************************************************************************************
436 template< typename T1 // Type of the left-hand side dense matrix
437  , typename T2 > // Type of the right-hand side sparse matrix
438 const DMatTSMatAddExpr<T1,T2>
440 {
441  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
442  throw std::invalid_argument( "Matrix sizes do not match" );
443 
444  return DMatTSMatAddExpr<T1,T2>( ~rhs, ~lhs );
445 }
446 //*************************************************************************************************
447 
448 
449 
450 
451 //=================================================================================================
452 //
453 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
454 //
455 //=================================================================================================
456 
457 //*************************************************************************************************
470 template< typename T1 // Type of the dense matrix of the left-hand side expression
471  , typename T2 // Type of the sparse matrix of the left-hand side expression
472  , typename T3 // Type of the right-hand side dense matrix
473  , bool SO > // Storage order of the right-hand side dense matrix
474 inline const typename AddExprTrait< DMatTSMatAddExpr<T1,T2>, T3 >::Type
475  operator+( const DMatTSMatAddExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
476 {
477  return ( lhs.leftOperand() + (~rhs) ) + lhs.rightOperand();
478 }
480 //*************************************************************************************************
481 
482 
483 //*************************************************************************************************
496 template< typename T1 // Type of the dense matrix of the left-hand side expression
497  , typename T2 // Type of the sparse matrix of the left-hand side expression
498  , typename T3 // Type of the right-hand side dense matrix
499  , bool SO > // Storage order of the right-hand side dense matrix
500 inline const typename SubExprTrait< DMatTSMatAddExpr<T1,T2>, T3 >::Type
501  operator-( const DMatTSMatAddExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
502 {
503  return ( lhs.leftOperand() - (~rhs) ) + lhs.rightOperand();
504 }
506 //*************************************************************************************************
507 
508 
509 
510 
511 //=================================================================================================
512 //
513 // EXPRESSION TRAIT SPECIALIZATIONS
514 //
515 //=================================================================================================
516 
517 //*************************************************************************************************
519 template< typename MT1, typename MT2, typename MT3 >
520 struct DMatDMatAddExprTrait< DMatTSMatAddExpr<MT1,MT2>, MT3 >
521 {
522  public:
523  //**********************************************************************************************
525  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
526  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
527  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
528  , typename DMatTSMatAddExprTrait< typename DMatDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
529  , INVALID_TYPE >::Type Type;
531  //**********************************************************************************************
532 };
534 //*************************************************************************************************
535 
536 
537 //*************************************************************************************************
539 template< typename MT1, typename MT2, typename MT3 >
540 struct DMatTDMatAddExprTrait< DMatTSMatAddExpr<MT1,MT2>, MT3 >
541 {
542  public:
543  //**********************************************************************************************
545  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
546  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
547  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
548  , typename DMatTSMatAddExprTrait< typename DMatTDMatAddExprTrait<MT1,MT3>::Type, MT2 >::Type
549  , INVALID_TYPE >::Type Type;
551  //**********************************************************************************************
552 };
554 //*************************************************************************************************
555 
556 
557 //*************************************************************************************************
559 template< typename MT1, typename MT2, typename MT3 >
560 struct DMatDMatSubExprTrait< DMatTSMatAddExpr<MT1,MT2>, MT3 >
561 {
562  public:
563  //**********************************************************************************************
565  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
566  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
567  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
568  , typename DMatTSMatAddExprTrait< typename DMatDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
569  , INVALID_TYPE >::Type Type;
571  //**********************************************************************************************
572 };
574 //*************************************************************************************************
575 
576 
577 //*************************************************************************************************
579 template< typename MT1, typename MT2, typename MT3 >
580 struct DMatTDMatSubExprTrait< DMatTSMatAddExpr<MT1,MT2>, MT3 >
581 {
582  public:
583  //**********************************************************************************************
585  typedef typename SelectType< IsDenseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
586  IsSparseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
587  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
588  , typename DMatTSMatAddExprTrait< typename DMatTDMatSubExprTrait<MT1,MT3>::Type, MT2 >::Type
589  , INVALID_TYPE >::Type Type;
591  //**********************************************************************************************
592 };
594 //*************************************************************************************************
595 
596 } // namespace blaze
597 
598 #endif