Blame internal/ceres/dynamic_compressed_row_sparse_matrix.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 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: richie.stebbing@gmail.com (Richard Stebbing)
Packit ea1746
//
Packit ea1746
// A compressed row sparse matrix that provides an extended interface to
Packit ea1746
// allow dynamic insertion of entries. This is provided for the use case
Packit ea1746
// where the sparsity structure and number of non-zero entries is dynamic.
Packit ea1746
// This flexibility is achieved by using an (internal) scratch space that
Packit ea1746
// allows independent insertion of entries into each row (thread-safe).
Packit ea1746
// Once insertion is complete, the `Finalize` method must be called to ensure
Packit ea1746
// that the underlying `CompressedRowSparseMatrix` is consistent.
Packit ea1746
//
Packit ea1746
// This should only be used if you really do need a dynamic sparsity pattern.
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_
Packit ea1746
#define CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
Packit ea1746
#include "ceres/compressed_row_sparse_matrix.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class DynamicCompressedRowSparseMatrix : public CompressedRowSparseMatrix {
Packit ea1746
 public:
Packit ea1746
  // Set the number of rows and columns for the underlyig
Packit ea1746
  // `CompressedRowSparseMatrix` and set the initial number of maximum non-zero
Packit ea1746
  // entries. Note that following the insertion of entries, when `Finalize`
Packit ea1746
  // is called the number of non-zeros is determined and all internal
Packit ea1746
  // structures are adjusted as required. If you know the upper limit on the
Packit ea1746
  // number of non-zeros, then passing this value here can prevent future
Packit ea1746
  // memory reallocations which may improve performance. Otherwise, if no
Packit ea1746
  // upper limit is available a value of 0 is sufficient.
Packit ea1746
  //
Packit ea1746
  // Typical usage of this class is to define a new instance with a given
Packit ea1746
  // number of rows, columns and maximum number of non-zero elements
Packit ea1746
  // (if available). Next, entries are inserted at row and column positions
Packit ea1746
  // using `InsertEntry`. Finally, once all elements have been inserted,
Packit ea1746
  // `Finalize` must be called to make the underlying
Packit ea1746
  // `CompressedRowSparseMatrix` consistent.
Packit ea1746
  DynamicCompressedRowSparseMatrix(int num_rows,
Packit ea1746
                                   int num_cols,
Packit ea1746
                                   int initial_max_num_nonzeros);
Packit ea1746
Packit ea1746
  // Insert an entry at a given row and column position. This method is
Packit ea1746
  // thread-safe across rows i.e. different threads can insert values
Packit ea1746
  // simultaneously into different rows. It should be emphasised that this
Packit ea1746
  // method always inserts a new entry and does not check for existing
Packit ea1746
  // entries at the specified row and column position. Duplicate entries
Packit ea1746
  // for a given row and column position will result in undefined
Packit ea1746
  // behavior.
Packit ea1746
  void InsertEntry(int row, int col, const double& value);
Packit ea1746
Packit ea1746
  // Clear all entries for rows, starting from row index `row_start`
Packit ea1746
  // and proceeding for `num_rows`.
Packit ea1746
  void ClearRows(int row_start, int num_rows);
Packit ea1746
Packit ea1746
  // Make the underlying internal `CompressedRowSparseMatrix` data structures
Packit ea1746
  // consistent. Additional space for non-zero entries in the
Packit ea1746
  // `CompressedRowSparseMatrix` can be reserved by specifying
Packit ea1746
  // `num_additional_elements`. This is useful when it is known that rows will
Packit ea1746
  // be appended to the `CompressedRowSparseMatrix` (e.g. appending a diagonal
Packit ea1746
  // matrix to the jacobian) as it prevents need for future reallocation.
Packit ea1746
  void Finalize(int num_additional_elements);
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  std::vector<std::vector<int> > dynamic_cols_;
Packit ea1746
  std::vector<std::vector<double> > dynamic_values_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_