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>
48 #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 Expression
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 
126  enum { canAlias = IsComputation<VT2>::value && CanAlias<VT2>::value };
127  //**********************************************************************************************
128 
129  //**Constructor*********************************************************************************
135  explicit inline SVecDVecSubExpr( const VT1& lhs, const VT2& rhs )
136  : lhs_( lhs ) // Left-hand side sparse vector of the subtraction expression
137  , rhs_( rhs ) // Right-hand side dense vector of the subtraction expression
138  {
139  BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
140  }
141  //**********************************************************************************************
142 
143  //**Subscript operator**************************************************************************
149  inline ReturnType operator[]( size_t index ) const {
150  BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
151  return lhs_[index] - rhs_[index];
152  }
153  //**********************************************************************************************
154 
155  //**Size function*******************************************************************************
160  inline size_t size() const {
161  return lhs_.size();
162  }
163  //**********************************************************************************************
164 
165  //**Left operand access*************************************************************************
170  inline LeftOperand leftOperand() const {
171  return lhs_;
172  }
173  //**********************************************************************************************
174 
175  //**Right operand access************************************************************************
180  inline RightOperand rightOperand() const {
181  return rhs_;
182  }
183  //**********************************************************************************************
184 
185  //**********************************************************************************************
191  template< typename T >
192  inline bool isAliased( const T* alias ) const {
193  return IsComputation<VT2>::value && CanAlias<VT2>::value && rhs_.isAliased( alias );
194  }
195  //**********************************************************************************************
196 
197  private:
198  //**Member variables****************************************************************************
201  //**********************************************************************************************
202 
203  //**Assignment to dense vectors*****************************************************************
215  template< typename VT > // Type of the target dense vector
216  friend inline void assign( DenseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
217  {
218  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
219 
220  assign ( ~lhs, -rhs.rhs_ );
221  addAssign( ~lhs, rhs.lhs_ );
222  }
224  //**********************************************************************************************
225 
226  //**Assignment to sparse vectors****************************************************************
238  template< typename VT > // Type of the target sparse vector
239  friend inline void assign( SparseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
240  {
243  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
244 
245  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
246 
247  const ResultType tmp( rhs );
248  assign( ~lhs, tmp );
249  }
251  //**********************************************************************************************
252 
253  //**Addition assignment to dense vectors********************************************************
265  template< typename VT > // Type of the target dense vector
266  friend inline void addAssign( DenseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
267  {
268  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
269 
270  addAssign( ~lhs, rhs.lhs_ );
271  subAssign( ~lhs, rhs.rhs_ );
272  }
274  //**********************************************************************************************
275 
276  //**Addition assignment to sparse vectors*******************************************************
277  // No special implementation for the addition assignment to sparse vectors.
278  //**********************************************************************************************
279 
280  //**Subtraction assignment to dense vectors*****************************************************
292  template< typename VT > // Type of the target dense vector
293  friend inline void subAssign( DenseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
294  {
295  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
296 
297  subAssign( ~lhs, rhs.lhs_ );
298  addAssign( ~lhs, rhs.rhs_ );
299  }
301  //**********************************************************************************************
302 
303  //**Subtraction assignment to sparse vectors****************************************************
304  // No special implementation for the subtraction assignment to sparse vectors.
305  //**********************************************************************************************
306 
307  //**Multiplication assignment to dense vectors**************************************************
319  template< typename VT > // Type of the target dense vector
320  friend inline void multAssign( DenseVector<VT,TF>& lhs, const SVecDVecSubExpr& rhs )
321  {
324  BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE( typename ResultType::CompositeType );
325 
326  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
327 
328  const ResultType tmp( rhs );
329  multAssign( ~lhs, tmp );
330  }
332  //**********************************************************************************************
333 
334  //**Multiplication assignment to sparse vectors*************************************************
335  // No special implementation for the multiplication assignment to sparse vectors.
336  //**********************************************************************************************
337 
338  //**Compile time checks*************************************************************************
345  //**********************************************************************************************
346 };
347 //*************************************************************************************************
348 
349 
350 
351 
352 //=================================================================================================
353 //
354 // GLOBAL BINARY ARITHMETIC OPERATORS
355 //
356 //=================================================================================================
357 
358 //*************************************************************************************************
384 template< typename T1 // Type of the left-hand side sparse vector
385  , typename T2 // Type of the right-hand side dense vector
386  , bool TF > // Transpose flag
387 inline const SVecDVecSubExpr<T1,T2,TF>
389 {
390  if( (~lhs).size() != (~rhs).size() )
391  throw std::invalid_argument( "Vector sizes do not match" );
392 
393  return SVecDVecSubExpr<T1,T2,TF>( ~lhs, ~rhs );
394 }
395 //*************************************************************************************************
396 
397 
398 
399 
400 //=================================================================================================
401 //
402 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
403 //
404 //=================================================================================================
405 
406 //*************************************************************************************************
419 template< typename T1 // Type of the sparse vector of the left-hand side expression
420  , typename T2 // Type of the dense vector of the left-hand side expression
421  , bool TF // Transpose flag of the left-hand side expression
422  , typename T3 > // Type of right-hand side dense vector
423 inline const typename AddExprTrait< SVecDVecSubExpr<T1,T2,TF>, T3 >::Type
424  operator+( const SVecDVecSubExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
425 {
426  return ( (~rhs) - lhs.rightOperand() ) + lhs.leftOperand();
427 }
429 //*************************************************************************************************
430 
431 
432 //*************************************************************************************************
445 template< typename T1 // Type of the sparse vector of the left-hand side expression
446  , typename T2 // Type of the dense vector of the left-hand side expression
447  , bool TF // Transpose flag of the left-hand side expression
448  , typename T3 > // Type of right-hand side dense vector
449 inline const typename SubExprTrait< SVecDVecSubExpr<T1,T2,TF>, T3 >::Type
450  operator-( const SVecDVecSubExpr<T1,T2,TF>& lhs, const DenseVector<T3,TF>& rhs )
451 {
452  return lhs.leftOperand() - ( lhs.rightOperand() + (~rhs) );
453 }
455 //*************************************************************************************************
456 
457 
458 
459 
460 //=================================================================================================
461 //
462 // EXPRESSION TRAIT SPECIALIZATIONS
463 //
464 //=================================================================================================
465 
466 //*************************************************************************************************
468 template< typename VT1, typename VT2, typename VT3 >
469 struct DVecDVecAddExprTrait< SVecDVecSubExpr<VT1,VT2,false>, VT3 >
470 {
471  public:
472  //**********************************************************************************************
474  typedef typename SelectType< IsSparseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
475  IsDenseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
476  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
477  , typename DVecSVecAddExprTrait< typename DVecDVecSubExprTrait<VT3,VT2>::Type, VT1 >::Type
478  , INVALID_TYPE >::Type Type;
480  //**********************************************************************************************
481 };
483 //*************************************************************************************************
484 
485 
486 //*************************************************************************************************
488 template< typename VT1, typename VT2, typename VT3 >
489 struct TDVecTDVecAddExprTrait< SVecDVecSubExpr<VT1,VT2,true>, VT3 >
490 {
491  public:
492  //**********************************************************************************************
494  typedef typename SelectType< IsSparseVector<VT1>::value && IsTransposeVector<VT1>::value &&
495  IsDenseVector<VT2>::value && IsTransposeVector<VT2>::value &&
496  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
497  , typename TDVecTSVecAddExprTrait< typename TDVecTDVecSubExprTrait<VT3,VT2>::Type, VT1 >::Type
498  , INVALID_TYPE >::Type Type;
500  //**********************************************************************************************
501 };
503 //*************************************************************************************************
504 
505 
506 //*************************************************************************************************
508 template< typename VT1, typename VT2, typename VT3 >
509 struct DVecDVecSubExprTrait< SVecDVecSubExpr<VT1,VT2,false>, VT3 >
510 {
511  public:
512  //**********************************************************************************************
514  typedef typename SelectType< IsSparseVector<VT1>::value && !IsTransposeVector<VT1>::value &&
515  IsDenseVector<VT2>::value && !IsTransposeVector<VT2>::value &&
516  IsDenseVector<VT3>::value && !IsTransposeVector<VT3>::value
517  , typename SVecDVecSubExprTrait< VT1, typename DVecDVecAddExprTrait<VT2,VT3>::Type >::Type
518  , INVALID_TYPE >::Type Type;
520  //**********************************************************************************************
521 };
523 //*************************************************************************************************
524 
525 
526 //*************************************************************************************************
528 template< typename VT1, typename VT2, typename VT3 >
529 struct TDVecTDVecSubExprTrait< SVecDVecSubExpr<VT1,VT2,true>, VT3 >
530 {
531  public:
532  //**********************************************************************************************
534  typedef typename SelectType< IsSparseVector<VT1>::value && IsTransposeVector<VT1>::value &&
535  IsDenseVector<VT2>::value && IsTransposeVector<VT2>::value &&
536  IsDenseVector<VT3>::value && IsTransposeVector<VT3>::value
537  , typename TSVecTDVecSubExprTrait< VT1, typename TDVecTDVecAddExprTrait<VT2,VT3>::Type >::Type
538  , INVALID_TYPE >::Type Type;
540  //**********************************************************************************************
541 };
543 //*************************************************************************************************
544 
545 } // namespace blaze
546 
547 #endif