Blaze 3.9
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>
53#include <blaze/util/Types.h>
55
56
57namespace blaze {
58
59//=================================================================================================
60//
61// GLOBAL BINARY ARITHMETIC OPERATORS
62//
63//=================================================================================================
64
65//*************************************************************************************************
93template< typename VT1 // Type of the left-hand side sparse vector
94 , typename VT2 > // Type of the right-hand side sparse vector
95inline decltype(auto)
96 operator*( const SparseVector<VT1,true>& lhs, const SparseVector<VT2,false>& rhs )
97{
99
100 using CT1 = CompositeType_t<VT1>; // Composite type of the left-hand side sparse vector expression
101 using CT2 = CompositeType_t<VT2>; // Composite type of the right-hand side sparse vector expression
102 using XT1 = RemoveReference_t<CT1>; // Auxiliary type for the left-hand side composite type
103 using XT2 = RemoveReference_t<CT2>; // Auxiliary type for the right-hand side composite type
104 using ET1 = ElementType_t<XT1>; // Element type of the left-hand side sparse vector expression
105 using ET2 = ElementType_t<XT2>; // Element type of the right-hand side sparse vector expression
106 using MultType = MultTrait_t<ET1,ET2>; // Multiplication result typen
107
112
113 if( (*lhs).size() != (*rhs).size() ) {
114 BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
115 }
116
117 CT1 left ( *lhs ); // Evaluation of the left-hand side sparse vector operand
118 CT2 right( *rhs ); // Evaluation of the right-hand side sparse vector operand
119
120 BLAZE_INTERNAL_ASSERT( left.size() == (*lhs).size(), "Invalid vector size" );
121 BLAZE_INTERNAL_ASSERT( right.size() == (*rhs).size(), "Invalid vector size" );
122
123 MultType sp{};
124
125 if( IsOpposedView_v<VT1> && IsOpposedView_v<VT2> )
126 {
127 if( left.size() == 0UL ) return sp;
128
129 sp = left[0UL] * right[0UL];
130 for( size_t i=1UL; i<left.size(); ++i ) {
131 sp += left[i] * right[i];
132 }
133 }
134 else if( IsOpposedView_v<VT1> )
135 {
136 const auto rend( right.end() );
137 auto r( right.begin() );
138
139 if( r == rend ) return sp;
140
141 sp = left[r->index()] * r->value();
142 ++r;
143 for( ; r!=rend; ++r ) {
144 sp += left[r->index()] * r->value();
145 }
146 }
147 else if( IsOpposedView_v<VT2> )
148 {
149 const auto lend( left.end() );
150 auto l( left.begin() );
151
152 if( l == lend ) return sp;
153
154 sp = l->value() * right[l->index()];
155 ++l;
156 for( ; l!=lend; ++l ) {
157 sp += l->value() * right[l->index()];
158 }
159 }
160 else
161 {
162 const auto lend( left.end() );
163 const auto rend( right.end() );
164 auto l( left.begin() );
165 auto r( right.begin() );
166
167 if( l == lend || r == rend ) return sp;
168
169 while( true ) {
170 if( l->index() < r->index() ) {
171 ++l;
172 if( l == lend ) break;
173 }
174 else if( r->index() < l->index() ) {
175 ++r;
176 if( r == rend ) break;
177 }
178 else {
179 sp = l->value() * r->value();
180 ++l;
181 ++r;
182 break;
183 }
184 }
185
186 if( l != lend && r != rend )
187 {
188 while( true ) {
189 if( l->index() < r->index() ) {
190 ++l;
191 if( l == lend ) break;
192 }
193 else if( r->index() < l->index() ) {
194 ++r;
195 if( r == rend ) break;
196 }
197 else {
198 sp += l->value() * r->value();
199 ++l;
200 if( l == lend ) break;
201 ++r;
202 if( r == rend ) break;
203 }
204 }
205 }
206 }
207
208 return sp;
209}
210//*************************************************************************************************
211
212} // namespace blaze
213
214#endif
Header file for auxiliary alias declarations.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.
Definition: Aliases.h:110
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition: Aliases.h:190
Constraint on the transpose flag of vector types.
Header file for the function trace functionality.
Header file for the IsOpposedView type trait.
Deactivation of problematic macros.
Header file for the multiplication trait.
Header file for the RemoveReference type trait.
Constraint on the transpose flag of vector types.
Base class for sparse vectors.
Definition: SparseVector.h:72
Constraint on the data type.
Header file for the SparseVector base class.
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: SparseVector.h:61
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE(T)
Constraint on the data type.
Definition: ColumnVector.h:61
#define BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE(T)
Constraint on the data type.
Definition: RowVector.h:61
typename MultTrait< T1, T2 >::Type MultTrait_t
Auxiliary alias declaration for the MultTrait class template.
Definition: MultTrait.h:165
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
typename RemoveReference< T >::Type RemoveReference_t
Auxiliary alias declaration for the RemoveReference type trait.
Definition: RemoveReference.h:95
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition: Exception.h:235
#define BLAZE_FUNCTION_TRACE
Function trace macro.
Definition: FunctionTrace.h:94
Header file for the exception macros of the math module.
Header file for basic type definitions.