ADOL-C: Automatic Differentiation in C/C++ 2.7.3
Loading...
Searching...
No Matches
sparsematrix.h
Go to the documentation of this file.
1#ifndef ADOLC_SPARSE_MATRIX_H
2#define ADOLC_SPARSE_MATRIX_H
3
6#include <array>
7#include <cassert>
8#include <cmath>
9#include <cstddef>
10#include <utility>
11#include <vector>
12
13namespace ADOLC::Sparse {
14
23public:
24 using coordinate_type = unsigned int;
30
31private:
32 Coordinates coords_;
33 double value_{0.0};
34
35public:
38
41
44 operator=(CoordinateFormatTripled &&) noexcept = default;
45
52 constexpr CoordinateFormatTripled(Coordinates coords, double value)
53 : coords_{coords}, value_(value) {}
64
66 coordinate_type rowIndex() const noexcept { return coords_.rowIndex_; }
68 coordinate_type colIndex() const noexcept { return coords_.colIndex_; }
70 double value() const noexcept { return value_; }
71};
72
82public:
84 using storage_type = std::vector<value_type>;
85
86private:
87 storage_type entries_;
88
89public:
90 ~SparseMatrix() = default;
92 SparseMatrix() = default;
100 explicit SparseMatrix(storage_type &&entries) noexcept
101 : entries_(std::move(entries)) {}
102
111 template <size_t N>
112 explicit SparseMatrix(
113 const std::array<CoordinateFormatTripled, N> &entries) noexcept
114 : entries_(entries.begin(), entries.end()) {}
123 explicit SparseMatrix(size_t size) : entries_(size) {}
124
125 SparseMatrix(const SparseMatrix &other) = default;
126 SparseMatrix &operator=(const SparseMatrix &other) = default;
127
128 SparseMatrix(SparseMatrix &&other) noexcept = default;
129 SparseMatrix &operator=(SparseMatrix &&other) noexcept = default;
130
132 storage_type &entries() { return entries_; }
134 const storage_type &entries() const { return entries_; }
135
137 size_t size() const { return entries_.size(); }
139 bool empty() const { return entries_.empty(); }
140
146 void resize(size_t size) { entries_.resize(size); }
148 void clear() { entries_.clear(); }
149
155 void push_back(CoordinateFormatTripled value) { entries_.push_back(value); }
156
162 unsigned int rowIndex(size_t idx) const { return entries_[idx].rowIndex(); }
168 unsigned int colIndex(size_t idx) const { return entries_[idx].colIndex(); }
174 double value(size_t idx) const { return entries_[idx].value(); }
175
181 value_type &operator[](size_t idx) { return entries_[idx]; }
187 const value_type &operator[](size_t idx) const { return entries_[idx]; }
188};
189
190namespace detail {
191
193enum class SparseANFBlock { Y, J, Z, L };
194
195inline SparseANFBlock
197 int indep) {
198 const bool isDependentRow =
199 coords.rowIndex_ < static_cast<unsigned int>(depen);
200 const bool isIndependentCol =
201 coords.colIndex_ < static_cast<unsigned int>(indep);
202
203 if (isDependentRow && isIndependentCol)
204 return SparseANFBlock::Y;
205 else if (isDependentRow)
206 return SparseANFBlock::J;
207 else if (isIndependentCol)
208 return SparseANFBlock::Z;
209 else
210 return SparseANFBlock::L;
211}
212} // namespace detail
213
221 size_t y{0};
222 size_t j{0};
223 size_t z{0};
224 size_t l{0};
225
226 SparseShape() = default;
227 SparseShape(size_t y, size_t j, size_t z, size_t l)
228 : y(y), j(j), z(z), l(l) {};
229};
230
247struct SparseANF {
249
251
260
262 std::vector<double> cz;
264 std::vector<double> cy;
266 std::vector<double> y;
268 std::vector<double> z;
269
270 ~SparseANF() = default;
271 SparseANF() = default;
272
274
275 SparseANF(SparseANF &&) = default;
277
278 SparseANF(const SparseANF &) = default;
279 SparseANF &operator=(const SparseANF &) = default;
280
282 bool empty() const {
283 return shape.j == 0 && shape.l == 0 && shape.y == 0 && shape.z == 0;
284 }
285
287 void clear() {
288 shape = {};
289 Y.clear();
290 J.clear();
291 Z.clear();
292 L.clear();
293 cy.clear();
294 cz.clear();
295 }
296
297 Shape dims() const { return shape; }
298
300 void updateCy() {
301 cy = y;
302 for (size_t i = 0; i < J.size(); ++i) {
303 cy[J.rowIndex(i)] -= J.value(i) * std::fabs(z[J.colIndex(i)]);
304 }
305 }
306
308 void updateCz() {
309 cz = z;
310 for (size_t i = 0; i < L.size(); ++i) {
311 cz[L.rowIndex(i)] -= L.value(i) * std::fabs(z[L.colIndex(i)]);
312 }
313 }
314
321 shape = dims;
322 Y.resize(shape.y);
323 J.resize(shape.j);
324 Z.resize(shape.z);
325 L.resize(shape.l);
326 }
327};
328
329} // namespace ADOLC::Sparse
330
331#endif // ADOLC_SPARSE_MATRIX_H
One nonzero entry of a sparse matrix in coordinate format.
Definition sparsematrix.h:22
constexpr CoordinateFormatTripled(coordinate_type rowIndex, coordinate_type colIndex, double value)
Construct one coordinate-format triplet.
Definition sparsematrix.h:61
double value() const noexcept
Get the numerical value.
Definition sparsematrix.h:70
unsigned int coordinate_type
Definition sparsematrix.h:24
coordinate_type colIndex() const noexcept
Get the column index.
Definition sparsematrix.h:68
CoordinateFormatTripled & operator=(const CoordinateFormatTripled &)=default
CoordinateFormatTripled(CoordinateFormatTripled &&) noexcept=default
CoordinateFormatTripled(const CoordinateFormatTripled &)=default
coordinate_type rowIndex() const noexcept
Get the row index.
Definition sparsematrix.h:66
Owning sparse matrix container in coordinate-format.
Definition sparsematrix.h:81
const value_type & operator[](size_t idx) const
Read-only indexed access to one sparse entry.
Definition sparsematrix.h:187
void resize(size_t size)
Resize the number of stored entries.
Definition sparsematrix.h:146
size_t size() const
Get the number of stored nonzeros.
Definition sparsematrix.h:137
unsigned int colIndex(size_t idx) const
Get the column index of one stored entry.
Definition sparsematrix.h:168
SparseMatrix(const std::array< CoordinateFormatTripled, N > &entries) noexcept
Construct from an already prepared array of coordinate-format entries.
Definition sparsematrix.h:112
SparseMatrix & operator=(const SparseMatrix &other)=default
std::vector< value_type > storage_type
Definition sparsematrix.h:84
void push_back(CoordinateFormatTripled value)
Append one entry at the end of the sparse matrix.
Definition sparsematrix.h:155
const storage_type & entries() const
Get read-only access to the underlying entry storage.
Definition sparsematrix.h:134
SparseMatrix(const SparseMatrix &other)=default
bool empty() const
Check whether no entries are stored.
Definition sparsematrix.h:139
storage_type & entries()
Get mutable access to the underlying entry storage.
Definition sparsematrix.h:132
SparseMatrix(SparseMatrix &&other) noexcept=default
SparseMatrix(size_t size)
Construct a sparse matrix with storage for size entries.
Definition sparsematrix.h:123
void clear()
Remove all entries while keeping vector ownership.
Definition sparsematrix.h:148
SparseMatrix()=default
Construct an empty sparse matrix.
unsigned int rowIndex(size_t idx) const
Get the row index of one stored entry.
Definition sparsematrix.h:162
double value(size_t idx) const
Get the numerical value of one stored entry.
Definition sparsematrix.h:174
SparseMatrix(storage_type &&entries) noexcept
Construct from already prepared coordinate-format entries.
Definition sparsematrix.h:100
SparseMatrix & operator=(SparseMatrix &&other) noexcept=default
value_type & operator[](size_t idx)
Mutable indexed access to one sparse entry.
Definition sparsematrix.h:181
SparseANFBlock
Represents the different ANF blocks.
Definition sparsematrix.h:193
SparseANFBlock classifySparseANFBlock(CoordinateFormatTripled::Coordinates coords, int depen, int indep)
Definition sparsematrix.h:196
Definition adtl_indo.h:46
Base tag for abs-normal form shapes.
Definition absnormalformconcept.h:10
Zero-based row and column coordinates of one sparse entry.
Definition sparsematrix.h:26
coordinate_type rowIndex_
Definition sparsematrix.h:27
coordinate_type colIndex_
Definition sparsematrix.h:28
Stores the abs-normal form in sparse format.
Definition sparsematrix.h:247
Shape shape
Definition sparsematrix.h:250
void clear()
Clear all sparse blocks and dense vectors.
Definition sparsematrix.h:287
SparseMatrix L
Switching-equation block L = dz / d|z|.
Definition sparsematrix.h:259
std::vector< double > z
Switching-variable values at the evaluation point.
Definition sparsematrix.h:268
SparseMatrix Z
Switching-equation Jacobian block Z = dz / dx.
Definition sparsematrix.h:257
bool empty() const
Check whether all sparse blocks and dense vectors are empty.
Definition sparsematrix.h:282
SparseANF(SparseANF &&)=default
SparseMatrix J
Output block J = dy / d|z|.
Definition sparsematrix.h:255
void updateCz()
Compute cz = z - L|z| from the current data.
Definition sparsematrix.h:308
Shape dims() const
Definition sparsematrix.h:297
SparseMatrix Y
Output Jacobian block Y = dy / dx.
Definition sparsematrix.h:253
std::vector< double > y
Dependent values at the evaluation point.
Definition sparsematrix.h:266
std::vector< double > cy
Constant term of the dependent equations.
Definition sparsematrix.h:264
std::vector< double > cz
Constant term of the switching equations.
Definition sparsematrix.h:262
void updateCy()
Compute cy = y - J|z| from the current data.
Definition sparsematrix.h:300
SparseANF(Shape dims)
Definition sparsematrix.h:273
SparseANF & operator=(const SparseANF &)=default
SparseANF(const SparseANF &)=default
SparseANF & operator=(SparseANF &&)=default
void resize(Shape dims)
Resize sparse blocks according to the given block counts.
Definition sparsematrix.h:320
Nonzero counts for the four blocks of a sparse abs-normal form.
Definition sparsematrix.h:220
size_t z
Definition sparsematrix.h:223
size_t l
Definition sparsematrix.h:224
size_t y
Definition sparsematrix.h:221
size_t j
Definition sparsematrix.h:222
SparseShape(size_t y, size_t j, size_t z, size_t l)
Definition sparsematrix.h:227