Blaze 3.9
SVecSVecKronExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_SVECSVECKRONEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_SVECSVECKRONEXPR_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>
65#include <blaze/util/mpl/If.h>
66#include <blaze/util/TypeList.h>
67#include <blaze/util/Types.h>
68
69
70namespace blaze {
71
72//=================================================================================================
73//
74// CLASS SVECSVECKRONEXPR
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 VecVecKronExpr< SparseVector< SVecSVecKronExpr<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 SVecSVecKronExpr( const VT1& lhs, const VT2& rhs ) noexcept
149 : lhs_( lhs ) // Left-hand side sparse vector of the Kronecker product expression
150 , rhs_( rhs ) // Right-hand side sparse vector of the Kronecker product expression
151 {}
152 //**********************************************************************************************
153
154 //**Subscript operator**************************************************************************
160 inline ReturnType operator[]( size_t index ) const {
161 BLAZE_INTERNAL_ASSERT( index < size(), "Invalid vector access index" );
162 return lhs_[index/rhs_.size()] * rhs_[index%rhs_.size()];
163 }
164 //**********************************************************************************************
165
166 //**At function*********************************************************************************
173 inline ReturnType at( size_t index ) const {
174 if( index >= lhs_.size() ) {
175 BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
176 }
177 return (*this)[index];
178 }
179 //**********************************************************************************************
180
181 //**Size function*******************************************************************************
186 inline size_t size() const noexcept {
187 return lhs_.size() * rhs_.size();
188 }
189 //**********************************************************************************************
190
191 //**NonZeros function***************************************************************************
196 inline size_t nonZeros() const {
197 return lhs_.nonZeros() * rhs_.nonZeros();
198 }
199 //**********************************************************************************************
200
201 //**Left operand access*************************************************************************
206 inline LeftOperand leftOperand() const noexcept {
207 return lhs_;
208 }
209 //**********************************************************************************************
210
211 //**Right operand access************************************************************************
216 inline RightOperand rightOperand() const noexcept {
217 return rhs_;
218 }
219 //**********************************************************************************************
220
221 //**********************************************************************************************
227 template< typename T >
228 inline bool canAlias( const T* alias ) const noexcept {
229 return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
230 }
231 //**********************************************************************************************
232
233 //**********************************************************************************************
239 template< typename T >
240 inline bool isAliased( const T* alias ) const noexcept {
241 return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
242 }
243 //**********************************************************************************************
244
245 private:
246 //**Member variables****************************************************************************
249 //**********************************************************************************************
250
251 //**Assignment to dense vectors*****************************************************************
263 template< typename VT > // Type of the target dense vector
264 friend inline void assign( DenseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
265 {
267
268 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
269
270 if( rhs.size() == 0UL ) {
271 return;
272 }
273
274 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
275 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
276
277 const size_t N( y.size() );
278 const auto xend( x.end() );
279 const auto yend( y.end() );
280
281 for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
282 for( auto yelem=y.begin(); yelem!=yend; ++yelem ) {
283 (*lhs)[xelem->index()*N+yelem->index()] = xelem->value() * yelem->value();
284 }
285 }
286 }
288 //**********************************************************************************************
289
290 //**Assignment to sparse vectors****************************************************************
302 template< typename VT > // Type of the target sparse vector
303 friend inline void assign( SparseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
304 {
306
307 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
308
309 if( rhs.size() == 0UL ) {
310 return;
311 }
312
313 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
314 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
315
316 const size_t N( y.size() );
317 const auto xend( x.end() );
318 const auto yend( y.end() );
319
320 for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
321 for( auto yelem=y.begin(); yelem!=yend; ++yelem ) {
322 (*lhs).append( xelem->index()*N+yelem->index(), xelem->value() * yelem->value(), true );
323 }
324 }
325 }
327 //**********************************************************************************************
328
329 //**Addition assignment to dense vectors********************************************************
342 template< typename VT > // Type of the target dense vector
343 friend inline void addAssign( DenseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
344 {
346
347 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
348
349 if( rhs.size() == 0UL ) {
350 return;
351 }
352
353 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
354 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
355
356 const size_t N( y.size() );
357 const auto xend( x.end() );
358 const auto yend( y.end() );
359
360 for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
361 for( auto yelem=y.begin(); yelem!=yend; ++yelem ) {
362 (*lhs)[xelem->index()*N+yelem->index()] += xelem->value() * yelem->value();
363 }
364 }
365 }
367 //**********************************************************************************************
368
369 //**Addition assignment to sparse vectors*******************************************************
370 // No special implementation for the addition assignment to sparse vectors.
371 //**********************************************************************************************
372
373 //**Subtraction assignment to dense vectors*****************************************************
386 template< typename VT > // Type of the target dense vector
387 friend inline void subAssign( DenseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
388 {
390
391 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
392
393 if( rhs.size() == 0UL ) {
394 return;
395 }
396
397 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
398 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
399
400 const size_t N( y.size() );
401 const auto xend( x.end() );
402 const auto yend( y.end() );
403
404 for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
405 for( auto yelem=y.begin(); yelem!=yend; ++yelem ) {
406 (*lhs)[xelem->index()*N+yelem->index()] -= xelem->value() * yelem->value();
407 }
408 }
409 }
411 //**********************************************************************************************
412
413 //**Subtraction assignment to sparse vectors****************************************************
414 // No special implementation for the subtraction assignment to sparse vectors.
415 //**********************************************************************************************
416
417 //**Multiplication assignment to dense vectors**************************************************
430 template< typename VT > // Type of the target dense vector
431 friend inline void multAssign( DenseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
432 {
434
435 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
436
437 if( rhs.size() == 0UL ) {
438 return;
439 }
440
441 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
442 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
443
444 const size_t N( y.size() );
445 const auto xend( x.end() );
446 const auto yend( y.end() );
447 size_t i( 0UL );
448
449 for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
450 for( auto yelem=y.begin(); yelem!=yend; ++yelem, ++i ) {
451 const size_t index( xelem->index()*N+yelem->index() );
452 for( ; i<index; ++i )
453 reset( (*lhs)[i] );
454 (*lhs)[index] *= xelem->value() * yelem->value();
455 }
456 }
457
458 for( ; i<(*lhs).size(); ++i ) {
459 reset( (*lhs)[i] );
460 }
461 }
463 //**********************************************************************************************
464
465 //**Multiplication assignment to sparse vectors*************************************************
466 // No special implementation for the multiplication assignment to sparse vectors.
467 //**********************************************************************************************
468
469 //**Compile time checks*************************************************************************
479 //**********************************************************************************************
480};
481//*************************************************************************************************
482
483
484
485
486//=================================================================================================
487//
488// GLOBAL FUNCTIONS
489//
490//=================================================================================================
491
492//*************************************************************************************************
505template< typename VT1 // Type of the left-hand side sparse vector
506 , typename VT2 // Type of the right-hand side sparse vector
507 , bool TF // Transpose flag
508 , DisableIf_t< ( IsZero_v<VT1> || IsZero_v<VT2> ) >* = nullptr >
509inline const SVecSVecKronExpr<VT1,VT2,TF>
510 svecsveckron( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
511{
513
514 return SVecSVecKronExpr<VT1,VT2,TF>( *lhs, *rhs );
515}
517//*************************************************************************************************
518
519
520//*************************************************************************************************
533template< typename VT1 // Type of the left-hand side sparse vector
534 , typename VT2 // Type of the right-hand side sparse vector
535 , bool TF // Transpose flag
536 , EnableIf_t< IsZero_v<VT1> || IsZero_v<VT2> >* = nullptr >
537inline decltype(auto)
538 svecsveckron( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
539{
541
542 using ReturnType = const KronTrait_t< ResultType_t<VT1>, ResultType_t<VT2> >;
543
546
547 return ReturnType( (*lhs).size()*(*rhs).size() );
548}
550//*************************************************************************************************
551
552
553//*************************************************************************************************
574template< typename VT1 // Type of the left-hand side sparse vector
575 , typename VT2 // Type of the right-hand side sparse vector
576 , bool TF > // Transpose flag
577inline decltype(auto)
579{
581
582 return svecsveckron( *lhs, *rhs );
583}
584//*************************************************************************************************
585
586} // namespace blaze
587
588#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 isDefault shim.
Header file for the IsExpression type trait class.
Header file for the IsTemporary type trait class.
Header file for the Kron product trait.
Deactivation of problematic macros.
Header file for the type list functionality.
Constraint on the data type.
Base class for N-dimensional dense vectors.
Definition: DenseVector.h:77
Expression object for sparse vector-sparse vector Kronecker products.
Definition: SVecSVecKronExpr.h:91
CompositeType_t< VT2 > CT2
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:99
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecSVecKronExpr.h:131
ResultType_t< VT2 > RT2
Result type of the right-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:95
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecSVecKronExpr.h:228
CompositeType_t< VT1 > CT1
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:98
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecSVecKronExpr.h:240
const If_t< returnExpr, ExprReturnType, ElementType > ReturnType
Return type for expression template evaluations.
Definition: SVecSVecKronExpr.h:128
LeftOperand lhs_
Left-hand side sparse vector of the Kronecker product expression.
Definition: SVecSVecKronExpr.h:247
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecSVecKronExpr.h:196
decltype(std::declval< RN1 >() *std::declval< RN2 >()) ExprReturnType
Expression return type for the subscript operator.
Definition: SVecSVecKronExpr.h:112
LeftOperand leftOperand() const noexcept
Returns the left-hand side sparse vector operand.
Definition: SVecSVecKronExpr.h:206
If_t< IsExpression_v< VT2 >, const VT2, const VT2 & > RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:137
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SVecSVecKronExpr.h:142
static constexpr bool returnExpr
Compilation switch for the selection of the subscript operator return type.
Definition: SVecSVecKronExpr.h:109
ReturnType_t< VT2 > RN2
Return type of the right-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:97
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecSVecKronExpr.h:160
ElementType_t< ResultType > ElementType
Resulting element type.
Definition: SVecSVecKronExpr.h:125
KronTrait_t< RT1, RT2 > ResultType
Result type for expression template evaluations.
Definition: SVecSVecKronExpr.h:123
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecSVecKronExpr.h:173
If_t< IsExpression_v< VT1 >, const VT1, const VT1 & > LeftOperand
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:134
RightOperand rhs_
Right-hand side sparse vector of the Kronecker product expression.
Definition: SVecSVecKronExpr.h:248
RightOperand rightOperand() const noexcept
Returns the right-hand side sparse vector operand.
Definition: SVecSVecKronExpr.h:216
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecSVecKronExpr.h:186
SVecSVecKronExpr(const VT1 &lhs, const VT2 &rhs) noexcept
Constructor for the SVecSVecKronExpr class.
Definition: SVecSVecKronExpr.h:148
ResultType_t< VT1 > RT1
Result type of the left-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:94
ReturnType_t< VT1 > RN1
Return type of the left-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:96
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: SVecSVecKronExpr.h:124
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 VecVecKronExpr base class.
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:812
decltype(auto) kron(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the Kronecker product of two dense matrices ( ).
Definition: DMatDMatKronExpr.h:957
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.
Definition: TransposeFlag.h:63
#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_FORM_VALID_VECVECKRONEXPR(T1, T2)
Constraint on the data type.
Definition: VecVecKronExpr.h:102
#define BLAZE_CONSTRAINT_MUST_BE_ZERO_TYPE(T)
Constraint on the data type.
Definition: Zero.h:61
typename KronTrait< T1, T2 >::Type KronTrait_t
Auxiliary alias declaration for the KronTrait class template.
Definition: KronTrait.h:137
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
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
typename EnableIf<!Condition, T >::Type DisableIf_t
Auxiliary type for the EnableIf class template.
Definition: EnableIf.h:175
#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 Kronecker expression templates.
Definition: VecVecKronExpr.h:69
Header file for the IsZero type trait.
Header file for basic type definitions.