All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TSVecSVecMultExpr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_EXPRESSIONS_TSVECSVECMULTEXPR_H_
23 #define _BLAZE_MATH_EXPRESSIONS_TSVECSVECMULTEXPR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <stdexcept>
36 #include <blaze/util/Types.h>
38 
39 
40 namespace blaze {
41 
42 //=================================================================================================
43 //
44 // GLOBAL BINARY ARITHMETIC OPERATORS
45 //
46 //=================================================================================================
47 
48 //*************************************************************************************************
76 template< typename T1 // Type of the left-hand side sparse vector
77  , typename T2 > // Type of the right-hand side sparse vector
78 inline const typename MultTrait<typename T1::ElementType,typename T2::ElementType>::Type
80 {
82 
83  typedef typename T1::CompositeType Lhs; // Composite type of the left-hand side sparse vector expression
84  typedef typename T2::CompositeType Rhs; // Composite type of the right-hand side sparse vector expression
85  typedef typename RemoveReference<Lhs>::Type X1; // Auxiliary type for the left-hand side composite type
86  typedef typename RemoveReference<Rhs>::Type X2; // Auxiliary type for the right-hand side composite type
87  typedef typename X1::ElementType E1; // Element type of the left-hand side sparse vector expression
88  typedef typename X2::ElementType E2; // Element type of the right-hand side sparse vector expression
89  typedef typename MultTrait<E1,E2>::Type MultType; // Multiplication result type
90  typedef typename X1::ConstIterator LeftIterator; // Iterator type of the left-hand sparse vector expression
91  typedef typename X2::ConstIterator RightIterator; // Iterator type of the right-hand sparse vector expression
92 
97 
98  if( (~lhs).size() != (~rhs).size() )
99  throw std::invalid_argument( "Vector sizes do not match" );
100 
101  if( (~lhs).nonZeros() == 0UL || (~rhs).nonZeros() == 0UL ) return MultType();
102 
103  Lhs left ( ~lhs );
104  Rhs right( ~rhs );
105  const LeftIterator lend( left.end() );
106  const RightIterator rend( right.end() );
107  LeftIterator l( left.begin() );
108  RightIterator r( right.begin() );
109  MultType sp = MultType();
110 
111  for( ; l!=lend && r!=rend; ++l ) {
112  while( r->index() < l->index() && ++r != rend ) {}
113  if( r!=rend && l->index() == r->index() ) {
114  sp = l->value() * r->value();
115  ++r;
116  break;
117  }
118  }
119 
120  for( ; l!=lend && r!=rend; ++l ) {
121  while( r->index() < l->index() && ++r != rend ) {}
122  if( r!=rend && l->index() == r->index() ) {
123  sp += l->value() * r->value();
124  ++r;
125  }
126  }
127 
128  return sp;
129 }
130 //*************************************************************************************************
131 
132 } // namespace blaze
133 
134 #endif