Template Numerical Library: TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator > Class Template Reference

Template Numerical Library version\ main:be7e16d88

Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Attributes | List of all members
TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator > Class Template Reference

Implementation of sparse matrix, i.e. matrix storing only non-zero elements. More...

`#include <TNL/Matrices/SparseMatrix.h>`

Inheritance diagram for TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >:
[legend]
Collaboration diagram for TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >:
[legend]

## Public Types
using | ColumnIndexesVectorType = Containers::Vector< Index, Device, Index, IndexAllocator >
| Type of vector holding values of column indexes.
using | ConstViewType
| Matrix view type for constant instances.
using | IndexAllocatorType = IndexAllocator
| The allocator for matrix elements column indexes.
using | MatrixType = MatrixType_
| The type of matrix - general, symmetric or binary.
using | RealAllocatorType = RealAllocator
| The allocator for matrix elements values.
using | RowCapacitiesVectorType = Containers::Vector< Index, Device, Index, IndexAllocator >
| Type of vector holding values of row capacities.
template<typename Device_, typename Index_, typename IndexAllocator_>
using | SegmentsTemplate = Segments< Device_, Index_, IndexAllocator_ >
| Templated type of segments, i.e. sparse matrix format.
using | SegmentsType = Segments< Device, Index, IndexAllocator >
| Type of segments used by this matrix. It represents the sparse matrix format.
template<typename Device_, typename Index_>
using | SegmentsViewTemplate = typename SegmentsType::template ViewTemplate< Device_, Index >
| Templated view type of segments, i.e. sparse matrix format.
template<typename _Real = Real, typename _Device = Device, typename _Index = Index, typename _MatrixType = MatrixType, template< typename, typename, typename > class _Segments = SegmentsTemplate, typename _ComputeReal = ComputeReal, typename _RealAllocator = typename Allocators::Default< _Device >::template Allocator< _Real >, typename _IndexAllocator = typename Allocators::Default< _Device >::template Allocator< _Index >>
using | Self = SparseMatrix< _Real, _Device, _Index, _MatrixType, _Segments, _ComputeReal, _RealAllocator, _IndexAllocator >
| Helper type for getting self type or its modifications.
using | ValuesVectorType = Containers::Vector< Real, Device, Index, RealAllocator >
| Type of vector holding values of matrix elements.
using | ViewType = SparseMatrixView< Real, Device, Index, MatrixType, SegmentsViewTemplate, ComputeReal >
| Type of related matrix view.
Public Types inherited from TNL::Matrices::SparseMatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType, typename ChooseSparseMatrixComputeReal< double, int >::type >
using | ColumnIndexesViewType
using | ComputeRealType
using | ConstRowView
| Type for accessing constant matrix rows.
using | DefaultSegmentsReductionKernel
| Type of the kernel used for parallel reductions on segments.
using | DeviceType
| The device where the matrix is allocated.
using | IndexType
| The type used for matrix elements indexing.
using | RealType
| The type of matrix elements.
using | RowView
| Type for accessing matrix rows.
using | SegmentsViewType
| Type of segments view used by this matrix. It represents the sparse matrix format.
Public Types inherited from TNL::Matrices::MatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType::getOrganization() >
using | ConstValuesViewType
| Type of constant vector view holding values of matrix elements.
using | DeviceType
| The device where the matrix is allocated.
using | IndexType
| The type used for matrix elements indexing.
using | RealType
| The type of matrix elements.
using | RowCapacitiesType
using | ValuesViewType
| Type of vector view holding values of matrix elements.

## Public Member Functions
| SparseMatrix (const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType())
| Constructor only with values and column indexes allocators.
template<typename RowCapacitiesVector, std::enable_if_t< TNL::IsArrayType< RowCapacitiesVector >::value, int > = 0>
| SparseMatrix (const RowCapacitiesVector &rowCapacities, Index columns, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType())
| Constructor with matrix rows capacities given as a vector and number of columns.
| SparseMatrix (const SparseMatrix &matrix)
| Copy constructor.
template<typename ListIndex>
| SparseMatrix (const std::initializer_list< ListIndex > &rowCapacities, Index columns, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType())
| Constructor with matrix rows capacities and number of columns.
| SparseMatrix (Index rows, Index columns, const std::initializer_list< std::tuple< Index, Index, Real > > &data, SymmetricMatrixEncoding encoding=SymmetricMatrixEncoding::LowerPart, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType())
| Constructor with matrix dimensions and data in initializer list.
template<typename MapIndex, typename MapValue>
| SparseMatrix (Index rows, Index columns, const std::map< std::pair< MapIndex, MapIndex >, MapValue > &map, SymmetricMatrixEncoding encoding=SymmetricMatrixEncoding::LowerPart, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType())
| Constructor with matrix dimensions and data in std::map.
template<typename Index_t, std::enable_if_t< std::is_integral_v< Index_t >, int > = 0>
| SparseMatrix (Index_t rows, Index_t columns, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType())
| Constructor with matrix dimensions.
| SparseMatrix (SparseMatrix &&) noexcept=default
| Move constructor.
ConstViewType | getConstView () const
| Returns a non-modifiable view of the sparse matrix.
template<typename Real2, typename Index2, template< typename, typename, typename > class Segments2>
void | getTransposition (const SparseMatrix< Real2, Device, Index2, MatrixType, Segments2 > &matrix, const ComputeReal &matrixMultiplicator=1.0)
| Computes transposition of the matrix.
ViewType | getView ()
| Returns a modifiable view of the sparse matrix.
void | load (const String &fileName)
| Method for loading the matrix from the file with given filename.
template<typename Real_, typename Device_, typename Index_, ElementsOrganization Organization, typename RealAllocator_>
SparseMatrix & | operator= (const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > &matrix)
| Assignment of dense matrix.
template<typename RHSMatrix>
SparseMatrix & | operator= (const RHSMatrix &matrix)
| Assignment of any matrix type other then this and dense.
SparseMatrix & | operator= (const SparseMatrix &matrix)
| Copy-assignment of exactly the same matrix type.
SparseMatrix & | operator= (SparseMatrix &&matrix) noexcept(false)
| Move-assignment of exactly the same matrix type.
void | reset ()
| Resets the matrix to zero dimensions.
void | save (const String &fileName) const
| Method for saving the matrix to the file with given filename.
void | setColumnsWithoutReset (Index columns)
| Set number of columns of this matrix.
void | setDimensions (Index rows, Index columns)
| Set number of rows and columns of this matrix.
void | setDimensions (Index rows, Index columns, SegmentsType segments)
| Set number of rows and columns of this matrix and performs full allocation according to the given segments.
void | setElements (const std::initializer_list< std::tuple< Index, Index, Real > > &data, SymmetricMatrixEncoding encoding=SymmetricMatrixEncoding::LowerPart)
| This method sets the sparse matrix elements from initializer list.
template<typename MapIndex, typename MapValue>
void | setElements (const std::map< std::pair< MapIndex, MapIndex >, MapValue > &map, SymmetricMatrixEncoding encoding=SymmetricMatrixEncoding::LowerPart)
| This method sets the sparse matrix elements from std::map.
template<typename Matrix>
void | setLike (const Matrix &matrix)
| Set the number of matrix rows and columns by the given matrix.
template<typename RowCapacitiesVector>
void | setRowCapacities (const RowCapacitiesVector &rowCapacities)
| Allocates memory for non-zero matrix elements.
Public Member Functions inherited from TNL::Matrices::SparseMatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType, typename ChooseSparseMatrixComputeReal< double, int >::type >
__cuda_callable__ | SparseMatrixBase ()=default
| Constructor with no parameters.
__cuda_callable__ void | addElement (IndexType row, IndexType column, const RealType &value, const RealType &thisElementMultiplicator=1.0)
| Add element at given row and column to given value.
__cuda_callable__ IndexType | findElement (IndexType row, IndexType column) const
| Finds element in the matrix and returns its position in the arrays with values and columnIndexes.
void | forAllElements (Function &&function) const
| This method calls forElements for all matrix rows (for constant instances).
void | forAllRows (Function &&function)
| Method for parallel iteration over all matrix rows.
void | forElements (IndexType begin, IndexType end, Function &&function) const
| Method for iteration over all matrix rows for constant instances.
void | forRows (IndexType begin, IndexType end, Function &&function)
| Method for parallel iteration over matrix rows from interval [begin, end).
const ColumnIndexesViewType & | getColumnIndexes () const
| Getter of column indexes for constant instances.
void | getCompressedRowLengths (Vector &rowLengths) const
| Computes number of non-zeros in each row.
__cuda_callable__ RealType | getElement (IndexType row, IndexType column) const
| Returns value of matrix element at position given by its row and column index.
IndexType | getNonzeroElementsCount () const
| Returns number of non-zero matrix elements.
__cuda_callable__ ConstRowView | getRow (IndexType rowIdx) const
| Constant getter of simple structure for accessing given matrix row.
void | getRowCapacities (Vector &rowCapacities) const
| Compute capacities of all rows.
__cuda_callable__ IndexType | getRowCapacity (IndexType row) const
| Returns capacity of given matrix row.
SegmentsViewType & | getSegments ()
| Getter of segments for non-constant instances.
bool | operator!= (const Matrix &matrix) const
| Comparison operator with another arbitrary matrix type.
SparseMatrixBase & | operator= (const SparseMatrixBase &)=delete
| Copy-assignment operator.
bool | operator== (const Matrix &matrix) const
| Comparison operator with another arbitrary matrix type.
void | print (std::ostream &str) const
| Method for printing the matrix to output stream.
std::enable_if_t< Algorithms::SegmentsReductionKernels::isSegmentReductionKernel< SegmentsReductionKernel >::value > | reduceAllRows (Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchValue &identity, const SegmentsReductionKernel &kernel=SegmentsReductionKernel{}) const
| Method for performing general reduction on all matrix rows for constant instances.
std::enable_if_t< Algorithms::SegmentsReductionKernels::isSegmentReductionKernel< SegmentsReductionKernel >::value > | reduceRows (IndexType begin, IndexType end, Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchValue &identity, const SegmentsReductionKernel &kernel=SegmentsReductionKernel{}) const
| Method for performing general reduction on matrix rows for constant instances.
void | sequentialForAllRows (Function &&function) const
| This method calls sequentialForRows for all matrix rows (for constant instances).
void | sequentialForRows (IndexType begin, IndexType end, Function &&function) const
| Method for sequential iteration over all matrix rows for constant instances.
__cuda_callable__ void | setElement (IndexType row, IndexType column, const RealType &value)
| Sets element at given row and column to given value.
void | sortColumnIndexes ()
| Sort matrix elements in each row by column indexes in ascending order.
void | transposedVectorProduct (const InVector &inVector, OutVector &outVector, typename ChooseSparseMatrixComputeReal< double, int >::type matrixMultiplicator=1.0, typename ChooseSparseMatrixComputeReal< double, int >::type outVectorMultiplicator=0.0, int begin=0, int end=0) const
| Computes product of transposed matrix and vector.
void | vectorProduct (const InVector &inVector, OutVector &outVector, ComputeRealType matrixMultiplicator=1.0, ComputeRealType outVectorMultiplicator=0.0, IndexType begin=0, IndexType end=0, const SegmentsReductionKernel &kernel=SegmentsReductionKernel{}) const
| Computes product of matrix and vector.
Public Member Functions inherited from TNL::Matrices::MatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType::getOrganization() >
__cuda_callable__ | MatrixBase ()=default
| Basic constructor with no parameters.
IndexType | getAllocatedElementsCount () const
| Tells the number of allocated matrix elements.
__cuda_callable__ IndexType | getColumns () const
| Returns number of matrix columns.
__cuda_callable__ IndexType | getRows () const
| Returns number of matrix rows.
__cuda_callable__ const ValuesViewType & | getValues () const
| Returns a constant reference to a vector with the matrix elements values.
__cuda_callable__ MatrixBase & | operator= (const MatrixBase &)=delete
| Copy-assignment operator.

## Protected Attributes
ColumnIndexesVectorType | columnIndexes
| Vector containing the column indexes of allocated elements.
SegmentsType | segments
| Instance of the segments used for indexing in the sparse matrix.
ValuesVectorType | values
| Vector containing the allocated matrix elements.
Protected Attributes inherited from TNL::Matrices::SparseMatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType, typename ChooseSparseMatrixComputeReal< double, int >::type >
ColumnIndexesViewType | columnIndexes
SegmentsViewType | segments
Protected Attributes inherited from TNL::Matrices::MatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType::getOrganization() >
IndexType | columns
IndexType | rows
ValuesViewType | values

## Additional Inherited Members
Static Public Member Functions inherited from TNL::Matrices::SparseMatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType, typename ChooseSparseMatrixComputeReal< double, int >::type >
static std::string | getSerializationType ()
| Returns string with serialization type.
Static Public Member Functions inherited from TNL::Matrices::MatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType::getOrganization() >
static constexpr ElementsOrganization | getOrganization ()
| Matrix elements organization getter.
static constexpr bool | isBinary ()
| Test of binary matrix type.
static constexpr bool | isSymmetric ()
| Test of symmetric matrix type.
Protected Member Functions inherited from TNL::Matrices::SparseMatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType, typename ChooseSparseMatrixComputeReal< double, int >::type >
__cuda_callable__ void | bind (IndexType rows, IndexType columns, typename Base::ValuesViewType values, ColumnIndexesViewType columnIndexes, SegmentsViewType segments)
| Re-initializes the internal attributes of the base class.
Protected Member Functions inherited from TNL::Matrices::MatrixBase< double, Devices::Host, int, GeneralMatrix, Algorithms::Segments::CSR< Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< int > >::ViewType::getOrganization() >
__cuda_callable__ void | bind (IndexType rows, IndexType columns, ValuesViewType values)
| Re-initializes the internal attributes of the base class.

## Detailed Description

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
class TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >

Implementation of sparse matrix, i.e. matrix storing only non-zero elements.

Template Parameters

  Real | is a type of matrix elements. If Real equals bool the matrix is treated as binary and so the matrix elements values are not stored in the memory since we need to remember only coordinates of non-zero elements( which equal one).
  Device | is a device where the matrix is allocated.
  Index | is a type for indexing of the matrix elements.
  MatrixType | specifies a symmetry of matrix. See MatrixType. Symmetric matrices store only lower part of the matrix and its diagonal. The upper part is reconstructed on the fly. GeneralMatrix with no symmetry is used by default.
  Segments | is a structure representing the sparse matrix format. Depending on the pattern of the non-zero elements different matrix formats can perform differently especially on GPUs. By default Algorithms::Segments::CSR format is used. See also Algorithms::Segments::Ellpack, Algorithms::Segments::SlicedEllpack, Algorithms::Segments::ChunkedEllpack, and Algorithms::Segments::BiEllpack.
  ComputeReal | is the same as Real mostly but for binary matrices it is set to Index type. This can be changed by the user, of course.
  RealAllocator | is allocator for the matrix elements values.
  IndexAllocator | is allocator for the matrix elements column indexes.

## Member Typedef Documentation

## ◆ ConstViewType

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

using TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::ConstViewType

Initial value:
SparseMatrixView< std::add_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate, ComputeReal >
TNL::Matrices::SparseMatrixView
Implementation of sparse matrix view.
Definition SparseMatrixView.h:72
TNL::Matrices::SparseMatrix::MatrixType
MatrixType_ MatrixType
The type of matrix - general, symmetric or binary.
Definition SparseMatrix.h:82
TNL::Matrices::SparseMatrix::SegmentsViewTemplate
typename SegmentsType::template ViewTemplate< Device_, Index > SegmentsViewTemplate
Templated view type of segments, i.e. sparse matrix format.
Definition SparseMatrix.h:99

Matrix view type for constant instances.

See SparseMatrixView.

## ◆ ViewType

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

using TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::ViewType = SparseMatrixView< Real, Device, Index, MatrixType, SegmentsViewTemplate, ComputeReal >

Type of related matrix view.

See SparseMatrixView.

## Constructor & Destructor Documentation

## ◆ SparseMatrix() [1/6]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::SparseMatrix | ( | const RealAllocatorType & | realAllocator = RealAllocatorType(),
|  | const IndexAllocatorType & | indexAllocator = IndexAllocatorType() )

Constructor only with values and column indexes allocators.

Parameters

  realAllocator | is used for allocation of matrix elements values.
  indexAllocator | is used for allocation of matrix elements column indexes.

## ◆ SparseMatrix() [2/6]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename Index_t, std::enable_if_t< std::is_integral_v< Index_t >, int > = 0>

TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::SparseMatrix | ( | Index_t | rows,
|  | Index_t | columns,
|  | const RealAllocatorType & | realAllocator = RealAllocatorType(),
|  | const IndexAllocatorType & | indexAllocator = IndexAllocatorType() )

Constructor with matrix dimensions.

Parameters

  rows | is number of matrix rows.
  columns | is number of matrix columns.
  realAllocator | is used for allocation of matrix elements values.
  indexAllocator | is used for allocation of matrix elements column indexes.

## ◆ SparseMatrix() [3/6]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename ListIndex>

TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::SparseMatrix | ( | const std::initializer_list< ListIndex > & | rowCapacities,
|  | Index | columns,
|  | const RealAllocatorType & | realAllocator = RealAllocatorType(),
|  | const IndexAllocatorType & | indexAllocator = IndexAllocatorType() )
| explicit

Constructor with matrix rows capacities and number of columns.

The number of matrix rows is given by the size of rowCapacities list.

Template Parameters

  ListIndex | is the initializer list values type.

Parameters

  rowCapacities | is a list telling how many matrix elements must be allocated in each row.
  columns | is the number of matrix columns.
  realAllocator | is used for allocation of matrix elements values.
  indexAllocator | is used for allocation of matrix elements column indexes.

Example
  #include <iostream>
  #include <TNL/Matrices/SparseMatrix.h>
  #include <TNL/Devices/Host.h>
  template< typename Device >
  void
  initializerListExample()
  {
  TNL::Matrices::SparseMatrix< double, Device > matrix{ { 1, 2, 3, 4, 5 }, // row capacities
  6 }; // number of matrix columns
  for( int row = 0; row < matrix.getRows(); row++ )
  for( int column = 0; column <= row; column++ )
  matrix.setElement( row, column, row - column + 1 );
  std::cout << "General sparse matrix:\n" << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrices on CPU ...\n";
  initializerListExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrices on CUDA GPU ...\n";
  initializerListExample< TNL::Devices::Cuda >();
  #endif
  }
  std::cout
  TNL::Matrices::MatrixBase::getRows
  __cuda_callable__ IndexType getRows() const
  Returns number of matrix rows.
  TNL::Matrices::SparseMatrixBase::setElement
  __cuda_callable__ void setElement(IndexType row, IndexType column, const RealType &value)
  Sets element at given row and column to given value.
  TNL::Matrices::SparseMatrix
  Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
  Definition SparseMatrix.h:55

Output
  Creating matrices on CPU ...
  General sparse matrix:
  Row: 0 -> 0:1
  Row: 1 -> 0:2 1:1
  Row: 2 -> 0:3 1:2 2:1
  Row: 3 -> 0:4 1:3 2:2 3:1
  Row: 4 -> 0:5 1:4 2:3 3:2 4:1

## ◆ SparseMatrix() [4/6]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename RowCapacitiesVector, std::enable_if_t< TNL::IsArrayType< RowCapacitiesVector >::value, int > = 0>

TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::SparseMatrix | ( | const RowCapacitiesVector & | rowCapacities,
|  | Index | columns,
|  | const RealAllocatorType & | realAllocator = RealAllocatorType(),
|  | const IndexAllocatorType & | indexAllocator = IndexAllocatorType() )
| explicit

Constructor with matrix rows capacities given as a vector and number of columns.

The number of matrix rows is given by the size of rowCapacities vector.

Template Parameters

  RowCapacitiesVector | is the row capacities vector type. Usually it is some of TNL::Containers::Array, TNL::Containers::ArrayView, TNL::Containers::Vector or TNL::Containers::VectorView.

Parameters

  rowCapacities | is a vector telling how many matrix elements must be allocated in each row.
  columns | is the number of matrix columns.
  realAllocator | is used for allocation of matrix elements values.
  indexAllocator | is used for allocation of matrix elements column indexes.

Example
  #include <iostream>
  #include <TNL/Matrices/SparseMatrix.h>
  #include <TNL/Containers/Vector.h>
  #include <TNL/Devices/Host.h>
  template< typename Device >
  void
  initializerListExample()
  {
  TNL::Containers::Vector< int, Device > rowCapacities{ 1, 2, 3, 4, 5 };
  TNL::Matrices::SparseMatrix< double, Device > matrix{ rowCapacities, // row capacities
  6 }; // number of matrix columns
  for( int row = 0; row < matrix.getRows(); row++ )
  for( int column = 0; column <= row; column++ )
  matrix.setElement( row, column, row - column + 1 );
  std::cout << "General sparse matrix:\n" << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrices on CPU ...\n";
  initializerListExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrices on CUDA GPU ...\n";
  initializerListExample< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Containers::Vector
  Vector extends Array with algebraic operations.
  Definition Vector.h:36

Output
  Creating matrices on CPU ...
  General sparse matrix:
  Row: 0 -> 0:1
  Row: 1 -> 0:2 1:1
  Row: 2 -> 0:3 1:2 2:1
  Row: 3 -> 0:4 1:3 2:2 3:1
  Row: 4 -> 0:5 1:4 2:3 3:2 4:1

## ◆ SparseMatrix() [5/6]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::SparseMatrix | ( | Index | rows,
|  | Index | columns,
|  | const std::initializer_list< std::tuple< Index, Index, Real > > & | data,
|  | SymmetricMatrixEncoding | encoding = SymmetricMatrixEncoding::LowerPart,
|  | const RealAllocatorType & | realAllocator = RealAllocatorType(),
|  | const IndexAllocatorType & | indexAllocator = IndexAllocatorType() )
| explicit

Constructor with matrix dimensions and data in initializer list.

The matrix elements values are given as a list data of triples: { { row1, column1, value1 }, { row2, column2, value2 }, ... }.

Parameters

  rows | is number of matrix rows.
  columns | is number of matrix columns.
  data | is a list of matrix elements values.
  realAllocator | is used for allocation of matrix elements values.
  encoding | defines encoding for sparse symmetric matrices - see TNL::Matrices::SymmetricMatrixEncoding.
  indexAllocator | is used for allocation of matrix elements column indexes.

Example
  #include <iostream>
  #include <TNL/Matrices/SparseMatrix.h>
  #include <TNL/Devices/Host.h>
  template< typename Device >
  void
  initializerListExample()
  {
  TNL::Matrices::SparseMatrix< double, Device > matrix(
  // number of matrix rows
  5,
  // number of matrix columns
  5,
  // matrix elements definition
  {
  // clang-format off
  { 0, 0, 2.0 },
  { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 },
  { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 },
  { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 },
  { 4, 4, 2.0 },
  // clang-format on
  } );
  std::cout << "General sparse matrix:\n" << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrices on CPU ...\n";
  initializerListExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrices on CUDA GPU ...\n";
  initializerListExample< TNL::Devices::Cuda >();
  #endif
  }

Output
  Creating matrices on CPU ...
  General sparse matrix:
  Row: 0 -> 0:2
  Row: 1 -> 0:-1 1:2 2:-1
  Row: 2 -> 1:-1 2:2 3:-1
  Row: 3 -> 2:-1 3:2 4:-1
  Row: 4 -> 4:2

## ◆ SparseMatrix() [6/6]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename MapIndex, typename MapValue>

TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::SparseMatrix | ( | Index | rows,
|  | Index | columns,
|  | const std::map< std::pair< MapIndex, MapIndex >, MapValue > & | map,
|  | SymmetricMatrixEncoding | encoding = SymmetricMatrixEncoding::LowerPart,
|  | const RealAllocatorType & | realAllocator = RealAllocatorType(),
|  | const IndexAllocatorType & | indexAllocator = IndexAllocatorType() )
| explicit

Constructor with matrix dimensions and data in std::map.

The matrix elements values are given as a map data where keys are std::pair of matrix coordinates ( {row, column} ) and value is the matrix element value.

Template Parameters

  MapIndex | is a type for indexing rows and columns.
  MapValue | is a type for matrix elements values in the map.

Parameters

  rows | is number of matrix rows.
  columns | is number of matrix columns.
  map | is std::map containing matrix elements.
  encoding | defines encoding for sparse symmetric matrices - see TNL::Matrices::SymmetricMatrixEncoding.
  realAllocator | is used for allocation of matrix elements values.
  indexAllocator | is used for allocation of matrix elements column indexes.

Example
  #include <iostream>
  #include <map>
  #include <utility>
  #include <TNL/Matrices/SparseMatrix.h>
  #include <TNL/Devices/Host.h>
  template< typename Device >
  void
  initializerListExample()
  {
  std::map< std::pair< int, int >, double > map;
  map.insert( std::make_pair( std::make_pair( 0, 0 ), 2.0 ) );
  map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 1, 1 ), 2.0 ) );
  map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 2, 2 ), 2.0 ) );
  map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 3, 3 ), 2.0 ) );
  map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 4, 4 ), 2.0 ) );
  TNL::Matrices::SparseMatrix< double, Device > matrix( 5, 5, map );
  std::cout << "General sparse matrix:\n" << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrices on CPU ...\n";
  initializerListExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrices on CUDA GPU ...\n";
  initializerListExample< TNL::Devices::Cuda >();
  #endif
  }
  std::map::insert
  T insert(T... args)
  std::make_pair
  T make_pair(T... args)
  std::map

Output
  Creating matrices on CPU ...
  General sparse matrix:
  Row: 0 -> 0:2
  Row: 1 -> 0:-1 1:2 2:-1
  Row: 2 -> 1:-1 2:2 3:-1
  Row: 3 -> 2:-1 3:2 4:-1
  Row: 4 -> 4:2

## Member Function Documentation

## ◆ getConstView()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

ConstViewType TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::getConstView | ( |  | ) | const
| nodiscard

Returns a non-modifiable view of the sparse matrix.

See SparseMatrixView.

Returns
  sparse matrix view.

## ◆ getTransposition()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename Real2, typename Index2, template< typename, typename, typename > class Segments2>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::getTransposition | ( | const SparseMatrix< Real2, Device, Index2, MatrixType, Segments2 > & | matrix,
|  | const ComputeReal & | matrixMultiplicator = 1.0 )

Computes transposition of the matrix.

Template Parameters

  Real2 | is the real type of the input matrix.
  Index2 | is the index type of the input matrix.
  Segments2 | is the type of the segments of the input matrix.

Parameters

  matrix | is the input matrix.
  matrixMultiplicator | is the factor by which the matrix is multiplied.

## ◆ getView()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

ViewType TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::getView | ( |  | ) |
| nodiscard

Returns a modifiable view of the sparse matrix.

See SparseMatrixView.

Returns
  sparse matrix view.

## ◆ load()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::load | ( | const String & | fileName | ) |

Method for loading the matrix from the file with given filename.

Parameters

  fileName | is name of the file.

## ◆ operator=() [1/4]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename Real_, typename Device_, typename Index_, ElementsOrganization Organization, typename RealAllocator_>

SparseMatrix & TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator= | ( | const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > & | matrix | ) |

Assignment of dense matrix.

Parameters

  matrix | is input matrix for the assignment.

Returns
  reference to this matrix.

## ◆ operator=() [2/4]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename RHSMatrix>

SparseMatrix & TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator= | ( | const RHSMatrix & | matrix | ) |

Assignment of any matrix type other then this and dense.

Warning: Assignment of symmetric sparse matrix to general sparse matrix does not give correct result, currently. Only the diagonal and the lower part of the matrix is assigned.

Parameters

  matrix | is input matrix for the assignment.

Returns
  reference to this matrix.

## ◆ operator=() [3/4]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

SparseMatrix & TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator= | ( | const SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator > & | matrix | ) |

Copy-assignment of exactly the same matrix type.

Parameters

  matrix | is input matrix for the assignment.

Returns
  reference to this matrix.

## ◆ operator=() [4/4]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

SparseMatrix & TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator= | ( | SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator > && | matrix | ) |

Move-assignment of exactly the same matrix type.

Parameters

  matrix | is input matrix for the assignment.

Returns
  reference to this matrix.

## ◆ save()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::save | ( | const String & | fileName | ) | const

Method for saving the matrix to the file with given filename.

Parameters

  fileName | is name of the file.

## ◆ setColumnsWithoutReset()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::setColumnsWithoutReset | ( | Index | columns | ) |

Set number of columns of this matrix.

Unlike setDimensions, the storage is not reset in this operation. It is the user's responsibility to update the column indices stored in the matrix to be consistent with the new number of columns.

Parameters

  columns | is the number of matrix columns.

## ◆ setDimensions() [1/2]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::setDimensions | ( | Index | rows,
|  | Index | columns )

Set number of rows and columns of this matrix.

Parameters

  rows | is the number of matrix rows.
  columns | is the number of matrix columns.

## ◆ setDimensions() [2/2]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::setDimensions | ( | Index | rows,
|  | Index | columns,
|  | SegmentsType | segments )

Set number of rows and columns of this matrix and performs full allocation according to the given segments.

Parameters

  rows | is the number of matrix rows.
  columns | is the number of matrix columns.
  segments | is the segments instance specifying the storage to be allocated.

## ◆ setElements() [1/2]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::setElements | ( | const std::initializer_list< std::tuple< Index, Index, Real > > & | data,
|  | SymmetricMatrixEncoding | encoding = SymmetricMatrixEncoding::LowerPart )

This method sets the sparse matrix elements from initializer list.

The number of matrix rows and columns must be set already. The matrix elements values are given as a list data of triples: { { row1, column1, value1 }, { row2, column2, value2 }, ... }.

Parameters

  data | is a initializer list of initializer lists representing list of matrix rows.
  encoding | defines encoding for sparse symmetric matrices - see TNL::Matrices::SymmetricMatrixEncoding.

Example
  #include <iostream>
  #include <TNL/Matrices/SparseMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  setElementsExample()
  {
  TNL::Matrices::SparseMatrix< double, Device > matrix( 5, 5 ); // matrix dimensions
  // matrix elements definition
  matrix.setElements( {
  // clang-format off
  { 0, 0, 2.0 },
  { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 },
  { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 },
  { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 },
  { 4, 4, 2.0 },
  // clang-format on
  } );
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Setting matrix elements on host:\n";
  setElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Setting matrix elements on CUDA device:\n";
  setElementsExample< TNL::Devices::Cuda >();
  #endif
  }

Output
  Setting matrix elements on host:
  Row: 0 -> 0:2
  Row: 1 -> 0:-1 1:2 2:-1
  Row: 2 -> 1:-1 2:2 3:-1
  Row: 3 -> 2:-1 3:2 4:-1
  Row: 4 -> 4:2

## ◆ setElements() [2/2]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename MapIndex, typename MapValue>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::setElements | ( | const std::map< std::pair< MapIndex, MapIndex >, MapValue > & | map,
|  | SymmetricMatrixEncoding | encoding = SymmetricMatrixEncoding::LowerPart )

This method sets the sparse matrix elements from std::map.

The matrix elements values are given as a map data where keys are std::pair of matrix coordinates ( {row, column} ) and value is the matrix element value.

Template Parameters

  MapIndex | is a type for indexing rows and columns.
  MapValue | is a type for matrix elements values in the map.

Parameters

  map | is std::map containing matrix elements.
  encoding | defines encoding for sparse symmetric matrices - see TNL::Matrices::SymmetricMatrixEncoding.

Example
  #include <iostream>
  #include <map>
  #include <utility>
  #include <TNL/Matrices/SparseMatrix.h>
  #include <TNL/Devices/Host.h>
  template< typename Device >
  void
  setElementsExample()
  {
  std::map< std::pair< int, int >, double > map;
  map.insert( std::make_pair( std::make_pair( 0, 0 ), 2.0 ) );
  map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 1, 1 ), 2.0 ) );
  map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 2, 2 ), 2.0 ) );
  map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 3, 3 ), 2.0 ) );
  map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) );
  map.insert( std::make_pair( std::make_pair( 4, 4 ), 2.0 ) );
  TNL::Matrices::SparseMatrix< double, Device > matrix( 5, 5 );
  matrix.setElements( map );
  std::cout << "General sparse matrix:\n" << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrices on CPU ...\n";
  setElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrices on CUDA GPU ...\n";
  setElementsExample< TNL::Devices::Cuda >();
  #endif
  }

Output
  Creating matrices on CPU ...
  General sparse matrix:
  Row: 0 -> 0:2
  Row: 1 -> 0:-1 1:2 2:-1
  Row: 2 -> 1:-1 2:2 3:-1
  Row: 3 -> 2:-1 3:2 4:-1
  Row: 4 -> 4:2

## ◆ setLike()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename Matrix>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::setLike | ( | const Matrix & | matrix | ) |

Set the number of matrix rows and columns by the given matrix.

Template Parameters

  Matrix | is matrix type. This can be any matrix having methods getRows and getColumns.

Parameters

  matrix | in the input matrix dimensions of which are to be adopted.

## ◆ setRowCapacities()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename MatrixType_ = GeneralMatrix, template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Algorithms::Segments::CSR, typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type, typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename RowCapacitiesVector>

void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType_, Segments, ComputeReal, RealAllocator, IndexAllocator >::setRowCapacities | ( | const RowCapacitiesVector & | rowCapacities | ) |

Allocates memory for non-zero matrix elements.

The size of the input vector must be equal to the number of matrix rows. The number of allocated matrix elements for each matrix row depends on the sparse matrix format. Some formats may allocate more elements than required.

Template Parameters

  RowCapacitiesVector | is a type of vector/array used for row capacities setting.

Parameters

  rowCapacities | is a vector telling the number of required non-zero matrix elements in each row.

Example
  #include <iostream>
  #include <TNL/Containers/Vector.h>
  #include <TNL/Matrices/SparseMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  setRowCapacitiesExample()
  {
  TNL::Matrices::SparseMatrix< double, Device > matrix( 5, 5 );
  TNL::Containers::Vector< int, Device > rowCapacities{ 1, 2, 3, 4, 5 };
  matrix.setRowCapacities( rowCapacities );
  for( int row = 0; row < 5; row++ )
  for( int column = 0; column <= row; column++ )
  matrix.setElement( row, column, row - column + 1 );
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on CPU ...\n";
  setRowCapacitiesExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA GPU ...\n";
  setRowCapacitiesExample< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Containers::Array::setElement
  __cuda_callable__ void setElement(IndexType i, ValueType value)
  Sets the value of the i-th element to v.

Output
  Creating matrix on CPU ...
  Row: 0 -> 0:1
  Row: 1 -> 0:2 1:1
  Row: 2 -> 0:3 1:2 2:1
  Row: 3 -> 0:4 1:3 2:2 3:1
  Row: 4 -> 0:5 1:4 2:3 3:2 4:1

The documentation for this class was generated from the following file:

- src/TNL/Matrices/SparseMatrix.h