All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SMatTDMatSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATTDMATSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_SMATTDMATSUBEXPR_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 SMATTDMATSUBEXPR
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
68 template< typename MT1 // Type of the left-hand side sparse matrix
69  , typename MT2 > // Type of the right-hand side dense matrix
70 class SMatTDMatSubExpr : public DenseMatrix< SMatTDMatSubExpr<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<MT2>::value };
122  //**********************************************************************************************
123 
124  //**Constructor*********************************************************************************
130  explicit inline SMatTDMatSubExpr( const MT1& lhs, const MT2& rhs )
131  : lhs_( lhs ) // Left-hand side sparse matrix of the subtraction expression
132  , rhs_( rhs ) // Right-hand side dense 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<MT2>::value && rhs_.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 SMatTDMatSubExpr& 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  assign ( ~lhs, -rhs.rhs_ );
231  addAssign( ~lhs, rhs.lhs_ );
232  }
234  //**********************************************************************************************
235 
236  //**Assignment to sparse matrices***************************************************************
248  template< typename MT // Type of the target sparse matrix
249  , bool SO2 > // Storage order of the target sparse matrix
250  friend inline void assign( SparseMatrix<MT,SO2>& lhs, const SMatTDMatSubExpr& rhs )
251  {
252  typedef typename SelectType< SO2, OppositeType, ResultType >::Type TmpType;
253 
259  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename TmpType::CompositeType );
260 
261  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
262  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
263 
264  const TmpType tmp( rhs );
265  assign( ~lhs, tmp );
266  }
268  //**********************************************************************************************
269 
270  //**Addition assignment to dense matrices*******************************************************
283  template< typename MT // Type of the target dense matrix
284  , bool SO2 > // Storage order of the target dense matrix
285  friend inline void addAssign( DenseMatrix<MT,SO2>& lhs, const SMatTDMatSubExpr& rhs )
286  {
287  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
288  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
289 
290  addAssign( ~lhs, rhs.lhs_ );
291  subAssign( ~lhs, rhs.rhs_ );
292  }
294  //**********************************************************************************************
295 
296  //**Addition assignment to sparse matrices******************************************************
297  // No special implementation for the addition assignment to sparse matrices.
298  //**********************************************************************************************
299 
300  //**Subtraction assignment to dense matrices****************************************************
313  template< typename MT // Type of the target dense matrix
314  , bool SO2 > // Storage order of the target dense matrix
315  friend inline void subAssign( DenseMatrix<MT,SO2>& lhs, const SMatTDMatSubExpr& rhs )
316  {
317  BLAZE_INTERNAL_ASSERT( (~lhs).rows() == rhs.rows() , "Invalid number of rows" );
318  BLAZE_INTERNAL_ASSERT( (~lhs).columns() == rhs.columns(), "Invalid number of columns" );
319 
320  subAssign( ~lhs, rhs.lhs_ );
321  addAssign( ~lhs, rhs.rhs_ );
322  }
324  //**********************************************************************************************
325 
326  //**Subtraction assignment to sparse matrices***************************************************
327  // No special implementation for the subtraction assignment to sparse matrices.
328  //**********************************************************************************************
329 
330  //**Multiplication assignment to dense matrices*************************************************
331  // No special implementation for the multiplication assignment to dense matrices.
332  //**********************************************************************************************
333 
334  //**Multiplication assignment to sparse matrices************************************************
335  // No special implementation for the multiplication assignment to sparse matrices.
336  //**********************************************************************************************
337 
338  //**Compile time checks*************************************************************************
345  //**********************************************************************************************
346 };
347 //*************************************************************************************************
348 
349 
350 
351 
352 //=================================================================================================
353 //
354 // GLOBAL BINARY ARITHMETIC OPERATORS
355 //
356 //=================================================================================================
357 
358 //*************************************************************************************************
389 template< typename T1 // Type of the left-hand side sparse matrix
390  , typename T2 > // Type of the right-hand side dense matrix
391 inline const SMatTDMatSubExpr<T1,T2>
393 {
394  if( (~lhs).rows() != (~rhs).rows() || (~lhs).columns() != (~rhs).columns() )
395  throw std::invalid_argument( "Matrix sizes do not match" );
396 
397  return SMatTDMatSubExpr<T1,T2>( ~lhs, ~rhs );
398 }
399 //*************************************************************************************************
400 
401 
402 
403 
404 //=================================================================================================
405 //
406 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
407 //
408 //=================================================================================================
409 
410 //*************************************************************************************************
423 template< typename T1 // Type of the sparse matrix of the left-hand side expression
424  , typename T2 // Type of the dense matrix of the left-hand side expression
425  , typename T3 // Type of the right-hand side dense matrix
426  , bool SO > // Storage order of the right-hand side dense matrix
427 inline const typename AddExprTrait< SMatTDMatSubExpr<T1,T2>, T3 >::Type
428  operator+( const SMatTDMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
429 {
430  return ( (~rhs) - lhs.rightOperand() ) + lhs.leftOperand();
431 }
433 //*************************************************************************************************
434 
435 
436 //*************************************************************************************************
449 template< typename T1 // Type of the sparse matrix of the left-hand side expression
450  , typename T2 // Type of the dense matrix of the left-hand side expression
451  , typename T3 // Type of the right-hand side dense matrix
452  , bool SO > // Storage order of the right-hand side dense matrix
453 inline const typename SubExprTrait< SMatTDMatSubExpr<T1,T2>, T3 >::Type
454  operator-( const SMatTDMatSubExpr<T1,T2>& lhs, const DenseMatrix<T3,SO>& rhs )
455 {
456  return lhs.leftOperand() - ( lhs.rightOperand() + (~rhs) );
457 }
459 //*************************************************************************************************
460 
461 
462 
463 
464 //=================================================================================================
465 //
466 // EXPRESSION TRAIT SPECIALIZATIONS
467 //
468 //=================================================================================================
469 
470 //*************************************************************************************************
472 template< typename MT1, typename MT2, typename MT3 >
473 struct DMatDMatAddExprTrait< SMatTDMatSubExpr<MT1,MT2>, MT3 >
474 {
475  public:
476  //**********************************************************************************************
478  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
479  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
480  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
481  , typename DMatSMatAddExprTrait< typename DMatTDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
482  , INVALID_TYPE >::Type Type;
484  //**********************************************************************************************
485 };
487 //*************************************************************************************************
488 
489 
490 //*************************************************************************************************
492 template< typename MT1, typename MT2, typename MT3 >
493 struct DMatTDMatAddExprTrait< SMatTDMatSubExpr<MT1,MT2>, MT3 >
494 {
495  public:
496  //**********************************************************************************************
498  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
499  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
500  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
501  , typename TDMatSMatAddExprTrait< typename TDMatTDMatSubExprTrait<MT3,MT2>::Type, MT1 >::Type
502  , INVALID_TYPE >::Type Type;
504  //**********************************************************************************************
505 };
507 //*************************************************************************************************
508 
509 
510 //*************************************************************************************************
512 template< typename MT1, typename MT2, typename MT3 >
513 struct DMatDMatSubExprTrait< SMatTDMatSubExpr<MT1,MT2>, MT3 >
514 {
515  public:
516  //**********************************************************************************************
518  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
519  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
520  IsDenseMatrix<MT3>::value && IsRowMajorMatrix<MT3>::value
521  , typename SMatDMatSubExprTrait< MT1, typename TDMatDMatAddExprTrait<MT2,MT3>::Type >::Type
522  , INVALID_TYPE >::Type Type;
524  //**********************************************************************************************
525 };
527 //*************************************************************************************************
528 
529 
530 //*************************************************************************************************
532 template< typename MT1, typename MT2, typename MT3 >
533 struct DMatTDMatSubExprTrait< SMatTDMatSubExpr<MT1,MT2>, MT3 >
534 {
535  public:
536  //**********************************************************************************************
538  typedef typename SelectType< IsSparseMatrix<MT1>::value && IsRowMajorMatrix<MT1>::value &&
539  IsDenseMatrix<MT2>::value && IsColumnMajorMatrix<MT2>::value &&
540  IsDenseMatrix<MT3>::value && IsColumnMajorMatrix<MT3>::value
541  , typename SMatTDMatSubExprTrait< MT1, typename TDMatTDMatAddExprTrait<MT2,MT3>::Type >::Type
542  , INVALID_TYPE >::Type Type;
544  //**********************************************************************************************
545 };
547 //*************************************************************************************************
548 
549 } // namespace blaze
550 
551 #endif