Fix a reallocation bug in CreateJacobianBlockSparsityTranspose.
CreateJacobianBlockSparsityTranspose starts with a conservative
estimate of the size of the block sparsity pattern of the Jacobian.
When the Jacobian has more non-zeros than that, the TripletSparseMatrix
being used to store the sparsity has a Reallocate method which
allows one to resize the matrix and IF num_nonzeros is set, then the
existing values in the array are also copied into the newly allocated
memory.
Unfortunately the pattern we follow in ceres code is to call
set_num_nonzeros after one is done populating the sparsity pattern
of a matrix. This does not mix well with Reallocate and results
in the matrix having uninitialized memory.
This patch fixes this problem and adds a test that verifies the fix.
Thanks to Yuliy Schwartzburg for reporting this bug and providing
code to reproduce it.
Change-Id: I58583714ffaebd880d85af16e3685b2d6ee053e8
diff --git a/internal/ceres/solver_impl.cc b/internal/ceres/solver_impl.cc
index 76e9c92..f0ac2f6 100644
--- a/internal/ceres/solver_impl.cc
+++ b/internal/ceres/solver_impl.cc
@@ -1418,6 +1418,7 @@
// Re-size the matrix if needed.
if (num_nonzeros >= tsm->max_num_nonzeros()) {
+ tsm->set_num_nonzeros(num_nonzeros);
tsm->Reserve(2 * num_nonzeros);
rows = tsm->mutable_rows();
cols = tsm->mutable_cols();