1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: richie.stebbing@gmail.com (Richard Stebbing)
31 // A compressed row sparse matrix that provides an extended interface to
32 // allow dynamic insertion of entries. This is provided for the use case
33 // where the sparsity structure and number of non-zero entries is dynamic.
34 // This flexibility is achieved by using an (internal) scratch space that
35 // allows independent insertion of entries into each row (thread-safe).
36 // Once insertion is complete, the `Finalize` method must be called to ensure
37 // that the underlying `CompressedRowSparseMatrix` is consistent.
39 // This should only be used if you really do need a dynamic sparsity pattern.
41 #ifndef CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_
42 #define CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_
46 #include "ceres/compressed_row_sparse_matrix.h"
51 class DynamicCompressedRowSparseMatrix : public CompressedRowSparseMatrix {
53 // Set the number of rows and columns for the underlyig
54 // `CompressedRowSparseMatrix` and set the initial number of maximum non-zero
55 // entries. Note that following the insertion of entries, when `Finalize`
56 // is called the number of non-zeros is determined and all internal
57 // structures are adjusted as required. If you know the upper limit on the
58 // number of non-zeros, then passing this value here can prevent future
59 // memory reallocations which may improve performance. Otherwise, if no
60 // upper limit is available a value of 0 is sufficient.
62 // Typical usage of this class is to define a new instance with a given
63 // number of rows, columns and maximum number of non-zero elements
64 // (if available). Next, entries are inserted at row and column positions
65 // using `InsertEntry`. Finally, once all elements have been inserted,
66 // `Finalize` must be called to make the underlying
67 // `CompressedRowSparseMatrix` consistent.
68 DynamicCompressedRowSparseMatrix(int num_rows,
70 int initial_max_num_nonzeros);
72 // Insert an entry at a given row and column position. This method is
73 // thread-safe across rows i.e. different threads can insert values
74 // simultaneously into different rows. It should be emphasised that this
75 // method always inserts a new entry and does not check for existing
76 // entries at the specified row and column position. Duplicate entries
77 // for a given row and column position will result in undefined
79 void InsertEntry(int row, int col, const double& value);
81 // Clear all entries for rows, starting from row index `row_start`
82 // and proceeding for `num_rows`.
83 void ClearRows(int row_start, int num_rows);
85 // Make the underlying internal `CompressedRowSparseMatrix` data structures
86 // consistent. Additional space for non-zero entries in the
87 // `CompressedRowSparseMatrix` can be reserved by specifying
88 // `num_additional_elements`. This is useful when it is known that rows will
89 // be appended to the `CompressedRowSparseMatrix` (e.g. appending a diagonal
90 // matrix to the jacobian) as it prevents need for future reallocation.
91 void Finalize(int num_additional_elements);
94 std::vector<std::vector<int> > dynamic_cols_;
95 std::vector<std::vector<double> > dynamic_values_;
98 } // namespace internal
101 #endif // CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_