Blaze  3.6
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>
49 #include <blaze/math/Exception.h>
55 #include <blaze/math/shims/Reset.h>
61 #include <blaze/util/Assert.h>
62 #include <blaze/util/DisableIf.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 
70 namespace blaze {
71 
72 //=================================================================================================
73 //
74 // CLASS SVECSVECKRONEXPR
75 //
76 //=================================================================================================
77 
78 //*************************************************************************************************
85 template< 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
88 class SVecSVecKronExpr
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****************************************************************************
122 
125 
127  using CompositeType = const ResultType;
128 
130  using LeftOperand = If_t< IsExpression_v<VT1>, const VT1, const VT1& >;
131 
133  using RightOperand = If_t< IsExpression_v<VT2>, const VT2, const VT2& >;
134  //**********************************************************************************************
135 
136  //**Compilation flags***************************************************************************
138  static constexpr bool smpAssignable = false;
139  //**********************************************************************************************
140 
141  //**Constructor*********************************************************************************
144  explicit inline SVecSVecKronExpr( const VT1& lhs, const VT2& rhs ) noexcept
145  : lhs_( lhs ) // Left-hand side sparse vector of the Kronecker product expression
146  , rhs_( rhs ) // Right-hand side sparse vector of the Kronecker product expression
147  {}
148  //**********************************************************************************************
149 
150  //**Subscript operator**************************************************************************
156  inline ReturnType operator[]( size_t index ) const {
157  BLAZE_INTERNAL_ASSERT( index < size(), "Invalid vector access index" );
158  return lhs_[index/rhs_.size()] * rhs_[index%rhs_.size()];
159  }
160  //**********************************************************************************************
161 
162  //**At function*********************************************************************************
169  inline ReturnType at( size_t index ) const {
170  if( index >= lhs_.size() ) {
171  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
172  }
173  return (*this)[index];
174  }
175  //**********************************************************************************************
176 
177  //**Size function*******************************************************************************
182  inline size_t size() const noexcept {
183  return lhs_.size() * rhs_.size();
184  }
185  //**********************************************************************************************
186 
187  //**NonZeros function***************************************************************************
192  inline size_t nonZeros() const {
193  return lhs_.nonZeros() * rhs_.nonZeros();
194  }
195  //**********************************************************************************************
196 
197  //**Left operand access*************************************************************************
202  inline LeftOperand leftOperand() const noexcept {
203  return lhs_;
204  }
205  //**********************************************************************************************
206 
207  //**Right operand access************************************************************************
212  inline RightOperand rightOperand() const noexcept {
213  return rhs_;
214  }
215  //**********************************************************************************************
216 
217  //**********************************************************************************************
223  template< typename T >
224  inline bool canAlias( const T* alias ) const noexcept {
225  return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
226  }
227  //**********************************************************************************************
228 
229  //**********************************************************************************************
235  template< typename T >
236  inline bool isAliased( const T* alias ) const noexcept {
237  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
238  }
239  //**********************************************************************************************
240 
241  private:
242  //**Member variables****************************************************************************
245  //**********************************************************************************************
246 
247  //**Assignment to dense vectors*****************************************************************
259  template< typename VT > // Type of the target dense vector
260  friend inline void assign( DenseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
261  {
263 
264  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
265 
266  if( rhs.size() == 0UL ) {
267  return;
268  }
269 
270  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
271  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
272 
273  const size_t N( y.size() );
274  const auto xend( x.end() );
275  const auto yend( y.end() );
276 
277  for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
278  for( auto yelem=y.begin(); yelem!=yend; ++yelem ) {
279  (~lhs)[xelem->index()*N+yelem->index()] = xelem->value() * yelem->value();
280  }
281  }
282  }
284  //**********************************************************************************************
285 
286  //**Assignment to sparse vectors****************************************************************
298  template< typename VT > // Type of the target sparse vector
299  friend inline void assign( SparseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
300  {
302 
303  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
304 
305  if( rhs.size() == 0UL ) {
306  return;
307  }
308 
309  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
310  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
311 
312  const size_t N( y.size() );
313  const auto xend( x.end() );
314  const auto yend( y.end() );
315 
316  for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
317  for( auto yelem=y.begin(); yelem!=yend; ++yelem ) {
318  (~lhs).append( xelem->index()*N+yelem->index(), xelem->value() * yelem->value(), true );
319  }
320  }
321  }
323  //**********************************************************************************************
324 
325  //**Addition assignment to dense vectors********************************************************
338  template< typename VT > // Type of the target dense vector
339  friend inline void addAssign( DenseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
340  {
342 
343  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
344 
345  if( rhs.size() == 0UL ) {
346  return;
347  }
348 
349  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
350  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
351 
352  const size_t N( y.size() );
353  const auto xend( x.end() );
354  const auto yend( y.end() );
355 
356  for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
357  for( auto yelem=y.begin(); yelem!=yend; ++yelem ) {
358  (~lhs)[xelem->index()*N+yelem->index()] += xelem->value() * yelem->value();
359  }
360  }
361  }
363  //**********************************************************************************************
364 
365  //**Addition assignment to sparse vectors*******************************************************
366  // No special implementation for the addition assignment to sparse vectors.
367  //**********************************************************************************************
368 
369  //**Subtraction assignment to dense vectors*****************************************************
382  template< typename VT > // Type of the target dense vector
383  friend inline void subAssign( DenseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
384  {
386 
387  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
388 
389  if( rhs.size() == 0UL ) {
390  return;
391  }
392 
393  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
394  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
395 
396  const size_t N( y.size() );
397  const auto xend( x.end() );
398  const auto yend( y.end() );
399 
400  for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
401  for( auto yelem=y.begin(); yelem!=yend; ++yelem ) {
402  (~lhs)[xelem->index()*N+yelem->index()] -= xelem->value() * yelem->value();
403  }
404  }
405  }
407  //**********************************************************************************************
408 
409  //**Subtraction assignment to sparse vectors****************************************************
410  // No special implementation for the subtraction assignment to sparse vectors.
411  //**********************************************************************************************
412 
413  //**Multiplication assignment to dense vectors**************************************************
426  template< typename VT > // Type of the target dense vector
427  friend inline void multAssign( DenseVector<VT,TF>& lhs, const SVecSVecKronExpr& rhs )
428  {
430 
431  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
432 
433  if( rhs.size() == 0UL ) {
434  return;
435  }
436 
437  CT1 x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
438  CT2 y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
439 
440  const size_t N( y.size() );
441  const auto xend( x.end() );
442  const auto yend( y.end() );
443  size_t i( 0UL );
444 
445  for( auto xelem=x.begin(); xelem!=xend; ++xelem ) {
446  for( auto yelem=y.begin(); yelem!=yend; ++yelem, ++i ) {
447  const size_t index( xelem->index()*N+yelem->index() );
448  for( ; i<index; ++i )
449  reset( (~lhs)[i] );
450  (~lhs)[index] *= xelem->value() * yelem->value();
451  }
452  }
453 
454  for( ; i<(~lhs).size(); ++i ) {
455  reset( (~lhs)[i] );
456  }
457  }
459  //**********************************************************************************************
460 
461  //**Multiplication assignment to sparse vectors*************************************************
462  // No special implementation for the multiplication assignment to sparse vectors.
463  //**********************************************************************************************
464 
465  //**Compile time checks*************************************************************************
475  //**********************************************************************************************
476 };
477 //*************************************************************************************************
478 
479 
480 
481 
482 //=================================================================================================
483 //
484 // GLOBAL FUNCTIONS
485 //
486 //=================================================================================================
487 
488 //*************************************************************************************************
501 template< typename VT1 // Type of the left-hand side sparse vector
502  , typename VT2 // Type of the right-hand side sparse vector
503  , bool TF // Transpose flag
504  , DisableIf_t< ( IsZero_v<VT1> || IsZero_v<VT2> ) >* = nullptr >
505 inline const SVecSVecKronExpr<VT1,VT2,TF>
506  svecsveckron( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
507 {
509 
510  return SVecSVecKronExpr<VT1,VT2,TF>( ~lhs, ~rhs );
511 }
513 //*************************************************************************************************
514 
515 
516 //*************************************************************************************************
529 template< typename VT1 // Type of the left-hand side sparse vector
530  , typename VT2 // Type of the right-hand side sparse vector
531  , bool TF // Transpose flag
532  , EnableIf_t< IsZero_v<VT1> || IsZero_v<VT2> >* = nullptr >
533 inline decltype(auto)
534  svecsveckron( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
535 {
537 
538  using ReturnType = const KronTrait_t< ResultType_t<VT1>, ResultType_t<VT2> >;
539 
542 
543  return ReturnType( (~lhs).size()*(~rhs).size() );
544 }
546 //*************************************************************************************************
547 
548 
549 //*************************************************************************************************
570 template< typename VT1 // Type of the left-hand side sparse vector
571  , typename VT2 // Type of the right-hand side sparse vector
572  , bool TF > // Transpose flag
573 inline decltype(auto)
574  kron( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
575 {
577 
578  return svecsveckron( ~lhs, ~rhs );
579 }
580 //*************************************************************************************************
581 
582 } // namespace blaze
583 
584 #endif
const If_t< returnExpr, ExprReturnType, ElementType > ReturnType
Return type for expression template evaluations.
Definition: SVecSVecKronExpr.h:124
Header file for auxiliary alias declarations.
SVecSVecKronExpr(const VT1 &lhs, const VT2 &rhs) noexcept
Constructor for the SVecSVecKronExpr class.
Definition: SVecSVecKronExpr.h:144
RightOperand rightOperand() const noexcept
Returns the right-hand side sparse vector operand.
Definition: SVecSVecKronExpr.h:212
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
If_t< IsExpression_v< VT1 >, const VT1, const VT1 & > LeftOperand
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:130
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.
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecSVecKronExpr.h:236
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:595
Constraint on the data type.
Constraint on the data type.
Header file for the Computation base class.
Header file for the reset shim.
ResultType_t< VT2 > RT2
Result type of the right-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:95
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.The ReturnType_t alias declaration provides ...
Definition: Aliases.h:410
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
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SVecSVecKronExpr.h:138
Expression object for sparse vector-sparse vector Kronecker products.The SVecSVecKronExpr class repre...
Definition: Forward.h:161
Header file for the DisableIf class template.
Header file for the IsTemporary type trait class.
ResultType_t< VT1 > RT1
Result type of the left-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:94
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the If class template.
ReturnType_t< VT2 > RN2
Return type of the right-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:97
#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
ElementType_t< ResultType > ElementType
Resulting element type.
Definition: SVecSVecKronExpr.h:121
Constraint on the data type.
Header file for the Kron product trait.
Constraint on the data type.
RightOperand rhs_
Right-hand side sparse vector of the Kronecker product expression.
Definition: SVecSVecKronExpr.h:244
Header file for the exception macros of the math module.
Header file for the EnableIf class template.
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecSVecKronExpr.h:169
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:127
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.The TransposeType_t alias declaration pro...
Definition: Aliases.h:470
KronTrait_t< RT1, RT2 > ResultType
Result type for expression template evaluations.
Definition: SVecSVecKronExpr.h:119
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
LeftOperand lhs_
Left-hand side sparse vector of the Kronecker product expression.
Definition: SVecSVecKronExpr.h:243
Header file for the Unique class template.
CompositeType_t< VT1 > CT1
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:98
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
Header file for all forward declarations for expression class templates.
Header file for the isDefault shim.
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
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecSVecKronExpr.h:192
static constexpr bool returnExpr
Compilation switch for the selection of the subscript operator return type.
Definition: SVecSVecKronExpr.h:109
decltype(std::declval< RN1 >() *std::declval< RN2 >()) ExprReturnType
Expression return type for the subscript operator.
Definition: SVecSVecKronExpr.h:112
#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
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecSVecKronExpr.h:224
If_t< IsExpression_v< VT2 >, const VT2, const VT2 & > RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:133
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:146
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecSVecKronExpr.h:156
typename DisableIf< Condition, T >::Type DisableIf_t
Auxiliary type for the DisableIf class template.The DisableIf_t alias declaration provides a convenie...
Definition: DisableIf.h:138
ReturnType_t< VT1 > RN1
Return type of the left-hand side sparse vector expression.
Definition: SVecSVecKronExpr.h:96
#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
LeftOperand leftOperand() const noexcept
Returns the left-hand side sparse vector operand.
Definition: SVecSVecKronExpr.h:202
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecSVecKronExpr.h:182
Header file for the IsExpression type trait class.
Header file for the function trace functionality.
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: SVecSVecKronExpr.h:120