proxsuite
0.7.3
The Advanced Proximal Optimization Toolbox
Toggle main menu visibility
Loading...
Searching...
No Matches
eigen.hpp
Go to the documentation of this file.
1
//
2
// Copyright (c) 2022 INRIA
3
//
7
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
16
namespace
cereal
{
17
18
// dense matrices
19
template
<
class
Archive,
class
Derived>
20
inline
void
21
save
(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
36
template
<
class
Archive,
class
Derived>
37
inline
void
38
load
(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
65
template
<
class
Archive,
typename
_Scalar,
int
_Options,
typename
_StorageIndex>
66
inline
void
67
save
(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
88
template
<
class
Archive,
typename
_Scalar,
int
_Options,
typename
_StorageIndex>
89
inline
void
90
load
(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 */
cereal
Definition
eigen.hpp:16
cereal::save
void save(Archive &ar, Eigen::PlainObjectBase< Derived > const &m)
Definition
eigen.hpp:21
cereal::load
void load(Archive &ar, Eigen::PlainObjectBase< Derived > &m)
Definition
eigen.hpp:38
include
proxsuite
serialization
eigen.hpp
Generated by
1.17.0