Blaze 3.9
SMatSerialExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_SMATSERIALEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_SMATSERIALEXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <blaze/math/Aliases.h>
52#include <blaze/util/Assert.h>
54#include <blaze/util/mpl/If.h>
55#include <blaze/util/Types.h>
56
57
58namespace blaze {
59
60//=================================================================================================
61//
62// CLASS SMATSERIALEXPR
63//
64//=================================================================================================
65
66//*************************************************************************************************
73template< typename MT // Type of the sparse matrix
74 , bool SO > // Storage order
76 : public MatSerialExpr< SparseMatrix< SMatSerialExpr<MT,SO>, SO > >
77 , private Computation
78{
79 public:
80 //**Type definitions****************************************************************************
83
86
92
95
97 using Operand = If_t< IsExpression_v<MT>, const MT, const MT& >;
98 //**********************************************************************************************
99
100 //**Compilation flags***************************************************************************
102 static constexpr bool smpAssignable = MT::smpAssignable;
103 //**********************************************************************************************
104
105 //**Constructor*********************************************************************************
110 explicit inline SMatSerialExpr( const MT& sm ) noexcept
111 : sm_( sm ) // Sparse matrix of the serial evaluation expression
112 {}
113 //**********************************************************************************************
114
115 //**Access operator*****************************************************************************
122 inline ReturnType operator()( size_t i, size_t j ) const {
123 BLAZE_INTERNAL_ASSERT( i < sm_.rows() , "Invalid row access index" );
124 BLAZE_INTERNAL_ASSERT( j < sm_.columns(), "Invalid column access index" );
125 return sm_(i,j);
126 }
127 //**********************************************************************************************
128
129 //**At function*********************************************************************************
137 inline ReturnType at( size_t i, size_t j ) const {
138 if( i >= sm_.rows() ) {
139 BLAZE_THROW_OUT_OF_RANGE( "Invalid row access index" );
140 }
141 if( j >= sm_.columns() ) {
142 BLAZE_THROW_OUT_OF_RANGE( "Invalid column access index" );
143 }
144 return (*this)(i,j);
145 }
146 //**********************************************************************************************
147
148 //**Rows function*******************************************************************************
153 inline size_t rows() const noexcept {
154 return sm_.rows();
155 }
156 //**********************************************************************************************
157
158 //**Columns function****************************************************************************
163 inline size_t columns() const noexcept {
164 return sm_.columns();
165 }
166 //**********************************************************************************************
167
168 //**NonZeros function***************************************************************************
173 inline size_t nonZeros() const {
174 return sm_.nonZeros();
175 }
176 //**********************************************************************************************
177
178 //**NonZeros function***************************************************************************
184 inline size_t nonZeros( size_t i ) const {
185 return sm_.nonZeros(i);
186 }
187 //**********************************************************************************************
188
189 //**Operand access******************************************************************************
194 inline Operand operand() const noexcept {
195 return sm_;
196 }
197 //**********************************************************************************************
198
199 //**Conversion operator*************************************************************************
204 inline operator Operand() const noexcept {
205 return sm_;
206 }
207 //**********************************************************************************************
208
209 //**********************************************************************************************
215 template< typename T >
216 inline bool canAlias( const T* alias ) const noexcept {
217 return sm_.canAlias( alias );
218 }
219 //**********************************************************************************************
220
221 //**********************************************************************************************
227 template< typename T >
228 inline bool isAliased( const T* alias ) const noexcept {
229 return sm_.isAliased( alias );
230 }
231 //**********************************************************************************************
232
233 //**********************************************************************************************
238 inline bool canSMPAssign() const noexcept {
239 return sm_.canSMPAssign();
240 }
241 //**********************************************************************************************
242
243 private:
244 //**Member variables****************************************************************************
246 //**********************************************************************************************
247
248 //**Assignment to dense matrices****************************************************************
260 template< typename MT2 // Type of the target dense matrix
261 , bool SO2 > // Storage order of the target dense matrix
262 friend inline void assign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
263 {
265
266 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
267 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
268
269 assign( *lhs, rhs.sm_ );
270 }
272 //**********************************************************************************************
273
274 //**Assignment to sparse matrices***************************************************************
286 template< typename MT2 // Type of the target sparse matrix
287 , bool SO2 > // Storage order of the target sparse matrix
288 friend inline void assign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
289 {
291
292 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
293 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
294
295 assign( *lhs, rhs.sm_ );
296 }
298 //**********************************************************************************************
299
300 //**Addition assignment to dense matrices*******************************************************
312 template< typename MT2 // Type of the target dense matrix
313 , bool SO2 > // Storage order of the target dense matrix
314 friend inline void addAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
315 {
317
318 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
319 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
320
321 addAssign( *lhs, rhs.sm_ );
322 }
324 //**********************************************************************************************
325
326 //**Addition assignment to sparse matrices******************************************************
338 template< typename MT2 // Type of the target sparse matrix
339 , bool SO2 > // Storage order of the target sparse matrix
340 friend inline void addAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
341 {
343
344 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
345 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
346
347 addAssign( *lhs, rhs.sm_ );
348 }
350 //**********************************************************************************************
351
352 //**Subtraction assignment to dense matrices****************************************************
365 template< typename MT2 // Type of the target dense matrix
366 , bool SO2 > // Storage order of the target dense matrix
367 friend inline void subAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
368 {
370
371 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
372 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
373
374 subAssign( *lhs, rhs.sm_ );
375 }
377 //**********************************************************************************************
378
379 //**Subtraction assignment to sparse matrices***************************************************
392 template< typename MT2 // Type of the target sparse matrix
393 , bool SO2 > // Storage order of the target sparse matrix
394 friend inline void subAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
395 {
397
398 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
399 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
400
401 subAssign( *lhs, rhs.sm_ );
402 }
404 //**********************************************************************************************
405
406 //**Schur product assignment to dense matrices**************************************************
419 template< typename MT2 // Type of the target dense matrix
420 , bool SO2 > // Storage order of the target dense matrix
421 friend inline void schurAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
422 {
424
425 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
426 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
427
428 schurAssign( *lhs, rhs.sm_ );
429 }
431 //**********************************************************************************************
432
433 //**Schur product assignment to sparse matrices*************************************************
446 template< typename MT2 // Type of the target sparse matrix
447 , bool SO2 > // Storage order of the target sparse matrix
448 friend inline void schurAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
449 {
451
452 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
453 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
454
455 schurAssign( *lhs, rhs.sm_ );
456 }
458 //**********************************************************************************************
459
460 //**Multiplication assignment to dense matrices*************************************************
473 template< typename MT2 // Type of the target dense matrix
474 , bool SO2 > // Storage order of the target dense matrix
475 friend inline void multAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
476 {
478
479 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
480 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
481
482 multAssign( *lhs, rhs.sm_ );
483 }
485 //**********************************************************************************************
486
487 //**Multiplication assignment to sparse matrices************************************************
500 template< typename MT2 // Type of the target sparse matrix
501 , bool SO2 > // Storage order of the target sparse matrix
502 friend inline void multAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
503 {
505
506 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
507 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
508
509 multAssign( *lhs, rhs.sm_ );
510 }
512 //**********************************************************************************************
513
514 //**SMP assignment to dense matrices************************************************************
526 template< typename MT2 // Type of the target dense matrix
527 , bool SO2 > // Storage order of the target dense matrix
528 friend inline void smpAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
529 {
531
532 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
533 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
534
535 assign( *lhs, rhs.sm_ );
536 }
538 //**********************************************************************************************
539
540 //**SMP assignment to sparse matrices***********************************************************
552 template< typename MT2 // Type of the target sparse matrix
553 , bool SO2 > // Storage order of the target sparse matrix
554 friend inline void smpAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
555 {
557
558 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
559 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
560
561 assign( *lhs, rhs.sm_ );
562 }
564 //**********************************************************************************************
565
566 //**SMP addition assignment to dense matrices***************************************************
579 template< typename MT2 // Type of the target dense matrix
580 , bool SO2 > // Storage order of the target dense matrix
581 friend inline void smpAddAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
582 {
584
585 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
586 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
587
588 addAssign( *lhs, rhs.sm_ );
589 }
591 //**********************************************************************************************
592
593 //**SMP addition assignment to sparse matrices**************************************************
606 template< typename MT2 // Type of the target sparse matrix
607 , bool SO2 > // Storage order of the target sparse matrix
608 friend inline void smpAddAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
609 {
611
612 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
613 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
614
615 addAssign( *lhs, rhs.sm_ );
616 }
618 //**********************************************************************************************
619
620 //**SMP subtraction assignment to dense matrices************************************************
633 template< typename MT2 // Type of the target dense matrix
634 , bool SO2 > // Storage order of the target dense matrix
635 friend inline void smpSubAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
636 {
638
639 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
640 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
641
642 subAssign( *lhs, rhs.sm_ );
643 }
645 //**********************************************************************************************
646
647 //**SMP subtraction assignment to sparse matrices***********************************************
660 template< typename MT2 // Type of the target sparse matrix
661 , bool SO2 > // Storage order of the target sparse matrix
662 friend inline void smpSubAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
663 {
665
666 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
667 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
668
669 subAssign( *lhs, rhs.sm_ );
670 }
672 //**********************************************************************************************
673
674 //**SMP Schur product assignment to dense matrices**********************************************
687 template< typename MT2 // Type of the target dense matrix
688 , bool SO2 > // Storage order of the target dense matrix
689 friend inline void smpSchurAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
690 {
692
693 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
694 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
695
696 schurAssign( *lhs, rhs.sm_ );
697 }
699 //**********************************************************************************************
700
701 //**SMP Schur product assignment to sparse matrices*********************************************
714 template< typename MT2 // Type of the target sparse matrix
715 , bool SO2 > // Storage order of the target sparse matrix
716 friend inline void smpSchurAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
717 {
719
720 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
721 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
722
723 schurAssign( *lhs, rhs.sm_ );
724 }
726 //**********************************************************************************************
727
728 //**SMP multiplication assignment to dense matrices*********************************************
741 template< typename MT2 // Type of the target dense matrix
742 , bool SO2 > // Storage order of the target dense matrix
743 friend inline void smpMultAssign( DenseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
744 {
746
747 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
748 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
749
750 multAssign( *lhs, rhs.sm_ );
751 }
753 //**********************************************************************************************
754
755 //**SMP multiplication assignment to sparse matrices********************************************
768 template< typename MT2 // Type of the target sparse matrix
769 , bool SO2 > // Storage order of the target sparse matrix
770 friend inline void smpMultAssign( SparseMatrix<MT2,SO2>& lhs, const SMatSerialExpr& rhs )
771 {
773
774 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
775 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
776
777 multAssign( *lhs, rhs.sm_ );
778 }
780 //**********************************************************************************************
781
782 //**Compile time checks*************************************************************************
787 //**********************************************************************************************
788};
789//*************************************************************************************************
790
791
792
793
794//=================================================================================================
795//
796// GLOBAL FUNCTIONS
797//
798//=================================================================================================
799
800//*************************************************************************************************
817template< typename MT // Type of the sparse matrix
818 , bool SO > // Storage order
819inline decltype(auto) serial( const SparseMatrix<MT,SO>& sm )
820{
822
823 using ReturnType = const SMatSerialExpr<MT,SO>;
824 return ReturnType( *sm );
825}
826//*************************************************************************************************
827
828} // namespace blaze
829
830#endif
Header file for auxiliary alias declarations.
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.
Definition: Aliases.h:470
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.
Definition: Aliases.h:450
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition: Aliases.h:190
typename T::OppositeType OppositeType_t
Alias declaration for nested OppositeType type definitions.
Definition: Aliases.h:310
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.
Definition: Aliases.h:550
Header file for run time assertion macros.
Header file for the function trace functionality.
Header file for the If class template.
Header file for the IsExpression type trait class.
Base class for dense matrices.
Definition: DenseMatrix.h:82
Expression object for the forced serial evaluation of sparse matrices.
Definition: SMatSerialExpr.h:78
size_t columns() const noexcept
Returns the current number of columns of the matrix.
Definition: SMatSerialExpr.h:163
ReturnType at(size_t i, size_t j) const
Checked access to the matrix elements.
Definition: SMatSerialExpr.h:137
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SMatSerialExpr.h:216
SMatSerialExpr(const MT &sm) noexcept
Constructor for the SMatSerialExpr class.
Definition: SMatSerialExpr.h:110
If_t< IsExpression_v< MT >, const MT, const MT & > Operand
Composite data type of the sparse matrix expression.
Definition: SMatSerialExpr.h:97
ReturnType_t< MT > ReturnType
Return type for expression template evaluations.
Definition: SMatSerialExpr.h:91
ReturnType operator()(size_t i, size_t j) const
2D-access to the matrix elements.
Definition: SMatSerialExpr.h:122
size_t rows() const noexcept
Returns the current number of rows of the matrix.
Definition: SMatSerialExpr.h:153
Operand sm_
Sparse matrix of the serial evaluation expression.
Definition: SMatSerialExpr.h:245
ResultType_t< MT > ResultType
Result type for expression template evaluations.
Definition: SMatSerialExpr.h:87
size_t nonZeros(size_t i) const
Returns the number of non-zero elements in the specified row.
Definition: SMatSerialExpr.h:184
Operand operand() const noexcept
Returns the sparse matrix operand.
Definition: SMatSerialExpr.h:194
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SMatSerialExpr.h:228
OppositeType_t< MT > OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: SMatSerialExpr.h:88
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SMatSerialExpr.h:102
TransposeType_t< MT > TransposeType
Transpose type for expression template evaluations.
Definition: SMatSerialExpr.h:89
size_t nonZeros() const
Returns the number of non-zero elements in the sparse matrix.
Definition: SMatSerialExpr.h:173
const ResultType CompositeType
Data type for composite expression templates.
Definition: SMatSerialExpr.h:94
bool canSMPAssign() const noexcept
Returns whether the expression can be used in SMP assignments.
Definition: SMatSerialExpr.h:238
ElementType_t< MT > ElementType
Resulting element type.
Definition: SMatSerialExpr.h:90
Base class for sparse matrices.
Definition: SparseMatrix.h:77
Constraint on the data type.
Header file for the Computation base class.
Header file for the MatSerialExpr base class.
Header file for the SparseMatrix base class.
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:812
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: SparseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_BE_MATRIX_WITH_STORAGE_ORDER(T, SO)
Constraint on the data type.
Definition: StorageOrder.h:63
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
auto smpSubAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP subtraction assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:162
auto smpAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:100
auto smpMultAssign(Vector< VT1, TF1 > &lhs, const Vector< VT2, TF2 > &rhs) -> EnableIf_t< IsDenseVector_v< VT1 > >
Default implementation of the SMP multiplication assignment of a vector to a dense vector.
Definition: DenseVector.h:192
auto smpSchurAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP Schur product assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:194
auto smpAddAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP addition assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:131
typename If< Condition >::template Type< T1, T2 > If_t
Auxiliary alias template for the If class template.
Definition: If.h:108
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.
Definition: Exception.h:331
#define BLAZE_FUNCTION_TRACE
Function trace macro.
Definition: FunctionTrace.h:94
Header file for the exception macros of the math module.
Constraints on the storage order of matrix types.
Header file for all forward declarations for expression class templates.
Base class for all compute expression templates.
Definition: Computation.h:68
Base class for all matrix serial evaluation expression templates.
Definition: MatSerialExpr.h:69
Header file for basic type definitions.