MatrixSerialization.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 // \callgraph
20 
21 // Defined only if boost serialization is enabled
22 #if GTSAM_ENABLE_BOOST_SERIALIZATION
23 #pragma once
24 
25 #include <gtsam/base/Matrix.h>
26 
27 #include <Eigen/Sparse>
28 #include <boost/serialization/array.hpp>
29 #include <boost/serialization/nvp.hpp>
30 #include <boost/serialization/split_free.hpp>
31 
32 namespace boost {
33 namespace serialization {
34 
50 // split version - sends sizes ahead
51 template <class Archive, typename Scalar_, int Rows_, int Cols_, int Ops_,
52  int MaxRows_, int MaxCols_>
53 void save(
54  Archive& ar,
56  const unsigned int /*version*/) {
57  const size_t rows = m.rows(), cols = m.cols();
58  ar << BOOST_SERIALIZATION_NVP(rows);
59  ar << BOOST_SERIALIZATION_NVP(cols);
60  ar << make_nvp("data", make_array(m.data(), m.size()));
61 }
62 
63 template <class Archive, typename Scalar_, int Rows_, int Cols_, int Ops_,
64  int MaxRows_, int MaxCols_>
65 void load(Archive& ar,
67  const unsigned int /*version*/) {
68  size_t rows, cols;
69  ar >> BOOST_SERIALIZATION_NVP(rows);
70  ar >> BOOST_SERIALIZATION_NVP(cols);
71  m.resize(rows, cols);
72  ar >> make_nvp("data", make_array(m.data(), m.size()));
73 }
74 
75 // templated version of BOOST_SERIALIZATION_SPLIT_FREE(Eigen::Matrix);
76 template <class Archive, typename Scalar_, int Rows_, int Cols_, int Ops_,
77  int MaxRows_, int MaxCols_>
78 void serialize(
79  Archive& ar,
81  const unsigned int version) {
82  split_free(ar, m, version);
83 }
84 
85 // specialized to Matrix for MATLAB wrapper
86 template <class Archive>
87 void serialize(Archive& ar, gtsam::Matrix& m, const unsigned int version) {
88  split_free(ar, m, version);
89 }
90 
91 /******************************************************************************/
93 template <class Archive, typename _Scalar, int _Options, typename _Index>
95  const unsigned int /*version*/) {
96  _Index size = m.size();
97 
98  std::vector<std::pair<Eigen::Index, _Scalar>> data;
100  it(m);
101  it; ++it)
102  data.push_back({it.index(), it.value()});
103 
104  ar << BOOST_SERIALIZATION_NVP(size);
105  ar << BOOST_SERIALIZATION_NVP(data);
106 }
107 
108 template <class Archive, typename _Scalar, int _Options, typename _Index>
109 void load(Archive& ar, Eigen::SparseVector<_Scalar, _Options, _Index>& m,
110  const unsigned int /*version*/) {
111  _Index size;
112  ar >> BOOST_SERIALIZATION_NVP(size);
113  m.resize(size);
114 
115  std::vector<std::pair<Eigen::Index, _Scalar>> data;
116  ar >> BOOST_SERIALIZATION_NVP(data);
117 
118  for (auto&& d : data) {
119  m.coeffRef(d.first) = d.second;
120  }
121 }
122 
123 template <class Archive, typename _Scalar, int _Options, typename _Index>
124 void serialize(Archive& ar, Eigen::SparseVector<_Scalar, _Options, _Index>& m,
125  const unsigned int version) {
126  split_free(ar, m, version);
127 }
128 /******************************************************************************/
129 
130 } // namespace serialization
131 } // namespace boost
132 #endif
libsize.save
save
Definition: libsize.py:15
d
static const double d[K][N]
Definition: igam.h:11
Matrix.h
typedef and functions to augment Eigen's MatrixXd
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
boost
Definition: boostmultiprec.cpp:109
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
Eigen::SparseCompressedBase::InnerIterator
Definition: SparseCompressedBase.h:158
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
data
int data[]
Definition: Map_placement_new.cpp:1
conf.version
version
Definition: gtsam/3rdparty/GeographicLib/python/doc/conf.py:67
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::SparseVector
a sparse vector class
Definition: SparseUtil.h:54
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
cols
int cols
Definition: Tutorial_commainit_02.cpp:1


gtsam
Author(s):
autogenerated on Tue Jan 7 2025 04:03:05