All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TSVecTDMatMultExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_TSVECTDMATMULTEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_TSVECTDMATMULTEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
53 #include <blaze/math/shims/Reset.h>
62 #include <blaze/util/Assert.h>
64 #include <blaze/util/DisableIf.h>
65 #include <blaze/util/EnableIf.h>
67 #include <blaze/util/SelectType.h>
68 #include <blaze/util/Types.h>
70 
71 
72 namespace blaze {
73 
74 //=================================================================================================
75 //
76 // CLASS TSVECDMATMULTEXPR
77 //
78 //=================================================================================================
79 
80 //*************************************************************************************************
87 template< typename VT // Type of the left-hand side sparse vector
88  , typename MT > // Type of the right-hand side dense matrix
89 class TSVecTDMatMultExpr : public DenseVector< TSVecTDMatMultExpr<VT,MT>, true >
90  , private TVecMatMultExpr
91  , private Computation
92 {
93  private:
94  //**Type definitions****************************************************************************
95  typedef typename VT::ResultType VRT;
96  typedef typename MT::ResultType MRT;
97  typedef typename VT::CompositeType VCT;
98  typedef typename MT::CompositeType MCT;
99  //**********************************************************************************************
100 
101  //**********************************************************************************************
103 
109  enum { useAssign = ( IsComputation<VT>::value || RequiresEvaluation<MT>::value ) };
110  //**********************************************************************************************
111 
112  //**********************************************************************************************
114  template< typename VT2 >
116  struct UseAssign {
117  enum { value = useAssign };
118  };
120  //**********************************************************************************************
121 
122  public:
123  //**Type definitions****************************************************************************
128  typedef const ElementType ReturnType;
129 
132 
134  typedef typename SelectType< IsExpression<VT>::value, const VT, const VT& >::Type LeftOperand;
135 
137  typedef typename SelectType< IsExpression<MT>::value, const MT, const MT& >::Type RightOperand;
138 
140  typedef typename SelectType< IsComputation<VT>::value, const VRT, VCT >::Type LT;
141 
143  typedef MCT RT;
144  //**********************************************************************************************
145 
146  //**Compilation flags***************************************************************************
148  enum { vectorizable = 0 };
149 
151  enum { smpAssignable = 0 };
152  //**********************************************************************************************
153 
154  //**Constructor*********************************************************************************
160  explicit inline TSVecTDMatMultExpr( const VT& vec, const MT& mat )
161  : vec_( vec ) // Left-hand side sparse vector of the multiplication expression
162  , mat_( mat ) // Right-hand side dense matrix of the multiplication expression
163  {
164  BLAZE_INTERNAL_ASSERT( vec_.size() == mat_.rows(), "Invalid vector and matrix sizes" );
165  }
166  //**********************************************************************************************
167 
168  //**Subscript operator**************************************************************************
174  inline ReturnType operator[]( size_t index ) const {
175  BLAZE_INTERNAL_ASSERT( index < mat_.columns(), "Invalid vector access index" );
176 
178 
179  VCT x( vec_ ); // Evaluation of the left-hand side sparse vector operand
180 
181  BLAZE_INTERNAL_ASSERT( x.size() == vec_.size(), "Invalid vector size" );
182 
183  const ConstIterator end( x.end() );
184  ConstIterator element( x.begin() );
185  ElementType res;
186 
187  if( element != end ) {
188  res = element->value() * mat_( element->index(), index );
189  ++element;
190  for( ; element!=end; ++element )
191  res += element->value() * mat_( element->index(), index );
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_.columns();
208  }
209  //**********************************************************************************************
210 
211  //**Left operand access*************************************************************************
216  inline LeftOperand leftOperand() const {
217  return vec_;
218  }
219  //**********************************************************************************************
220 
221  //**Right operand access************************************************************************
226  inline RightOperand rightOperand() const {
227  return mat_;
228  }
229  //**********************************************************************************************
230 
231  //**********************************************************************************************
237  template< typename T >
238  inline bool canAlias( const T* alias ) const {
239  return vec_.isAliased( alias ) || mat_.isAliased( alias );
240  }
241  //**********************************************************************************************
242 
243  //**********************************************************************************************
249  template< typename T >
250  inline bool isAliased( const T* alias ) const {
251  return vec_.isAliased( alias ) || mat_.isAliased( alias );
252  }
253  //**********************************************************************************************
254 
255  private:
256  //**Member variables****************************************************************************
257  LeftOperand vec_;
258  RightOperand mat_;
259  //**********************************************************************************************
260 
261  //**Assignment to dense vectors*****************************************************************
277  template< typename VT2 > // Type of the target dense vector
278  friend inline typename EnableIf< UseAssign<VT2> >::Type
280  {
282 
283  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
284 
286 
287  // Evaluation of the left-hand side sparse vector operand
288  LT x( rhs.vec_ );
289  if( x.nonZeros() == 0UL ) {
290  reset( ~lhs );
291  return;
292  }
293 
294  // Evaluation of the right-hand side dense matrix operand
295  RT A( rhs.mat_ );
296 
297  // Checking the evaluated operands
298  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
299  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
300  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
301  BLAZE_INTERNAL_ASSERT( A.columns() == (~lhs).size() , "Invalid vector size" );
302 
303  // Performing the sparse vector-dense matrix 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] = element->value() * A(element->index(),i);
311  ++element;
312  for( ; element!=end; ++element ) {
313  (~lhs)[i] += element->value() * A(element->index(),i);
314  }
315  }
316  }
318  //**********************************************************************************************
319 
320  //**Assignment to sparse vectors****************************************************************
333  template< typename VT2 > // Type of the target sparse vector
334  friend inline void assign( SparseVector<VT2,true>& lhs, const TSVecTDMatMultExpr& rhs )
335  {
337 
341 
342  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
343 
344  const ResultType tmp( rhs );
345  assign( ~lhs, tmp );
346  }
348  //**********************************************************************************************
349 
350  //**Addition assignment to dense vectors********************************************************
362  template< typename VT2 > // Type of the target dense vector
363  friend inline void addAssign( DenseVector<VT2,true>& lhs, const TSVecTDMatMultExpr& rhs )
364  {
366 
367  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
368 
370 
371  // Evaluation of the left-hand side sparse vector operand
372  LT x( rhs.vec_ );
373  if( x.nonZeros() == 0UL ) return;
374 
375  // Evaluation of the right-hand side dense matrix operand
376  RT A( rhs.mat_ );
377 
378  // Checking the evaluated operands
379  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
380  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
381  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
382  BLAZE_INTERNAL_ASSERT( A.columns() == (~lhs).size() , "Invalid vector size" );
383 
384  // Performing the sparse vector-dense matrix multiplication
385  const ConstIterator end( x.end() );
386  ConstIterator element( x.begin() );
387 
388  for( ; element!=end; ++element ) {
389  for( size_t i=0UL; i<A.columns(); ++i ) {
390  (~lhs)[i] += element->value() * A(element->index(),i);
391  }
392  }
393  }
394  //**********************************************************************************************
395 
396  //**Addition assignment to sparse vectors*******************************************************
397  // No special implementation for the addition assignment to sparse vectors.
398  //**********************************************************************************************
399 
400  //**Subtraction assignment to dense vectors*****************************************************
412  template< typename VT2 > // Type of the target dense vector
413  friend inline void subAssign( DenseVector<VT2,true>& lhs, const TSVecTDMatMultExpr& rhs )
414  {
416 
417  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
418 
420 
421  // Evaluation of the left-hand side sparse vector operand
422  LT x( rhs.vec_ );
423  if( x.nonZeros() == 0UL ) return;
424 
425  // Evaluation of the right-hand side dense matrix operand
426  RT A( rhs.mat_ );
427 
428  // Checking the evaluated operands
429  BLAZE_INTERNAL_ASSERT( x.size() == rhs.vec_.size() , "Invalid vector size" );
430  BLAZE_INTERNAL_ASSERT( A.rows() == rhs.mat_.rows() , "Invalid number of rows" );
431  BLAZE_INTERNAL_ASSERT( A.columns() == rhs.mat_.columns(), "Invalid number of columns" );
432  BLAZE_INTERNAL_ASSERT( A.columns() == (~lhs).size() , "Invalid vector size" );
433 
434  // Performing the sparse vector-dense matrix multiplication
435  const ConstIterator end( x.end() );
436  ConstIterator element( x.begin() );
437 
438  for( ; element!=end; ++element ) {
439  for( size_t i=0UL; i<A.columns(); ++i ) {
440  (~lhs)[i] -= element->value() * A(element->index(),i);
441  }
442  }
443  }
444  //**********************************************************************************************
445 
446  //**Subtraction assignment to sparse vectors****************************************************
447  // No special implementation for the subtraction assignment to sparse vectors.
448  //**********************************************************************************************
449 
450  //**Multiplication assignment to dense vectors**************************************************
462  template< typename VT2 > // Type of the target dense vector
463  friend inline void multAssign( DenseVector<VT2,true>& lhs, const TSVecTDMatMultExpr& rhs )
464  {
466 
470 
471  BLAZE_INTERNAL_ASSERT( (~lhs).size() == rhs.size(), "Invalid vector sizes" );
472 
473  const ResultType tmp( rhs );
474  multAssign( ~lhs, tmp );
475  }
476  //**********************************************************************************************
477 
478  //**Multiplication assignment to sparse vectors*************************************************
479  // No special implementation for the multiplication assignment to sparse vectors.
480  //**********************************************************************************************
481 
482  //**Compile time checks*************************************************************************
489  //**********************************************************************************************
490 };
491 //*************************************************************************************************
492 
493 
494 
495 
496 //=================================================================================================
497 //
498 // GLOBAL BINARY ARITHMETIC OPERATORS
499 //
500 //=================================================================================================
501 
502 //*************************************************************************************************
533 template< typename T1, typename T2 >
534 inline const typename DisableIf< IsMatMatMultExpr<T2>, TSVecTDMatMultExpr<T1,T2> >::Type
536 {
538 
539  if( (~vec).size() != (~mat).rows() )
540  throw std::invalid_argument( "Vector and matrix sizes do not match" );
541 
542  return TSVecTDMatMultExpr<T1,T2>( ~vec, ~mat );
543 }
544 //*************************************************************************************************
545 
546 
547 
548 
549 //=================================================================================================
550 //
551 // EXPRESSION TRAIT SPECIALIZATIONS
552 //
553 //=================================================================================================
554 
555 //*************************************************************************************************
557 template< typename VT, typename MT >
558 struct SubvectorExprTrait< TSVecTDMatMultExpr<VT,MT> >
559 {
560  public:
561  //**********************************************************************************************
562  typedef typename MultExprTrait< VT, typename SubmatrixExprTrait<const MT>::Type >::Type Type;
563  //**********************************************************************************************
564 };
566 //*************************************************************************************************
567 
568 } // namespace blaze
569 
570 #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
bool canAlias(const T *alias) const
Returns whether the expression can alias with the given address alias.
Definition: TSVecTDMatMultExpr.h:238
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
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
SelectType< IsExpression< MT >::value, const MT, const MT & >::Type RightOperand
Composite type of the right-hand side sparse matrix expression.
Definition: TSVecTDMatMultExpr.h:137
MT::ResultType MRT
Result type of the right-hand side dense matrix expression.
Definition: TSVecTDMatMultExpr.h:96
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
Header file for the DenseVector base class.
ResultType::ElementType ElementType
Resulting element type.
Definition: TSVecTDMatMultExpr.h:127
Header file for the Computation base class.
Header file for the RequiresEvaluation type trait.
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.
VT::ResultType VRT
Result type of the left-hand side sparse vector expression.
Definition: TSVecTDMatMultExpr.h:95
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
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.
RightOperand rightOperand() const
Returns the right-hand side transpose dense matrix operand.
Definition: TSVecTDMatMultExpr.h:226
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a column-major dense or sparse matri...
Definition: StorageOrder.h:161
VT::CompositeType VCT
Composite type of the left-hand side sparse vector expression.
Definition: TSVecTDMatMultExpr.h:97
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2379
MultTrait< VRT, MRT >::Type ResultType
Result type for expression template evaluations.
Definition: TSVecTDMatMultExpr.h:125
Header file for the IsMatMatMultExpr type trait class.
LeftOperand vec_
Left-hand side sparse vector of the multiplication expression.
Definition: TSVecTDMatMultExpr.h:257
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
LeftOperand leftOperand() const
Returns the left-hand side sparse vector operand.
Definition: TSVecTDMatMultExpr.h:216
SelectType< useAssign, const ResultType, const TSVecTDMatMultExpr & >::Type CompositeType
Data type for composite expression templates.
Definition: TSVecTDMatMultExpr.h:131
const ElementType ReturnType
Return type for expression template evaluations.
Definition: TSVecTDMatMultExpr.h:128
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< IsComputation< VT >::value, const VRT, VCT >::Type LT
Type for the assignment of the left-hand side sparse vector operand.
Definition: TSVecTDMatMultExpr.h:140
bool isAliased(const T *alias) const
Returns whether the expression is aliased with the given address alias.
Definition: TSVecTDMatMultExpr.h:250
Constraint on the data type.
#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.
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: TSVecTDMatMultExpr.h:126
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.
RightOperand mat_
Right-hand side dense matrix of the multiplication expression.
Definition: TSVecTDMatMultExpr.h:258
SelectType< IsExpression< VT >::value, const VT, const VT & >::Type LeftOperand
Composite type of the left-hand side sparse vector expression.
Definition: TSVecTDMatMultExpr.h:134
Header file for the SubmatrixExprTrait class template.
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
Header file for run time assertion macros.
Base template for the MultTrait class.
Definition: MultTrait.h:141
MT::CompositeType MCT
Composite type of the right-hand side dense matrix expression.
Definition: TSVecTDMatMultExpr.h:98
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
Header file for the reset shim.
Header file for the TVecMatMultExpr base class.
friend void addAssign(DenseVector< VT2, true > &lhs, const TSVecTDMatMultExpr &rhs)
Addition assignment of a transpose sparse vector-transpose dense matrix multiplication to a dense vec...
Definition: TSVecTDMatMultExpr.h:363
TSVecTDMatMultExpr(const VT &vec, const MT &mat)
Constructor for the TSVecTDMatMultExpr class.
Definition: TSVecTDMatMultExpr.h:160
Header file for the RemoveReference type trait.
MCT RT
Type for the assignment of the left-hand side dense matrix operand.
Definition: TSVecTDMatMultExpr.h:143
Expression object for transpose sparse vector-transpose dense matrix multiplications.The TSVecTDMatMultExpr class represents the compile time expression for multiplications between transpose sparse vectors and column-major dense matrices.
Definition: Forward.h:145
#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
TSVecTDMatMultExpr< VT, MT > This
Type of this TSVecTDMatMultExpr instance.
Definition: TSVecTDMatMultExpr.h:124
Header file for the IsComputation type trait class.
friend void multAssign(DenseVector< VT2, true > &lhs, const TSVecTDMatMultExpr &rhs)
Multiplication assignment of a transpose sparse vector-transpose dense matrix multiplication to a den...
Definition: TSVecTDMatMultExpr.h:463
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
friend void subAssign(DenseVector< VT2, true > &lhs, const TSVecTDMatMultExpr &rhs)
Subtraction assignment of a transpose sparse vector-transpose dense matrix multiplication to a dense ...
Definition: TSVecTDMatMultExpr.h:413
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2370
size_t size() const
Returns the current size/dimension of the vector.
Definition: TSVecTDMatMultExpr.h:206
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: TSVecTDMatMultExpr.h:174
Header file for basic type definitions.
#define BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a row dense or sparse vector type (i...
Definition: TransposeFlag.h:81
Header file for the SubvectorExprTrait class template.
size_t rows(const Matrix< MT, SO > &m)
Returns the current number of rows of the matrix.
Definition: Matrix.h:138
#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.