QPALM 1.1.5a1
Proximal Augmented Lagrangian method for Quadratic Programs
Loading...
Searching...
No Matches
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#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 ladel_int nz = mat.innerNonZeroPtr() == nullptr ? FALSE : TRUE;
32 auto res = ::ladel_sparse_alloc(mat.rows(), mat.cols(), mat.nonZeros(),
33 UNSYMMETRIC, TRUE, nz);
34 assert(mat.outerSize() + 1 <= res->ncol + 1);
35 std::copy_n(mat.outerIndexPtr(), mat.outerSize() + 1, res->p);
36 assert(mat.nonZeros() <= res->nzmax);
37 std::copy_n(mat.innerIndexPtr(), mat.nonZeros(), res->i);
38 assert(mat.nonZeros() <= res->nzmax);
39 std::copy_n(mat.valuePtr(), mat.nonZeros(), res->x);
40 if (mat.innerNonZeroPtr() != nullptr) {
41 assert(mat.outerSize() <= res->ncol);
42 std::copy_n(mat.innerNonZeroPtr(), mat.outerSize(), res->nz);
43 }
44 return ladel_sparse_matrix_ptr{res};
45}
46
47} // namespace qpalm
#define TRUE
Definition constants.h:18
#define FALSE
Definition constants.h:19
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
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::SparseMatrix< c_float, Eigen::ColMajor, sp_index_t > sparse_mat_t
Owning sparse matrix type.
Definition sparse.hpp:22
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:30
void operator()(ladel_sparse_matrix *) const
Definition sparse.cpp:25