1 // Ceres Solver - A fast non-linear least squares minimizer 2 // Copyright 2015 Google Inc. All rights reserved. 3 // http://ceres-solver.org/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are met: 7 // 8 // * Redistributions of source code must retain the above copyright notice, 9 // this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright notice, 11 // this list of conditions and the following disclaimer in the documentation 12 // and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors may be 14 // used to endorse or promote products derived from this software without 15 // specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 // POSSIBILITY OF SUCH DAMAGE. 28 // 29 // Author: sameeragarwal@google.com (Sameer Agarwal) 30 // 31 // Implementation of the SparseMatrix interface for block sparse 32 // matrices. 33 34 #ifndef CERES_INTERNAL_BLOCK_SPARSE_MATRIX_H_ 35 #define CERES_INTERNAL_BLOCK_SPARSE_MATRIX_H_ 36 37 #include <memory> 38 #include "ceres/block_structure.h" 39 #include "ceres/sparse_matrix.h" 40 #include "ceres/internal/eigen.h" 41 42 namespace ceres { 43 namespace internal { 44 45 class TripletSparseMatrix; 46 47 // This class implements the SparseMatrix interface for storing and 48 // manipulating block sparse matrices. The block structure is stored 49 // in the CompressedRowBlockStructure object and one is needed to 50 // initialize the matrix. For details on how the blocks structure of 51 // the matrix is stored please see the documentation 52 // 53 // internal/ceres/block_structure.h 54 // 55 class BlockSparseMatrix : public SparseMatrix { 56 public: 57 // Construct a block sparse matrix with a fully initialized 58 // CompressedRowBlockStructure objected. The matrix takes over 59 // ownership of this object and destroys it upon destruction. 60 // 61 // TODO(sameeragarwal): Add a function which will validate legal 62 // CompressedRowBlockStructure objects. 63 explicit BlockSparseMatrix(CompressedRowBlockStructure* block_structure); 64 65 BlockSparseMatrix(); 66 BlockSparseMatrix(const BlockSparseMatrix&) = delete; 67 void operator=(const BlockSparseMatrix&) = delete; 68 69 virtual ~BlockSparseMatrix(); 70 71 // Implementation of SparseMatrix interface. 72 void SetZero() final; 73 void RightMultiply(const double* x, double* y) const final; 74 void LeftMultiply(const double* x, double* y) const final; 75 void SquaredColumnNorm(double* x) const final; 76 void ScaleColumns(const double* scale) final; 77 void ToDenseMatrix(Matrix* dense_matrix) const final; 78 void ToTextFile(FILE* file) const final; 79 num_rows()80 int num_rows() const final { return num_rows_; } num_cols()81 int num_cols() const final { return num_cols_; } num_nonzeros()82 int num_nonzeros() const final { return num_nonzeros_; } values()83 const double* values() const final { return values_.get(); } mutable_values()84 double* mutable_values() final { return values_.get(); } 85 86 void ToTripletSparseMatrix(TripletSparseMatrix* matrix) const; 87 const CompressedRowBlockStructure* block_structure() const; 88 89 // Append the contents of m to the bottom of this matrix. m must 90 // have the same column blocks structure as this matrix. 91 void AppendRows(const BlockSparseMatrix& m); 92 93 // Delete the bottom delta_rows_blocks. 94 void DeleteRowBlocks(int delta_row_blocks); 95 96 static BlockSparseMatrix* CreateDiagonalMatrix( 97 const double* diagonal, 98 const std::vector<Block>& column_blocks); 99 100 struct RandomMatrixOptions { 101 int num_row_blocks = 0; 102 int min_row_block_size = 0; 103 int max_row_block_size = 0; 104 int num_col_blocks = 0; 105 int min_col_block_size = 0; 106 int max_col_block_size = 0; 107 108 // 0 < block_density <= 1 is the probability of a block being 109 // present in the matrix. A given random matrix will not have 110 // precisely this density. 111 double block_density = 0.0; 112 113 // If col_blocks is non-empty, then the generated random matrix 114 // has this block structure and the column related options in this 115 // struct are ignored. 116 std::vector<Block> col_blocks; 117 }; 118 119 // Create a random BlockSparseMatrix whose entries are normally 120 // distributed and whose structure is determined by 121 // RandomMatrixOptions. 122 // 123 // Caller owns the result. 124 static BlockSparseMatrix* CreateRandomMatrix( 125 const RandomMatrixOptions& options); 126 127 private: 128 int num_rows_; 129 int num_cols_; 130 int num_nonzeros_; 131 int max_num_nonzeros_; 132 std::unique_ptr<double[]> values_; 133 std::unique_ptr<CompressedRowBlockStructure> block_structure_; 134 }; 135 136 // A number of algorithms like the SchurEliminator do not need 137 // access to the full BlockSparseMatrix interface. They only 138 // need read only access to the values array and the block structure. 139 // 140 // BlockSparseDataMatrix a struct that carries these two bits of 141 // information 142 class BlockSparseMatrixData { 143 public: BlockSparseMatrixData(const BlockSparseMatrix & m)144 BlockSparseMatrixData(const BlockSparseMatrix& m) 145 : block_structure_(m.block_structure()), values_(m.values()){}; 146 BlockSparseMatrixData(const CompressedRowBlockStructure * block_structure,const double * values)147 BlockSparseMatrixData(const CompressedRowBlockStructure* block_structure, 148 const double* values) 149 : block_structure_(block_structure), values_(values) {} 150 block_structure()151 const CompressedRowBlockStructure* block_structure() const { 152 return block_structure_; 153 } values()154 const double* values() const { return values_; } 155 156 private: 157 const CompressedRowBlockStructure* block_structure_; 158 const double* values_; 159 }; 160 161 } // namespace internal 162 } // namespace ceres 163 164 #endif // CERES_INTERNAL_BLOCK_SPARSE_MATRIX_H_ 165