Blame internal/ceres/inner_product_computer.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2017 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_INNER_PRODUCT_COMPUTER_H_
Packit ea1746
#define CERES_INTERNAL_INNER_PRODUCT_COMPUTER_H_
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
Packit ea1746
#include "ceres/block_sparse_matrix.h"
Packit ea1746
#include "ceres/compressed_row_sparse_matrix.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
// This class is used to repeatedly compute the inner product
Packit ea1746
//
Packit ea1746
//   result = m' * m
Packit ea1746
//
Packit ea1746
// where the sparsity structure of m remains constant across calls.
Packit ea1746
//
Packit ea1746
// Upon creation, the class computes and caches information needed to
Packit ea1746
// compute v, and then uses it to efficiently compute the product
Packit ea1746
// every time InnerProductComputer::Compute is called.
Packit ea1746
//
Packit ea1746
// See sparse_normal_cholesky_solver.cc for example usage.
Packit ea1746
//
Packit ea1746
// Note that the result matrix is a block upper or lower-triangular
Packit ea1746
// matrix, i.e., it will contain entries in the upper or lower
Packit ea1746
// triangular part of the matrix corresponding to the block that occur
Packit ea1746
// along its diagonal.
Packit ea1746
//
Packit ea1746
// This is not a problem as sparse linear algebra libraries can ignore
Packit ea1746
// these entries with ease and the space used is minimal/linear in the
Packit ea1746
// size of the matrices.
Packit ea1746
class InnerProductComputer {
Packit ea1746
 public:
Packit ea1746
  // Factory
Packit ea1746
  //
Packit ea1746
  // m is the input matrix
Packit ea1746
  //
Packit ea1746
  // Since m' * m is a symmetric matrix, we only compute half of the
Packit ea1746
  // matrix and the value of storage_type which must be
Packit ea1746
  // UPPER_TRIANGULAR or LOWER_TRIANGULAR determines which half is
Packit ea1746
  // computed.
Packit ea1746
  //
Packit ea1746
  // The user must ensure that the matrix m is valid for the life time
Packit ea1746
  // of this object.
Packit ea1746
  static InnerProductComputer* Create(
Packit ea1746
      const BlockSparseMatrix& m,
Packit ea1746
      CompressedRowSparseMatrix::StorageType storage_type);
Packit ea1746
Packit ea1746
  // This factory method allows the user control over range of row
Packit ea1746
  // blocks of m that should be used to compute the inner product.
Packit ea1746
  //
Packit ea1746
  // a = m(start_row_block : end_row_block, :);
Packit ea1746
  // result = a' * a;
Packit ea1746
  static InnerProductComputer* Create(
Packit ea1746
      const BlockSparseMatrix& m,
Packit ea1746
      int start_row_block,
Packit ea1746
      int end_row_block,
Packit ea1746
      CompressedRowSparseMatrix::StorageType storage_type);
Packit ea1746
Packit ea1746
  // Update result_ to be numerically equal to m' * m.
Packit ea1746
  void Compute();
Packit ea1746
Packit ea1746
  // Accessors for the result containing the inner product.
Packit ea1746
  //
Packit ea1746
  // Compute must be called before accessing this result for
Packit ea1746
  // the first time.
Packit ea1746
  const CompressedRowSparseMatrix& result() const { return *result_; }
Packit ea1746
  CompressedRowSparseMatrix* mutable_result() const { return result_.get(); }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  // A ProductTerm is a term in the block inner product of a matrix
Packit ea1746
  // with itself.
Packit ea1746
  struct ProductTerm {
Packit ea1746
    ProductTerm(const int row, const int col, const int index)
Packit ea1746
        : row(row), col(col), index(index) {}
Packit ea1746
Packit ea1746
    bool operator<(const ProductTerm& right) const {
Packit ea1746
      if (row == right.row) {
Packit ea1746
        if (col == right.col) {
Packit ea1746
          return index < right.index;
Packit ea1746
        }
Packit ea1746
        return col < right.col;
Packit ea1746
      }
Packit ea1746
      return row < right.row;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    int row;
Packit ea1746
    int col;
Packit ea1746
    int index;
Packit ea1746
  };
Packit ea1746
Packit ea1746
  InnerProductComputer(const BlockSparseMatrix& m,
Packit ea1746
                       int start_row_block,
Packit ea1746
                       int end_row_block);
Packit ea1746
Packit ea1746
  void Init(CompressedRowSparseMatrix::StorageType storage_type);
Packit ea1746
Packit ea1746
  CompressedRowSparseMatrix* CreateResultMatrix(
Packit ea1746
      const CompressedRowSparseMatrix::StorageType storage_type,
Packit ea1746
      int num_nonzeros);
Packit ea1746
Packit ea1746
  int ComputeNonzeros(const std::vector<ProductTerm>& product_terms,
Packit ea1746
                      std::vector<int>* row_block_nnz);
Packit ea1746
Packit ea1746
  void ComputeOffsetsAndCreateResultMatrix(
Packit ea1746
      const CompressedRowSparseMatrix::StorageType storage_type,
Packit ea1746
      const std::vector<ProductTerm>& product_terms);
Packit ea1746
Packit ea1746
  const BlockSparseMatrix& m_;
Packit ea1746
  const int start_row_block_;
Packit ea1746
  const int end_row_block_;
Packit ea1746
  scoped_ptr<CompressedRowSparseMatrix> result_;
Packit ea1746
Packit ea1746
  // For each term in the inner product, result_offsets_ contains the
Packit ea1746
  // location in the values array of the result_ matrix where it
Packit ea1746
  // should be stored.
Packit ea1746
  //
Packit ea1746
  // This is the principal look up table that allows this class to
Packit ea1746
  // compute the inner product fast.
Packit ea1746
  std::vector<int> result_offsets_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_INNER_PRODUCT_COMPUTER_H_