alpaqa 1.0.0a19
Nonconvex constrained optimization
Loading...
Searching...
No Matches
sparsity.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <alpaqa/export.hpp>
5#include <cassert>
6#include <concepts>
7#include <variant>
8
9namespace alpaqa::sparsity {
10
11/// Describes the symmetry of matrices.
12enum class Symmetry {
13 Unsymmetric = 0, ///< No symmetry.
14 Upper = 1, ///< Symmetric, upper-triangular part is stored.
15 Lower = 2, ///< Symmetric, lower-triangular part is stored.
16};
17
18/// Dense matrix structure. Stores all elements in column-major storage.
19/// Symmetric dense matrices always store all elements.
20template <Config Conf>
26
27/// Sparse compressed-column structure (CCS or CSC).
28template <Config Conf, class StorageIndex>
29struct SparseCSC {
32 using index_vector_t = Eigen::VectorX<storage_index_t>;
33 using index_vector_view_t = Eigen::Ref<const index_vector_t>;
34 using index_vector_map_t = Eigen::Map<const index_vector_t>;
35 length_t rows = 0, cols = 0;
39 enum Order {
40 /// The row indices are not sorted.
42 /// Within each column, all row indices are sorted in ascending order.
44 };
46
47 /// Get the number of structurally nonzero elements.
48 length_t nnz() const {
49 assert(outer_ptr.size() == cols + 1);
50 return inner_idx.size();
51 }
52};
53
54/// Sparse coordinate list structure (COO).
55template <Config Conf, class StorageIndex>
56struct SparseCOO {
59 using index_vector_t = Eigen::VectorX<storage_index_t>;
60 using index_vector_view_t = Eigen::Ref<const index_vector_t>;
61 using index_vector_map_t = Eigen::Map<const index_vector_t>;
62 length_t rows = 0, cols = 0;
66 enum Order {
67 /// The indices are not sorted.
69 /// The indices are sorted by column first, and within each column, the
70 /// rows are sorted as well.
72 /// The indices are sorted by column, but the rows within each column
73 /// are not sorted.
75 /// The indices are sorted by row first, and within each row, the
76 /// columns are sorted as well.
78 /// The indices are sorted by row, but the columns within each row are
79 /// not sorted.
81 };
83 storage_index_t first_index = 0; ///< Zero for C/C++, one for Fortran.
84
85 /// Get the number of structurally nonzero elements.
86 length_t nnz() const {
87 assert(row_indices.size() == col_indices.size());
88 return row_indices.size();
89 }
90};
91
92template <Config Conf>
93using SparsityVariant = std::variant< //
94 Dense<Conf>, //
101 >;
102
103/// Stores any of the supported sparsity patterns.
104/// @see @ref SparsityConverter<Sparsity<Conf>, To>
105template <Config Conf>
106struct Sparsity {
107 Sparsity(std::convertible_to<SparsityVariant<Conf>> auto value) : value{std::move(value)} {}
109};
110
111namespace detail {
112template <class... Ts>
113struct overloaded : Ts... {
114 using Ts::operator()...;
115};
116template <class... Ts>
117overloaded(Ts...) -> overloaded<Ts...>;
118} // namespace detail
119
120/// Returns true if the sparsity pattern represents a dense matrix.
121template <Config Conf>
124 [](const Dense<Conf> &) { return true; },
125 [](const auto &) { return false; },
126 };
127 return std::visit(visitor, sp.value);
128}
129
130/// Get the number of structurally nonzero elements.
131template <Config Conf>
134 [](const Dense<Conf> &d) { return d.rows * d.cols; },
135 [](const auto &s) { return s.nnz(); },
136 };
137 return std::visit(visitor, sp.value);
138}
139
140/// Returns the symmetry of the sparsity pattern.
141template <Config Conf>
143 return std::visit([](const auto &s) { return s.symmetry; }, sp.value);
144}
145
146} // namespace alpaqa::sparsity
147
148namespace alpaqa {
149using sparsity::Sparsity;
150} // namespace alpaqa
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
Symmetry
Describes the symmetry of matrices.
Definition sparsity.hpp:12
@ Upper
Symmetric, upper-triangular part is stored.
@ Lower
Symmetric, lower-triangular part is stored.
length_t< Conf > get_nnz(const Sparsity< Conf > &sp)
Get the number of structurally nonzero elements.
Definition sparsity.hpp:132
bool is_dense(const Sparsity< Conf > &sp)
Returns true if the sparsity pattern represents a dense matrix.
Definition sparsity.hpp:122
Symmetry get_symmetry(const Sparsity< Conf > &sp)
Returns the symmetry of the sparsity pattern.
Definition sparsity.hpp:142
std::variant< Dense< Conf >, SparseCSC< Conf, int >, SparseCSC< Conf, long >, SparseCSC< Conf, long long >, SparseCOO< Conf, int >, SparseCOO< Conf, long >, SparseCOO< Conf, long long > > SparsityVariant
Definition sparsity.hpp:101
Dense matrix structure.
Definition sparsity.hpp:21
typename Conf::length_t length_t
Definition config.hpp:103
constexpr const auto inf
Definition config.hpp:112
Sparse coordinate list structure (COO).
Definition sparsity.hpp:56
Eigen::Map< const index_vector_t > index_vector_map_t
Definition sparsity.hpp:61
index_vector_view_t col_indices
Definition sparsity.hpp:65
storage_index_t first_index
Zero for C/C++, one for Fortran.
Definition sparsity.hpp:83
Eigen::Ref< const index_vector_t > index_vector_view_t
Definition sparsity.hpp:60
length_t nnz() const
Get the number of structurally nonzero elements.
Definition sparsity.hpp:86
Eigen::VectorX< storage_index_t > index_vector_t
Definition sparsity.hpp:59
index_vector_view_t row_indices
Definition sparsity.hpp:64
@ SortedByRowsAndCols
The indices are sorted by row first, and within each row, the columns are sorted as well.
Definition sparsity.hpp:77
@ SortedByColsAndRows
The indices are sorted by column first, and within each column, the rows are sorted as well.
Definition sparsity.hpp:71
@ SortedByRowsOnly
The indices are sorted by row, but the columns within each row are not sorted.
Definition sparsity.hpp:80
@ Unsorted
The indices are not sorted.
Definition sparsity.hpp:68
@ SortedByColsOnly
The indices are sorted by column, but the rows within each column are not sorted.
Definition sparsity.hpp:74
Sparse compressed-column structure (CCS or CSC).
Definition sparsity.hpp:29
Eigen::Map< const index_vector_t > index_vector_map_t
Definition sparsity.hpp:34
index_vector_view_t outer_ptr
Definition sparsity.hpp:38
Eigen::Ref< const index_vector_t > index_vector_view_t
Definition sparsity.hpp:33
length_t nnz() const
Get the number of structurally nonzero elements.
Definition sparsity.hpp:48
Eigen::VectorX< storage_index_t > index_vector_t
Definition sparsity.hpp:32
index_vector_view_t inner_idx
Definition sparsity.hpp:37
@ Unsorted
The row indices are not sorted.
Definition sparsity.hpp:41
@ SortedRows
Within each column, all row indices are sorted in ascending order.
Definition sparsity.hpp:43
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:106
SparsityVariant< Conf > value
Definition sparsity.hpp:108
Sparsity(std::convertible_to< SparsityVariant< Conf > > auto value)
Definition sparsity.hpp:107