Blaze  3.6
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>
51 #include <blaze/math/Exception.h>
58 #include <blaze/math/shims/Reset.h>
63 #include <blaze/util/Assert.h>
64 #include <blaze/util/DisableIf.h>
65 #include <blaze/util/EnableIf.h>
67 #include <blaze/util/MaybeUnused.h>
68 #include <blaze/util/mpl/If.h>
69 #include <blaze/util/TypeList.h>
70 #include <blaze/util/Types.h>
71 
72 
73 namespace blaze {
74 
75 //=================================================================================================
76 //
77 // CLASS DVECSVECKRONEXPR
78 //
79 //=================================================================================================
80 
81 //*************************************************************************************************
88 template< typename VT1 // Type of the left-hand side dense vector
89  , typename VT2 // Type of the right-hand side sparse vector
90  , bool TF > // Transpose flag
92  : public VecVecKronExpr< SparseVector< DVecSVecKronExpr<VT1,VT2,TF>, TF > >
93  , private Computation
94 {
95  private:
96  //**Type definitions****************************************************************************
105  //**********************************************************************************************
106 
107  //**Return type evaluation**********************************************************************
109 
114  static constexpr bool returnExpr = ( !IsTemporary_v<RN1> && !IsTemporary_v<RN2> );
115 
117  using ExprReturnType = decltype( std::declval<RN1>() + std::declval<RN2>() );
118  //**********************************************************************************************
119 
120  public:
121  //**Type definitions****************************************************************************
127 
130 
132  using CompositeType = const ResultType;
133 
135  using LeftOperand = If_t< IsExpression_v<VT1>, const VT1, const VT1& >;
136 
138  using RightOperand = If_t< IsExpression_v<VT2>, const VT2, const VT2& >;
139  //**********************************************************************************************
140 
141  //**Compilation flags***************************************************************************
143  static constexpr bool smpAssignable = false;
144  //**********************************************************************************************
145 
146  //**Constructor*********************************************************************************
152  explicit inline DVecSVecKronExpr( const VT1& lhs, const VT2& rhs ) noexcept
153  : lhs_( lhs ) // Left-hand side dense vector of the Kronecker product expression
154  , rhs_( rhs ) // Right-hand side sparse vector of the Kronecker product expression
155  {}
156  //**********************************************************************************************
157 
158  //**Subscript operator**************************************************************************
164  inline ReturnType operator[]( size_t index ) const {
165  BLAZE_INTERNAL_ASSERT( index < size(), "Invalid vector access index" );
166  return lhs_[index/rhs_.size()] * rhs_[index%rhs_.size()];
167  }
168  //**********************************************************************************************
169 
170  //**At function*********************************************************************************
177  inline ReturnType at( size_t index ) const {
178  if( index >= lhs_.size() ) {
179  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
180  }
181  return (*this)[index];
182  }
183  //**********************************************************************************************
184 
185  //**Size function*******************************************************************************
190  inline size_t size() const noexcept {
191  return lhs_.size() * rhs_.size();
192  }
193  //**********************************************************************************************
194 
195  //**NonZeros function***************************************************************************
200  inline size_t nonZeros() const {
201  return lhs_.size() * rhs_.nonZeros();
202  }
203  //**********************************************************************************************
204 
205  //**Left operand access*************************************************************************
210  inline LeftOperand leftOperand() const noexcept {
211  return lhs_;
212  }
213  //**********************************************************************************************
214 
215  //**Right operand access************************************************************************
220  inline RightOperand rightOperand() const noexcept {
221  return rhs_;
222  }
223  //**********************************************************************************************
224 
225  //**********************************************************************************************
231  template< typename T >
232  inline bool canAlias( const T* alias ) const noexcept {
233  return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
234  }
235  //**********************************************************************************************
236 
237  //**********************************************************************************************
243  template< typename T >
244  inline bool isAliased( const T* alias ) const noexcept {
245  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
246  }
247  //**********************************************************************************************
248 
249  private:
250  //**Member variables****************************************************************************
253  //**********************************************************************************************
254 
255  //**Assignment to dense vectors*****************************************************************
267  template< typename VT > // Type of the target dense vector
268  friend inline void assign( DenseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
269  {
271 
272  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
273 
274  if( rhs.size() == 0UL ) {
275  return;
276  }
277 
278  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
279  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
280 
281  const size_t N( y.size() );
282  const auto yend( y.end() );
283 
284  for( size_t i=0UL; i<x.size(); ++i ) {
285  if( !isDefault( x[i] ) ) {
286  for( auto yelem=y.begin(); yelem!=yend; ++yelem )
287  (~lhs)[i*N+yelem->index()] = x[i] * yelem->value();
288  }
289  }
290  }
292  //**********************************************************************************************
293 
294  //**Assignment to sparse vectors****************************************************************
306  template< typename VT > // Type of the target sparse vector
307  friend inline void assign( SparseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
308  {
310 
311  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
312 
313  if( rhs.size() == 0UL ) {
314  return;
315  }
316 
317  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
318  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
319 
320  const size_t N( y.size() );
321  const auto yend( y.end() );
322 
323  for( size_t i=0UL; i<x.size(); ++i ) {
324  if( !isDefault( x[i] ) ) {
325  for( auto yelem=y.begin(); yelem!=yend; ++yelem )
326  (~lhs).append( i*N+yelem->index(), x[i] * yelem->value(), true );
327  }
328  }
329  }
331  //**********************************************************************************************
332 
333  //**Addition assignment to dense vectors********************************************************
345  template< typename VT > // Type of the target dense vector
346  friend inline void addAssign( DenseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
347  {
349 
350  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
351 
352  if( rhs.size() == 0UL ) {
353  return;
354  }
355 
356  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
357  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
358 
359  const size_t N( y.size() );
360  const auto yend( y.end() );
361 
362  for( size_t i=0UL; i<x.size(); ++i ) {
363  if( !isDefault( x[i] ) ) {
364  for( auto yelem=y.begin(); yelem!=yend; ++yelem )
365  (~lhs)[i*N+yelem->index()] += x[i] * yelem->value();
366  }
367  }
368  }
370  //**********************************************************************************************
371 
372  //**Addition assignment to sparse vectors*******************************************************
373  // No special implementation for the addition assignment to sparse vectors.
374  //**********************************************************************************************
375 
376  //**Subtraction assignment to dense vectors*****************************************************
389  template< typename VT > // Type of the target dense vector
390  friend inline void subAssign( DenseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
391  {
393 
394  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
395 
396  if( rhs.size() == 0UL ) {
397  return;
398  }
399 
400  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
401  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
402 
403  const size_t N( y.size() );
404  const auto yend( y.end() );
405 
406  for( size_t i=0UL; i<x.size(); ++i ) {
407  if( !isDefault( x[i] ) ) {
408  for( auto yelem=y.begin(); yelem!=yend; ++yelem )
409  (~lhs)[i*N+yelem->index()] -= x[i] * yelem->value();
410  }
411  }
412  }
414  //**********************************************************************************************
415 
416  //**Subtraction assignment to sparse vectors****************************************************
417  // No special implementation for the subtraction assignment to sparse vectors.
418  //**********************************************************************************************
419 
420  //**Multiplication assignment to dense vectors**************************************************
433  template< typename VT > // Type of the target dense vector
434  friend inline void multAssign( DenseVector<VT,TF>& lhs, const DVecSVecKronExpr& rhs )
435  {
437 
438  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
439 
440  if( rhs.size() == 0UL ) {
441  return;
442  }
443 
444  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side dense vector operand
445  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
446 
447  const size_t N( y.size() );
448  const auto yend( y.end() );
449 
450  for( size_t i=0UL; i<x.size(); ++i ) {
451  if( !isDefault( x[i] ) )
452  {
453  size_t j( 0UL );
454 
455  for( auto yelem=y.begin(); yelem!=yend; ++yelem, ++j ) {
456  for( ; j<yelem->index(); ++j )
457  reset( (~lhs)[i*N+j] );
458  (~lhs)[i*N+yelem->index()] *= x[i] * yelem->value();
459  }
460 
461  for( ; j<N; ++j )
462  reset( (~lhs)[i*N+j] );
463  }
464  else {
465  for( size_t j=0UL; j<N; ++j )
466  reset( (~lhs)[i*N+j] );
467  }
468  }
469  }
471  //**********************************************************************************************
472 
473  //**Multiplication assignment to sparse vectors*************************************************
474  // No special implementation for the multiplication assignment to sparse vectors.
475  //**********************************************************************************************
476 
477  //**Compile time checks*************************************************************************
486  //**********************************************************************************************
487 };
488 //*************************************************************************************************
489 
490 
491 
492 
493 //=================================================================================================
494 //
495 // GLOBAL FUNCTIONS
496 //
497 //=================================================================================================
498 
499 //*************************************************************************************************
512 template< typename VT1 // Type of the left-hand side dense vector
513  , typename VT2 // Type of the right-hand side sparse vector
514  , bool TF // Transpose flag
515  , DisableIf_t< IsZero_v<VT2> >* = nullptr >
516 inline const DVecSVecKronExpr<VT1,VT2,TF>
517  dvecsveckron( const DenseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
518 {
520 
521  return DVecSVecKronExpr<VT1,VT2,TF>( ~lhs, ~rhs );
522 }
524 //*************************************************************************************************
525 
526 
527 //*************************************************************************************************
540 template< typename VT1 // Type of the left-hand side dense vector
541  , typename VT2 // Type of the right-hand side sparse vector
542  , bool TF // Transpose flag
543  , EnableIf_t< IsZero_v<VT2> >* = nullptr >
544 inline decltype(auto)
545  dvecsveckron( const DenseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
546 {
548 
549  using ReturnType = const KronTrait_t< ResultType_t<VT1>, ResultType_t<VT2> >;
550 
553 
554  return ReturnType( (~lhs).size()*(~rhs).size() );
555 }
557 //*************************************************************************************************
558 
559 
560 //*************************************************************************************************
582 template< typename VT1 // Type of the left-hand side dense vector
583  , typename VT2 // Type of the right-hand side sparse vector
584  , bool TF > // Transpose flag
585 inline decltype(auto)
586  kron( const DenseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
587 {
589 
590  return dvecsveckron( ~lhs, ~rhs );
591 }
592 //*************************************************************************************************
593 
594 } // namespace blaze
595 
596 #endif
Header file for auxiliary alias declarations.
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: DVecSVecKronExpr.h:177
Header file for the alignment flag values.
Header file for basic type definitions.
Header file for the SparseVector base class.
typename If< Condition, T1, T2 >::Type If_t
Auxiliary alias template for the If class template.The If_t alias template provides a convenient shor...
Definition: If.h:109
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.The ResultType_t alias declaration provides ...
Definition: Aliases.h:390
Header file for the serial shim.
const If_t< returnExpr, ExprReturnType, ElementType > ReturnType
Return type for expression template evaluations.
Definition: DVecSVecKronExpr.h:129
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:595
TransposeType_t< VT1 > TT1
Transpose type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:103
Constraint on the data type.
Header file for the DenseVector base class.
CompositeType_t< VT2 > CT2
Composite type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:102
Header file for the MAYBE_UNUSED function template.
Constraint on the data type.
ResultType_t< VT1 > RT1
Result type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:97
decltype(std::declval< RN1 >()+std::declval< RN2 >()) ExprReturnType
Expression return type for the subscript operator.
Definition: DVecSVecKronExpr.h:117
Header file for the Computation base class.
Header file for the reset shim.
LeftOperand lhs_
Left-hand side dense vector of the Kronecker product expression.
Definition: DVecSVecKronExpr.h:251
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.The ReturnType_t alias declaration provides ...
Definition: Aliases.h:410
static constexpr bool returnExpr
Compilation switch for the selection of the subscript operator return type.
Definition: DVecSVecKronExpr.h:114
decltype(auto) kron(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the Kronecker product of two dense matrices ( ).
Definition: DMatDMatKronExpr.h:954
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
DVecSVecKronExpr(const VT1 &lhs, const VT2 &rhs) noexcept
Constructor for the DVecSVecKronExpr class.
Definition: DVecSVecKronExpr.h:152
ResultType_t< VT2 > RT2
Result type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:98
Header file for the DisableIf class template.
Header file for the IsTemporary type trait class.
ReturnType_t< VT1 > RN1
Return type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:99
Base class for all vector/vector Kronecker expression templates.The VecVecKronExpr class serves as a ...
Definition: VecVecKronExpr.h:67
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the If class template.
#define BLAZE_CONSTRAINT_MUST_BE_ZERO_TYPE(T)
Constraint on the data type.In case the given data type T is not a zero vector or matrix type,...
Definition: Zero.h:61
typename KronTrait< T1, T2 >::Type KronTrait_t
Auxiliary alias declaration for the KronTrait class template.The KronTrait_t alias declaration provid...
Definition: KronTrait.h:163
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.This macro encapsulates the default way of Bl...
Definition: Exception.h:331
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
Header file for the VecVecKronExpr base class.
#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
Constraint on the data type.
Header file for the Kron product trait.
Constraint on the data type.
Header file for the exception macros of the math module.
ElementType_t< ResultType > ElementType
Resulting element type.
Definition: DVecSVecKronExpr.h:126
Constraint on the data type.
Expression object for dense vector-sparse vector Kronecker product.The DVecSVecKronExpr class represe...
Definition: DVecSVecKronExpr.h:91
Header file for the EnableIf class template.
KronTrait_t< RT1, RT2 > ResultType
Result type for expression template evaluations.
Definition: DVecSVecKronExpr.h:124
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.The TransposeType_t alias declaration pro...
Definition: Aliases.h:470
Header file for run time assertion macros.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
Header file for the Unique class template.
If_t< IsExpression_v< VT1 >, const VT1, const VT1 & > LeftOperand
Composite type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:135
Header file for the IsZero type trait.
#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
const ResultType CompositeType
Data type for composite expression templates.
Definition: DVecSVecKronExpr.h:132
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: DVecSVecKronExpr.h:143
Header file for the isDefault shim.
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: DVecSVecKronExpr.h:200
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:808
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: DVecSVecKronExpr.h:125
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: DVecSVecKronExpr.h:190
If_t< IsExpression_v< VT2 >, const VT2, const VT2 & > RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:138
TransposeType_t< VT2 > TT2
Transpose type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:104
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional vector type,...
Definition: DenseVector.h:61
ReturnType_t< VT2 > RN2
Return type of the right-hand side sparse vector expression.
Definition: DVecSVecKronExpr.h:100
RightOperand rightOperand() const noexcept
Returns the right-hand side sparse vector operand.
Definition: DVecSVecKronExpr.h:220
#define BLAZE_CONSTRAINT_MUST_FORM_VALID_VECVECKRONEXPR(T1, T2)
Constraint on the data type.In case the given data types T1 and T2 do not form a valid vector/vector ...
Definition: VecVecKronExpr.h:102
Base class for all compute expression templates.The Computation class serves as a tag for all computa...
Definition: Computation.h:66
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:146
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: DVecSVecKronExpr.h:232
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: DVecSVecKronExpr.h:244
RightOperand rhs_
Right-hand side sparse vector of the Kronecker product expression.
Definition: DVecSVecKronExpr.h:252
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:635
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: DVecSVecKronExpr.h:164
LeftOperand leftOperand() const noexcept
Returns the left-hand side dense vector operand.
Definition: DVecSVecKronExpr.h:210
CompositeType_t< VT1 > CT1
Composite type of the left-hand side dense vector expression.
Definition: DVecSVecKronExpr.h:101
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ZERO_TYPE(T)
Constraint on the data type.In case the given data type T is a zero vector or matrix type,...
Definition: Zero.h:81
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.In case the given data type T is not a dense or sparse vector type and in...
Definition: TransposeFlag.h:63
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
Header file for the IsExpression type trait class.
Header file for the function trace functionality.