cyqlone develop
Fast, parallel and vectorized solver for linear systems with optimal control structure.
Loading...
Searching...
No Matches
sparse.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// Sparse matrix utilities.
5/// @ingroup topic-utilities
6
7#include <cyqlone/config.hpp>
8#include <batmat/assume.hpp>
9#include <batmat/linalg/structure.hpp>
10#include <guanaqo/linalg/sparsity.hpp>
11#include <guanaqo/mat-view.hpp>
12
13#include <concepts>
14#include <ranges>
15#include <vector>
16
17namespace cyqlone {
18
19/// @addtogroup topic-utilities
20/// @{
21
22using guanaqo::linalg::sparsity::SparseCOO;
24
25/// A sparse matrix in COO format.
27 const std::vector<index_t> row_indices, col_indices;
28 const std::vector<real_t> values;
30
31 [[nodiscard]] auto iter_coo() const {
32 return std::views::zip(row_indices, col_indices, values);
33 }
34};
35
36/// A builder for constructing a SparseMatrix incrementally.
38 index_t rows = -1, cols = -1;
39 Symmetry symmetry = Symmetry::Unsymmetric;
40 std::vector<index_t> row_indices{}, col_indices{};
41 std::vector<real_t> values{};
42
43 void add(index_t row, index_t col, real_t value) {
44 BATMAT_ASSUME(row >= 0);
45 BATMAT_ASSUME(col >= 0);
46 if (rows >= 0)
47 BATMAT_ASSUME(row < rows);
48 if (cols >= 0)
49 BATMAT_ASSUME(col < cols);
50 row_indices.push_back(row);
51 col_indices.push_back(col);
52 values.push_back(value);
53 }
54
55 template <std::convertible_to<real_t> T, class I, class S, guanaqo::StorageOrder O>
56 void add(index_t row, index_t col, guanaqo::MatrixView<T, I, S, O> dense,
57 std::remove_cvref_t<T> scale = 1,
59 if (rows >= 0)
60 BATMAT_ASSUME(row + dense.rows <= rows);
61 if (cols >= 0)
62 BATMAT_ASSUME(col + dense.cols <= cols);
63 switch (structure) {
65 for (index_t j = 0; j < dense.cols; ++j)
66 for (index_t i = 0; i < dense.rows; ++i)
67 add(row + i, col + j, scale * dense(i, j));
68 break;
70 for (index_t j = 0; j < dense.cols; ++j)
71 for (index_t i = j; i < dense.rows; ++i)
72 add(row + i, col + j, scale * dense(i, j));
73 break;
75 BATMAT_ASSERT(dense.rows == dense.cols);
76 for (index_t j = 0; j < dense.cols; ++j)
77 for (index_t i = 0; i <= j; ++i)
78 add(row + i, col + j, scale * dense(i, j));
79 break;
80 default: BATMAT_ASSERT(false);
81 }
82 }
83
84 void add_diag(index_t row, index_t col, real_t value, index_t n) {
85 if (rows >= 0)
86 BATMAT_ASSUME(row + n <= rows);
87 if (cols >= 0)
88 BATMAT_ASSUME(col + n <= cols);
89 for (index_t i = 0; i < n; ++i)
90 add(row + i, col + i, value);
91 }
92
93 [[nodiscard]] SparseMatrix build() const & {
94 return {.row_indices = row_indices,
95 .col_indices = col_indices,
96 .values = values,
97 .sparsity = SparseCOO<index_t>{.rows = rows,
98 .cols = cols,
99 .symmetry = symmetry,
100 .row_indices = std::span{row_indices},
101 .col_indices = std::span{col_indices}}};
102 }
103
104 [[nodiscard]] SparseMatrix build() && {
105 SparseCOO<index_t> sparsity{.rows = rows,
106 .cols = cols,
107 .symmetry = symmetry,
108 .row_indices = std::span{row_indices},
109 .col_indices = std::span{col_indices}};
110 return {.row_indices = std::move(row_indices),
111 .col_indices = std::move(col_indices),
112 .values = std::move(values),
113 .sparsity = std::move(sparsity)};
114 }
115};
116
117/// @}
118
119} // namespace cyqlone
#define BATMAT_ASSUME(x)
#define BATMAT_ASSERT(x)
void scale(T alpha, Vx &&x, Vz &&z)
Multiply a vector by a scalar z = αx.
Definition linalg.hpp:294
A builder for constructing a SparseMatrix incrementally.
Definition sparse.hpp:37
void add_diag(index_t row, index_t col, real_t value, index_t n)
Definition sparse.hpp:84
SparseMatrix build() const &
Definition sparse.hpp:93
void add(index_t row, index_t col, real_t value)
Definition sparse.hpp:43
std::vector< index_t > row_indices
Definition sparse.hpp:40
std::vector< real_t > values
Definition sparse.hpp:41
void add(index_t row, index_t col, guanaqo::MatrixView< T, I, S, O > dense, std::remove_cvref_t< T > scale=1, batmat::linalg::MatrixStructure structure=batmat::linalg::MatrixStructure::General)
Definition sparse.hpp:56
std::vector< index_t > col_indices
Definition sparse.hpp:40
SparseMatrix build() &&
Definition sparse.hpp:104
A sparse matrix in COO format.
Definition sparse.hpp:26
const SparseCOO< index_t > sparsity
Definition sparse.hpp:29
const std::vector< index_t > row_indices
Definition sparse.hpp:27
auto iter_coo() const
Definition sparse.hpp:31
const std::vector< index_t > col_indices
Definition sparse.hpp:27
const std::vector< real_t > values
Definition sparse.hpp:28