QPALM 1.2.4
Proximal Augmented Lagrangian method for Quadratic Programs
Loading...
Searching...
No Matches
sparse.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Sparse> // Eigen::SparseMatrix
4#include <Eigen/src/Core/Map.h> // Eigen::Map
5#include <Eigen/src/Core/Matrix.h> // Eigen::Matrix
6
7#include <ladel_constants.h> // UNSYMMETRIC
8#include <ladel_types.h> // ladel_sparse_matrix
9#include <qpalm/global_opts.h> // c_float
10
11#include <memory> // unique_ptr
12
13#include <qpalm/qpalm_cxx-export.hpp>
14
15namespace qpalm {
16
17/// Index types for vectors and matrices.
18using index_t = Eigen::Index;
19/// Index type for sparse matrices representation.
20using sp_index_t = ladel_int;
21/// Owning sparse matrix type.
22using sparse_mat_t = Eigen::SparseMatrix<c_float, Eigen::ColMajor, sp_index_t>;
23/// Read-only view on a sparse matrix.
24using sparse_mat_view_t = Eigen::Map<const sparse_mat_t>;
25/// Read-only reference to a sparse matrix.
26using sparse_mat_ref_t = Eigen::Ref<const sparse_mat_t>;
27/// Type for (row, column, value) triplets for initializing sparse matrices.
28using triplet_t = Eigen::Triplet<c_float, sp_index_t>;
29/// Owning dense vector type.
30using vec_t = Eigen::Matrix<c_float, Eigen::Dynamic, 1>;
31/// Borrowed dense vector type (vector view).
32using borrowed_vec_t = Eigen::Map<vec_t>;
33/// Read-only borrowed dense vector type (vector view).
34using const_borrowed_vec_t = Eigen::Map<const vec_t>;
35/// Reference to a dense vector (vector view).
36using ref_vec_t = Eigen::Ref<vec_t>;
37/// Read-only reference to a dense vector (vector view).
38using const_ref_vec_t = Eigen::Ref<const vec_t>;
39
40namespace alloc {
42 QPALM_CXX_EXPORT void operator()(ladel_sparse_matrix *) const;
43};
44} // namespace alloc
45
46/// Smart pointer that automatically cleans up an owning ladel_sparse_matrix
47/// object.
49 std::unique_ptr<ladel_sparse_matrix, alloc::ladel_sparse_matrix_deleter>;
50
51/// Convert an Eigen sparse matrix to a LADEL sparse matrix, without creating
52/// a copy.
53/// @note The returned object contains pointers to the data of @p mat, so do
54/// not reallocate or deallocate using the @c ladel_sparse_free
55/// and similar functions. Modifications of the returned LADEL matrix
56/// will affect the original Eigen matrix, so make sure that the
57/// representation remains consistent.
58QPALM_CXX_EXPORT ladel_sparse_matrix
59eigen_to_ladel(sparse_mat_t &mat, ladel_int symmetry = UNSYMMETRIC);
60
61/// Create an LADEL sparse matrix of the given dimensions.
62/// @param rows Number of rows.
63/// @param cols Number of columns.
64/// @param nnz Number of nonzeros.
65/// @param symmetry Either @c UNSYMMETRIC, @c UPPER or @c LOWER.
66/// @param values Whether to allocate the array of nonzero values.
67/// @param nonzeros Whether to allocate the array of nonzero counts.
68/// @see ladel_sparse_alloc
69QPALM_CXX_EXPORT ladel_sparse_matrix_ptr
70ladel_sparse_create(index_t rows, index_t cols, index_t nnz, ladel_int symmetry,
71 bool values = true, bool nonzeros = false);
72
73/// Similar to @ref eigen_to_ladel, but creates a copy of all data, in such a
74/// way that the returned matrix is completely decoupled from @p mat, and such
75/// that it can be reallocated and deallocated by the @c ladel_sparse_free
76/// and similar functions.
78 const sparse_mat_ref_t &mat, ladel_int symmetry = UNSYMMETRIC);
79
80} // namespace qpalm
Custom memory allocation, print and utility functions, and data types for floats and ints.
Eigen::Matrix< c_float, Eigen::Dynamic, 1 > vec_t
Owning dense vector type.
Definition sparse.hpp:30
Eigen::Ref< const vec_t > const_ref_vec_t
Read-only reference to a dense vector (vector view).
Definition sparse.hpp:38
Eigen::Map< const sparse_mat_t > sparse_mat_view_t
Read-only view on a sparse matrix.
Definition sparse.hpp:24
ladel_int sp_index_t
Index type for sparse matrices representation.
Definition sparse.hpp:20
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::Map< vec_t > borrowed_vec_t
Borrowed dense vector type (vector view).
Definition sparse.hpp:32
Eigen::Ref< vec_t > ref_vec_t
Reference to a dense vector (vector view).
Definition sparse.hpp:36
Eigen::SparseMatrix< c_float, Eigen::ColMajor, sp_index_t > sparse_mat_t
Owning sparse matrix type.
Definition sparse.hpp:22
Eigen::Map< const vec_t > const_borrowed_vec_t
Read-only borrowed dense vector type (vector view).
Definition sparse.hpp:34
Eigen::Triplet< c_float, sp_index_t > triplet_t
Type for (row, column, value) triplets for initializing sparse matrices.
Definition sparse.hpp:28
void operator()(ladel_sparse_matrix *) const
Definition sparse.cpp:25