gtsam
Loading...
Searching...
No Matches
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
18
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
32namespace boost {
33namespace serialization {
34
49
50// split version - sends sizes ahead
51template <class Archive, typename Scalar_, int Rows_, int Cols_, int Ops_,
52 int MaxRows_, int MaxCols_>
53void save(
54 Archive& ar,
55 const Eigen::Matrix<Scalar_, Rows_, Cols_, Ops_, MaxRows_, MaxCols_>& m,
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
63template <class Archive, typename Scalar_, int Rows_, int Cols_, int Ops_,
64 int MaxRows_, int MaxCols_>
65void load(Archive& ar,
66 Eigen::Matrix<Scalar_, Rows_, Cols_, Ops_, MaxRows_, MaxCols_>& m,
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);
76template <class Archive, typename Scalar_, int Rows_, int Cols_, int Ops_,
77 int MaxRows_, int MaxCols_>
78void serialize(
79 Archive& ar,
80 Eigen::Matrix<Scalar_, Rows_, Cols_, Ops_, MaxRows_, MaxCols_>& m,
81 const unsigned int version) {
82 split_free(ar, m, version);
83}
84
85// specialized to Matrix for MATLAB wrapper
86template <class Archive>
87void serialize(Archive& ar, gtsam::Matrix& m, const unsigned int version) {
88 split_free(ar, m, version);
89}
90
91/******************************************************************************/
93template <class Archive, typename _Scalar, int _Options, typename _Index>
94void save(Archive& ar, const Eigen::SparseVector<_Scalar, _Options, _Index>& m,
95 const unsigned int /*version*/) {
96 _Index size = m.size();
97
98 std::vector<std::pair<Eigen::Index, _Scalar>> data;
99 for (typename Eigen::SparseVector<_Scalar, _Options, _Index>::InnerIterator
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
108template <class Archive, typename _Scalar, int _Options, typename _Index>
109void 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
123template <class Archive, typename _Scalar, int _Options, typename _Index>
124void 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
typedef and functions to augment Eigen's MatrixXd
void save(const Matrix &A, const string &s, const string &filename)
save a matrix to file, which can be loaded by matlab
Definition Matrix.cpp:154