9 std::vector<real>
vals,
10 std::vector<idx> col_idx,
11 std::vector<idx> row_ptr)
12 : n_rows_(n_rows), n_cols_(n_cols),
17 if (row_ptr_.size() != n_rows_ + 1)
18 throw std::invalid_argument(
"SparseMatrix: row_ptr must have length n_rows+1");
19 if (col_idx_.size() != vals_.size())
20 throw std::invalid_argument(
"SparseMatrix: col_idx and vals must have equal length");
24 const std::vector<idx>& rows,
25 const std::vector<idx>& cols,
26 const std::vector<real>&
vals) {
27 if (rows.size() != cols.size() || rows.size() !=
vals.size())
28 throw std::invalid_argument(
"SparseMatrix::from_triplets: inconsistent input sizes");
32 for (
idx k = 0;
k < rows.size(); ++
k) {
34 throw std::out_of_range(
"SparseMatrix::from_triplets: index out of range");
48 for (
idx k = 0;
k < rows.size(); ++
k) {
61 [&](
idx a,
idx b){ return out_col[start + a] < out_col[start + b]; });
63 std::vector<real> sv(end -
start);
64 std::vector<idx>
sc(end -
start);
105 for (
idx k = row_ptr_[
i];
k < row_ptr_[
i + 1]; ++
k)
106 if (col_idx_[
k] ==
j)
return vals_[
k];
111 if (
A.n_cols() != x.
size() ||
A.n_rows() != y.
size())
112 throw std::invalid_argument(
"Dimension mismatch in sparse_matvec");
113 for (
idx i = 0;
i <
A.n_rows(); ++
i) {
115 for (
idx k =
A.row_ptr()[
i];
k <
A.row_ptr()[
i + 1]; ++
k)
116 sum +=
A.values()[
k] * x[
A.col_idx()[
k]];
constexpr idx size() const noexcept
Sparse matrix in Compressed Sparse Row (CSR) format.
real operator()(idx i, idx j) const
Element access A(i,j); returns 0 if outside stored pattern – O(nnz/n)
SparseMatrix(idx n_rows, idx n_cols, std::vector< real > vals, std::vector< idx > col_idx, std::vector< idx > row_ptr)
Construct from raw CSR arrays (takes ownership)
static SparseMatrix from_triplets(idx n_rows, idx n_cols, const std::vector< idx > &rows, const std::vector< idx > &cols, const std::vector< real > &vals)
Build from coordinate (COO / triplet) lists.
const idx * row_ptr() const
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
void sparse_matvec(const SparseMatrix &A, const Vector &x, Vector &y)
y = A * x
Compressed Sparse Row (CSR) matrix and operations.