Blaze 3.9
DVecSVecKronExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_DVECSVECKRONEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_DVECSVECKRONEXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <utility>
44#include <blaze/math/Aliases.h>
64#include <blaze/util/Assert.h>
65#include <blaze/util/EnableIf.h>
67#include <blaze/util/mpl/If.h>
68#include <blaze/util/TypeList.h>
69#include <blaze/util/Types.h>
70
71
72namespace blaze {
73
74//=================================================================================================
75//
76// CLASS DVECSVECKRONEXPR
77//
78//=================================================================================================
79
80//*************************************************************************************************
87template< typename VT1 // Type of the left-hand side dense vector
88 , typename VT2 // Type of the right-hand side sparse vector
89 , bool TF > // Transpose flag
91 : public VecVecKronExpr< SparseVector< DVecSVecKronExpr<VT1,VT2,TF>, TF > >
92 , private Computation
93{
94 private:
95 //**Type definitions****************************************************************************
104 //**********************************************************************************************
105
106 //**Return type evaluation**********************************************************************
108
113 static constexpr bool returnExpr = ( !IsTemporary_v<RN1> && !IsTemporary_v<RN2> );
114
116 using ExprReturnType = decltype( std::declval<RN1>() + std::declval<RN2>() );
117 //**********************************************************************************************
118
119 public:
120 //**Type definitions****************************************************************************
123
126
130
133
136
138 using LeftOperand = If_t< IsExpression_v<VT1>, const VT1, const VT1& >;
139
141 using RightOperand = If_t< IsExpression_v<VT2>, const VT2, const VT2& >;
142 //**********************************************************************************************
143
144 //**Compilation flags***************************************************************************
146 static constexpr bool smpAssignable = false;
147 //**********************************************************************************************
148
149 //**Constructor*********************************************************************************
155 inline DVecSVecKronExpr( const VT1& lhs, const VT2& rhs ) noexcept
156 : lhs_( lhs ) // Left-hand side dense vector of the Kronecker product expression
157 , rhs_( rhs ) // Right-hand side sparse vector of the Kronecker product expression
158 {}
159 //**********************************************************************************************
160
161 //**Subscript operator**************************************************************************
167 inline ReturnType operator[]( size_t index ) const {
168 BLAZE_INTERNAL_ASSERT( index < size(), "Invalid vector access index" );
169 return lhs_[index/rhs_.size()] * rhs_[index%rhs_.size()];
170 }
171 //**********************************************************************************************
172
173 //**At function*********************************************************************************
180 inline ReturnType at( size_t index ) const {
181 if( index >= lhs_.size() ) {
182 BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
183 }
184 return (*this)[index];
185 }
186 //**********************************************************************************************
187
188 //**Size function*******************************************************************************
193 inline size_t size() const noexcept {
194 return lhs_.size() * rhs_.size();
195 }
196 //**********************************************************************************************
197
198 //**NonZeros function***************************************************************************
203 inline size_t nonZeros() const {
204 return lhs_.size() * rhs_.nonZeros();
205 }
206 //**********************************************************************************************
207
208 //**Left operand access*************************************************************************
213 inline LeftOperand leftOperand() const noexcept {
214 return lhs_;
215 }
216 //**********************************************************************************************
217
218 //**Right operand access************************************************************************
223 inline RightOperand rightOperand() const noexcept {
224 return rhs_;
225 }
226 //**********************************************************************************************
227
228 //**********************************************************************************************
234 template< typename T >
235 inline bool canAlias( const T* alias ) const noexcept {
236 return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
237 }
238 //**********************************************************************************************
239
240 //**********************************************************************************************
246 template< typename T >
247 inline bool isAliased( const T* alias ) const noexcept {
248 return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
249 }
250 //**********************************************************************************************
251
252 private:
253 //**Member variables****************************************************************************
256 //**********************************************************************************************
257
258 //**Assignment to dense vectors*****************************************************************
270 template< typename VT > // Type of the target dense vector
271 friend inline void assign( DenseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
272 {
274
275 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
276
277 if( rhs.size() == 0UL ) {
278 return;
279 }
280
281 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
282 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
283
284 const size_t N( y.size() );
285 const auto yend( y.end() );
286
287 for( size_t i=0UL; i<x.size(); ++i ) {
288 if( !isDefault<strict>( x[i] ) ) {
289 for( auto yelem=y.begin(); yelem!=yend; ++yelem )
290 (*lhs)[i*N+yelem->index()] = x[i] * yelem->value();
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 DVecSVecKronExpr& rhs )
311 {
313
314 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
315
316 if( rhs.size() == 0UL ) {
317 return;
318 }
319
320 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
321 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
322
323 const size_t N( y.size() );
324 const auto yend( y.end() );
325
326 for( size_t i=0UL; i<x.size(); ++i ) {
327 if( !isDefault<strict>( x[i] ) ) {
328 for( auto yelem=y.begin(); yelem!=yend; ++yelem )
329 (*lhs).append( i*N+yelem->index(), x[i] * yelem->value(), true );
330 }
331 }
332 }
334 //**********************************************************************************************
335
336 //**Addition assignment to dense vectors********************************************************
348 template< typename VT > // Type of the target dense vector
349 friend inline void addAssign( DenseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
350 {
352
353 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
354
355 if( rhs.size() == 0UL ) {
356 return;
357 }
358
359 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
360 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
361
362 const size_t N( y.size() );
363 const auto yend( y.end() );
364
365 for( size_t i=0UL; i<x.size(); ++i ) {
366 if( !isDefault<strict>( x[i] ) ) {
367 for( auto yelem=y.begin(); yelem!=yend; ++yelem )
368 (*lhs)[i*N+yelem->index()] += x[i] * yelem->value();
369 }
370 }
371 }
373 //**********************************************************************************************
374
375 //**Addition assignment to sparse vectors*******************************************************
376 // No special implementation for the addition assignment to sparse vectors.
377 //**********************************************************************************************
378
379 //**Subtraction assignment to dense vectors*****************************************************
392 template< typename VT > // Type of the target dense vector
393 friend inline void subAssign( DenseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
394 {
396
397 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
398
399 if( rhs.size() == 0UL ) {
400 return;
401 }
402
403 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
404 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
405
406 const size_t N( y.size() );
407 const auto yend( y.end() );
408
409 for( size_t i=0UL; i<x.size(); ++i ) {
410 if( !isDefault<strict>( x[i] ) ) {
411 for( auto yelem=y.begin(); yelem!=yend; ++yelem )
412 (*lhs)[i*N+yelem->index()] -= x[i] * yelem->value();
413 }
414 }
415 }
417 //**********************************************************************************************
418
419 //**Subtraction assignment to sparse vectors****************************************************
420 // No special implementation for the subtraction assignment to sparse vectors.
421 //**********************************************************************************************
422
423 //**Multiplication assignment to dense vectors**************************************************
436 template< typename VT > // Type of the target dense vector
437 friend inline void multAssign( DenseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
438 {
440
441 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
442
443 if( rhs.size() == 0UL ) {
444 return;
445 }
446
447 CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
448 CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
449
450 const size_t N( y.size() );
451 const auto yend( y.end() );
452
453 for( size_t i=0UL; i<x.size(); ++i ) {
454 if( !isDefault<strict>( x[i] ) )
455 {
456 size_t j( 0UL );
457
458 for( auto yelem=y.begin(); yelem!=yend; ++yelem, ++j ) {
459 for( ; j<yelem->index(); ++j )
460 reset( (*lhs)[i*N+j] );
461 (*lhs)[i*N+yelem->index()] *= x[i] * yelem->value();
462 }
463
464 for( ; j<N; ++j )
465 reset( (*lhs)[i*N+j] );
466 }
467 else {
468 for( size_t j=0UL; j<N; ++j )
469 reset( (*lhs)[i*N+j] );
470 }
471 }
472 }
474 //**********************************************************************************************
475
476 //**Multiplication assignment to sparse vectors*************************************************
477 // No special implementation for the multiplication assignment to sparse vectors.
478 //**********************************************************************************************
479
480 //**Compile time checks*************************************************************************
489 //**********************************************************************************************
490};
491//*************************************************************************************************
492
493
494
495
496//=================================================================================================
497//
498// GLOBAL FUNCTIONS
499//
500//=================================================================================================
501
502//*************************************************************************************************
515template< typename VT1 // Type of the left-hand side dense vector
516 , typename VT2 // Type of the right-hand side sparse vector
517 , bool TF // Transpose flag
518 , DisableIf_t< IsZero_v<VT2> >* = nullptr >
519inline const DVecSVecKronExpr<VT1,VT2,TF>
520 dvecsveckron( const DenseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
521{
523
524 return DVecSVecKronExpr<VT1,VT2,TF>( *lhs, *rhs );
525}
527//*************************************************************************************************
528
529
530//*************************************************************************************************
543template< typename VT1 // Type of the left-hand side dense vector
544 , typename VT2 // Type of the right-hand side sparse vector
545 , bool TF // Transpose flag
546 , EnableIf_t< IsZero_v<VT2> >* = nullptr >
547inline decltype(auto)
548 dvecsveckron( const DenseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
549{
551
552 using ReturnType = const KronTrait_t< ResultType_t<VT1>, ResultType_t<VT2> >;
553
556
557 return ReturnType( (*lhs).size()*(*rhs).size() );
558}
560//*************************************************************************************************
561
562
563//*************************************************************************************************
585template< typename VT1 // Type of the left-hand side dense vector
586 , typename VT2 // Type of the right-hand side sparse vector
587 , bool TF > // Transpose flag
588inline decltype(auto)
590{
592
593 return dvecsveckron( *lhs, *rhs );
594}
595//*************************************************************************************************
596
597} // namespace blaze
598
599#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 the alignment flag enumeration.
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.
Expression object for dense vector-sparse vector Kronecker product.
Definition: DVecSVecKronExpr.h:93
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: DVecSVecKronExpr.h:247
static constexpr bool returnExpr
Compilation switch for the selection of the subscript operator return type.
Definition: DVecSVecKronExpr.h:113
ReturnType_t< VT1 > RN1
Return type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:98
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: DVecSVecKronExpr.h:167
decltype(std::declval< RN1 >()+std::declval< RN2 >()) ExprReturnType
Expression return type for the subscript operator.
Definition: DVecSVecKronExpr.h:116
TransposeType_t< VT1 > TT1
Transpose type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:102
CompositeType_t< VT2 > CT2
Composite type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:101
ReturnType_t< VT2 > RN2
Return type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:99
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: DVecSVecKronExpr.h:180
TransposeType_t< VT2 > TT2
Transpose type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:103
const If_t< returnExpr, ExprReturnType, ElementType > ReturnType
Return type for expression template evaluations.
Definition: DVecSVecKronExpr.h:132
const ResultType CompositeType
Data type for composite expression templates.
Definition: DVecSVecKronExpr.h:135
LeftOperand leftOperand() const noexcept
Returns the left-hand side dense vector operand.
Definition: DVecSVecKronExpr.h:213
DVecSVecKronExpr(const VT1 &lhs, const VT2 &rhs) noexcept
Constructor for the DVecSVecKronExpr class.
Definition: DVecSVecKronExpr.h:155
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: DVecSVecKronExpr.h:128
RightOperand rightOperand() const noexcept
Returns the right-hand side sparse vector operand.
Definition: DVecSVecKronExpr.h:223
ResultType_t< VT2 > RT2
Result type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:97
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: DVecSVecKronExpr.h:203
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: DVecSVecKronExpr.h:146
ResultType_t< VT1 > RT1
Result type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:96
CompositeType_t< VT1 > CT1
Composite type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:100
KronTrait_t< RT1, RT2 > ResultType
Result type for expression template evaluations.
Definition: DVecSVecKronExpr.h:127
ElementType_t< ResultType > ElementType
Resulting element type.
Definition: DVecSVecKronExpr.h:129
LeftOperand lhs_
Left-hand side dense vector of the Kronecker product expression.
Definition: DVecSVecKronExpr.h:254
If_t< IsExpression_v< VT2 >, const VT2, const VT2 & > RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:141
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: DVecSVecKronExpr.h:235
If_t< IsExpression_v< VT1 >, const VT1, const VT1 & > LeftOperand
Composite type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:138
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: DVecSVecKronExpr.h:193
RightOperand rhs_
Right-hand side sparse vector of the Kronecker product expression.
Definition: DVecSVecKronExpr.h:255
Base class for N-dimensional dense vectors.
Definition: DenseVector.h:77
Base class for sparse vectors.
Definition: SparseVector.h:72
Constraint on the data type.
Constraint on the data type.
Constraint on the data type.
Header file for the Computation base class.
Header file for the DenseVector 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
#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_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: DenseVector.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
decltype(auto) kron(const DenseVector< VT1, TF > &lhs, const SparseVector< VT2, TF > &rhs)
Computes the Kronecker product of a dense vector and a sparse vector ( ).
Definition: DVecSVecKronExpr.h:589
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_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 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.