All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVecSVecAddExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECSVECADDEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECSVECADDEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
50 #include <blaze/math/Functions.h>
60 #include <blaze/util/Assert.h>
62 #include <blaze/util/DisableIf.h>
63 #include <blaze/util/EnableIf.h>
65 #include <blaze/util/SelectType.h>
66 #include <blaze/util/Types.h>
68 
69 
70 namespace blaze {
71 
72 //=================================================================================================
73 //
74 // CLASS SVECSVECADDEXPR
75 //
76 //=================================================================================================
77 
78 //*************************************************************************************************
85 template< typename VT1 // Type of the left-hand side sparse vector
86  , typename VT2 // Type of the right-hand side sparse vector
87  , bool TF > // Transpose flag
88 class SVecSVecAddExpr : public SparseVector< SVecSVecAddExpr<VT1,VT2,TF>, TF >
89  , private VecVecAddExpr
90  , private Computation
91 {
92  private:
93  //**Type definitions****************************************************************************
94  typedef typename VT1::ResultType RT1;
95  typedef typename VT2::ResultType RT2;
96  typedef typename VT1::ReturnType RN1;
97  typedef typename VT2::ReturnType RN2;
98  typedef typename VT1::CompositeType CT1;
99  typedef typename VT2::CompositeType CT2;
100  typedef typename VT1::TransposeType TT1;
101  typedef typename VT2::TransposeType TT2;
102  //**********************************************************************************************
103 
104  //**Return type evaluation**********************************************************************
106 
111  enum { returnExpr = !IsTemporary<RN1>::value && !IsTemporary<RN2>::value };
112 
115  //**********************************************************************************************
116 
117  public:
118  //**Type definitions****************************************************************************
123 
126 
128  typedef const ResultType CompositeType;
129 
131  typedef typename SelectType< IsExpression<VT1>::value, const VT1, const VT1& >::Type LeftOperand;
132 
134  typedef typename SelectType< IsExpression<VT2>::value, const VT2, const VT2& >::Type RightOperand;
135  //**********************************************************************************************
136 
137  //**Compilation flags***************************************************************************
139  enum { smpAssignable = 0 };
140  //**********************************************************************************************
141 
142  //**Constructor*********************************************************************************
145  explicit inline SVecSVecAddExpr( const VT1& lhs, const VT2& rhs )
146  : lhs_( lhs ) // Left-hand side sparse vector of the addition expression
147  , rhs_( rhs ) // Right-hand side sparse vector of the addition expression
148  {
149  BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
150  }
151  //**********************************************************************************************
152 
153  //**Subscript operator**************************************************************************
159  inline ReturnType operator[]( size_t index ) const {
160  BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
161  return lhs_[index] + rhs_[index];
162  }
163  //**********************************************************************************************
164 
165  //**Size function*******************************************************************************
170  inline size_t size() const {
171  return lhs_.size();
172  }
173  //**********************************************************************************************
174 
175  //**NonZeros function***************************************************************************
180  inline size_t nonZeros() const {
181  return min( lhs_.size(), lhs_.nonZeros() + rhs_.nonZeros() );
182  }
183  //**********************************************************************************************
184 
185  //**Left operand access*************************************************************************
190  inline LeftOperand leftOperand() const {
191  return lhs_;
192  }
193  //**********************************************************************************************
194 
195  //**Right operand access************************************************************************
200  inline RightOperand rightOperand() const {
201  return rhs_;
202  }
203  //**********************************************************************************************
204 
205  //**********************************************************************************************
211  template< typename T >
212  inline bool canAlias( const T* alias ) const {
213  return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
214  }
215  //**********************************************************************************************
216 
217  //**********************************************************************************************
223  template< typename T >
224  inline bool isAliased( const T* alias ) const {
225  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
226  }
227  //**********************************************************************************************
228 
229  private:
230  //**Member variables****************************************************************************
233  //**********************************************************************************************
234 
235  //**Default assignment to dense vectors*********************************************************
248  template< typename VT > // Type of the target dense vector
249  friend inline typename EnableIf< IsResizable<typename VT::ElementType> >::Type
250  assign( DenseVector<VT,TF>& lhs, const SVecSVecAddExpr& rhs )
251  {
253 
254  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
255 
256  typedef typename RemoveReference<CT1>::Type::ConstIterator LeftIterator;
257  typedef typename RemoveReference<CT2>::Type::ConstIterator RightIterator;
258 
259  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side sparse vector operand
260  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
261 
262  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
263  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
264  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
265 
266  const LeftIterator lend( x.end() );
267  const RightIterator rend( y.end() );
268 
269  for( LeftIterator l=x.begin(); l!=lend; ++l ) {
270  (~lhs)[l->index()] = l->value();
271  }
272 
273  for( RightIterator r=y.begin(); r!=rend; ++r ) {
274  if( isDefault( (~lhs)[r->index()] ) )
275  (~lhs)[r->index()] = r->value();
276  else
277  (~lhs)[r->index()] += r->value();
278  }
279  }
281  //**********************************************************************************************
282 
283  //**Optimized assignment to dense vectors*******************************************************
296  template< typename VT > // Type of the target dense vector
297  friend inline typename DisableIf< IsResizable<typename VT::ElementType> >::Type
298  assign( DenseVector<VT,TF>& lhs, const SVecSVecAddExpr& rhs )
299  {
301 
302  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
303 
304  typedef typename RemoveReference<CT1>::Type::ConstIterator LeftIterator;
305  typedef typename RemoveReference<CT2>::Type::ConstIterator RightIterator;
306 
307  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side sparse vector operand
308  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
309 
310  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
311  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
312  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
313 
314  const LeftIterator lend( x.end() );
315  const RightIterator rend( y.end() );
316 
317  for( LeftIterator l=x.begin(); l!=lend; ++l ) {
318  (~lhs)[l->index()] = l->value();
319  }
320 
321  for( RightIterator r=y.begin(); r!=rend; ++r ) {
322  (~lhs)[r->index()] += r->value();
323  }
324  }
326  //**********************************************************************************************
327 
328  //**Assignment to sparse vectors****************************************************************
340  template< typename VT > // Type of the target sparse vector
341  friend inline void assign( SparseVector<VT,TF>& lhs, const SVecSVecAddExpr& rhs )
342  {
344 
345  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
346 
347  typedef typename RemoveReference<CT1>::Type::ConstIterator LeftIterator;
348  typedef typename RemoveReference<CT2>::Type::ConstIterator RightIterator;
349 
350  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side sparse vector operand
351  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
352 
353  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
354  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
355  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
356 
357  const LeftIterator lend( x.end() );
358  const RightIterator rend( y.end() );
359 
360  LeftIterator l( x.begin() );
361  RightIterator r( y.begin() );
362 
363  while( l != lend && r != rend )
364  {
365  if( l->index() < r->index() ) {
366  (~lhs).append( l->index(), l->value() );
367  ++l;
368  }
369  else if( l->index() > r->index() ) {
370  (~lhs).append( r->index(), r->value() );
371  ++r;
372  }
373  else {
374  (~lhs).append( l->index(), l->value() + r->value() );
375  ++l;
376  ++r;
377  }
378  }
379 
380  while( l != lend ) {
381  (~lhs).append( l->index(), l->value() );
382  ++l;
383  }
384 
385  while( r != rend ) {
386  (~lhs).append( r->index(), r->value() );
387  ++r;
388  }
389  }
391  //**********************************************************************************************
392 
393  //**Addition assignment to dense vectors********************************************************
405  template< typename VT > // Type of the target dense vector
406  friend inline void addAssign( DenseVector<VT,TF>& lhs, const SVecSVecAddExpr& rhs )
407  {
409 
410  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
411 
412  smpAddAssign( ~lhs, rhs.lhs_ );
413  smpAddAssign( ~lhs, rhs.rhs_ );
414  }
416  //**********************************************************************************************
417 
418  //**Addition assignment to sparse vectors*******************************************************
419  // No special implementation for the addition assignment to sparse vectors.
420  //**********************************************************************************************
421 
422  //**Subtraction assignment to dense vectors*****************************************************
434  template< typename VT > // Type of the target dense vector
435  friend inline void subAssign( DenseVector<VT,TF>& lhs, const SVecSVecAddExpr& rhs )
436  {
438 
439  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
440 
441  smpSubAssign( ~lhs, rhs.lhs_ );
442  smpSubAssign( ~lhs, rhs.rhs_ );
443  }
445  //**********************************************************************************************
446 
447  //**Subtraction assignment to sparse vectors****************************************************
448  // No special implementation for the subtraction assignment to sparse vectors.
449  //**********************************************************************************************
450 
451  //**Multiplication assignment to dense vectors**************************************************
463  template< typename VT > // Type of the target dense vector
464  friend inline void multAssign( DenseVector<VT,TF>& lhs, const SVecSVecAddExpr& rhs )
465  {
467 
471 
472  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
473 
474  const ResultType tmp( rhs );
475  smpMultAssign( ~lhs, tmp );
476  }
478  //**********************************************************************************************
479 
480  //**Multiplication assignment to sparse vectors*************************************************
481  // No special implementation for the multiplication assignment to sparse vectors.
482  //**********************************************************************************************
483 
484  //**Compile time checks*************************************************************************
491  //**********************************************************************************************
492 };
493 //*************************************************************************************************
494 
495 
496 
497 
498 //=================================================================================================
499 //
500 // GLOBAL BINARY ARITHMETIC OPERATORS
501 //
502 //=================================================================================================
503 
504 //*************************************************************************************************
528 template< typename T1 // Type of the left-hand side sparse vector
529  , typename T2 // Type of the right-hand side sparse vector
530  , bool TF > // Transpose flag
531 inline const SVecSVecAddExpr<T1,T2,TF>
533 {
535 
536  if( (~lhs).size() != (~rhs).size() )
537  throw std::invalid_argument( "Vector sizes do not match" );
538 
539  return SVecSVecAddExpr<T1,T2,TF>( ~lhs, ~rhs );
540 }
541 //*************************************************************************************************
542 
543 
544 
545 
546 //=================================================================================================
547 //
548 // EXPRESSION TRAIT SPECIALIZATIONS
549 //
550 //=================================================================================================
551 
552 //*************************************************************************************************
554 template< typename VT1, typename VT2, bool TF, bool AF >
555 struct SubvectorExprTrait< SVecSVecAddExpr<VT1,VT2,TF>, AF >
556 {
557  public:
558  //**********************************************************************************************
559  typedef typename AddExprTrait< typename SubvectorExprTrait<const VT1,AF>::Type
560  , typename SubvectorExprTrait<const VT2,AF>::Type >::Type Type;
561  //**********************************************************************************************
562 };
564 //*************************************************************************************************
565 
566 } // namespace blaze
567 
568 #endif
Header file for mathematical functions.
Evaluation of the return type of an addition expression.Via this type trait it is possible to evaluat...
Definition: AddExprTrait.h:103
Compile time check whether the given type is a temporary vector or matrix type.This type trait class ...
Definition: IsTemporary.h:87
VT2::ResultType RT2
Result type of the right-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:95
Header file for the SparseVector base class.
void smpSubAssign(DenseMatrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP subtraction assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:151
bool isDefault(const DynamicMatrix< Type, SO > &m)
Returns whether the given dense matrix is in default state.
Definition: DynamicMatrix.h:4622
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:197
void smpMultAssign(DenseVector< VT1, TF1 > &lhs, const Vector< VT2, TF2 > &rhs)
Default implementation of the SMP multiplication assignment of a vector to a dense vector...
Definition: DenseVector.h:178
LeftOperand leftOperand() const
Returns the left-hand side sparse vector operand.
Definition: SVecSVecAddExpr.h:190
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2384
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:249
VT1::ReturnType RN1
Return type of the left-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:96
AddExprTrait< RN1, RN2 >::Type ExprReturnType
Expression return type for the subscript operator.
Definition: SVecSVecAddExpr.h:114
Header file for the AddExprTrait class template.
Header file for the Computation base class.
bool isAliased(const T *alias) const
Returns whether the expression is aliased with the given address alias.
Definition: SVecSVecAddExpr.h:224
RightOperand rhs_
Right-hand side sparse vector of the addition expression.
Definition: SVecSVecAddExpr.h:232
Constraint on the data type.
Expression object for sparse vector-sparse vector additions.The SVecSVecAddExpr class represents the ...
Definition: Forward.h:113
void smpAddAssign(DenseMatrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the SMP addition assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:121
SVecSVecAddExpr< VT1, VT2, TF > This
Type of this SVecSVecAddExpr instance.
Definition: SVecSVecAddExpr.h:119
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:251
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.
Header file for the dense vector SMP implementation.
RightOperand rightOperand() const
Returns the right-hand side sparse vector operand.
Definition: SVecSVecAddExpr.h:200
Header file for the VecVecAddExpr base class.
VT2::TransposeType TT2
Transpose type of the right-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:101
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2388
bool canAlias(const T *alias) const
Returns whether the expression can alias with the given address alias.
Definition: SVecSVecAddExpr.h:212
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecSVecAddExpr.h:180
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
const DenseIterator< Type > operator+(const DenseIterator< Type > &it, ptrdiff_t inc)
Addition between a DenseIterator and an integral value.
Definition: DenseIterator.h:556
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:2381
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 SelectType class template.
Header file for all forward declarations for expression class templates.
Constraint on the data type.
Header file for the EnableIf class template.
VT1::CompositeType CT1
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:98
VT1::ResultType RT1
Result type of the left-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:94
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2383
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecSVecAddExpr.h:128
Header file for run time assertion macros.
Base template for the AddTrait class.
Definition: AddTrait.h:141
Header file for the addition trait.
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: SVecSVecAddExpr.h:121
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
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
VT2::ReturnType RN2
Return type of the right-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:97
VT2::CompositeType CT2
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:99
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
size_t size() const
Returns the current size/dimension of the vector.
Definition: SVecSVecAddExpr.h:170
Header file for the isDefault shim.
SVecSVecAddExpr(const VT1 &lhs, const VT2 &rhs)
Constructor for the SVecSVecAddExpr class.
Definition: SVecSVecAddExpr.h:145
VT1::TransposeType TT1
Transpose type of the left-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:100
SelectType< IsExpression< VT2 >::value, const VT2, const VT2 & >::Type RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:134
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
ResultType::ElementType ElementType
Resulting element type.
Definition: SVecSVecAddExpr.h:122
const SelectType< returnExpr, ExprReturnType, ElementType >::Type ReturnType
Return type for expression template evaluations.
Definition: SVecSVecAddExpr.h:125
AddTrait< RT1, RT2 >::Type ResultType
Result type for expression template evaluations.
Definition: SVecSVecAddExpr.h:120
Header file for the IsComputation type trait class.
SelectType< IsExpression< VT1 >::value, const VT1, const VT1 & >::Type LeftOperand
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecAddExpr.h:131
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
#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
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2379
Header file for basic type definitions.
Header file for the SubvectorExprTrait class template.
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecSVecAddExpr.h:159
LeftOperand lhs_
Left-hand side sparse vector of the addition expression.
Definition: SVecSVecAddExpr.h:231
Header file for the IsResizable type trait.
#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.