ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CsrMatrix.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File CsrMatrix.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
73/*--------------------------------------------------------------------------*/
74/*----------------------------- DEFINITIONS --------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77#ifndef __ADS_CsrMatrix
78 #define __ADS_CsrMatrix
79/* self-identification: #endif constant */
80
81/*--------------------------------------------------------------------------*/
82/*------------------------------ INCLUDES ----------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85#include "ADS/Contract.h"
89
90#include <algorithm>
91#include <concepts>
92#include <cstddef>
93#include <cstdint>
94#include <limits>
95#include <numeric>
96#include <string_view>
97#include <tuple>
98#include <type_traits>
99#include <utility>
100#include <vector>
101
102/*--------------------------------------------------------------------------*/
103/*--------------------------- NAMESPACE ------------------------------------*/
104/*--------------------------------------------------------------------------*/
105
106namespace ADS
107{
108
109/*--------------------------------------------------------------------------*/
110/*-------------------------- CLASS BasicCsrMatrix --------------------------*/
111/*--------------------------------------------------------------------------*/
112
113namespace detail
114{
115
118template< class I >
119using CsrOffset =
120 std::conditional_t< std::is_same_v< I , std::uint32_t > , std::size_t , I >;
121
122} // end( namespace detail )
123
126template< class T = double , std::integral I = std::uint32_t ,
127 std::integral P = detail::CsrOffset< I > >
129{
130 public:
131 using Value = T;
132 using Index = I;
133 using Offset = P;
134
136 using Triplet = std::tuple< std::size_t , std::size_t , T >;
137
139 BasicCsrMatrix( std::size_t m = 0 , std::size_t n = 0 );
140
164 BasicCsrMatrix( std::size_t m , std::size_t n , std::vector< Triplet > E );
165
168 explicit BasicCsrMatrix( const SparseMatrix & S );
169
171 [[nodiscard]] std::size_t rows( void ) const { return( f_m ); }
173 [[nodiscard]] std::size_t cols( void ) const { return( f_n ); }
175 [[nodiscard]] std::size_t nonzeros( void ) const { return( f_val.size() ); }
176
179 [[nodiscard]] T get( std::size_t i , std::size_t j ) const;
180
183 [[nodiscard]] std::vector< T > multiply( const std::vector< T > & x ) const;
184
200 void multiply( const T * x , T * y ) const;
201
225 [[nodiscard]] BasicCsrMatrix transpose( void ) const;
226
228 [[nodiscard]] const P * rowStart( void ) const { return( f_row.data() ); }
230 [[nodiscard]] const I * columns( void ) const { return( f_col.data() ); }
232 [[nodiscard]] const T * values( void ) const { return( f_val.data() ); }
233
235 bool operator==( const BasicCsrMatrix & ) const = default;
236
237/*----------------------------- THE PERSISTENCE ----------------------------*/
238
240 static constexpr std::string_view persistName = "CsrMatrix";
241
243 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
244 return( detail::persistMix( { sizeof( T ) , std::is_floating_point_v< T > ,
245 sizeof( I ) , std::is_signed_v< I > ,
246 sizeof( P ) , std::is_signed_v< P > } ) );
247 }
248
251 template< class A >
252 void persist( A & a );
253
254 private:
256 [[nodiscard]] bool sorted( void ) const;
257
258 template< class U > // not zeroed when they grow, a view of a mapped file
259 using Vector = std::vector< U , MappableAllocator< U , false > >;
260
263 template< class U >
264 static constexpr bool fits( std::size_t x ) {
265 return( ( x == 0 ) ||
266 ( x - 1 <= std::size_t( std::numeric_limits< U >::max() ) ) );
267 }
268
269 std::size_t f_m ,
270 f_n;
271 Vector< P > f_row;
272 Vector< I > f_col;
273 Vector< T > f_val;
274 }; // end( class BasicCsrMatrix )
275
278
279/*--------------------------------------------------------------------------*/
280
281} // end( namespace ADS )
282
283/*--------------------------------------------------------------------------*/
284/*-------------------------- IMPLEMENTATION --------------------------------*/
285/*--------------------------------------------------------------------------*/
286
287#include "ADS/numeric/impl/CsrMatrix.tpp"
288
289/*--------------------------------------------------------------------------*/
290
291#endif /* CsrMatrix.h included */
292
293/*--------------------------------------------------------------------------*/
294/*-------------------------- End File CsrMatrix.h --------------------------*/
295/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The dense matrices by rows, beyond the book: DenseMatrix keeps the entries by rows in one contiguous...
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
A sparse matrix on orthogonal lists, beyond the book (section 2.2.6 of Knuth): each nonzero entry is ...
a sparse matrix of values T in compressed sparse rows, the columns of the type I and the offsets of ...
Definition CsrMatrix.h:129
std::vector< T > multiply(const std::vector< T > &x) const
the product , a new vector
const I * columns(void) const
the column of each nonzero, row by row
Definition CsrMatrix.h:230
std::size_t nonzeros(void) const
the number of the entries kept
Definition CsrMatrix.h:175
std::size_t rows(void) const
the number of rows
Definition CsrMatrix.h:171
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
void multiply(const T *x, T *y) const
the product into , which must have entries
T Value
the type of the values
Definition CsrMatrix.h:131
BasicCsrMatrix(const SparseMatrix &S)
the matrix of the orthogonal lists of the book, row by row
static constexpr std::uint64_t persistTag(void)
the types of the values, of the columns and of the offsets, in a file
Definition CsrMatrix.h:243
BasicCsrMatrix(std::size_t m, std::size_t n, std::vector< Triplet > E)
the matrix of the triplets , the values of equal places summed
P Offset
the type of the offsets of the rows
Definition CsrMatrix.h:133
const P * rowStart(void) const
the offsets of the rows
Definition CsrMatrix.h:228
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition CsrMatrix.h:240
T get(std::size_t i, std::size_t j) const
the entry , by a binary search in the row
const T * values(void) const
the value of each nonzero, row by row
Definition CsrMatrix.h:232
I Index
the type of the columns
Definition CsrMatrix.h:132
std::tuple< std::size_t, std::size_t, T > Triplet
an entry
Definition CsrMatrix.h:136
bool operator==(const BasicCsrMatrix &) const =default
true if the two matrices have the same sizes and the same entries kept
BasicCsrMatrix transpose(void) const
the transpose,
BasicCsrMatrix(std::size_t m=0, std::size_t n=0)
the zero matrix
std::size_t cols(void) const
the number of columns
Definition CsrMatrix.h:173
a sparse matrix on orthogonal circular lists
Definition SparseMatrix.h:119
BasicCsrMatrix<> CsrMatrix
the matrix of doubles of 32-bit columns and std::size_t offsets
Definition CsrMatrix.h:277
Definition ADT.h:96