QPALM 1.2.5
Proximal Augmented Lagrangian method for Quadratic Programs
Loading...
Searching...
No Matches
sparse.cpp
Go to the documentation of this file.
1#include <qpalm/sparse.hpp>
2
3#include <ladel_global.h> // ladel_sparse_free etc.
4
5#include <algorithm> // copy_n
6#include <cassert>
7
8namespace qpalm {
9
10ladel_sparse_matrix eigen_to_ladel(sparse_mat_t &mat, ladel_int symmetry) {
11 ladel_sparse_matrix res{};
12 res.nzmax = mat.nonZeros();
13 res.nrow = mat.rows();
14 res.ncol = mat.cols();
15 res.p = mat.outerIndexPtr(); // column pointers
16 res.i = mat.innerIndexPtr(); // row indices
17 res.x = mat.valuePtr();
18 res.nz = mat.innerNonZeroPtr();
19 res.values = TRUE;
20 res.symmetry = symmetry;
21 return res;
22}
23
24namespace alloc {
25void ladel_sparse_matrix_deleter::operator()(ladel_sparse_matrix *M) const {
26 ::ladel_sparse_free(M);
27}
28} // namespace alloc
29
31 index_t nnz, ladel_int symmetry,
32 bool values, bool nonzeros) {
34 ::ladel_sparse_alloc(static_cast<ladel_int>(rows),
35 static_cast<ladel_int>(cols),
36 static_cast<ladel_int>(nnz), symmetry,
37 values ? TRUE : FALSE, nonzeros ? TRUE : FALSE),
38 };
39}
40
42 ladel_int symmetry) {
43 bool nz = mat.innerNonZeroPtr() != nullptr;
44 auto res = ladel_sparse_create(mat.rows(), mat.cols(), mat.nonZeros(),
45 symmetry, true, nz);
46 assert(mat.outerSize() + 1 <= res->ncol + 1);
47 std::copy_n(mat.outerIndexPtr(), mat.outerSize() + 1, res->p);
48 assert(mat.nonZeros() <= res->nzmax);
49 std::copy_n(mat.innerIndexPtr(), mat.nonZeros(), res->i);
50 assert(mat.nonZeros() <= res->nzmax);
51 std::copy_n(mat.valuePtr(), mat.nonZeros(), res->x);
52 if (nz) {
53 assert(mat.outerSize() <= res->ncol);
54 std::copy_n(mat.innerNonZeroPtr(), mat.outerSize(), res->nz);
55 }
56 return res;
57}
58
59} // namespace qpalm
#define TRUE
Definition constants.h:18
#define FALSE
Definition constants.h:19
ladel_sparse_matrix_ptr eigen_to_ladel_copy(const sparse_mat_ref_t &mat, ladel_int symmetry=UNSYMMETRIC)
Similar to eigen_to_ladel, but creates a copy of all data, in such a way that the returned matrix is ...
Definition sparse.cpp:41
Eigen::Ref< const sparse_mat_t > sparse_mat_ref_t
Read-only reference to a sparse matrix.
Definition sparse.hpp:26
ladel_sparse_matrix_ptr ladel_sparse_create(index_t rows, index_t cols, index_t nnz, ladel_int symmetry, bool values=true, bool nonzeros=false)
Create an LADEL sparse matrix of the given dimensions.
Definition sparse.cpp:30
std::unique_ptr< ladel_sparse_matrix, alloc::ladel_sparse_matrix_deleter > ladel_sparse_matrix_ptr
Smart pointer that automatically cleans up an owning ladel_sparse_matrix object.
Definition sparse.hpp:49
ladel_sparse_matrix eigen_to_ladel(sparse_mat_t &mat, ladel_int symmetry=UNSYMMETRIC)
Convert an Eigen sparse matrix to a LADEL sparse matrix, without creating a copy.
Definition sparse.cpp:10
Eigen::Index index_t
Index types for vectors and matrices.
Definition sparse.hpp:18
Eigen::SparseMatrix< c_float, Eigen::ColMajor, sp_index_t > sparse_mat_t
Owning sparse matrix type.
Definition sparse.hpp:22
void operator()(ladel_sparse_matrix *) const
Definition sparse.cpp:25