10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: keir@google.com (Keir Mierle)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/block_jacobi_preconditioner.h"
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "Eigen/Cholesky"
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/block_sparse_matrix.h"
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/block_structure.h"
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/casts.h"
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/integral_types.h"
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/eigen.h"
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal {
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
431d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha HaeberlingBlockJacobiPreconditioner::BlockJacobiPreconditioner(
441d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    const BlockSparseMatrix& A)
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    : num_rows_(A.num_rows()),
461d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling      block_structure_(*A.block_structure()) {
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Calculate the amount of storage needed.
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int storage_needed = 0;
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int c = 0; c < block_structure_.cols.size(); ++c) {
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    int size = block_structure_.cols[c].size;
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    storage_needed += size * size;
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Size the offsets and storage.
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  blocks_.resize(block_structure_.cols.size());
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  block_storage_.resize(storage_needed);
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Put pointers to the storage in the offsets.
590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  double* block_cursor = &block_storage_[0];
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int c = 0; c < block_structure_.cols.size(); ++c) {
610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    int size = block_structure_.cols[c].size;
620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    blocks_[c] = block_cursor;
630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    block_cursor += size * size;
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
671d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha HaeberlingBlockJacobiPreconditioner::~BlockJacobiPreconditioner() {}
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
691d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingbool BlockJacobiPreconditioner::UpdateImpl(const BlockSparseMatrix& A,
701d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling                                           const double* D) {
710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  const CompressedRowBlockStructure* bs = A.block_structure();
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Compute the diagonal blocks by block inner products.
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  std::fill(block_storage_.begin(), block_storage_.end(), 0.0);
751d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  const double* values = A.values();
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int r = 0; r < bs->rows.size(); ++r) {
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const int row_block_size = bs->rows[r].block.size;
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const vector<Cell>& cells = bs->rows[r].cells;
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    for (int c = 0; c < cells.size(); ++c) {
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      const int col_block_size = bs->cols[cells[c].block_id].size;
811d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling      ConstMatrixRef m(values + cells[c].position,
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                       row_block_size,
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                       col_block_size);
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      MatrixRef(blocks_[cells[c].block_id],
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                col_block_size,
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                col_block_size).noalias() += m.transpose() * m;
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      // TODO(keir): Figure out when the below expression is actually faster
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      // than doing the full rank update. The issue is that for smaller sizes,
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      // the rankUpdate() function is slower than the full product done above.
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      //
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      // On the typical bundling problems, the above product is ~5% faster.
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      //
950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      //   MatrixRef(blocks_[cells[c].block_id],
960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      //             col_block_size,
970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      //             col_block_size).selfadjointView<Eigen::Upper>().rankUpdate(m);
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      //
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    }
1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Add the diagonal and invert each block.
1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int c = 0; c < bs->cols.size(); ++c) {
1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const int size = block_structure_.cols[c].size;
1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const int position = block_structure_.cols[c].position;
1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    MatrixRef block(blocks_[c], size, size);
1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    if (D != NULL) {
1091d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling      block.diagonal() +=
1101d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling          ConstVectorRef(D + position, size).array().square().matrix();
1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    }
1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    block = block.selfadjointView<Eigen::Upper>()
114399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                 .llt()
1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                 .solve(Matrix::Identity(size, size));
1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1171d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  return true;
1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1201d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingvoid BlockJacobiPreconditioner::RightMultiply(const double* x,
1211d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling                                              double* y) const {
1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int c = 0; c < block_structure_.cols.size(); ++c) {
1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const int size = block_structure_.cols[c].size;
1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const int position = block_structure_.cols[c].position;
1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    ConstMatrixRef D(blocks_[c], size, size);
1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    ConstVectorRef x_block(x + position, size);
1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    VectorRef y_block(y + position, size);
1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    y_block += D * x_block;
1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongvoid BlockJacobiPreconditioner::LeftMultiply(const double* x, double* y) const {
1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  RightMultiply(x, y);
1340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
1350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace internal
1370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
138