Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 38 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 39 | #include "ceres/block_structure.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 40 | #include "ceres/internal/eigen.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 41 | #include "ceres/internal/port.h" |
| 42 | #include "ceres/sparse_matrix.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 43 | |
| 44 | namespace ceres { |
| 45 | namespace internal { |
| 46 | |
| 47 | class TripletSparseMatrix; |
| 48 | |
| 49 | // This class implements the SparseMatrix interface for storing and |
| 50 | // manipulating block sparse matrices. The block structure is stored |
| 51 | // in the CompressedRowBlockStructure object and one is needed to |
| 52 | // initialize the matrix. For details on how the blocks structure of |
| 53 | // the matrix is stored please see the documentation |
| 54 | // |
| 55 | // internal/ceres/block_structure.h |
| 56 | // |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 57 | class CERES_EXPORT_INTERNAL BlockSparseMatrix : public SparseMatrix { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 58 | public: |
| 59 | // Construct a block sparse matrix with a fully initialized |
| 60 | // CompressedRowBlockStructure objected. The matrix takes over |
| 61 | // ownership of this object and destroys it upon destruction. |
| 62 | // |
| 63 | // TODO(sameeragarwal): Add a function which will validate legal |
| 64 | // CompressedRowBlockStructure objects. |
| 65 | explicit BlockSparseMatrix(CompressedRowBlockStructure* block_structure); |
| 66 | |
| 67 | BlockSparseMatrix(); |
| 68 | BlockSparseMatrix(const BlockSparseMatrix&) = delete; |
| 69 | void operator=(const BlockSparseMatrix&) = delete; |
| 70 | |
| 71 | virtual ~BlockSparseMatrix(); |
| 72 | |
| 73 | // Implementation of SparseMatrix interface. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 74 | void SetZero() final; |
| 75 | void RightMultiply(const double* x, double* y) const final; |
| 76 | void LeftMultiply(const double* x, double* y) const final; |
| 77 | void SquaredColumnNorm(double* x) const final; |
| 78 | void ScaleColumns(const double* scale) final; |
| 79 | void ToDenseMatrix(Matrix* dense_matrix) const final; |
| 80 | void ToTextFile(FILE* file) const final; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 82 | // clang-format off |
| 83 | int num_rows() const final { return num_rows_; } |
| 84 | int num_cols() const final { return num_cols_; } |
| 85 | int num_nonzeros() const final { return num_nonzeros_; } |
| 86 | const double* values() const final { return values_.get(); } |
| 87 | double* mutable_values() final { return values_.get(); } |
| 88 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 89 | |
| 90 | void ToTripletSparseMatrix(TripletSparseMatrix* matrix) const; |
| 91 | const CompressedRowBlockStructure* block_structure() const; |
| 92 | |
| 93 | // Append the contents of m to the bottom of this matrix. m must |
| 94 | // have the same column blocks structure as this matrix. |
| 95 | void AppendRows(const BlockSparseMatrix& m); |
| 96 | |
| 97 | // Delete the bottom delta_rows_blocks. |
| 98 | void DeleteRowBlocks(int delta_row_blocks); |
| 99 | |
| 100 | static BlockSparseMatrix* CreateDiagonalMatrix( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 101 | const double* diagonal, const std::vector<Block>& column_blocks); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 102 | |
| 103 | struct RandomMatrixOptions { |
| 104 | int num_row_blocks = 0; |
| 105 | int min_row_block_size = 0; |
| 106 | int max_row_block_size = 0; |
| 107 | int num_col_blocks = 0; |
| 108 | int min_col_block_size = 0; |
| 109 | int max_col_block_size = 0; |
| 110 | |
| 111 | // 0 < block_density <= 1 is the probability of a block being |
| 112 | // present in the matrix. A given random matrix will not have |
| 113 | // precisely this density. |
| 114 | double block_density = 0.0; |
| 115 | |
| 116 | // If col_blocks is non-empty, then the generated random matrix |
| 117 | // has this block structure and the column related options in this |
| 118 | // struct are ignored. |
| 119 | std::vector<Block> col_blocks; |
| 120 | }; |
| 121 | |
| 122 | // Create a random BlockSparseMatrix whose entries are normally |
| 123 | // distributed and whose structure is determined by |
| 124 | // RandomMatrixOptions. |
| 125 | // |
| 126 | // Caller owns the result. |
| 127 | static BlockSparseMatrix* CreateRandomMatrix( |
| 128 | const RandomMatrixOptions& options); |
| 129 | |
| 130 | private: |
| 131 | int num_rows_; |
| 132 | int num_cols_; |
| 133 | int num_nonzeros_; |
| 134 | int max_num_nonzeros_; |
| 135 | std::unique_ptr<double[]> values_; |
| 136 | std::unique_ptr<CompressedRowBlockStructure> block_structure_; |
| 137 | }; |
| 138 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 139 | // A number of algorithms like the SchurEliminator do not need |
| 140 | // access to the full BlockSparseMatrix interface. They only |
| 141 | // need read only access to the values array and the block structure. |
| 142 | // |
| 143 | // BlockSparseDataMatrix a struct that carries these two bits of |
| 144 | // information |
| 145 | class BlockSparseMatrixData { |
| 146 | public: |
| 147 | BlockSparseMatrixData(const BlockSparseMatrix& m) |
| 148 | : block_structure_(m.block_structure()), values_(m.values()){}; |
| 149 | |
| 150 | BlockSparseMatrixData(const CompressedRowBlockStructure* block_structure, |
| 151 | const double* values) |
| 152 | : block_structure_(block_structure), values_(values) {} |
| 153 | |
| 154 | const CompressedRowBlockStructure* block_structure() const { |
| 155 | return block_structure_; |
| 156 | } |
| 157 | const double* values() const { return values_; } |
| 158 | |
| 159 | private: |
| 160 | const CompressedRowBlockStructure* block_structure_; |
| 161 | const double* values_; |
| 162 | }; |
| 163 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 164 | } // namespace internal |
| 165 | } // namespace ceres |
| 166 | |
| 167 | #endif // CERES_INTERNAL_BLOCK_SPARSE_MATRIX_H_ |