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