SVecSVecInnerExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECSVECINNEREXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECSVECINNEREXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Exception.h>
52 #include <blaze/util/Types.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // GLOBAL BINARY ARITHMETIC OPERATORS
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
92 template< typename VT1 // Type of the left-hand side sparse vector
93  , typename VT2 > // Type of the right-hand side sparse vector
94 inline decltype(auto)
95  operator*( const SparseVector<VT1,true>& lhs, const SparseVector<VT2,false>& rhs )
96 {
98 
99  using Lhs = CompositeType_<VT1>; // Composite type of the left-hand side sparse vector expression
100  using Rhs = CompositeType_<VT2>; // Composite type of the right-hand side sparse vector expression
101  using X1 = RemoveReference_<Lhs>; // Auxiliary type for the left-hand side composite type
102  using X2 = RemoveReference_<Rhs>; // Auxiliary type for the right-hand side composite type
103  using E1 = ElementType_<X1>; // Element type of the left-hand side sparse vector expression
104  using E2 = ElementType_<X2>; // Element type of the right-hand side sparse vector expression
105  using MultType = MultTrait_<E1,E2>; // Multiplication result type
106  using LeftIterator = ConstIterator_<X1>; // Iterator type of the left-hand sparse vector expression
107  using RightIterator = ConstIterator_<X2>; // Iterator type of the right-hand sparse vector expression
108 
113 
114  if( (~lhs).size() != (~rhs).size() ) {
115  BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
116  }
117 
118  Lhs left ( ~lhs ); // Evaluation of the left-hand side sparse vector operand
119  Rhs right( ~rhs ); // Evaluation of the right-hand side sparse vector operand
120 
121  BLAZE_INTERNAL_ASSERT( left.size() == (~lhs).size(), "Invalid vector size" );
122  BLAZE_INTERNAL_ASSERT( right.size() == (~rhs).size(), "Invalid vector size" );
123 
124  MultType sp{};
125 
127  {
128  if( left.size() == 0UL ) return sp;
129 
130  sp = left[0UL] * right[0UL];
131  for( size_t i=1UL; i<left.size(); ++i ) {
132  sp += left[i] * right[i];
133  }
134  }
135  else if( IsOpposedView<VT1>::value )
136  {
137  const RightIterator rend( right.end() );
138  RightIterator r( right.begin() );
139 
140  if( r == rend ) return sp;
141 
142  sp = left[r->index()] * r->value();
143  ++r;
144  for( ; r!=rend; ++r ) {
145  sp += left[r->index()] * r->value();
146  }
147  }
148  else if( IsOpposedView<VT2>::value )
149  {
150  const LeftIterator lend( left.end() );
151  LeftIterator l( left.begin() );
152 
153  if( l == lend ) return sp;
154 
155  sp = l->value() * right[l->index()];
156  ++l;
157  for( ; l!=lend; ++l ) {
158  sp += l->value() * right[l->index()];
159  }
160  }
161  else
162  {
163  const LeftIterator lend( left.end() );
164  const RightIterator rend( right.end() );
165  LeftIterator l( left.begin() );
166  RightIterator r( right.begin() );
167 
168  if( l == lend || r == rend ) return sp;
169 
170  while( true ) {
171  if( l->index() < r->index() ) {
172  ++l;
173  if( l == lend ) break;
174  }
175  else if( r->index() < l->index() ) {
176  ++r;
177  if( r == rend ) break;
178  }
179  else {
180  sp = l->value() * r->value();
181  ++l;
182  ++r;
183  break;
184  }
185  }
186 
187  if( l != lend && r != rend )
188  {
189  while( true ) {
190  if( l->index() < r->index() ) {
191  ++l;
192  if( l == lend ) break;
193  }
194  else if( r->index() < l->index() ) {
195  ++r;
196  if( r == rend ) break;
197  }
198  else {
199  sp += l->value() * r->value();
200  ++l;
201  if( l == lend ) break;
202  ++r;
203  if( r == rend ) break;
204  }
205  }
206  }
207  }
208 
209  return sp;
210 }
211 //*************************************************************************************************
212 
213 } // namespace blaze
214 
215 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for auxiliary alias declarations.
Header file for basic type definitions.
Header file for the SparseVector base class.
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a column dense or sparse vector type...
Definition: ColumnVector.h:61
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
Compile time check for resizable data types.This type trait tests whether the given data type is an o...
Definition: IsOpposedView.h:81
typename MultTrait< T1, T2 >::Type MultTrait_
Auxiliary alias declaration for the MultTrait class template.The MultTrait_ alias declaration provide...
Definition: MultTrait.h:291
Constraint on the transpose flag of vector types.
typename T::CompositeType CompositeType_
Alias declaration for nested CompositeType type definitions.The CompositeType_ alias declaration prov...
Definition: Aliases.h:83
Header file for the multiplication trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
#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:61
Header file for the IsOpposedView type trait.
typename RemoveReference< T >::Type RemoveReference_
Auxiliary alias declaration for the RemoveReference type trait.The RemoveReference_ alias declaration...
Definition: RemoveReference.h:95
Header file for the exception macros of the math module.
Constraint on the data type.
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:94
Header file for the RemoveReference type trait.
typename T::ConstIterator ConstIterator_
Alias declaration for nested ConstIterator type definitions.The ConstIterator_ alias declaration prov...
Definition: Aliases.h:103
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:130
#define BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a row dense or sparse vector type (i...
Definition: RowVector.h:61
#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
Constraint on the transpose flag of vector types.
Header file for the function trace functionality.