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>
31 #include <boost/type_traits/remove_reference.hpp>
37 #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  using boost::remove_reference;
84 
85  typedef typename T1::CompositeType Lhs; // Composite type of the left-hand side sparse vector expression
86  typedef typename T2::CompositeType Rhs; // Composite type of the right-hand side sparse vector expression
87  typedef typename remove_reference<Lhs>::type X1; // Auxiliary type for the left-hand side composite type
88  typedef typename remove_reference<Rhs>::type X2; // Auxiliary type for the right-hand side composite type
89  typedef typename X1::ElementType E1; // Element type of the left-hand side sparse vector expression
90  typedef typename X2::ElementType E2; // Element type of the right-hand side sparse vector expression
91  typedef typename MultTrait<E1,E2>::Type MultType; // Multiplication result type
92  typedef typename X1::ConstIterator LeftIterator; // Iterator type of the left-hand sparse vector expression
93  typedef typename X2::ConstIterator RightIterator; // Iterator type of the right-hand sparse vector expression
94 
99 
100  if( (~lhs).size() != (~rhs).size() )
101  throw std::invalid_argument( "Vector sizes do not match" );
102 
103  if( (~lhs).nonZeros() == 0UL || (~rhs).nonZeros() == 0UL ) return MultType();
104 
105  Lhs left ( ~lhs );
106  Rhs right( ~rhs );
107  const LeftIterator lend( left.end() );
108  const RightIterator rend( right.end() );
109  LeftIterator l( left.begin() );
110  RightIterator r( right.begin() );
111  MultType sp = MultType();
112 
113  for( ; l!=lend && r!=rend; ++l ) {
114  while( r->index() < l->index() && ++r != rend ) {}
115  if( r!=rend && l->index() == r->index() ) {
116  sp = l->value() * r->value();
117  ++r;
118  break;
119  }
120  }
121 
122  for( ; l!=lend && r!=rend; ++l ) {
123  while( r->index() < l->index() && ++r != rend ) {}
124  if( r!=rend && l->index() == r->index() ) {
125  sp += l->value() * r->value();
126  ++r;
127  }
128  }
129 
130  return sp;
131 }
132 //*************************************************************************************************
133 
134 } // namespace blaze
135 
136 #endif