18#ifndef SPARSE_SOLVER_HPP
19#define SPARSE_SOLVER_HPP
25#include <tbb/parallel_sort.h>
26#define ezp_sort tbb::parallel_sort
29#define ezp_sort std::sort
38 sparse_coo_mat(
const IT n,
const IT nnz, IT*
const row, IT*
const col, DT*
const data)
45 auto is_valid()
const {
return row && col && data; }
50 const IT*
const row_idx;
51 const IT*
const col_idx;
58 bool operator()(
const IT
idx_a,
const IT
idx_b)
const {
64 template<data_t T>
bool approx_equal(
const T x,
const T y,
const int ulp = 2) {
65 using R =
decltype(std::abs(x));
66 return std::abs(x -
y) <= std::numeric_limits<R>::epsilon() * std::abs(x +
y) *
ulp || std::abs(x -
y) < std::numeric_limits<R>::min();
72 IT *row_ptr{
nullptr}, *col_idx{
nullptr};
75 std::vector<IT> row_storage, col_storage;
76 std::vector<DT> data_storage;
82 , row_ptr(
other.row_ptr)
83 , col_idx(
other.col_idx)
85 , row_storage(
other.row_storage)
86 , col_storage(
other.col_storage)
87 , data_storage(
other.data_storage) {
88 if(!row_storage.empty()) row_ptr = row_storage.data();
89 if(!col_storage.empty()) col_idx = col_storage.data();
90 if(!data_storage.empty()) data = data_storage.data();
97 sparse_csr_mat(
const IT n,
const IT nnz, IT*
const row_ptr, IT*
const col_idx, DT*
const data)
114 if(!
coo.is_valid())
return;
116 std::vector<IT2> index(nnz);
117 std::iota(index.begin(), index.end(),
IT2(0));
120 row_storage.resize(n + 1);
121 col_storage.resize(nnz);
122 data_storage.resize(nnz);
126 row_ptr = row_storage.data();
127 col_idx = col_storage.data();
128 data = data_storage.data();
159 for(
auto I = IT{0};
I < nnz; ++
I) {
160 const auto idx = index[
I];
161 const auto row =
static_cast<IT
>(
coo.row[
idx]), col =
static_cast<IT
>(
coo.col[
idx]);
177 col_storage.resize(nnz);
178 data_storage.resize(nnz);
193 for(
auto row = IT{0}; row < n; ++row) {
195 for(
auto k = row_storage[row] -
shift;
k < row_storage[row + 1] -
shift; ++
k)
196 if(col_storage[
k] == row +
shift) {
205 std::vector<IT>
new_row(n + 1);
210 for(
auto row = IT{0}; row < n; ++row) {
213 for(
auto k = row_storage[row] -
shift;
k < row_storage[row + 1] -
shift; ++
k) {
214 const auto col = col_storage[
k];
232 row_storage = std::move(
new_row);
233 col_storage = std::move(
new_col);
237 auto is_valid() {
return row_ptr && col_idx && data; }
Definition sparse_solver.hpp:49
Solver for general sparse matrices.
Definition sparse_solver.hpp:33
Definition sparse_solver.hpp:70
sparse_csr_mat(const sparse_coo_mat< DT2, IT2 > &coo, const bool one_based=false, const bool full=false)
Definition sparse_solver.hpp:111
auto insert_missing_diagonals(const IT shift)
Definition sparse_solver.hpp:191
auto condense(const sparse_coo_mat< DT2, IT2 > &coo, const std::vector< IT2 > &index, const bool one_based, const bool full)
Definition sparse_solver.hpp:138