blob: cc9fee572a2f9e9858b198ae7adb867eba47c2bb [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_TRIPLET_SPARSE_MATRIX_H_
32#define CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H_
33
34#include <memory>
35#include <vector>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/internal/eigen.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038#include "ceres/internal/port.h"
39#include "ceres/sparse_matrix.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080040#include "ceres/types.h"
41
42namespace ceres {
43namespace internal {
44
45// An implementation of the SparseMatrix interface to store and
46// manipulate sparse matrices in triplet (i,j,s) form. This object is
47// inspired by the design of the cholmod_triplet struct used in the
48// SuiteSparse package and is memory layout compatible with it.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080049class CERES_EXPORT_INTERNAL TripletSparseMatrix : public SparseMatrix {
Austin Schuh70cc9552019-01-21 19:46:48 -080050 public:
51 TripletSparseMatrix();
52 TripletSparseMatrix(int num_rows, int num_cols, int max_num_nonzeros);
53 TripletSparseMatrix(int num_rows,
54 int num_cols,
55 const std::vector<int>& rows,
56 const std::vector<int>& cols,
57 const std::vector<double>& values);
58
59 explicit TripletSparseMatrix(const TripletSparseMatrix& orig);
60
61 TripletSparseMatrix& operator=(const TripletSparseMatrix& rhs);
62
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080063 virtual ~TripletSparseMatrix();
Austin Schuh70cc9552019-01-21 19:46:48 -080064
65 // Implementation of the SparseMatrix interface.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080066 void SetZero() final;
67 void RightMultiply(const double* x, double* y) const final;
68 void LeftMultiply(const double* x, double* y) const final;
69 void SquaredColumnNorm(double* x) const final;
70 void ScaleColumns(const double* scale) final;
71 void ToDenseMatrix(Matrix* dense_matrix) const final;
72 void ToTextFile(FILE* file) const final;
73 // clang-format off
74 int num_rows() const final { return num_rows_; }
75 int num_cols() const final { return num_cols_; }
76 int num_nonzeros() const final { return num_nonzeros_; }
77 const double* values() const final { return values_.get(); }
78 double* mutable_values() final { return values_.get(); }
79 // clang-format on
80 void set_num_nonzeros(int num_nonzeros);
Austin Schuh70cc9552019-01-21 19:46:48 -080081
82 // Increase max_num_nonzeros and correspondingly increase the size
83 // of rows_, cols_ and values_. If new_max_num_nonzeros is smaller
84 // than max_num_nonzeros_, then num_non_zeros should be less than or
85 // equal to new_max_num_nonzeros, otherwise data loss is possible
86 // and the method crashes.
87 void Reserve(int new_max_num_nonzeros);
88
89 // Append the matrix B at the bottom of this matrix. B should have
90 // the same number of columns as num_cols_.
91 void AppendRows(const TripletSparseMatrix& B);
92
93 // Append the matrix B at the right of this matrix. B should have
94 // the same number of rows as num_rows_;
95 void AppendCols(const TripletSparseMatrix& B);
96
97 // Resize the matrix. Entries which fall outside the new matrix
98 // bounds are dropped and the num_non_zeros changed accordingly.
99 void Resize(int new_num_rows, int new_num_cols);
100
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800101 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800102 int max_num_nonzeros() const { return max_num_nonzeros_; }
103 const int* rows() const { return rows_.get(); }
104 const int* cols() const { return cols_.get(); }
105 int* mutable_rows() { return rows_.get(); }
106 int* mutable_cols() { return cols_.get(); }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800107 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800108
109 // Returns true if the entries of the matrix obey the row, column,
110 // and column size bounds and false otherwise.
111 bool AllTripletsWithinBounds() const;
112
113 bool IsValid() const { return AllTripletsWithinBounds(); }
114
115 // Build a sparse diagonal matrix of size num_rows x num_rows from
116 // the array values. Entries of the values array are copied into the
117 // sparse matrix.
118 static TripletSparseMatrix* CreateSparseDiagonalMatrix(const double* values,
119 int num_rows);
120
121 // Options struct to control the generation of random
122 // TripletSparseMatrix objects.
123 struct RandomMatrixOptions {
124 int num_rows;
125 int num_cols;
126 // 0 < density <= 1 is the probability of an entry being
127 // structurally non-zero. A given random matrix will not have
128 // precisely this density.
129 double density;
130 };
131
132 // Create a random CompressedRowSparseMatrix whose entries are
133 // normally distributed and whose structure is determined by
134 // RandomMatrixOptions.
135 //
136 // Caller owns the result.
137 static TripletSparseMatrix* CreateRandomMatrix(
138 const TripletSparseMatrix::RandomMatrixOptions& options);
139
140 private:
141 void AllocateMemory();
142 void CopyData(const TripletSparseMatrix& orig);
143
144 int num_rows_;
145 int num_cols_;
146 int max_num_nonzeros_;
147 int num_nonzeros_;
148
149 // The data is stored as three arrays. For each i, values_[i] is
150 // stored at the location (rows_[i], cols_[i]). If the there are
151 // multiple entries with the same (rows_[i], cols_[i]), the values_
152 // entries corresponding to them are summed up.
153 std::unique_ptr<int[]> rows_;
154 std::unique_ptr<int[]> cols_;
155 std::unique_ptr<double[]> values_;
156};
157
158} // namespace internal
159} // namespace ceres
160
161#endif // CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H__