Skip to content

Commit b93b5c6

Browse files
committed
Ben's suggestions.
* Store rows/cols explicitly for Matrix serialization. * Clarifications.
1 parent ec0471b commit b93b5c6

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

albatross/cereal/eigen.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ template <class Archive, typename _Scalar, int _Rows, int _Cols>
2929
inline void save(Archive &archive,
3030
const Eigen::Matrix<_Scalar, _Rows, _Cols> &v) {
3131
size_type rows = static_cast<size_type>(v.rows());
32+
size_type cols = static_cast<size_type>(v.cols());
3233
archive(cereal::make_size_tag(rows));
34+
// Storing the rows and columns is redundant information but
35+
// makes loading a lot easier.
36+
archive(rows);
37+
archive(cols);
3338
for (size_type i = 0; i < rows; i++) {
3439
Eigen::Matrix<_Scalar, _Cols, 1> row = v.row(i);
3540
archive(row);
@@ -38,28 +43,21 @@ inline void save(Archive &archive,
3843

3944
template <class Archive, typename _Scalar, int _Rows, int _Cols>
4045
inline void load(Archive &archive, Eigen::Matrix<_Scalar, _Rows, _Cols> &v) {
41-
size_type rows;
42-
archive(cereal::make_size_tag(rows));
46+
size_type rows_plus_two, rows, cols;
47+
archive(cereal::make_size_tag(rows_plus_two));
48+
archive(rows);
49+
archive(cols);
50+
assert(rows == rows_plus_two - 2);
4351
/*
4452
* In order to determine the size of a matrix, we have to first determine
4553
* how many rows, then inspect the size of the first row to get the
4654
* number of columns.
4755
*/
48-
if (rows > 0) {
49-
Eigen::Matrix<_Scalar, _Rows, 1> first;
50-
archive(first);
51-
size_type cols = first.rows();
52-
v.resize(rows, cols);
53-
v.row(0) = first;
54-
55-
for (size_type i = 1; i < rows; i++) {
56-
Eigen::Matrix<_Scalar, _Cols, 1> row;
57-
archive(row);
58-
v.row(i) = row;
59-
}
60-
} else {
61-
// Serialized matrix is empty.
62-
v.resize(0, 0);
56+
v.resize(rows, cols);
57+
for (size_type i = 0; i < rows; i++) {
58+
Eigen::Matrix<_Scalar, _Cols, 1> row;
59+
archive(row);
60+
v.row(i) = row;
6361
}
6462
};
6563

albatross/eigen/serializable_ldlt.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ inline void load_lower_triangle(Archive &archive,
4141
// TODO: understand why the storage size upon load is always augmented by two.
4242
storage_size -= 2;
4343
// We assume the matrix is square and compute the number of rows from the
44-
// storage size.
45-
double drows = (std::sqrt(static_cast<double>(storage_size * 8 + 1)) - 1) / 2;
46-
cereal::size_type rows = static_cast<cereal::size_type>(drows);
44+
// storage size using the quadratic formula.
45+
// rows^2 + rows - 2 * storage_size = 0
46+
double a = 1;
47+
double b = 1;
48+
double c = -2. * static_cast<double>(storage_size);
49+
double rows_as_double = (std::sqrt(b * b - 4 * a * c) - b) / (2 * a);
50+
assert(rows_as_double - static_cast<Eigen::Index>(rows_as_double) == 0. &&
51+
"inferred a non integer number of rows");
52+
cereal::size_type rows = static_cast<cereal::size_type>(rows_as_double);
4753
v.resize(rows, rows);
4854
for (cereal::size_type i = 0; i < rows; i++) {
4955
for (cereal::size_type j = 0; j <= i; j++) {
@@ -80,16 +86,12 @@ class SerializableLDLT : public LDLT<MatrixXd, Lower> {
8086
this->transpositionsP() * Eigen::MatrixXd::Identity(n, n);
8187
this->matrixL().solveInPlace(inverse_cholesky);
8288

83-
Eigen::VectorXd sqrt_diag = this->vectorD();
84-
for (Eigen::Index i = 0; i < n; i++) {
85-
sqrt_diag[i] = 1. / std::sqrt(sqrt_diag[i]);
86-
}
89+
Eigen::VectorXd sqrt_diag = this->vectorD().array().sqrt().matrix();
8790

8891
Eigen::VectorXd inv_diag(n);
8992
for (Eigen::Index i = 0; i < n; i++) {
9093
const Eigen::VectorXd col_i =
91-
inverse_cholesky.col(i).cwiseProduct(sqrt_diag);
92-
;
94+
inverse_cholesky.col(i).cwiseQuotient(sqrt_diag);
9395
inv_diag[i] = col_i.dot(col_i);
9496
}
9597

0 commit comments

Comments
 (0)