All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVecDVecSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECDVECSUBEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_SVECDVECSUBEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
47 #include <blaze/util/Assert.h>
50 #include <blaze/util/SelectType.h>
51 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // CLASS SVECDVECSUBEXPR
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
69 template< typename VT1 // Type of the left-hand side sparse vector
70  , typename VT2 // Type of the right-hand side dense vector
71  , bool TF > // Transpose flag
72 class SVecDVecSubExpr : public DenseVector< SVecDVecSubExpr<VT1,VT2,TF>, TF >
73  , private VecVecSubExpr
74  , private Computation
75 {
76  private:
77  //**Type definitions****************************************************************************
78  typedef typename VT1::ResultType RT1;
79  typedef typename VT2::ResultType RT2;
80  typedef typename VT1::ReturnType RN1;
81  typedef typename VT2::ReturnType RN2;
82  typedef typename VT1::CompositeType CT1;
83  typedef typename VT2::CompositeType CT2;
84  typedef typename VT1::TransposeType TT1;
85  typedef typename VT2::TransposeType TT2;
86  //**********************************************************************************************
87 
88  //**Return type evaluation**********************************************************************
90 
95  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
96 
99  //**********************************************************************************************
100 
101  public:
102  //**Type definitions****************************************************************************
105  typedef typename ResultType::TransposeType TransposeType;
106  typedef typename ResultType::ElementType ElementType;
107 
110 
112  typedef const ResultType CompositeType;
113 
115  typedef typename SelectType< IsExpression<VT1>::value, const VT1, const VT1& >::Type LeftOperand;
116 
118  typedef typename SelectType< IsExpression<VT2>::value, const VT2, const VT2& >::Type RightOperand;
119  //**********************************************************************************************
120 
121  //**Compilation flags***************************************************************************
123  enum { vectorizable = 0 };
124  //**********************************************************************************************
125 
126  //**Constructor*********************************************************************************
132  explicit inline SVecDVecSubExpr( const VT1& lhs, const VT2& rhs )
133  : lhs_( lhs ) // Left-hand side sparse vector of the subtraction expression
134  , rhs_( rhs ) // Right-hand side dense vector of the subtraction expression
135  {
136  BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
137  }
138  //**********************************************************************************************
139 
140  //**Subscript operator**************************************************************************
146  inline ReturnType operator[]( size_t index ) const {
147  BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
148  return lhs_[index] - rhs_[index];
149  }
150  //**********************************************************************************************
151 
152  //**Size function*******************************************************************************
157  inline size_t size() const {
158  return lhs_.size();
159  }
160  //**********************************************************************************************
161 
162  //**Left operand access*************************************************************************
167  inline LeftOperand leftOperand() const {
168  return lhs_;
169  }
170  //**********************************************************************************************
171 
172  //**Right operand access************************************************************************
177  inline RightOperand rightOperand() const {
178  return rhs_;
179  }
180  //**********************************************************************************************
181 
182  //**********************************************************************************************
188  template< typename T >
189  inline bool canAlias( const T* alias ) const {
190  return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
191  }
192  //**********************************************************************************************
193 
194  //**********************************************************************************************
200  template< typename T >
201  inline bool isAliased( const T* alias ) const {
202  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
203  }
204  //**********************************************************************************************
205 
206  private:
207  //**Member variables****************************************************************************
210  //**********************************************************************************************
211 
212  //**Assignment to dense vectors*****************************************************************
224  template< typename VT > // Type of the target dense vector
225  friend inline void assign( DenseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
226  {
228 
229  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
230 
231  assign ( ~lhs, -rhs.rhs_ );
232  addAssign( ~lhs, rhs.lhs_ );
233  }
235  //**********************************************************************************************
236 
237  //**Assignment to sparse vectors****************************************************************
249  template< typename VT > // Type of the target sparse vector
250  friend inline void assign( SparseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
251  {
253 
256  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
257 
258  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
259 
260  const ResultType tmp( rhs );
261  assign( ~lhs, tmp );
262  }
264  //**********************************************************************************************
265 
266  //**Addition assignment to dense vectors********************************************************
278  template< typename VT > // Type of the target dense vector
279  friend inline void addAssign( DenseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
280  {
282 
283  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
284 
285  subAssign( ~lhs, rhs.rhs_ );
286  addAssign( ~lhs, rhs.lhs_ );
287  }
289  //**********************************************************************************************
290 
291  //**Addition assignment to sparse vectors*******************************************************
292  // No special implementation for the addition assignment to sparse vectors.
293  //**********************************************************************************************
294 
295  //**Subtraction assignment to dense vectors*****************************************************
307  template< typename VT > // Type of the target dense vector
308  friend inline void subAssign( DenseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
309  {
311 
312  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
313 
314  addAssign( ~lhs, rhs.rhs_ );
315  subAssign( ~lhs, rhs.lhs_ );
316  }
318  //**********************************************************************************************
319 
320  //**Subtraction assignment to sparse vectors****************************************************
321  // No special implementation for the subtraction assignment to sparse vectors.
322  //**********************************************************************************************
323 
324  //**Multiplication assignment to dense vectors**************************************************
336  template< typename VT > // Type of the target dense vector
337  friend inline void multAssign( DenseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
338  {
340 
343  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
344 
345  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
346 
347  const ResultType tmp( rhs );
348  multAssign( ~lhs, tmp );
349  }
351  //**********************************************************************************************
352 
353  //**Multiplication assignment to sparse vectors*************************************************
354  // No special implementation for the multiplication assignment to sparse vectors.
355  //**********************************************************************************************
356 
357  //**Compile time checks*************************************************************************
364  //**********************************************************************************************
365 };
366 //*************************************************************************************************
367 
368 
369 
370 
371 //=================================================================================================
372 //
373 // GLOBAL BINARY ARITHMETIC OPERATORS
374 //
375 //=================================================================================================
376 
377 //*************************************************************************************************
403 template< typename T1 // Type of the left-hand side sparse vector
404  , typename T2 // Type of the right-hand side dense vector
405  , bool TF > // Transpose flag
406 inline const SVecDVecSubExpr<T1,T2,TF>
408 {
410 
411  if( (~lhs).size() != (~rhs).size() )
412  throw std::invalid_argument( "Vector sizes do not match" );
413 
414  return SVecDVecSubExpr<T1,T2,TF>( ~lhs, ~rhs );
415 }
416 //*************************************************************************************************
417 
418 
419 
420 
421 //=================================================================================================
422 //
423 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
424 //
425 //=================================================================================================
426 
427 //*************************************************************************************************
440 template< typename T1 // Type of the sparse vector of the left-hand side expression
441  , typename T2 // Type of the dense vector of the left-hand side expression
442  , bool TF // Transpose flag of the left-hand side expression
443  , typename T3 > // Type of right-hand side dense vector
444 inline const typename AddExprTrait< SVecDVecSubExpr<T1,T2,TF>, T3 >::Type
445  operator+( const SVecDVecSubExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
446 {
448 
449  return ( (~rhs) - lhs.rightOperand() ) + lhs.leftOperand();
450 }
452 //*************************************************************************************************
453 
454 
455 //*************************************************************************************************
468 template< typename T1 // Type of the sparse vector of the left-hand side expression
469  , typename T2 // Type of the dense vector of the left-hand side expression
470  , bool TF // Transpose flag of the left-hand side expression
471  , typename T3 > // Type of right-hand side dense vector
472 inline const typename SubExprTrait< SVecDVecSubExpr<T1,T2,TF>, T3 >::Type
473  operator-( const SVecDVecSubExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
474 {
476 
477  return lhs.leftOperand() - ( lhs.rightOperand() + (~rhs) );
478 }
480 //*************************************************************************************************
481 
482 
483 
484 
485 //=================================================================================================
486 //
487 // EXPRESSION TRAIT SPECIALIZATIONS
488 //
489 //=================================================================================================
490 
491 //*************************************************************************************************
493 template< typename VT1, typename VT2, typename VT3 >
494 struct DVecDVecAddExprTrait< SVecDVecSubExpr<VT1,VT2,false>, VT3 >
495 {
496  public:
497  //**********************************************************************************************
499  typedef typename SelectType< IsSparseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
500  IsDenseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
501  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
502  , typename DVecSVecAddExprTrait< typename DVecDVecSubExprTrait<VT3,VT2>::Type, VT1 >::Type
503  , INVALID_TYPE >::Type Type;
505  //**********************************************************************************************
506 };
508 //*************************************************************************************************
509 
510 
511 //*************************************************************************************************
513 template< typename VT1, typename VT2, typename VT3 >
514 struct TDVecTDVecAddExprTrait< SVecDVecSubExpr<VT1,VT2,true>, VT3 >
515 {
516  public:
517  //**********************************************************************************************
519  typedef typename SelectType< IsSparseVector<VT1>::value && IsTransposeVector<VT1>::value &&
520  IsDenseVector<VT2>::value && IsTransposeVector<VT2>::value &&
521  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
522  , typename TDVecTSVecAddExprTrait< typename TDVecTDVecSubExprTrait<VT3,VT2>::Type, VT1 >::Type
523  , INVALID_TYPE >::Type Type;
525  //**********************************************************************************************
526 };
528 //*************************************************************************************************
529 
530 
531 //*************************************************************************************************
533 template< typename VT1, typename VT2, typename VT3 >
534 struct DVecDVecSubExprTrait< SVecDVecSubExpr<VT1,VT2,false>, VT3 >
535 {
536  public:
537  //**********************************************************************************************
539  typedef typename SelectType< IsSparseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
540  IsDenseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
541  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
542  , typename SVecDVecSubExprTrait< VT1, typename DVecDVecAddExprTrait<VT2,VT3>::Type >::Type
543  , INVALID_TYPE >::Type Type;
545  //**********************************************************************************************
546 };
548 //*************************************************************************************************
549 
550 
551 //*************************************************************************************************
553 template< typename VT1, typename VT2, typename VT3 >
554 struct TDVecTDVecSubExprTrait< SVecDVecSubExpr<VT1,VT2,true>, VT3 >
555 {
556  public:
557  //**********************************************************************************************
559  typedef typename SelectType< IsSparseVector<VT1>::value && IsTransposeVector<VT1>::value &&
560  IsDenseVector<VT2>::value && IsTransposeVector<VT2>::value &&
561  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
562  , typename TSVecTDVecSubExprTrait< VT1, typename TDVecTDVecAddExprTrait<VT2,VT3>::Type >::Type
563  , INVALID_TYPE >::Type Type;
565  //**********************************************************************************************
566 };
568 //*************************************************************************************************
569 
570 } // namespace blaze
571 
572 #endif