blob: 53207fe300e9d76a9103e90a464651bef739bf4f [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: keir@google.com (Keir Mierle)
30
31#include "ceres/dense_sparse_matrix.h"
32
33#include <algorithm>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080034
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include "ceres/internal/eigen.h"
36#include "ceres/internal/port.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080037#include "ceres/triplet_sparse_matrix.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "glog/logging.h"
39
40namespace ceres {
41namespace internal {
42
43DenseSparseMatrix::DenseSparseMatrix(int num_rows, int num_cols)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080044 : has_diagonal_appended_(false), has_diagonal_reserved_(false) {
Austin Schuh70cc9552019-01-21 19:46:48 -080045 m_.resize(num_rows, num_cols);
46 m_.setZero();
47}
48
49DenseSparseMatrix::DenseSparseMatrix(int num_rows,
50 int num_cols,
51 bool reserve_diagonal)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080052 : has_diagonal_appended_(false), has_diagonal_reserved_(reserve_diagonal) {
Austin Schuh70cc9552019-01-21 19:46:48 -080053 if (reserve_diagonal) {
54 // Allocate enough space for the diagonal.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080055 m_.resize(num_rows + num_cols, num_cols);
Austin Schuh70cc9552019-01-21 19:46:48 -080056 } else {
57 m_.resize(num_rows, num_cols);
58 }
59 m_.setZero();
60}
61
62DenseSparseMatrix::DenseSparseMatrix(const TripletSparseMatrix& m)
63 : m_(Eigen::MatrixXd::Zero(m.num_rows(), m.num_cols())),
64 has_diagonal_appended_(false),
65 has_diagonal_reserved_(false) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080066 const double* values = m.values();
67 const int* rows = m.rows();
68 const int* cols = m.cols();
Austin Schuh70cc9552019-01-21 19:46:48 -080069 int num_nonzeros = m.num_nonzeros();
70
71 for (int i = 0; i < num_nonzeros; ++i) {
72 m_(rows[i], cols[i]) += values[i];
73 }
74}
75
76DenseSparseMatrix::DenseSparseMatrix(const ColMajorMatrix& m)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080077 : m_(m), has_diagonal_appended_(false), has_diagonal_reserved_(false) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080078
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080079void DenseSparseMatrix::SetZero() { m_.setZero(); }
Austin Schuh70cc9552019-01-21 19:46:48 -080080
81void DenseSparseMatrix::RightMultiply(const double* x, double* y) const {
82 VectorRef(y, num_rows()) += matrix() * ConstVectorRef(x, num_cols());
83}
84
85void DenseSparseMatrix::LeftMultiply(const double* x, double* y) const {
86 VectorRef(y, num_cols()) +=
87 matrix().transpose() * ConstVectorRef(x, num_rows());
88}
89
90void DenseSparseMatrix::SquaredColumnNorm(double* x) const {
91 VectorRef(x, num_cols()) = m_.colwise().squaredNorm();
92}
93
94void DenseSparseMatrix::ScaleColumns(const double* scale) {
95 m_ *= ConstVectorRef(scale, num_cols()).asDiagonal();
96}
97
98void DenseSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
99 *dense_matrix = m_.block(0, 0, num_rows(), num_cols());
100}
101
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800102void DenseSparseMatrix::AppendDiagonal(double* d) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800103 CHECK(!has_diagonal_appended_);
104 if (!has_diagonal_reserved_) {
105 ColMajorMatrix tmp = m_;
106 m_.resize(m_.rows() + m_.cols(), m_.cols());
107 m_.setZero();
108 m_.block(0, 0, tmp.rows(), tmp.cols()) = tmp;
109 has_diagonal_reserved_ = true;
110 }
111
112 m_.bottomLeftCorner(m_.cols(), m_.cols()) =
113 ConstVectorRef(d, m_.cols()).asDiagonal();
114 has_diagonal_appended_ = true;
115}
116
117void DenseSparseMatrix::RemoveDiagonal() {
118 CHECK(has_diagonal_appended_);
119 has_diagonal_appended_ = false;
120 // Leave the diagonal reserved.
121}
122
123int DenseSparseMatrix::num_rows() const {
124 if (has_diagonal_reserved_ && !has_diagonal_appended_) {
125 return m_.rows() - m_.cols();
126 }
127 return m_.rows();
128}
129
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800130int DenseSparseMatrix::num_cols() const { return m_.cols(); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800131
132int DenseSparseMatrix::num_nonzeros() const {
133 if (has_diagonal_reserved_ && !has_diagonal_appended_) {
134 return (m_.rows() - m_.cols()) * m_.cols();
135 }
136 return m_.rows() * m_.cols();
137}
138
139ConstColMajorMatrixRef DenseSparseMatrix::matrix() const {
140 return ConstColMajorMatrixRef(
141 m_.data(),
142 ((has_diagonal_reserved_ && !has_diagonal_appended_)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800143 ? m_.rows() - m_.cols()
144 : m_.rows()),
Austin Schuh70cc9552019-01-21 19:46:48 -0800145 m_.cols(),
146 Eigen::Stride<Eigen::Dynamic, 1>(m_.rows(), 1));
147}
148
149ColMajorMatrixRef DenseSparseMatrix::mutable_matrix() {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800150 return ColMajorMatrixRef(m_.data(),
151 ((has_diagonal_reserved_ && !has_diagonal_appended_)
152 ? m_.rows() - m_.cols()
153 : m_.rows()),
154 m_.cols(),
155 Eigen::Stride<Eigen::Dynamic, 1>(m_.rows(), 1));
Austin Schuh70cc9552019-01-21 19:46:48 -0800156}
157
Austin Schuh70cc9552019-01-21 19:46:48 -0800158void DenseSparseMatrix::ToTextFile(FILE* file) const {
159 CHECK(file != nullptr);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800160 const int active_rows = (has_diagonal_reserved_ && !has_diagonal_appended_)
161 ? (m_.rows() - m_.cols())
162 : m_.rows();
Austin Schuh70cc9552019-01-21 19:46:48 -0800163
164 for (int r = 0; r < active_rows; ++r) {
165 for (int c = 0; c < m_.cols(); ++c) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800166 fprintf(file, "% 10d % 10d %17f\n", r, c, m_(r, c));
Austin Schuh70cc9552019-01-21 19:46:48 -0800167 }
168 }
169}
170
171} // namespace internal
172} // namespace ceres