proxsuite 0.6.7
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
eigen.hpp
Go to the documentation of this file.
1//
2// Copyright (c) 2022 INRIA
3//
8#ifndef PROXSUITE_SERIALIZATION_EIGEN_HPP
9#define PROXSUITE_SERIALIZATION_EIGEN_HPP
10
11#include <Eigen/Sparse>
12#include <Eigen/Dense>
13
14#include <cereal/cereal.hpp>
15
16namespace cereal {
17
18// dense matrices
19template<class Archive, class Derived>
20inline void
21save(Archive& ar, Eigen::PlainObjectBase<Derived> const& m)
22{
23 typedef Eigen::PlainObjectBase<Derived> PlainType;
24
25 Eigen::Index rows = m.rows();
26 Eigen::Index cols = m.cols();
27 ar(CEREAL_NVP(rows));
28 ar(CEREAL_NVP(cols));
29 bool is_row_major = PlainType::IsRowMajor;
30 ar(CEREAL_NVP(is_row_major));
31
32 for (Eigen::Index i = 0; i < m.size(); i++)
33 ar(m.data()[i]);
34}
35
36template<class Archive, class Derived>
37inline void
38load(Archive& ar, Eigen::PlainObjectBase<Derived>& m)
39{
40 typedef Eigen::PlainObjectBase<Derived> PlainType;
41
42 Eigen::Index rows;
43 Eigen::Index cols;
44 bool is_row_major;
45 ar(CEREAL_NVP(rows));
46 ar(CEREAL_NVP(cols));
47 ar(CEREAL_NVP(is_row_major));
48
49 m.resize(rows, cols);
50
51 for (Eigen::Index i = 0; i < m.size(); i++)
52 ar(m.data()[i]);
53
54 // Account for different storage orders
55 if (is_row_major != PlainType::IsRowMajor) {
56#if EIGEN_VERSION_AT_LEAST(3, 4, 0)
57 m.transposeInPlace();
58#else
59 m = m.transpose().eval();
60#endif
61 }
62}
63
64// sparse matrices
65template<class Archive, typename _Scalar, int _Options, typename _StorageIndex>
66inline void
67save(Archive& ar,
68 Eigen::SparseMatrix<_Scalar, _Options, _StorageIndex> const& m)
69{
70 Eigen::Index innerSize = m.innerSize();
71 Eigen::Index outerSize = m.outerSize();
72 typedef typename Eigen::Triplet<_Scalar> Triplet;
73 std::vector<Triplet> triplets;
74
75 for (Eigen::Index i = 0; i < outerSize; ++i) {
76 for (typename Eigen::SparseMatrix<_Scalar, _Options, _StorageIndex>::
77 InnerIterator it(m, i);
78 it;
79 ++it) {
80 triplets.push_back(Triplet(it.row(), it.col(), it.value()));
81 }
82 }
83 ar(innerSize);
84 ar(outerSize);
85 ar(triplets);
86}
87
88template<class Archive, typename _Scalar, int _Options, typename _StorageIndex>
89inline void
90load(Archive& ar, Eigen::SparseMatrix<_Scalar, _Options, _StorageIndex>& m)
91{
92 Eigen::Index innerSize;
93 Eigen::Index outerSize;
94 ar(innerSize);
95 ar(outerSize);
96 Eigen::Index rows = m.IsRowMajor ? outerSize : innerSize;
97 Eigen::Index cols = m.IsRowMajor ? innerSize : outerSize;
98 m.resize(rows, cols);
99 typedef typename Eigen::Triplet<_Scalar> Triplet;
100 std::vector<Triplet> triplets;
101 ar(triplets);
102 m.setFromTriplets(triplets.begin(), triplets.end());
103}
104
105} // namespace cereal
106
107#endif /* end of include guard PROXSUITE_SERIALIZATION_EIGEN_HPP */
void save(Archive &ar, Eigen::PlainObjectBase< Derived > const &m)
Definition eigen.hpp:21
void load(Archive &ar, Eigen::PlainObjectBase< Derived > &m)
Definition eigen.hpp:38