Blaze 3.9
SVecSVecMultExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_SVECSVECMULTEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_SVECSVECMULTEXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <utility>
44#include <blaze/math/Aliases.h>
62#include <blaze/util/Assert.h>
63#include <blaze/util/EnableIf.h>
66#include <blaze/util/mpl/If.h>
67#include <blaze/util/Types.h>
68
69
70namespace blaze {
71
72//=================================================================================================
73//
74// CLASS SVECSVECMULTEXPR
75//
76//=================================================================================================
77
78//*************************************************************************************************
85template< typename VT1 // Type of the left-hand side sparse vector
86 , typename VT2 // Type of the right-hand side sparse vector
87 , bool TF > // Transpose flag
89 : public VecVecMultExpr< SparseVector< SVecSVecMultExpr<VT1,VT2,TF>, TF > >
90 , private Computation
91{
92 private:
93 //**Type definitions****************************************************************************
100 //**********************************************************************************************
101
102 //**Return type evaluation**********************************************************************
104
109 static constexpr bool returnExpr = ( !IsTemporary_v<RN1> && !IsTemporary_v<RN2> );
110
112 using ExprReturnType = decltype( std::declval<RN1>() * std::declval<RN2>() );
113 //**********************************************************************************************
114
115 public:
116 //**Type definitions****************************************************************************
119
122
126
129
132
134 using LeftOperand = If_t< IsExpression_v<VT1>, const VT1, const VT1& >;
135
137 using RightOperand = If_t< IsExpression_v<VT2>, const VT2, const VT2& >;
138 //**********************************************************************************************
139
140 //**Compilation flags***************************************************************************
142 static constexpr bool smpAssignable = false;
143 //**********************************************************************************************
144
145 //**Constructor*********************************************************************************
148 inline SVecSVecMultExpr( const VT1& lhs, const VT2& rhs ) noexcept
149 : lhs_( lhs ) // Left-hand side sparse vector of the multiplication expression
150 , rhs_( rhs ) // Right-hand side sparse vector of the multiplication expression
151 {
152 BLAZE_INTERNAL_ASSERT( lhs.size() == rhs.size(), "Invalid vector sizes" );
153 }
154 //**********************************************************************************************
155
156 //**Subscript operator**************************************************************************
162 inline ReturnType operator[]( size_t index ) const {
163 BLAZE_INTERNAL_ASSERT( index < lhs_.size(), "Invalid vector access index" );
164 return lhs_[index] * rhs_[index];
165 }
166 //**********************************************************************************************
167
168 //**At function*********************************************************************************
175 inline ReturnType at( size_t index ) const {
176 if( index >= lhs_.size() ) {
177 BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
178 }
179 return (*this)[index];
180 }
181 //**********************************************************************************************
182
183 //**Size function*******************************************************************************
188 inline size_t size() const noexcept {
189 return lhs_.size();
190 }
191 //**********************************************************************************************
192
193 //**NonZeros function***************************************************************************
198 inline size_t nonZeros() const {
199 return min( lhs_.nonZeros(), rhs_.nonZeros() );
200 }
201 //**********************************************************************************************
202
203 //**Left operand access*************************************************************************
208 inline LeftOperand leftOperand() const noexcept {
209 return lhs_;
210 }
211 //**********************************************************************************************
212
213 //**Right operand access************************************************************************
218 inline RightOperand rightOperand() const noexcept {
219 return rhs_;
220 }
221 //**********************************************************************************************
222
223 //**********************************************************************************************
229 template< typename T >
230 inline bool canAlias( const T* alias ) const noexcept {
231 return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
232 }
233 //**********************************************************************************************
234
235 //**********************************************************************************************
241 template< typename T >
242 inline bool isAliased( const T* alias ) const noexcept {
243 return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
244 }
245 //**********************************************************************************************
246
247 private:
248 //**Member variables****************************************************************************
251 //**********************************************************************************************
252
253 //**Assignment to dense vectors*****************************************************************
265 template< typename VT > // Type of the target dense vector
266 friend inline void assign( DenseVector<VT,TF>& lhs, const SVecSVecMultExpr& rhs )
267 {
269
270 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
271
272 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
273 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
274
275 BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
276 BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
277 BLAZE_INTERNAL_ASSERT( x.size() == (*lhs).size() , "Invalid vector size" );
278
279 const auto lend( x.end() );
280 const auto rend( y.end() );
281
282 auto l( x.begin() );
283 auto r( y.begin() );
284
285 for( ; l!=lend; ++l ) {
286 while( r!=rend && r->index() < l->index() ) ++r;
287 if( r==rend ) break;
288 if( l->index() == r->index() ) {
289 (*lhs)[l->index()] = l->value() * r->value();
290 ++r;
291 }
292 }
293 }
295 //**********************************************************************************************
296
297 //**Assignment to sparse vectors****************************************************************
309 template< typename VT > // Type of the target sparse vector
310 friend inline void assign( SparseVector<VT,TF>& lhs, const SVecSVecMultExpr& rhs )
311 {
313
314 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
315
316 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
317 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
318
319 BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
320 BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
321 BLAZE_INTERNAL_ASSERT( x.size() == (*lhs).size() , "Invalid vector size" );
322
323 // Final memory allocation (based on the evaluated operands)
324 (*lhs).reserve( min( x.nonZeros(), y.nonZeros() ) );
325
326 // Performing the vector multiplication
327 const auto lend( x.end() );
328 const auto rend( y.end() );
329
330 auto l( x.begin() );
331 auto r( y.begin() );
332
333 for( ; l!=lend; ++l ) {
334 while( r!=rend && r->index() < l->index() ) ++r;
335 if( r==rend ) break;
336 if( l->index() == r->index() ) {
337 (*lhs).append( l->index(), l->value() * r->value() );
338 ++r;
339 }
340 }
341 }
343 //**********************************************************************************************
344
345 //**Addition assignment to dense vectors********************************************************
357 template< typename VT > // Type of the target dense vector
358 friend inline void addAssign( DenseVector<VT,TF>& lhs, const SVecSVecMultExpr& rhs )
359 {
361
362 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
363
364 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
365 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
366
367 BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
368 BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
369 BLAZE_INTERNAL_ASSERT( x.size() == (*lhs).size() , "Invalid vector size" );
370
371 const auto lend( x.end() );
372 const auto rend( y.end() );
373
374 auto l( x.begin() );
375 auto r( y.begin() );
376
377 for( ; l!=lend; ++l ) {
378 while( r!=rend && r->index() < l->index() ) ++r;
379 if( r==rend ) break;
380 if( l->index() == r->index() ) {
381 (*lhs)[l->index()] += l->value() * r->value();
382 ++r;
383 }
384 }
385 }
387 //**********************************************************************************************
388
389 //**Addition assignment to sparse vectors*******************************************************
390 // No special implementation for the addition assignment to sparse vectors.
391 //**********************************************************************************************
392
393 //**Subtraction assignment to dense vectors*****************************************************
405 template< typename VT > // Type of the target dense vector
406 friend inline void subAssign( DenseVector<VT,TF>& lhs, const SVecSVecMultExpr& rhs )
407 {
409
410 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
411
412 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
413 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
414
415 BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
416 BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
417 BLAZE_INTERNAL_ASSERT( x.size() == (*lhs).size() , "Invalid vector size" );
418
419 const auto lend( x.end() );
420 const auto rend( y.end() );
421
422 auto l( x.begin() );
423 auto r( y.begin() );
424
425 for( ; l!=lend; ++l ) {
426 while( r!=rend && r->index() < l->index() ) ++r;
427 if( r==rend ) break;
428 if( l->index() == r->index() ) {
429 (*lhs)[l->index()] -= l->value() * r->value();
430 ++r;
431 }
432 }
433 }
435 //**********************************************************************************************
436
437 //**Subtraction assignment to sparse vectors****************************************************
438 // No special implementation for the subtraction assignment to sparse vectors.
439 //**********************************************************************************************
440
441 //**Multiplication assignment to dense vectors**************************************************
453 template< typename VT > // Type of the target dense vector
454 friend inline void multAssign( DenseVector<VT,TF>& lhs, const SVecSVecMultExpr& rhs )
455 {
457
458 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
459
460 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
461 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
462
463 BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
464 BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
465 BLAZE_INTERNAL_ASSERT( x.size() == (*lhs).size() , "Invalid vector size" );
466
467 const auto lend( x.end() );
468 const auto rend( y.end() );
469
470 auto l( x.begin() );
471 auto r( y.begin() );
472
473 size_t i( 0UL );
474
475 for( ; l!=lend; ++l ) {
476 while( r!=rend && r->index() < l->index() ) ++r;
477 if( r==rend ) break;
478 if( l->index() == r->index() ) {
479 for( ; i<r->index(); ++i )
480 reset( (*lhs)[i] );
481 (*lhs)[l->index()] *= l->value() * r->value();
482 ++r;
483 ++i;
484 }
485 }
486
487 for( ; i<rhs.size(); ++i )
488 reset( (*lhs)[i] );
489 }
491 //**********************************************************************************************
492
493 //**Multiplication assignment to sparse vectors*************************************************
505 template< typename VT > // Type of the target sparse vector
506 friend inline void multAssign( SparseVector<VT,TF>& lhs, const SVecSVecMultExpr& rhs )
507 {
509
510 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
511
512 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
513 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
514
515 BLAZE_INTERNAL_ASSERT( x.size() == rhs.lhs_.size(), "Invalid vector size" );
516 BLAZE_INTERNAL_ASSERT( y.size() == rhs.rhs_.size(), "Invalid vector size" );
517 BLAZE_INTERNAL_ASSERT( x.size() == (*lhs).size() , "Invalid vector size" );
518
519 VT tmp( rhs.size(), rhs.nonZeros() );
520
521 const auto end1( (*lhs).end() );
522 const auto end2( x.end() );
523 const auto end3( y.end() );
524
525 auto i1( (*lhs).begin() );
526 auto i2( x.begin() );
527 auto i3( y.begin() );
528
529 for( ; i1!=end1; ++i1 ) {
530 while( i2!=end2 && i2->index() < i1->index() ) ++i2;
531 if( i2==end2 ) break;
532 while( i3!=end3 && i3->index() < i1->index() ) ++i3;
533 if( i3==end3 ) break;
534 if( i1->index() == i2->index() && i1->index() == i3->index() ) {
535 tmp.append( i1->index(), i1->value() * i2->value() * i3->value() );
536 ++i2;
537 ++i3;
538 }
539 }
540
541 swap( *lhs, tmp );
542 }
544 //**********************************************************************************************
545
546 //**Compile time checks*************************************************************************
556 //**********************************************************************************************
557};
558//*************************************************************************************************
559
560
561
562
563//=================================================================================================
564//
565// GLOBAL BINARY ARITHMETIC OPERATORS
566//
567//=================================================================================================
568
569//*************************************************************************************************
582template< typename VT1 // Type of the left-hand side sparse vector
583 , typename VT2 // Type of the right-hand side sparse vector
584 , bool TF // Transpose flag
585 , DisableIf_t< IsZero_v<VT1> || IsZero_v<VT2> >* = nullptr >
586inline const SVecSVecMultExpr<VT1,VT2,TF>
587 svecsvecmult( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
588{
590
591 BLAZE_INTERNAL_ASSERT( (*lhs).size() == (*rhs).size(), "Invalid vector sizes" );
592
593 return SVecSVecMultExpr<VT1,VT2,TF>( *lhs, *rhs );
594}
596//*************************************************************************************************
597
598
599//*************************************************************************************************
612template< typename VT1 // Type of the left-hand side sparse vector
613 , typename VT2 // Type of the right-hand side sparse vector
614 , bool TF // Transpose flag
615 , EnableIf_t< IsZero_v<VT1> || IsZero_v<VT2> >* = nullptr >
616inline decltype(auto)
617 svecsvecmult( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
618{
620
621 MAYBE_UNUSED( rhs );
622
623 BLAZE_INTERNAL_ASSERT( (*lhs).size() == (*rhs).size(), "Invalid vector sizes" );
624
625 using ReturnType = const MultTrait_t< ResultType_t<VT1>, ResultType_t<VT2> >;
626
629
630 return ReturnType( (*lhs).size() );
631}
633//*************************************************************************************************
634
635
636//*************************************************************************************************
661template< typename VT1 // Type of the left-hand side sparse vector
662 , typename VT2 // Type of the right-hand side sparse vector
663 , bool TF > // Transpose flag
664inline decltype(auto)
665 operator*( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
666{
668
669 if( (*lhs).size() != (*rhs).size() ) {
670 BLAZE_THROW_INVALID_ARGUMENT( "Vector sizes do not match" );
671 }
672
673 return svecsvecmult( *lhs, *rhs );
674}
675//*************************************************************************************************
676
677} // namespace blaze
678
679#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::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.
Definition: Aliases.h:470
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.
Definition: Aliases.h:450
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition: Aliases.h:190
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.
Definition: Aliases.h:550
Header file for run time assertion macros.
Header file for the EnableIf class template.
Header file for the function trace functionality.
Header file for the If class template.
Header file for the IsExpression type trait class.
Header file for the IsTemporary type trait class.
Deactivation of problematic macros.
Header file for the MAYBE_UNUSED function template.
Header file for the multiplication trait.
Constraint on the data type.
Base class for N-dimensional dense vectors.
Definition: DenseVector.h:77
Expression object for sparse vector-sparse vector multiplications.
Definition: SVecSVecMultExpr.h:91
RightOperand rightOperand() const noexcept
Returns the right-hand side sparse vector operand.
Definition: SVecSVecMultExpr.h:218
ReturnType_t< VT1 > RN1
Return type of the left-hand side sparse vector expression.
Definition: SVecSVecMultExpr.h:96
ResultType_t< VT2 > RT2
Result type of the right-hand side sparse vector expression.
Definition: SVecSVecMultExpr.h:95
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecSVecMultExpr.h:230
ElementType_t< ResultType > ElementType
Resulting element type.
Definition: SVecSVecMultExpr.h:125
CompositeType_t< VT2 > CT2
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecMultExpr.h:99
decltype(std::declval< RN1 >() *std::declval< RN2 >()) ExprReturnType
Expression return type for the subscript operator.
Definition: SVecSVecMultExpr.h:112
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecSVecMultExpr.h:242
const If_t< returnExpr, ExprReturnType, ElementType > ReturnType
Return type for expression template evaluations.
Definition: SVecSVecMultExpr.h:128
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecSVecMultExpr.h:188
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecSVecMultExpr.h:198
MultTrait_t< RT1, RT2 > ResultType
Result type for expression template evaluations.
Definition: SVecSVecMultExpr.h:123
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SVecSVecMultExpr.h:142
LeftOperand leftOperand() const noexcept
Returns the left-hand side sparse vector operand.
Definition: SVecSVecMultExpr.h:208
SVecSVecMultExpr(const VT1 &lhs, const VT2 &rhs) noexcept
Constructor for the SVecSVecMultExpr class.
Definition: SVecSVecMultExpr.h:148
CompositeType_t< VT1 > CT1
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecMultExpr.h:98
ReturnType_t< VT2 > RN2
Return type of the right-hand side sparse vector expression.
Definition: SVecSVecMultExpr.h:97
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecSVecMultExpr.h:162
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecSVecMultExpr.h:131
If_t< IsExpression_v< VT1 >, const VT1, const VT1 & > LeftOperand
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecMultExpr.h:134
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: SVecSVecMultExpr.h:124
LeftOperand lhs_
Left-hand side sparse vector of the multiplication expression.
Definition: SVecSVecMultExpr.h:249
If_t< IsExpression_v< VT2 >, const VT2, const VT2 & > RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecMultExpr.h:137
static constexpr bool returnExpr
Compilation switch for the selection of the subscript operator return type.
Definition: SVecSVecMultExpr.h:109
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecSVecMultExpr.h:175
ResultType_t< VT1 > RT1
Result type of the left-hand side sparse vector expression.
Definition: SVecSVecMultExpr.h:94
RightOperand rhs_
Right-hand side sparse vector of the multiplication expression.
Definition: SVecSVecMultExpr.h:250
Base class for sparse vectors.
Definition: SparseVector.h:72
Constraint on the data type.
Constraint on the data type.
Header file for the Computation base class.
Header file for the SparseVector base class.
Header file for the VecVecMultExpr base class.
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1339
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:812
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b) noexcept
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:225
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.
Definition: TransposeFlag.h:63
#define BLAZE_CONSTRAINT_MUST_FORM_VALID_VECVECMULTEXPR(T1, T2)
Constraint on the data type.
Definition: VecVecMultExpr.h:104
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ZERO_TYPE(T)
Constraint on the data type.
Definition: Zero.h:81
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: SparseVector.h:61
#define BLAZE_CONSTRAINT_MUST_BE_ZERO_TYPE(T)
Constraint on the data type.
Definition: Zero.h:61
typename MultTrait< T1, T2 >::Type MultTrait_t
Auxiliary alias declaration for the MultTrait class template.
Definition: MultTrait.h:165
constexpr void reset(Matrix< MT, SO > &matrix)
Resetting the given matrix.
Definition: Matrix.h:806
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
typename If< Condition >::template Type< T1, T2 > If_t
Auxiliary alias template for the If class template.
Definition: If.h:108
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.
Definition: Exception.h:331
#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.
Constraint on the data type.
Header file for all forward declarations for expression class templates.
Header file for the reset shim.
Header file for the serial shim.
Base class for all compute expression templates.
Definition: Computation.h:68
Base class for all vector/vector multiplication expression templates.
Definition: VecVecMultExpr.h:68
Header file for the IsZero type trait.
Header file for basic type definitions.
Header file for the generic min algorithm.