Blame internal/ceres/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: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_COMPRESSED_ROW_SPARSE_MATRIX_H_
Packit ea1746
#define CERES_INTERNAL_COMPRESSED_ROW_SPARSE_MATRIX_H_
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/internal/macros.h"
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
#include "ceres/sparse_matrix.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
struct CRSMatrix;
Packit ea1746
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class TripletSparseMatrix;
Packit ea1746
Packit ea1746
class CompressedRowSparseMatrix : public SparseMatrix {
Packit ea1746
 public:
Packit ea1746
  enum StorageType {
Packit ea1746
    UNSYMMETRIC,
Packit ea1746
    // Matrix is assumed to be symmetric but only the lower triangular
Packit ea1746
    // part of the matrix is stored.
Packit ea1746
    LOWER_TRIANGULAR,
Packit ea1746
    // Matrix is assumed to be symmetric but only the upper triangular
Packit ea1746
    // part of the matrix is stored.
Packit ea1746
    UPPER_TRIANGULAR
Packit ea1746
  };
Packit ea1746
Packit ea1746
  // Create a matrix with the same content as the TripletSparseMatrix
Packit ea1746
  // input. We assume that input does not have any repeated
Packit ea1746
  // entries.
Packit ea1746
  //
Packit ea1746
  // The storage type of the matrix is set to UNSYMMETRIC.
Packit ea1746
  //
Packit ea1746
  // Caller owns the result.
Packit ea1746
  static CompressedRowSparseMatrix* FromTripletSparseMatrix(
Packit ea1746
      const TripletSparseMatrix& input);
Packit ea1746
Packit ea1746
  // Create a matrix with the same content as the TripletSparseMatrix
Packit ea1746
  // input transposed. We assume that input does not have any repeated
Packit ea1746
  // entries.
Packit ea1746
  //
Packit ea1746
  // The storage type of the matrix is set to UNSYMMETRIC.
Packit ea1746
  //
Packit ea1746
  // Caller owns the result.
Packit ea1746
  static CompressedRowSparseMatrix* FromTripletSparseMatrixTransposed(
Packit ea1746
      const TripletSparseMatrix& input);
Packit ea1746
Packit ea1746
  // Use this constructor only if you know what you are doing. This
Packit ea1746
  // creates a "blank" matrix with the appropriate amount of memory
Packit ea1746
  // allocated. However, the object itself is in an inconsistent state
Packit ea1746
  // as the rows and cols matrices do not match the values of
Packit ea1746
  // num_rows, num_cols and max_num_nonzeros.
Packit ea1746
  //
Packit ea1746
  // The use case for this constructor is that when the user knows the
Packit ea1746
  // size of the matrix to begin with and wants to update the layout
Packit ea1746
  // manually, instead of going via the indirect route of first
Packit ea1746
  // constructing a TripletSparseMatrix, which leads to more than
Packit ea1746
  // double the peak memory usage.
Packit ea1746
  //
Packit ea1746
  // The storage type is set to UNSYMMETRIC.
Packit ea1746
  CompressedRowSparseMatrix(int num_rows,
Packit ea1746
                            int num_cols,
Packit ea1746
                            int max_num_nonzeros);
Packit ea1746
Packit ea1746
  // Build a square sparse diagonal matrix with num_rows rows and
Packit ea1746
  // columns. The diagonal m(i,i) = diagonal(i);
Packit ea1746
  //
Packit ea1746
  // The storage type is set to UNSYMMETRIC
Packit ea1746
  CompressedRowSparseMatrix(const double* diagonal, int num_rows);
Packit ea1746
Packit ea1746
  // SparseMatrix interface.
Packit ea1746
  virtual ~CompressedRowSparseMatrix();
Packit ea1746
  virtual void SetZero();
Packit ea1746
  virtual void RightMultiply(const double* x, double* y) const;
Packit ea1746
  virtual void LeftMultiply(const double* x, double* y) const;
Packit ea1746
  virtual void SquaredColumnNorm(double* x) const;
Packit ea1746
  virtual void ScaleColumns(const double* scale);
Packit ea1746
Packit ea1746
  virtual void ToDenseMatrix(Matrix* dense_matrix) const;
Packit ea1746
  virtual void ToTextFile(FILE* file) const;
Packit ea1746
  virtual int num_rows() const { return num_rows_; }
Packit ea1746
  virtual int num_cols() const { return num_cols_; }
Packit ea1746
  virtual int num_nonzeros() const { return rows_[num_rows_]; }
Packit ea1746
  virtual const double* values() const { return &values_[0]; }
Packit ea1746
  virtual double* mutable_values() { return &values_[0]; }
Packit ea1746
Packit ea1746
  // Delete the bottom delta_rows.
Packit ea1746
  // num_rows -= delta_rows
Packit ea1746
  void DeleteRows(int delta_rows);
Packit ea1746
Packit ea1746
  // Append the contents of m to the bottom of this matrix. m must
Packit ea1746
  // have the same number of columns as this matrix.
Packit ea1746
  void AppendRows(const CompressedRowSparseMatrix& m);
Packit ea1746
Packit ea1746
  void ToCRSMatrix(CRSMatrix* matrix) const;
Packit ea1746
Packit ea1746
  CompressedRowSparseMatrix* Transpose() const;
Packit ea1746
Packit ea1746
  // Destructive array resizing method.
Packit ea1746
  void SetMaxNumNonZeros(int num_nonzeros);
Packit ea1746
Packit ea1746
  // Non-destructive array resizing method.
Packit ea1746
  void set_num_rows(const int num_rows) { num_rows_ = num_rows; }
Packit ea1746
  void set_num_cols(const int num_cols) { num_cols_ = num_cols; }
Packit ea1746
Packit ea1746
  // Low level access methods that expose the structure of the matrix.
Packit ea1746
  const int* cols() const { return &cols_[0]; }
Packit ea1746
  int* mutable_cols() { return &cols_[0]; }
Packit ea1746
Packit ea1746
  const int* rows() const { return &rows_[0]; }
Packit ea1746
  int* mutable_rows() { return &rows_[0]; }
Packit ea1746
Packit ea1746
  const StorageType storage_type() const { return storage_type_; }
Packit ea1746
  void set_storage_type(const StorageType storage_type) {
Packit ea1746
    storage_type_ = storage_type;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  const std::vector<int>& row_blocks() const { return row_blocks_; }
Packit ea1746
  std::vector<int>* mutable_row_blocks() { return &row_blocks_; }
Packit ea1746
Packit ea1746
  const std::vector<int>& col_blocks() const { return col_blocks_; }
Packit ea1746
  std::vector<int>* mutable_col_blocks() { return &col_blocks_; }
Packit ea1746
Packit ea1746
  // Create a block diagonal CompressedRowSparseMatrix with the given
Packit ea1746
  // block structure. The individual blocks are assumed to be laid out
Packit ea1746
  // contiguously in the diagonal array, one block at a time.
Packit ea1746
  //
Packit ea1746
  // Caller owns the result.
Packit ea1746
  static CompressedRowSparseMatrix* CreateBlockDiagonalMatrix(
Packit ea1746
      const double* diagonal,
Packit ea1746
      const std::vector<int>& blocks);
Packit ea1746
Packit ea1746
  // Options struct to control the generation of random block sparse
Packit ea1746
  // matrices in compressed row sparse format.
Packit ea1746
  //
Packit ea1746
  // The random matrix generation proceeds as follows.
Packit ea1746
  //
Packit ea1746
  // First the row and column block structure is determined by
Packit ea1746
  // generating random row and column block sizes that lie within the
Packit ea1746
  // given bounds.
Packit ea1746
  //
Packit ea1746
  // Then we walk the block structure of the resulting matrix, and with
Packit ea1746
  // probability block_density detemine whether they are structurally
Packit ea1746
  // zero or not. If the answer is no, then we generate entries for the
Packit ea1746
  // block which are distributed normally.
Packit ea1746
  struct RandomMatrixOptions {
Packit ea1746
    RandomMatrixOptions()
Packit ea1746
        : num_row_blocks(0),
Packit ea1746
          min_row_block_size(0),
Packit ea1746
          max_row_block_size(0),
Packit ea1746
          num_col_blocks(0),
Packit ea1746
          min_col_block_size(0),
Packit ea1746
          max_col_block_size(0),
Packit ea1746
          block_density(0.0) {
Packit ea1746
    }
Packit ea1746
Packit ea1746
    int num_row_blocks;
Packit ea1746
    int min_row_block_size;
Packit ea1746
    int max_row_block_size;
Packit ea1746
    int num_col_blocks;
Packit ea1746
    int min_col_block_size;
Packit ea1746
    int max_col_block_size;
Packit ea1746
Packit ea1746
    // 0 < block_density <= 1 is the probability of a block being
Packit ea1746
    // present in the matrix. A given random matrix will not have
Packit ea1746
    // precisely this density.
Packit ea1746
    double block_density;
Packit ea1746
  };
Packit ea1746
Packit ea1746
  // Create a random CompressedRowSparseMatrix whose entries are
Packit ea1746
  // normally distributed and whose structure is determined by
Packit ea1746
  // RandomMatrixOptions.
Packit ea1746
  //
Packit ea1746
  // Caller owns the result.
Packit ea1746
  static CompressedRowSparseMatrix* CreateRandomMatrix(
Packit ea1746
      const RandomMatrixOptions& options);
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  static CompressedRowSparseMatrix* FromTripletSparseMatrix(
Packit ea1746
      const TripletSparseMatrix& input, bool transpose);
Packit ea1746
Packit ea1746
  int num_rows_;
Packit ea1746
  int num_cols_;
Packit ea1746
  std::vector<int> rows_;
Packit ea1746
  std::vector<int> cols_;
Packit ea1746
  std::vector<double> values_;
Packit ea1746
  StorageType storage_type_;
Packit ea1746
Packit ea1746
  // If the matrix has an underlying block structure, then it can also
Packit ea1746
  // carry with it row and column block sizes. This is auxilliary and
Packit ea1746
  // optional information for use by algorithms operating on the
Packit ea1746
  // matrix. The class itself does not make use of this information in
Packit ea1746
  // any way.
Packit ea1746
  std::vector<int> row_blocks_;
Packit ea1746
  std::vector<int> col_blocks_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_COMPRESSED_ROW_SPARSE_MATRIX_H_