All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVecSVecSubExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECSVECSUBEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECSVECSUBEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
50 #include <blaze/math/Functions.h>
59 #include <blaze/util/Assert.h>
61 #include <blaze/util/DisableIf.h>
62 #include <blaze/util/EnableIf.h>
64 #include <blaze/util/SelectType.h>
65 #include <blaze/util/Types.h>
67 
68 
69 namespace blaze {
70 
71 //=================================================================================================
72 //
73 // CLASS SVECSVECSUBEXPR
74 //
75 //=================================================================================================
76 
77 //*************************************************************************************************
84 template< typename VT1 // Type of the left-hand side sparse vector
85  , typename VT2 // Type of the right-hand side sparse vector
86  , bool TF > // Transpose flag
87 class SVecSVecSubExpr : public SparseVector< SVecSVecSubExpr<VT1,VT2,TF>, TF >
88  , private VecVecSubExpr
89  , private Computation
90 {
91  private:
92  //**Type definitions****************************************************************************
93  typedef typename VT1::ResultType RT1;
94  typedef typename VT2::ResultType RT2;
95  typedef typename VT1::ReturnType RN1;
96  typedef typename VT2::ReturnType RN2;
97  typedef typename VT1::CompositeType CT1;
98  typedef typename VT2::CompositeType CT2;
99  typedef typename VT1::TransposeType TT1;
100  typedef typename VT2::TransposeType TT2;
101  //**********************************************************************************************
102 
103  //**Return type evaluation**********************************************************************
105 
110  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
111 
114  //**********************************************************************************************
115 
116  public:
117  //**Type definitions****************************************************************************
122 
125 
127  typedef const ResultType CompositeType;
128 
130  typedef typename SelectType< IsExpression<VT1>::value, const VT1, const VT1& >::Type LeftOperand;
131 
133  typedef typename SelectType< IsExpression<VT2>::value, const VT2, const VT2& >::Type RightOperand;
134  //**********************************************************************************************
135 
136  //**Constructor*********************************************************************************
139  explicit inline SVecSVecSubExpr( const VT1& lhs, const VT2& rhs )
140  : lhs_( lhs ) // Left-hand side sparse vector of the subtraction expression
141  , rhs_( rhs ) // Right-hand side sparse vector of the subtraction expression
142  {
143  BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
144  }
145  //**********************************************************************************************
146 
147  //**Subscript operator**************************************************************************
153  inline ReturnType operator[]( size_t index ) const {
154  BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
155  return lhs_[index] - rhs_[index];
156  }
157  //**********************************************************************************************
158 
159  //**Size function*******************************************************************************
164  inline size_t size() const {
165  return lhs_.size();
166  }
167  //**********************************************************************************************
168 
169  //**NonZeros function***************************************************************************
174  inline size_t nonZeros() const {
175  return min( lhs_.size(), lhs_.nonZeros() + rhs_.nonZeros() );
176  }
177  //**********************************************************************************************
178 
179  //**Left operand access*************************************************************************
184  inline LeftOperand leftOperand() const {
185  return lhs_;
186  }
187  //**********************************************************************************************
188 
189  //**Right operand access************************************************************************
194  inline RightOperand rightOperand() const {
195  return rhs_;
196  }
197  //**********************************************************************************************
198 
199  //**********************************************************************************************
205  template< typename T >
206  inline bool canAlias( const T* alias ) const {
207  return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
208  }
209  //**********************************************************************************************
210 
211  //**********************************************************************************************
217  template< typename T >
218  inline bool isAliased( const T* alias ) const {
219  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
220  }
221  //**********************************************************************************************
222 
223  private:
224  //**Member variables****************************************************************************
227  //**********************************************************************************************
228 
229  //**Default assignment to dense vectors*********************************************************
242  template< typename VT > // Type of the target dense vector
243  friend inline typename EnableIf< IsResizable<typename VT::ElementType> >::Type
244  assign( DenseVector<VT,TF>& lhs, const SVecSVecSubExpr& rhs )
245  {
247 
248  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
249 
250  typedef typename RemoveReference<CT1>::Type::ConstIterator LeftIterator;
251  typedef typename RemoveReference<CT2>::Type::ConstIterator RightIterator;
252 
253  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side sparse vector operand
254  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
255 
256  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
257  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
258  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
259 
260  const LeftIterator lend( x.end() );
261  const RightIterator rend( y.end() );
262 
263  for( LeftIterator l=x.begin(); l!=lend; ++l ) {
264  (~lhs)[l->index()] = l->value();
265  }
266 
267  for( RightIterator r=y.begin(); r!=rend; ++r ) {
268  if( isDefault( (~lhs)[r->index()] ) )
269  (~lhs)[r->index()] = -r->value();
270  else
271  (~lhs)[r->index()] -= r->value();
272  }
273  }
275  //**********************************************************************************************
276 
277  //**Optimized assignment to dense vectors*******************************************************
290  template< typename VT > // Type of the target dense vector
291  friend inline typename DisableIf< IsResizable<typename VT::ElementType> >::Type
292  assign( DenseVector<VT,TF>& lhs, const SVecSVecSubExpr& rhs )
293  {
295 
296  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
297 
298  typedef typename RemoveReference<CT1>::Type::ConstIterator LeftIterator;
299  typedef typename RemoveReference<CT2>::Type::ConstIterator RightIterator;
300 
301  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side sparse vector operand
302  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
303 
304  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
305  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
306  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
307 
308  const LeftIterator lend( x.end() );
309  const RightIterator rend( y.end() );
310 
311  for( LeftIterator l=x.begin(); l!=lend; ++l ) {
312  (~lhs)[l->index()] = l->value();
313  }
314 
315  for( RightIterator r=y.begin(); r!=rend; ++r ) {
316  (~lhs)[r->index()] -= r->value();
317  }
318  }
320  //**********************************************************************************************
321 
322  //**Assignment to sparse vectors****************************************************************
334  template< typename VT > // Type of the target sparse vector
335  friend inline void assign( SparseVector<VT,TF>& lhs, const SVecSVecSubExpr& rhs )
336  {
338 
339  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
340 
341  typedef typename RemoveReference<CT1>::Type::ConstIterator LeftIterator;
342  typedef typename RemoveReference<CT2>::Type::ConstIterator RightIterator;
343 
344  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side sparse vector operand
345  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
346 
347  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
348  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
349  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
350 
351  const LeftIterator lend( x.end() );
352  const RightIterator rend( y.end() );
353 
354  LeftIterator l( x.begin() );
355  RightIterator r( y.begin() );
356 
357  while( l != lend && r != rend )
358  {
359  if( l->index() < r->index() ) {
360  (~lhs).append( l->index(), l->value() );
361  ++l;
362  }
363  else if( l->index() > r->index() ) {
364  (~lhs).append( r->index(), -r->value() );
365  ++r;
366  }
367  else {
368  (~lhs).append( l->index(), l->value() - r->value() );
369  ++l;
370  ++r;
371  }
372  }
373 
374  while( l != lend ) {
375  (~lhs).append( l->index(), l->value() );
376  ++l;
377  }
378 
379  while( r != rend ) {
380  (~lhs).append( r->index(), -r->value() );
381  ++r;
382  }
383  }
385  //**********************************************************************************************
386 
387  //**Addition assignment to dense vectors********************************************************
399  template< typename VT > // Type of the target dense vector
400  friend inline void addAssign( DenseVector<VT,TF>& lhs, const SVecSVecSubExpr& rhs )
401  {
403 
404  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
405 
406  addAssign( ~lhs, rhs.lhs_ );
407  subAssign( ~lhs, rhs.rhs_ );
408  }
410  //**********************************************************************************************
411 
412  //**Addition assignment to sparse vectors*******************************************************
413  // No special implementation for the addition assignment to sparse vectors.
414  //**********************************************************************************************
415 
416  //**Subtraction assignment to dense vectors*****************************************************
428  template< typename VT > // Type of the target dense vector
429  friend inline void subAssign( DenseVector<VT,TF>& lhs, const SVecSVecSubExpr& rhs )
430  {
432 
433  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
434 
435  subAssign( ~lhs, rhs.lhs_ );
436  addAssign( ~lhs, rhs.rhs_ );
437  }
439  //**********************************************************************************************
440 
441  //**Subtraction assignment to sparse vectors****************************************************
442  // No special implementation for the subtraction assignment to sparse vectors.
443  //**********************************************************************************************
444 
445  //**Multiplication assignment to dense vectors**************************************************
457  template< typename VT > // Type of the target dense vector
458  friend inline void multAssign( DenseVector<VT,TF>& lhs, const SVecSVecSubExpr& rhs )
459  {
461 
465 
466  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
467 
468  const ResultType tmp( rhs );
469  multAssign( ~lhs, tmp );
470  }
472  //**********************************************************************************************
473 
474  //**Multiplication assignment to sparse vectors*************************************************
475  // No special implementation for the multiplication assignment to sparse vectors.
476  //**********************************************************************************************
477 
478  //**Compile time checks*************************************************************************
485  //**********************************************************************************************
486 };
487 //*************************************************************************************************
488 
489 
490 
491 
492 //=================================================================================================
493 //
494 // GLOBAL BINARY ARITHMETIC OPERATORS
495 //
496 //=================================================================================================
497 
498 //*************************************************************************************************
522 template< typename T1 // Type of the left-hand side sparse vector
523  , typename T2 // Type of the right-hand side sparse vector
524  , bool TF > // Transpose flag
525 inline const SVecSVecSubExpr<T1,T2,TF>
527 {
529 
530  if( (~lhs).size() != (~rhs).size() )
531  throw std::invalid_argument( "Vector sizes do not match" );
532 
533  return SVecSVecSubExpr<T1,T2,TF>( ~lhs, ~rhs );
534 }
535 //*************************************************************************************************
536 
537 
538 
539 
540 //=================================================================================================
541 //
542 // EXPRESSION TRAIT SPECIALIZATIONS
543 //
544 //=================================================================================================
545 
546 //*************************************************************************************************
548 template< typename VT1, typename VT2, bool TF >
549 struct SubvectorExprTrait< SVecSVecSubExpr<VT1,VT2,TF> >
550 {
551  public:
552  //**********************************************************************************************
553  typedef typename SubExprTrait< typename SubvectorExprTrait<const VT1>::Type
554  , typename SubvectorExprTrait<const VT2>::Type >::Type Type;
555  //**********************************************************************************************
556 };
558 //*************************************************************************************************
559 
560 } // namespace blaze
561 
562 #endif
Header file for mathematical functions.
Compile time check whether the given type is a temporary vector or matrix type.This type trait class ...
Definition: IsTemporary.h:87
Header file for the subtraction trait.
Header file for the SparseVector base class.
RightOperand rhs_
Right-hand side sparse vector of the subtraction expression.
Definition: SVecSVecSubExpr.h:226
bool isDefault(const DynamicMatrix< Type, SO > &m)
Returns whether the given dense matrix is in default state.
Definition: DynamicMatrix.h:4555
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:196
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2375
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:248
bool canAlias(const T *alias) const
Returns whether the expression can alias with the given address alias.
Definition: SVecSVecSubExpr.h:206
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: SVecSVecSubExpr.h:120
Header file for the Computation base class.
VT2::ResultType RT2
Result type of the right-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:94
const SelectType< returnExpr, ExprReturnType, ElementType >::Type ReturnType
Return type for expression template evaluations.
Definition: SVecSVecSubExpr.h:124
Constraint on the data type.
bool isAliased(const T *alias) const
Returns whether the expression is aliased with the given address alias.
Definition: SVecSVecSubExpr.h:218
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:250
Compile time type selection.The SelectType class template selects one of the two given types T1 and T...
Definition: SelectType.h:59
Header file for the DisableIf class template.
Header file for the IsTemporary type trait class.
Expression object for sparse vector-sparse vector subtractions.The SVecSVecSubExpr class represents t...
Definition: Forward.h:116
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecSVecSubExpr.h:174
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2379
VT1::CompositeType CT1
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:97
RightOperand rightOperand() const
Returns the right-hand side sparse vector operand.
Definition: SVecSVecSubExpr.h:194
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:179
VT1::TransposeType TT1
Transpose type of the left-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:99
VT2::CompositeType CT2
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:98
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type...
Definition: SparseVector.h:79
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:78
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2372
Evaluation of the return type of a subtraction expression.Via this type trait it is possible to evalu...
Definition: SubExprTrait.h:103
LeftOperand leftOperand() const
Returns the left-hand side sparse vector operand.
Definition: SVecSVecSubExpr.h:184
void multAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the multiplication assignment of a matrix to a matrix.
Definition: Matrix.h:269
Header file for the VecVecSubExpr base class.
Header file for the SelectType class template.
Header file for all forward declarations for expression class templates.
Constraint on the data type.
SelectType< IsExpression< VT1 >::value, const VT1, const VT1 & >::Type LeftOperand
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:130
SubExprTrait< RN1, RN2 >::Type ExprReturnType
Expression return type for the subscript operator.
Definition: SVecSVecSubExpr.h:113
Header file for the EnableIf class template.
SelectType< IsExpression< VT2 >::value, const VT2, const VT2 & >::Type RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:133
SVecSVecSubExpr(const VT1 &lhs, const VT2 &rhs)
Constructor for the SVecSVecSubExpr class.
Definition: SVecSVecSubExpr.h:139
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecSVecSubExpr.h:127
LeftOperand lhs_
Left-hand side sparse vector of the subtraction expression.
Definition: SVecSVecSubExpr.h:225
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2374
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
Header file for run time assertion macros.
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:209
const DenseIterator< Type > operator-(const DenseIterator< Type > &it, ptrdiff_t inc)
Subtraction between a DenseIterator and an integral value.
Definition: DenseIterator.h:585
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:239
Header file for the isDefault shim.
VT1::ResultType RT1
Result type of the left-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:93
size_t size() const
Returns the current size/dimension of the vector.
Definition: SVecSVecSubExpr.h:164
Header file for the RemoveReference type trait.
Substitution Failure Is Not An Error (SFINAE) class.The DisableIf class template is an auxiliary tool...
Definition: DisableIf.h:184
VT1::ReturnType RN1
Return type of the left-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:95
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecSVecSubExpr.h:153
Header file for the IsComputation type trait class.
VT2::TransposeType TT2
Transpose type of the right-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:100
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
VT2::ReturnType RN2
Return type of the right-hand side sparse vector expression.
Definition: SVecSVecSubExpr.h:96
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:157
SVecSVecSubExpr< VT1, VT2, TF > This
Type of this SVecSVecSubExpr instance.
Definition: SVecSVecSubExpr.h:118
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2370
Header file for basic type definitions.
Header file for the SubvectorExprTrait class template.
Base template for the SubTrait class.
Definition: SubTrait.h:141
ResultType::ElementType ElementType
Resulting element type.
Definition: SVecSVecSubExpr.h:121
SubTrait< RT1, RT2 >::Type ResultType
Result type for expression template evaluations.
Definition: SVecSVecSubExpr.h:119
Header file for the IsResizable type trait.
Header file for the SubExprTrait class template.
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.In case the given data type T is not a dense or sparse vector type and in...
Definition: TransposeFlag.h:238
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the IsExpression type trait class.
Header file for the FunctionTrace class.