All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DVecSVecMultExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_DVECSVECMULTEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_DVECSVECMULTEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <iterator>
31 #include <stdexcept>
45 #include <blaze/util/Assert.h>
47 #include <blaze/util/EnableIf.h>
49 #include <blaze/util/SelectType.h>
50 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // CLASS DVECSVECMULTEXPR
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
69 template< typename VT1 // Type of the left-hand side dense vector
70  , typename VT2 // Type of the right-hand side sparse vector
71  , bool TF > // Transpose flag
72 class DVecSVecMultExpr : public SparseVector< DVecSVecMultExpr<VT1,VT2,TF>, TF >
73  , private VecVecMultExpr
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  //**Evaluation strategy*************************************************************************
103 
110 
112 
113  template< typename VT >
114  struct UseAssign {
115  enum { value = useAssign };
116  };
118  //**********************************************************************************************
119 
120  public:
121  //**Type definitions****************************************************************************
124  typedef typename ResultType::TransposeType TransposeType;
125  typedef typename ResultType::ElementType ElementType;
126 
129 
132 
134  typedef typename SelectType< IsExpression<VT1>::value, const VT1, const VT1& >::Type LeftOperand;
135 
137  typedef typename SelectType< IsExpression<VT2>::value, const VT2, const VT2& >::Type RightOperand;
138  //**********************************************************************************************
139 
140  //**ConstIterator class definition**************************************************************
144  {
145  public:
146  //**Type definitions*************************************************************************
149 
152 
153  typedef std::forward_iterator_tag IteratorCategory;
154  typedef Element ValueType;
158 
159  // STL iterator requirements
165  //*******************************************************************************************
166 
167  //**Constructor******************************************************************************
170  inline ConstIterator( LeftOperand vec, IteratorType it )
171  : vec_( vec ) // Left-hand side dense vector expression
172  , it_ ( it ) // Iterator over the elements of the right-hand side sparse vector expression
173  {}
174  //*******************************************************************************************
175 
176  //**Prefix increment operator****************************************************************
182  ++it_;
183  return *this;
184  }
185  //*******************************************************************************************
186 
187  //**Element access operator******************************************************************
192  inline const Element operator*() const {
193  return Element( vec_[it_->index()] * it_->value(), it_->index() );
194  }
195  //*******************************************************************************************
196 
197  //**Element access operator******************************************************************
202  inline const ConstIterator* operator->() const {
203  return this;
204  }
205  //*******************************************************************************************
206 
207  //**Value function***************************************************************************
212  inline ReturnType value() const {
213  return vec_[it_->index()] * it_->value();
214  }
215  //*******************************************************************************************
216 
217  //**Index function***************************************************************************
222  inline size_t index() const {
223  return it_->index();
224  }
225  //*******************************************************************************************
226 
227  //**Equality operator************************************************************************
233  inline bool operator==( const ConstIterator& rhs ) const {
234  return it_ == rhs.it_;
235  }
236  //*******************************************************************************************
237 
238  //**Inequality operator**********************************************************************
244  inline bool operator!=( const ConstIterator& rhs ) const {
245  return it_ != rhs.it_;
246  }
247  //*******************************************************************************************
248 
249  //**Subtraction operator*********************************************************************
255  inline DifferenceType operator-( const ConstIterator& rhs ) const {
256  return it_ - rhs.it_;
257  }
258  //*******************************************************************************************
259 
260  private:
261  //**Member variables*************************************************************************
262  LeftOperand vec_;
264  //*******************************************************************************************
265  };
266  //**********************************************************************************************
267 
268  //**Constructor*********************************************************************************
274  explicit inline DVecSVecMultExpr( const VT1& lhs, const VT2& rhs )
275  : lhs_( lhs ) // Left-hand side dense vector of the multiplication expression
276  , rhs_( rhs ) // Right-hand side sparse vector of the multiplication expression
277  {
278  BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
279  }
280  //**********************************************************************************************
281 
282  //**Subscript operator**************************************************************************
288  inline ReturnType operator[]( size_t index ) const {
289  BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
290  return lhs_[index] * rhs_[index];
291  }
292  //**********************************************************************************************
293 
294  //**Begin function******************************************************************************
299  inline ConstIterator begin() const {
300  return ConstIterator( lhs_, rhs_.begin() );
301  }
302  //**********************************************************************************************
303 
304  //**End function********************************************************************************
309  inline ConstIterator end() const {
310  return ConstIterator( lhs_, rhs_.end() );
311  }
312  //**********************************************************************************************
313 
314  //**Size function*******************************************************************************
319  inline size_t size() const {
320  return lhs_.size();
321  }
322  //**********************************************************************************************
323 
324  //**NonZeros function***************************************************************************
329  inline size_t nonZeros() const {
330  return rhs_.nonZeros();
331  }
332  //**********************************************************************************************
333 
334  //**Left operand access*************************************************************************
339  inline LeftOperand leftOperand() const {
340  return lhs_;
341  }
342  //**********************************************************************************************
343 
344  //**Right operand access************************************************************************
349  inline RightOperand rightOperand() const {
350  return rhs_;
351  }
352  //**********************************************************************************************
353 
354  //**********************************************************************************************
360  template< typename T >
361  inline bool canAlias( const T* alias ) const {
362  return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
363  }
364  //**********************************************************************************************
365 
366  //**********************************************************************************************
372  template< typename T >
373  inline bool isAliased( const T* alias ) const {
374  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
375  }
376  //**********************************************************************************************
377 
378  private:
379  //**Member variables****************************************************************************
380  LeftOperand lhs_;
381  RightOperand rhs_;
382  //**********************************************************************************************
383 
384  //**Assignment to dense vectors*****************************************************************
398  template< typename VT > // Type of the target dense vector
399  friend inline typename EnableIf< UseAssign<VT> >::Type
400  assign( DenseVector<VT,TF>& lhs, const DVecSVecMultExpr& rhs )
401  {
403 
404  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
405 
407 
408  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side dense vector operand
409  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
410 
411  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
412  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
413  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
414 
415  for( ConstIterator element=y.begin(); element!=y.end(); ++element )
416  (~lhs)[element->index()] = x[element->index()] * element->value();
417  }
419  //**********************************************************************************************
420 
421  //**Assignment to sparse vectors****************************************************************
435  template< typename VT > // Type of the target sparse vector
436  friend inline typename EnableIf< UseAssign<VT> >::Type
437  assign( SparseVector<VT,TF>& lhs, const DVecSVecMultExpr& rhs )
438  {
440 
441  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
442 
443  typedef typename RemoveReference<CT2>::Type::ConstIterator ConstIterator;
444 
445  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side dense vector operand
446  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
447 
448  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
449  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
450  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
451 
452  for( ConstIterator element=y.begin(); element!=y.end(); ++element )
453  (~lhs).append( element->index(), x[element->index()] * element->value() );
454  }
456  //**********************************************************************************************
457 
458  //**Addition assignment to dense vectors********************************************************
472  template< typename VT > // Type of the target dense vector
473  friend inline typename EnableIf< UseAssign<VT> >::Type
474  addAssign( DenseVector<VT,TF>& lhs, const DVecSVecMultExpr& rhs )
475  {
477 
478  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
479 
480  typedef typename RemoveReference<CT2>::Type::ConstIterator ConstIterator;
481 
482  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side dense vector operand
483  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
484 
485  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
486  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
487  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
488 
489  for( ConstIterator element=y.begin(); element!=y.end(); ++element )
490  (~lhs)[element->index()] += x[element->index()] * element->value();
491  }
493  //**********************************************************************************************
494 
495  //**Addition assignment to sparse vectors*******************************************************
496  // No special implementation for the addition assignment to sparse vectors.
497  //**********************************************************************************************
498 
499  //**Subtraction assignment to dense vectors*****************************************************
513  template< typename VT > // Type of the target dense vector
514  friend inline typename EnableIf< UseAssign<VT> >::Type
515  subAssign( DenseVector<VT,TF>& lhs, const DVecSVecMultExpr& rhs )
516  {
518 
519  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
520 
521  typedef typename RemoveReference<CT2>::Type::ConstIterator ConstIterator;
522 
523  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side dense vector operand
524  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
525 
526  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
527  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
528  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
529 
530  for( ConstIterator element=y.begin(); element!=y.end(); ++element )
531  (~lhs)[element->index()] -= x[element->index()] * element->value();
532  }
534  //**********************************************************************************************
535 
536  //**Subtraction assignment to sparse vectors****************************************************
537  // No special implementation for the subtraction assignment to sparse vectors.
538  //**********************************************************************************************
539 
540  //**Multiplication assignment to dense vectors**************************************************
554  template< typename VT > // Type of the target dense vector
555  friend inline typename EnableIf< UseAssign<VT> >::Type
556  multAssign( DenseVector<VT,TF>& lhs, const DVecSVecMultExpr& rhs )
557  {
559 
560  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
561 
562  typedef typename RemoveReference<CT2>::Type::ConstIterator ConstIterator;
563 
564  CT1 x( rhs.lhs_ ); // Evaluation of the left-hand side dense vector operand
565  CT2 y( rhs.rhs_ ); // Evaluation of the right-hand side sparse vector operand
566 
567  BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
568  BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
569  BLAZE_INTERNAL_ASSERT( x.size() == (~lhs).size() , "Invalid vector size" );
570 
571  const ConstIterator end( y.end() );
572  ConstIterator begin( y.begin() );
573  size_t i( 0UL );
574 
575  for( ; begin!=end; ++begin ) {
576  const size_t index( begin->index() );
577  for( ; i<index; ++i )
578  reset( (~lhs)[i] );
579  (~lhs)[index] *= x[index] * begin->value();
580  ++i;
581  }
582 
583  for( ; i<(~lhs).size(); ++i )
584  reset( (~lhs)[i] );
585  }
587  //**********************************************************************************************
588 
589  //**Multiplication assignment to sparse vectors*************************************************
590  // No special implementation for the multiplication assignment to sparse vectors.
591  //**********************************************************************************************
592 
593  //**Compile time checks*************************************************************************
600  //**********************************************************************************************
601 };
602 //*************************************************************************************************
603 
604 
605 
606 
607 //=================================================================================================
608 //
609 // GLOBAL BINARY ARITHMETIC OPERATORS
610 //
611 //=================================================================================================
612 
613 //*************************************************************************************************
640 template< typename T1 // Type of the left-hand side dense vector
641  , typename T2 // Type of the right-hand side sparse vector
642  , bool TF > // Transpose flag
643 inline const DVecSVecMultExpr<T1,T2,TF>
645 {
647 
648  if( (~lhs).size() != (~rhs).size() )
649  throw std::invalid_argument( "Vector sizes do not match" );
650 
651  return DVecSVecMultExpr<T1,T2,TF>( ~lhs, ~rhs );
652 }
653 //*************************************************************************************************
654 
655 } // namespace blaze
656 
657 #endif