blob: 2b51b9bb57d5eee5de3743207b1978d4c15f91e5 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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#ifndef CERES_INTERNAL_COMPRESSED_ROW_SPARSE_MATRIX_H_
32#define CERES_INTERNAL_COMPRESSED_ROW_SPARSE_MATRIX_H_
33
34#include <vector>
35#include "ceres/internal/port.h"
36#include "ceres/sparse_matrix.h"
37#include "ceres/types.h"
38#include "glog/logging.h"
39
40namespace ceres {
41
42struct CRSMatrix;
43
44namespace internal {
45
46class TripletSparseMatrix;
47
48class CompressedRowSparseMatrix : public SparseMatrix {
49 public:
50 enum StorageType {
51 UNSYMMETRIC,
52 // Matrix is assumed to be symmetric but only the lower triangular
53 // part of the matrix is stored.
54 LOWER_TRIANGULAR,
55 // Matrix is assumed to be symmetric but only the upper triangular
56 // part of the matrix is stored.
57 UPPER_TRIANGULAR
58 };
59
60 // Create a matrix with the same content as the TripletSparseMatrix
61 // input. We assume that input does not have any repeated
62 // entries.
63 //
64 // The storage type of the matrix is set to UNSYMMETRIC.
65 //
66 // Caller owns the result.
67 static CompressedRowSparseMatrix* FromTripletSparseMatrix(
68 const TripletSparseMatrix& input);
69
70 // Create a matrix with the same content as the TripletSparseMatrix
71 // input transposed. We assume that input does not have any repeated
72 // entries.
73 //
74 // The storage type of the matrix is set to UNSYMMETRIC.
75 //
76 // Caller owns the result.
77 static CompressedRowSparseMatrix* FromTripletSparseMatrixTransposed(
78 const TripletSparseMatrix& input);
79
80 // Use this constructor only if you know what you are doing. This
81 // creates a "blank" matrix with the appropriate amount of memory
82 // allocated. However, the object itself is in an inconsistent state
83 // as the rows and cols matrices do not match the values of
84 // num_rows, num_cols and max_num_nonzeros.
85 //
86 // The use case for this constructor is that when the user knows the
87 // size of the matrix to begin with and wants to update the layout
88 // manually, instead of going via the indirect route of first
89 // constructing a TripletSparseMatrix, which leads to more than
90 // double the peak memory usage.
91 //
92 // The storage type is set to UNSYMMETRIC.
93 CompressedRowSparseMatrix(int num_rows, int num_cols, int max_num_nonzeros);
94
95 // Build a square sparse diagonal matrix with num_rows rows and
96 // columns. The diagonal m(i,i) = diagonal(i);
97 //
98 // The storage type is set to UNSYMMETRIC
99 CompressedRowSparseMatrix(const double* diagonal, int num_rows);
100
101 // SparseMatrix interface.
102 virtual ~CompressedRowSparseMatrix();
103 virtual void SetZero();
104 virtual void RightMultiply(const double* x, double* y) const;
105 virtual void LeftMultiply(const double* x, double* y) const;
106 virtual void SquaredColumnNorm(double* x) const;
107 virtual void ScaleColumns(const double* scale);
108 virtual void ToDenseMatrix(Matrix* dense_matrix) const;
109 virtual void ToTextFile(FILE* file) const;
110 virtual int num_rows() const { return num_rows_; }
111 virtual int num_cols() const { return num_cols_; }
112 virtual int num_nonzeros() const { return rows_[num_rows_]; }
113 virtual const double* values() const { return &values_[0]; }
114 virtual double* mutable_values() { return &values_[0]; }
115
116 // Delete the bottom delta_rows.
117 // num_rows -= delta_rows
118 void DeleteRows(int delta_rows);
119
120 // Append the contents of m to the bottom of this matrix. m must
121 // have the same number of columns as this matrix.
122 void AppendRows(const CompressedRowSparseMatrix& m);
123
124 void ToCRSMatrix(CRSMatrix* matrix) const;
125
126 CompressedRowSparseMatrix* Transpose() const;
127
128 // Destructive array resizing method.
129 void SetMaxNumNonZeros(int num_nonzeros);
130
131 // Non-destructive array resizing method.
132 void set_num_rows(const int num_rows) { num_rows_ = num_rows; }
133 void set_num_cols(const int num_cols) { num_cols_ = num_cols; }
134
135 // Low level access methods that expose the structure of the matrix.
136 const int* cols() const { return &cols_[0]; }
137 int* mutable_cols() { return &cols_[0]; }
138
139 const int* rows() const { return &rows_[0]; }
140 int* mutable_rows() { return &rows_[0]; }
141
142 const StorageType storage_type() const { return storage_type_; }
143 void set_storage_type(const StorageType storage_type) {
144 storage_type_ = storage_type;
145 }
146
147 const std::vector<int>& row_blocks() const { return row_blocks_; }
148 std::vector<int>* mutable_row_blocks() { return &row_blocks_; }
149
150 const std::vector<int>& col_blocks() const { return col_blocks_; }
151 std::vector<int>* mutable_col_blocks() { return &col_blocks_; }
152
153 // Create a block diagonal CompressedRowSparseMatrix with the given
154 // block structure. The individual blocks are assumed to be laid out
155 // contiguously in the diagonal array, one block at a time.
156 //
157 // Caller owns the result.
158 static CompressedRowSparseMatrix* CreateBlockDiagonalMatrix(
159 const double* diagonal, const std::vector<int>& blocks);
160
161 // Options struct to control the generation of random block sparse
162 // matrices in compressed row sparse format.
163 //
164 // The random matrix generation proceeds as follows.
165 //
166 // First the row and column block structure is determined by
167 // generating random row and column block sizes that lie within the
168 // given bounds.
169 //
170 // Then we walk the block structure of the resulting matrix, and with
171 // probability block_density detemine whether they are structurally
172 // zero or not. If the answer is no, then we generate entries for the
173 // block which are distributed normally.
174 struct RandomMatrixOptions {
175 // Type of matrix to create.
176 //
177 // If storage_type is UPPER_TRIANGULAR (LOWER_TRIANGULAR), then
178 // create a square symmetric matrix with just the upper triangular
179 // (lower triangular) part. In this case, num_col_blocks,
180 // min_col_block_size and max_col_block_size will be ignored and
181 // assumed to be equal to the corresponding row settings.
182 StorageType storage_type = UNSYMMETRIC;
183
184 int num_row_blocks = 0;
185 int min_row_block_size = 0;
186 int max_row_block_size = 0;
187 int num_col_blocks = 0;
188 int min_col_block_size = 0;
189 int max_col_block_size = 0;
190
191 // 0 < block_density <= 1 is the probability of a block being
192 // present in the matrix. A given random matrix will not have
193 // precisely this density.
194 double block_density = 0.0;
195 };
196
197 // Create a random CompressedRowSparseMatrix whose entries are
198 // normally distributed and whose structure is determined by
199 // RandomMatrixOptions.
200 //
201 // Caller owns the result.
202 static CompressedRowSparseMatrix* CreateRandomMatrix(
203 RandomMatrixOptions options);
204
205 private:
206 static CompressedRowSparseMatrix* FromTripletSparseMatrix(
207 const TripletSparseMatrix& input, bool transpose);
208
209 int num_rows_;
210 int num_cols_;
211 std::vector<int> rows_;
212 std::vector<int> cols_;
213 std::vector<double> values_;
214 StorageType storage_type_;
215
216 // If the matrix has an underlying block structure, then it can also
217 // carry with it row and column block sizes. This is auxilliary and
218 // optional information for use by algorithms operating on the
219 // matrix. The class itself does not make use of this information in
220 // any way.
221 std::vector<int> row_blocks_;
222 std::vector<int> col_blocks_;
223};
224
225} // namespace internal
226} // namespace ceres
227
228#endif // CERES_INTERNAL_COMPRESSED_ROW_SPARSE_MATRIX_H_