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>
36 #include <blaze/util/Types.h>
37 
38 
39 namespace blaze {
40 
41 //=================================================================================================
42 //
43 // GLOBAL BINARY ARITHMETIC OPERATORS
44 //
45 //=================================================================================================
46 
47 //*************************************************************************************************
75 template< typename T1 // Type of the left-hand side sparse vector
76  , typename T2 > // Type of the right-hand side sparse vector
77 inline const typename MultTrait<typename T1::ElementType,typename T2::ElementType>::Type
79 {
80  using boost::remove_reference;
81 
82  typedef typename T1::CompositeType Lhs; // Composite type of the left-hand side sparse vector expression
83  typedef typename T2::CompositeType Rhs; // Composite type of the right-hand side sparse vector expression
84  typedef typename remove_reference<Lhs>::type X1; // Auxiliary type for the left-hand side composite type
85  typedef typename remove_reference<Rhs>::type X2; // Auxiliary type for the right-hand side composite type
86  typedef typename X1::ElementType E1; // Element type of the left-hand side sparse vector expression
87  typedef typename X2::ElementType E2; // Element type of the right-hand side sparse vector expression
88  typedef typename MultTrait<E1,E2>::Type MultType; // Multiplication result type
89  typedef typename X1::ConstIterator LeftIterator; // Iterator type of the left-hand sparse vector expression
90  typedef typename X2::ConstIterator RightIterator; // Iterator type of the right-hand sparse vector expression
91 
96 
97  if( (~lhs).size() != (~rhs).size() )
98  throw std::invalid_argument( "Vector sizes do not match" );
99 
100  if( (~lhs).nonZeros() == 0UL || (~rhs).nonZeros() == 0UL ) return MultType();
101 
102  Lhs left ( ~lhs );
103  Rhs right( ~rhs );
104  const LeftIterator lend( left.end() );
105  const RightIterator rend( right.end() );
106  LeftIterator l( left.begin() );
107  RightIterator r( right.begin() );
108  MultType sp = MultType();
109 
110  for( ; l!=lend && r!=rend; ++l ) {
111  while( r->index() < l->index() && ++r != rend ) {}
112  if( r!=rend && l->index() == r->index() ) {
113  sp = l->value() * r->value();
114  ++r;
115  break;
116  }
117  }
118 
119  for( ; l!=lend && r!=rend; ++l ) {
120  while( r->index() < l->index() && ++r != rend ) {}
121  if( r!=rend && l->index() == r->index() ) {
122  sp += l->value() * r->value();
123  ++r;
124  }
125  }
126 
127  return sp;
128 }
129 //*************************************************************************************************
130 
131 } // namespace blaze
132 
133 #endif