ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SparseMatrix.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File SparseMatrix.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
37/*--------------------------------------------------------------------------*/
38/*----------------------------- DEFINITIONS --------------------------------*/
39/*--------------------------------------------------------------------------*/
40
41#ifndef __ADS_SparseMatrix
42 #define __ADS_SparseMatrix
43/* self-identification: #endif constant */
44
45/*--------------------------------------------------------------------------*/
46/*------------------------------ INCLUDES ----------------------------------*/
47/*--------------------------------------------------------------------------*/
48
49#include "ADS/Contract.h"
50
51#include <cmath>
52#include <cstddef>
53#include <iterator>
54#include <map>
55#include <ranges>
56#include <utility>
57#include <vector>
58
59/*--------------------------------------------------------------------------*/
60/*--------------------------- NAMESPACE ------------------------------------*/
61/*--------------------------------------------------------------------------*/
62
63namespace ADS
64{
65
66namespace detail
67{
68
72template< class Node >
73class OrthogonalIterator
74{
75 public:
76 using reference = std::pair< std::size_t , double >;
77 using value_type = reference;
78 using difference_type = std::ptrdiff_t;
79 using iterator_concept = std::forward_iterator_tag;
80 using iterator_category = std::input_iterator_tag;
81
82 OrthogonalIterator( void ) = default;
83 OrthogonalIterator( const std::vector< Node > * N , long q , bool byRow )
84 : f_N( N ) , f_q( q ) , f_row( byRow ) { }
85
87 reference operator*( void ) const {
88 const Node & x = ( *f_N )[ f_q ];
89 return( reference( f_row ? x.col : x.row , x.val ) );
90 }
91 OrthogonalIterator & operator++( void ) {
92 f_q = f_row ? ( *f_N )[ f_q ].right : ( *f_N )[ f_q ].down;
93 return( *this );
94 }
95 OrthogonalIterator operator++( int ) {
96 auto t = *this;
97 ++*this;
98 return( t );
99 }
100 bool operator==( const OrthogonalIterator & o ) const {
101 return( f_q == o.f_q );
102 }
103
104 private:
105 const std::vector< Node > * f_N = nullptr;
106 long f_q = -1;
107 bool f_row = true;
108
109 }; // end( class OrthogonalIterator )
110
111} // end( namespace detail )
112
113/*--------------------------------------------------------------------------*/
114/*-------------------------- CLASS SparseMatrix ----------------------------*/
115/*--------------------------------------------------------------------------*/
116
119{
120
121/*--------------------------------------------------------------------------*/
122 public:
125 SparseMatrix( std::size_t m , std::size_t n );
126
128 [[nodiscard]] std::size_t rows( void ) const;
130 [[nodiscard]] std::size_t cols( void ) const;
131
133 [[nodiscard]] std::size_t nonzeros( void ) const;
134
138 [[nodiscard]] double get( std::size_t i , std::size_t j ) const;
139
163 void set( std::size_t i , std::size_t j , double v );
164
167 [[nodiscard]] std::vector< std::pair< std::size_t , double > >
168 row( std::size_t i ) const;
169
172 [[nodiscard]] std::vector< std::pair< std::size_t , double > >
173 col( std::size_t j ) const;
174
204 void pivot( std::size_t r , std::size_t c , double eps = 0 );
205
206/*--------------------------------------------------------------------------*/
207 private:
209 struct Node
210 {
211 std::size_t row ,
212 col;
213 double val;
214 long right ,
215 down;
216 };
217
218 public:
219/*------------------------- THE STANDARD INTERFACE -------------------------*/
220
221 // the nonzeros of a row or of a column as ranges, forward along the
222 // orthogonal lists, instead of the vectors that row() and col() build
223
224 using const_iterator = detail::OrthogonalIterator< Node >;
225
228 [[nodiscard]] std::ranges::subrange< const_iterator >
229 rowEntries( std::size_t i ) const;
230
233 [[nodiscard]] std::ranges::subrange< const_iterator >
234 colEntries( std::size_t j ) const;
235
236 private:
239 [[nodiscard]] std::pair< long , long > findInRow( std::size_t i ,
240 std::size_t j ) const;
242 [[nodiscard]] long beforeInCol( std::size_t i , std::size_t j ) const;
243
245 [[nodiscard]] bool linked( void ) const;
246
247 std::size_t f_m ,
248 f_n;
250 std::vector< Node > f_N;
251 std::vector< long > f_freeNodes;
252 std::size_t f_nnz = 0;
253
254 }; // end( class SparseMatrix )
255
256/*--------------------------------------------------------------------------*/
257
258} // end( namespace ADS )
259
260/*--------------------------------------------------------------------------*/
261/*-------------------------- IMPLEMENTATION --------------------------------*/
262/*--------------------------------------------------------------------------*/
263
264#include "ADS/numeric/impl/SparseMatrix.tpp"
265
266/*--------------------------------------------------------------------------*/
267
268#endif /* SparseMatrix.h included */
269
270/*--------------------------------------------------------------------------*/
271/*------------------------ End File SparseMatrix.h -------------------------*/
272/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
std::ranges::subrange< const_iterator > rowEntries(std::size_t i) const
the nonzeros of the row as (column, value), by column
void set(std::size_t i, std::size_t j, double v)
sets the entry to , removing its node if is 0
std::vector< std::pair< std::size_t, double > > col(std::size_t j) const
the nonzeros of the column , by row
detail::OrthogonalIterator< Node > const_iterator
Definition SparseMatrix.h:224
std::vector< std::pair< std::size_t, double > > row(std::size_t i) const
the nonzeros of the row , by column
SparseMatrix(std::size_t m, std::size_t n)
the zero matrix of rows and columns, the head nodes alone
std::size_t cols(void) const
the number of columns
std::size_t nonzeros(void) const
the number of nonzero entries
std::size_t rows(void) const
the number of rows
std::ranges::subrange< const_iterator > colEntries(std::size_t j) const
the nonzeros of the column as (row, value), by row
void pivot(std::size_t r, std::size_t c, double eps=0)
the pivot step on the entry ; the entries of absolute value at most vanish
double get(std::size_t i, std::size_t j) const
the entry , 0 if it has no node, by a walk of the row
Definition ADT.h:96