QPALM 1.1.0
Proximal Augmented Lagrangian method for Quadratic Programs
sparse.cpp
Go to the documentation of this file.
1#include <sparse.hpp>
2
3#include <ladel_global.h> // ladel_sparse_free etc.
4
5#include <algorithm> // copy_n
6
7namespace qpalm {
8
9ladel_sparse_matrix eigen_to_ladel(sparse_mat_t &mat, ladel_int symmetry) {
10 ladel_sparse_matrix res{};
11 res.nzmax = mat.nonZeros();
12 res.nrow = mat.rows();
13 res.ncol = mat.cols();
14 res.p = mat.outerIndexPtr(); // column pointers
15 res.i = mat.innerIndexPtr(); // row indices
16 res.x = mat.valuePtr();
17 res.nz = mat.innerNonZeroPtr();
18 res.values = TRUE;
19 res.symmetry = symmetry;
20 return res;
21}
22
23namespace alloc {
24void ladel_sparse_matrix_deleter::operator()(ladel_sparse_matrix *M) const {
25 ::ladel_sparse_free(M);
26}
27} // namespace alloc
28
30 ladel_int nz = mat.innerNonZeroPtr() == nullptr ? FALSE : TRUE;
31 auto res = ::ladel_sparse_alloc(mat.rows(), mat.cols(), mat.nonZeros(),
32 UNSYMMETRIC, TRUE, nz);
33 assert(mat.outerSize() + 1 <= res->ncol + 1);
34 std::copy_n(mat.outerIndexPtr(), mat.outerSize() + 1, res->p);
35 assert(mat.nonZeros() <= res->nzmax);
36 std::copy_n(mat.innerIndexPtr(), mat.nonZeros(), res->i);
37 assert(mat.nonZeros() <= res->nzmax);
38 std::copy_n(mat.valuePtr(), mat.nonZeros(), res->x);
39 if (mat.innerNonZeroPtr() != nullptr) {
40 assert(mat.outerSize() <= res->ncol);
41 std::copy_n(mat.innerNonZeroPtr(), mat.outerSize(), res->nz);
42 }
43 return ladel_sparse_matrix_ptr{res};
44}
45
46} // namespace qpalm
#define TRUE
Definition: constants.h:18
#define FALSE
Definition: constants.h:19
Definition: qpalm.hpp:18
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:47
QPALM_CXX_EXPORT 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:9
Eigen::SparseMatrix< c_float, Eigen::ColMajor, sp_index_t > sparse_mat_t
Owning sparse matrix type.
Definition: sparse.hpp:22
QPALM_CXX_EXPORT ladel_sparse_matrix_ptr eigen_to_ladel_copy(const sparse_mat_t &mat)
Similar to eigen_to_ladel, but creates a copy of all data, in such a way that the returned matrix is ...
Definition: sparse.cpp:29
QPALM_CXX_EXPORT void operator()(ladel_sparse_matrix *) const
Definition: sparse.cpp:24