All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatSVecMultExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATSVECMULTEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_DMATSVECMULTEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
53 #include <blaze/math/shims/Reset.h>
63 #include <blaze/util/Assert.h>
65 #include <blaze/util/DisableIf.h>
66 #include <blaze/util/EnableIf.h>
68 #include <blaze/util/SelectType.h>
69 #include <blaze/util/Types.h>
71 
72 
73 namespace blaze {
74 
75 //=================================================================================================
76 //
77 // CLASS DMATSVECMULTEXPR
78 //
79 //=================================================================================================
80 
81 //*************************************************************************************************
88 template< typename MT // Type of the left-hand side dense matrix
89  , typename VT > // Type of the right-hand side sparse vector
90 class DMatSVecMultExpr : public DenseVector< DMatSVecMultExpr<MT,VT>, false >
91  , private MatVecMultExpr
92  , private Computation
93 {
94  private:
95  //**Type definitions****************************************************************************
96  typedef typename MT::ResultType MRT;
97  typedef typename VT::ResultType VRT;
98  typedef typename MT::CompositeType MCT;
99  typedef typename VT::CompositeType VCT;
100  //**********************************************************************************************
101 
102  //**********************************************************************************************
104 
111  //**********************************************************************************************
112 
113  //**********************************************************************************************
115  template< typename VT2 >
117  struct UseAssign {
118  enum { value = useAssign };
119  };
121  //**********************************************************************************************
122 
123  public:
124  //**Type definitions****************************************************************************
129  typedef const ElementType ReturnType;
130 
133 
135  typedef typename SelectType< IsExpression<MT>::value, const MT, const MT& >::Type LeftOperand;
136 
138  typedef typename SelectType< IsExpression<VT>::value, const VT, const VT& >::Type RightOperand;
139 
141  typedef MCT LT;
142 
144  typedef typename SelectType< IsComputation<VT>::value, const VRT, VCT >::Type RT;
145  //**********************************************************************************************
146 
147  //**Compilation flags***************************************************************************
149  enum { vectorizable = 0 };
150 
152  enum { smpAssignable = 0 };
153  //**********************************************************************************************
154 
155  //**Constructor*********************************************************************************
161  explicit inline DMatSVecMultExpr( const MT& mat, const VT& vec )
162  : mat_( mat ) // Left-hand side dense matrix of the multiplication expression
163  , vec_( vec ) // Right-hand side sparse vector of the multiplication expression
164  {
165  BLAZE_INTERNAL_ASSERT( mat_.columns() == vec_.size(), "Invalid matrix and vector sizes" );
166  }
167  //**********************************************************************************************
168 
169  //**Subscript operator**************************************************************************
175  inline ReturnType operator[]( size_t index ) const {
176  BLAZE_INTERNAL_ASSERT( index < mat_.rows(), "Invalid vector access index" );
177 
179 
180  VCT x( vec_ ); // Evaluation of the right-hand side sparse vector operand
181 
182  BLAZE_INTERNAL_ASSERT( x.size() == vec_.size(), "Invalid vector size" );
183 
184  ConstIterator element( x.begin() );
185  ElementType res;
186 
187  if( element != x.end() ) {
188  res = mat_( index, element->index() ) * element->value();
189  ++element;
190  for( ; element!=x.end(); ++element )
191  res += mat_( index, element->index() ) * element->value();
192  }
193  else {
194  reset( res );
195  }
196 
197  return res;
198  }
199  //**********************************************************************************************
200 
201  //**Size function*******************************************************************************
206  inline size_t size() const {
207  return mat_.rows();
208  }
209  //**********************************************************************************************
210 
211  //**Left operand access*************************************************************************
216  inline LeftOperand leftOperand() const {
217  return mat_;
218  }
219  //**********************************************************************************************
220 
221  //**Right operand access************************************************************************
226  inline RightOperand rightOperand() const {
227  return vec_;
228  }
229  //**********************************************************************************************
230 
231  //**********************************************************************************************
237  template< typename T >
238  inline bool canAlias( const T* alias ) const {
239  return mat_.isAliased( alias ) || vec_.isAliased( alias );
240  }
241  //**********************************************************************************************
242 
243  //**********************************************************************************************
249  template< typename T >
250  inline bool isAliased( const T* alias ) const {
251  return mat_.isAliased( alias ) || vec_.isAliased( alias );
252  }
253  //**********************************************************************************************
254 
255  private:
256  //**Member variables****************************************************************************
259  //**********************************************************************************************
260 
261  //**Assignment to dense vectors*****************************************************************
277  template< typename VT1 > // Type of the target dense vector
278  friend inline typename EnableIf< UseAssign<VT1> >::Type
280  {
282 
283  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
284 
286 
287  // Evaluation of the right-hand side sparse vector operand
288  RT x( rhs.vec_ );
289  if( x.nonZeros() == 0UL ) {
290  reset( ~lhs );
291  return;
292  }
293 
294  // Evaluation of the left-hand side dense matrix operand
295  LT A( rhs.mat_ );
296 
297  // Checking the evaluated operands
298  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
299  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
300  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
301  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).size() , "Invalid vector size" );
302 
303  // Performing the dense matrix-sparse vector multiplication
304  const ConstIterator end( x.end() );
305 
306  for( size_t i=0UL; i<(~lhs).size(); ++i )
307  {
308  ConstIterator element( x.begin() );
309 
310  (~lhs)[i] = A( i, element->index() ) * element->value();
311  ++element;
312  for( ; element!=end; ++element )
313  (~lhs)[i] += A( i, element->index() ) * element->value();
314  }
315  }
317  //**********************************************************************************************
318 
319  //**Assignment to sparse vectors****************************************************************
335  template< typename VT1 > // Type of the target sparse vector
336  friend inline typename EnableIf< UseAssign<VT1> >::Type
338  {
340 
344 
345  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
346 
347  const ResultType tmp( rhs );
348  assign( ~lhs, tmp );
349  }
351  //**********************************************************************************************
352 
353  //**Addition assignment to dense vectors********************************************************
369  template< typename VT1 > // Type of the target dense vector
370  friend inline typename EnableIf< UseAssign<VT1> >::Type
371  addAssign( DenseVector<VT1,false>& lhs, const DMatSVecMultExpr& rhs )
372  {
374 
375  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
376 
378 
379  // Evaluation of the right-hand side sparse vector operand
380  RT x( rhs.vec_ );
381  if( x.nonZeros() == 0UL ) return;
382 
383  // Evaluation of the left-hand side dense matrix operand
384  LT A( rhs.mat_ );
385 
386  // Checking the evaluated operands
387  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
388  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
389  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
390  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).size() , "Invalid vector size" );
391 
392  // Performing the dense matrix-sparse vector multiplication
393  const ConstIterator end( x.end() );
394 
395  for( size_t i=0UL; i<(~lhs).size(); ++i )
396  {
397  ConstIterator element( x.begin() );
398 
399  for( ; element!=end; ++element ) {
400  (~lhs)[i] += A( i, element->index() ) * element->value();
401  }
402  }
403  }
405  //**********************************************************************************************
406 
407  //**Addition assignment to sparse vectors*******************************************************
408  // No special implementation for the addition assignment to sparse vectors.
409  //**********************************************************************************************
410 
411  //**Subtraction assignment to dense vectors*****************************************************
427  template< typename VT1 > // Type of the target dense vector
428  friend inline typename EnableIf< UseAssign<VT1> >::Type
429  subAssign( DenseVector<VT1,false>& lhs, const DMatSVecMultExpr& rhs )
430  {
432 
433  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
434 
436 
437  // Evaluation of the right-hand side sparse vector operand
438  RT x( rhs.vec_ );
439  if( x.nonZeros() == 0UL ) return;
440 
441  // Evaluation of the left-hand side dense matrix operand
442  LT A( rhs.mat_ );
443 
444  // Checking the evaluated operands
445  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
446  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
447  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
448  BLAZE_INTERNAL_ASSERT( A.rows() == (~lhs).size() , "Invalid vector size" );
449 
450  // Performing the dense matrix-sparse vector multiplication
451  const ConstIterator end( x.end() );
452 
453  for( size_t i=0UL; i<(~lhs).size(); ++i )
454  {
455  ConstIterator element( x.begin() );
456 
457  for( ; element!=end; ++element ) {
458  (~lhs)[i] -= A( i, element->index() ) * element->value();
459  }
460  }
461  }
463  //**********************************************************************************************
464 
465  //**Subtraction assignment to sparse vectors****************************************************
466  // No special implementation for the subtraction assignment to sparse vectors.
467  //**********************************************************************************************
468 
469  //**Multiplication assignment to dense vectors**************************************************
485  template< typename VT1 > // Type of the target dense vector
486  friend inline typename EnableIf< UseAssign<VT1> >::Type
487  multAssign( DenseVector<VT1,false>& lhs, const DMatSVecMultExpr& rhs )
488  {
490 
494 
495  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
496 
497  const ResultType tmp( rhs );
498  multAssign( ~lhs, tmp );
499  }
501  //**********************************************************************************************
502 
503  //**Addition assignment to sparse vectors*******************************************************
504  // No special implementation for the multiplication assignment to sparse vectors.
505  //**********************************************************************************************
506 
507  //**Compile time checks*************************************************************************
514  //**********************************************************************************************
515 };
516 //*************************************************************************************************
517 
518 
519 
520 
521 //=================================================================================================
522 //
523 // GLOBAL BINARY ARITHMETIC OPERATORS
524 //
525 //=================================================================================================
526 
527 //*************************************************************************************************
559 template< typename T1 // Type of the left-hand side dense matrix
560  , typename T2 > // Type of the right-hand side sparse vector
561 inline const typename DisableIf< IsMatMatMultExpr<T1>, DMatSVecMultExpr<T1,T2> >::Type
563 {
565 
566  if( (~mat).columns() != (~vec).size() )
567  throw std::invalid_argument( "Matrix and vector sizes do not match" );
568 
569  return DMatSVecMultExpr<T1,T2>( ~mat, ~vec );
570 }
571 //*************************************************************************************************
572 
573 
574 
575 
576 //=================================================================================================
577 //
578 // GLOBAL RESTRUCTURING BINARY ARITHMETIC OPERATORS
579 //
580 //=================================================================================================
581 
582 //*************************************************************************************************
595 template< typename T1 // Type of the left-hand side dense matrix
596  , bool SO // Storage order of the left-hand side dense matrix
597  , typename T2 > // Type of the right-hand side sparse vector
598 inline const typename EnableIf< IsMatMatMultExpr<T1>, MultExprTrait<T1,T2> >::Type::Type
600 {
602 
603  return (~mat).leftOperand() * ( (~mat).rightOperand() * vec );
604 }
605 //*************************************************************************************************
606 
607 
608 
609 
610 //=================================================================================================
611 //
612 // EXPRESSION TRAIT SPECIALIZATIONS
613 //
614 //=================================================================================================
615 
616 //*************************************************************************************************
618 template< typename MT, typename VT >
619 struct SubvectorExprTrait< DMatSVecMultExpr<MT,VT> >
620 {
621  public:
622  //**********************************************************************************************
623  typedef typename MultExprTrait< typename SubmatrixExprTrait<const MT>::Type, VT >::Type Type;
624  //**********************************************************************************************
625 };
627 //*************************************************************************************************
628 
629 } // namespace blaze
630 
631 #endif
Compile time check whether the given type is a computational expression template.This type trait clas...
Definition: IsComputation.h:89
void reset(DynamicMatrix< Type, SO > &m)
Resetting the given dense matrix.
Definition: DynamicMatrix.h:4512
const DMatDMatMultExpr< T1, T2 > operator*(const DenseMatrix< T1, false > &lhs, const DenseMatrix< T2, false > &rhs)
Multiplication operator for the multiplication of two row-major dense matrices ( ).
Definition: DMatDMatMultExpr.h:3703
DMatSVecMultExpr< MT, VT > This
Type of this DMatSVecMultExpr instance.
Definition: DMatSVecMultExpr.h:125
LeftOperand leftOperand() const
Returns the left-hand side dense matrix operand.
Definition: DMatSVecMultExpr.h:216
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a column dense or sparse vector type...
Definition: TransposeFlag.h:159
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:196
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional matrix type...
Definition: DenseMatrix.h:79
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2375
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:248
MultTrait< MRT, VRT >::Type ResultType
Result type for expression template evaluations.
Definition: DMatSVecMultExpr.h:126
Header file for the DenseVector base class.
Header file for the Computation base class.
Header file for the RequiresEvaluation type trait.
LeftOperand mat_
Left-hand side dense matrix of the multiplication expression.
Definition: DMatSVecMultExpr.h:257
VT::ResultType VRT
Result type of the right-hand side sparse vector expression.
Definition: DMatSVecMultExpr.h:97
Base class for dense matrices.The DenseMatrix class is a base class for all dense matrix classes...
Definition: DenseMatrix.h:70
Constraint on the data type.
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: DMatSVecMultExpr.h:127
Constraint on the data type.
Header file for the MultExprTrait class template.
Compile time check to query the requirement to evaluate an expression.Via this type trait it is possi...
Definition: RequiresEvaluation.h:90
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:250
MCT LT
Type for the assignment of the left-hand side dense matrix operand.
Definition: DMatSVecMultExpr.h:141
Expression object for dense matrix-sparse vector multiplications.The DMatSVecMultExpr class represent...
Definition: DMatSVecMultExpr.h:90
Compile time type selection.The SelectType class template selects one of the two given types T1 and T...
Definition: SelectType.h:59
Header file for the DisableIf class template.
Header file for the multiplication trait.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2379
Header file for the IsMatMatMultExpr type trait class.
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:179
RightOperand vec_
Right-hand side sparse vector of the multiplication expression.
Definition: DMatSVecMultExpr.h:258
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
#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:79
SelectType< IsExpression< VT >::value, const VT, const VT & >::Type RightOperand
Composite type of the right-hand side dense vector expression.
Definition: DMatSVecMultExpr.h:138
Constraint on the data type.
Base class for all matrix/vector multiplication expression templates.The MatVecMultExpr class serves ...
Definition: MatVecMultExpr.h:66
RightOperand rightOperand() const
Returns the right-hand side sparse vector operand.
Definition: DMatSVecMultExpr.h:226
#define BLAZE_CONSTRAINT_MUST_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:78
Constraints on the storage order of matrix types.
Constraint on the data type.
void multAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the multiplication assignment of a matrix to a matrix.
Definition: Matrix.h:269
Header file for the SelectType class template.
Header file for all forward declarations for expression class templates.
Constraint on the data type.
Header file for the EnableIf class template.
MT::CompositeType MCT
Composite type of the left-hand side dense matrix expression.
Definition: DMatSVecMultExpr.h:98
Header file for the SubmatrixExprTrait class template.
#define BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a row-major dense or sparse matrix t...
Definition: StorageOrder.h:81
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
size_t size() const
Returns the current size/dimension of the vector.
Definition: DMatSVecMultExpr.h:206
Header file for run time assertion macros.
Base template for the MultTrait class.
Definition: MultTrait.h:141
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:209
SelectType< IsExpression< MT >::value, const MT, const MT & >::Type LeftOperand
Composite type of the left-hand side dense matrix expression.
Definition: DMatSVecMultExpr.h:135
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
ResultType::ElementType ElementType
Resulting element type.
Definition: DMatSVecMultExpr.h:128
Header file for the reset shim.
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:239
bool canAlias(const T *alias) const
Returns whether the expression can alias with the given address alias.
Definition: DMatSVecMultExpr.h:238
Header file for the RemoveReference type trait.
#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:79
const ElementType ReturnType
Return type for expression template evaluations.
Definition: DMatSVecMultExpr.h:129
Header file for the IsComputation type trait class.
bool isAliased(const T *alias) const
Returns whether the expression is aliased with the given address alias.
Definition: DMatSVecMultExpr.h:250
Base class for all compute expression templates.The Computation class serves as a tag for all computa...
Definition: Computation.h:59
SelectType< IsComputation< VT >::value, const VRT, VCT >::Type RT
Type for the assignment of the right-hand side dense matrix operand.
Definition: DMatSVecMultExpr.h:144
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:105
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:157
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2370
size_t columns(const Matrix< MT, SO > &m)
Returns the current number of columns of the matrix.
Definition: Matrix.h:154
Header file for basic type definitions.
Header file for the SubvectorExprTrait class template.
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: DMatSVecMultExpr.h:175
Header file for the MatVecMultExpr base class.
SelectType< useAssign, const ResultType, const DMatSVecMultExpr & >::Type CompositeType
Data type for composite expression templates.
Definition: DMatSVecMultExpr.h:132
DMatSVecMultExpr(const MT &mat, const VT &vec)
Constructor for the DMatSVecMultExpr class.
Definition: DMatSVecMultExpr.h:161
MT::ResultType MRT
Result type of the left-hand side dense matrix expression.
Definition: DMatSVecMultExpr.h:96
VT::CompositeType VCT
Composite type of the right-hand side sparse vector expression.
Definition: DMatSVecMultExpr.h:99
Header file for the thresholds for matrix/vector and matrix/matrix multiplications.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the IsExpression type trait class.
Header file for the FunctionTrace class.