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 CT1 = CompositeType_t<VT1>; // Composite type of the left-hand side sparse vector expression
100  using CT2 = CompositeType_t<VT2>; // Composite type of the right-hand side sparse vector expression
101  using XT1 = RemoveReference_t<CT1>; // Auxiliary type for the left-hand side composite type
102  using XT2 = RemoveReference_t<CT2>; // Auxiliary type for the right-hand side composite type
103  using ET1 = ElementType_t<XT1>; // Element type of the left-hand side sparse vector expression
104  using ET2 = ElementType_t<XT2>; // Element type of the right-hand side sparse vector expression
105  using MultType = MultTrait_t<ET1,ET2>; // Multiplication result typen
106 
111 
112  if( (~lhs).size() != (~rhs).size() ) {
113  BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
114  }
115 
116  CT1 left ( ~lhs ); // Evaluation of the left-hand side sparse vector operand
117  CT2 right( ~rhs ); // Evaluation of the right-hand side sparse vector operand
118 
119  BLAZE_INTERNAL_ASSERT( left.size() == (~lhs).size(), "Invalid vector size" );
120  BLAZE_INTERNAL_ASSERT( right.size() == (~rhs).size(), "Invalid vector size" );
121 
122  MultType sp{};
123 
124  if( IsOpposedView_v<VT1> && IsOpposedView_v<VT2> )
125  {
126  if( left.size() == 0UL ) return sp;
127 
128  sp = left[0UL] * right[0UL];
129  for( size_t i=1UL; i<left.size(); ++i ) {
130  sp += left[i] * right[i];
131  }
132  }
133  else if( IsOpposedView_v<VT1> )
134  {
135  const auto rend( right.end() );
136  auto r( right.begin() );
137 
138  if( r == rend ) return sp;
139 
140  sp = left[r->index()] * r->value();
141  ++r;
142  for( ; r!=rend; ++r ) {
143  sp += left[r->index()] * r->value();
144  }
145  }
146  else if( IsOpposedView_v<VT2> )
147  {
148  const auto lend( left.end() );
149  auto l( left.begin() );
150 
151  if( l == lend ) return sp;
152 
153  sp = l->value() * right[l->index()];
154  ++l;
155  for( ; l!=lend; ++l ) {
156  sp += l->value() * right[l->index()];
157  }
158  }
159  else
160  {
161  const auto lend( left.end() );
162  const auto rend( right.end() );
163  auto l( left.begin() );
164  auto r( right.begin() );
165 
166  if( l == lend || r == rend ) return sp;
167 
168  while( true ) {
169  if( l->index() < r->index() ) {
170  ++l;
171  if( l == lend ) break;
172  }
173  else if( r->index() < l->index() ) {
174  ++r;
175  if( r == rend ) break;
176  }
177  else {
178  sp = l->value() * r->value();
179  ++l;
180  ++r;
181  break;
182  }
183  }
184 
185  if( l != lend && r != rend )
186  {
187  while( true ) {
188  if( l->index() < r->index() ) {
189  ++l;
190  if( l == lend ) break;
191  }
192  else if( r->index() < l->index() ) {
193  ++r;
194  if( r == rend ) break;
195  }
196  else {
197  sp += l->value() * r->value();
198  ++l;
199  if( l == lend ) break;
200  ++r;
201  if( r == rend ) break;
202  }
203  }
204  }
205  }
206 
207  return sp;
208 }
209 //*************************************************************************************************
210 
211 } // namespace blaze
212 
213 #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
typename RemoveReference< T >::Type RemoveReference_t
Auxiliary alias declaration for the RemoveReference type trait.The RemoveReference_t alias declaratio...
Definition: RemoveReference.h:95
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
Constraint on the transpose flag of vector types.
Header file for the multiplication trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
#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.
Header file for the exception macros of the math module.
Constraint on the data type.
typename MultTrait< T1, T2 >::Type MultTrait_t
Auxiliary alias declaration for the MultTrait class template.The MultTrait_t alias declaration provid...
Definition: MultTrait.h:240
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
#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
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Header file for the RemoveReference type trait.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:138
#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.