blob: 5d392ba6c3bf14fc2a407ffe183b6f2173e09ee4 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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>
34#include "ceres/matrix_proto.h"
35#include "ceres/triplet_sparse_matrix.h"
36#include "ceres/internal/eigen.h"
37#include "ceres/internal/port.h"
38
39namespace ceres {
40namespace internal {
41
42DenseSparseMatrix::DenseSparseMatrix(int num_rows, int num_cols)
43 : has_diagonal_appended_(false),
44 has_diagonal_reserved_(false) {
45 // Allocate enough space for the diagonal.
46 m_.resize(num_rows, num_cols);
47 m_.setZero();
48}
49
50DenseSparseMatrix::DenseSparseMatrix(const TripletSparseMatrix& m)
51 : m_(Eigen::MatrixXd::Zero(m.num_rows(), m.num_cols())),
52 has_diagonal_appended_(false),
53 has_diagonal_reserved_(false) {
54 const double *values = m.values();
55 const int *rows = m.rows();
56 const int *cols = m.cols();
57 int num_nonzeros = m.num_nonzeros();
58
59 for (int i = 0; i < num_nonzeros; ++i) {
60 m_(rows[i], cols[i]) += values[i];
61 }
62}
63
64DenseSparseMatrix::DenseSparseMatrix(const Matrix& m)
65 : m_(m),
66 has_diagonal_appended_(false),
67 has_diagonal_reserved_(false) {
68}
69
70#ifndef CERES_DONT_HAVE_PROTOCOL_BUFFERS
71DenseSparseMatrix::DenseSparseMatrix(const SparseMatrixProto& outer_proto)
72 : m_(Eigen::MatrixXd::Zero(
73 outer_proto.dense_matrix().num_rows(),
74 outer_proto.dense_matrix().num_cols())),
75 has_diagonal_appended_(false),
76 has_diagonal_reserved_(false) {
77 const DenseSparseMatrixProto& proto = outer_proto.dense_matrix();
78 for (int i = 0; i < m_.rows(); ++i) {
79 for (int j = 0; j < m_.cols(); ++j) {
80 m_(i, j) = proto.values(m_.cols() * i + j);
81 }
82 }
83}
84#endif
85
86void DenseSparseMatrix::SetZero() {
87 m_.setZero();
88}
89
90void DenseSparseMatrix::RightMultiply(const double* x, double* y) const {
91 VectorRef(y, num_rows()) += matrix() * ConstVectorRef(x, num_cols());
92}
93
94void DenseSparseMatrix::LeftMultiply(const double* x, double* y) const {
95 VectorRef(y, num_cols()) +=
96 matrix().transpose() * ConstVectorRef(x, num_rows());
97}
98
99void DenseSparseMatrix::SquaredColumnNorm(double* x) const {
100 VectorRef(x, num_cols()) = m_.colwise().squaredNorm();
101}
102
103void DenseSparseMatrix::ScaleColumns(const double* scale) {
104 m_ *= ConstVectorRef(scale, num_cols()).asDiagonal();
105}
106
107void DenseSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
108 *dense_matrix = m_;
109}
110
111#ifndef CERES_DONT_HAVE_PROTOCOL_BUFFERS
112void DenseSparseMatrix::ToProto(SparseMatrixProto* outer_proto) const {
113 CHECK(!has_diagonal_appended_) << "Not supported.";
114 outer_proto->Clear();
115 DenseSparseMatrixProto* proto = outer_proto->mutable_dense_matrix();
116
117 proto->set_num_rows(num_rows());
118 proto->set_num_cols(num_cols());
119
120 int num_nnz = num_nonzeros();
121 for (int i = 0; i < num_nnz; ++i) {
122 proto->add_values(m_.data()[i]);
123 }
124}
125#endif
126
127void DenseSparseMatrix::AppendDiagonal(double *d) {
128 CHECK(!has_diagonal_appended_);
129 if (!has_diagonal_reserved_) {
130 Matrix tmp = m_;
131 m_.resize(m_.rows() + m_.cols(), m_.cols());
132 m_.setZero();
133 m_.block(0, 0, tmp.rows(), tmp.cols()) = tmp;
134 has_diagonal_reserved_ = true;
135 }
136
137 m_.bottomLeftCorner(m_.cols(), m_.cols()) =
138 ConstVectorRef(d, m_.cols()).asDiagonal();
139 has_diagonal_appended_ = true;
140}
141
142void DenseSparseMatrix::RemoveDiagonal() {
143 CHECK(has_diagonal_appended_);
144 has_diagonal_appended_ = false;
145 // Leave the diagonal reserved.
146}
147
148int DenseSparseMatrix::num_rows() const {
149 if (has_diagonal_reserved_ && !has_diagonal_appended_) {
150 return m_.rows() - m_.cols();
151 }
152 return m_.rows();
153}
154
155int DenseSparseMatrix::num_cols() const {
156 return m_.cols();
157}
158
159int DenseSparseMatrix::num_nonzeros() const {
160 if (has_diagonal_reserved_ && !has_diagonal_appended_) {
161 return (m_.rows() - m_.cols()) * m_.cols();
162 }
163 return m_.rows() * m_.cols();
164}
165
166ConstAlignedMatrixRef DenseSparseMatrix::matrix() const {
167 if (has_diagonal_reserved_ && !has_diagonal_appended_) {
168 return ConstAlignedMatrixRef(
169 m_.data(), m_.rows() - m_.cols(), m_.cols());
170 }
171 return ConstAlignedMatrixRef(m_.data(), m_.rows(), m_.cols());
172}
173
174AlignedMatrixRef DenseSparseMatrix::mutable_matrix() {
175 if (has_diagonal_reserved_ && !has_diagonal_appended_) {
176 return AlignedMatrixRef(
177 m_.data(), m_.rows() - m_.cols(), m_.cols());
178 }
179 return AlignedMatrixRef(m_.data(), m_.rows(), m_.cols());
180}
181
Sameer Agarwal82f4b882012-05-06 21:05:28 -0700182void DenseSparseMatrix::ToTextFile(FILE* file) const {
183 CHECK_NOTNULL(file);
184 const int active_rows =
185 (has_diagonal_reserved_ && !has_diagonal_appended_)
186 ? (m_.rows() - m_.cols())
187 : m_.rows();
188
189 for (int r = 0; r < active_rows; ++r) {
190 for (int c = 0; c < m_.cols(); ++c) {
191 fprintf(file, "% 10d % 10d %17f\n", r, c, m_(r, c));
192 }
193 }
194}
Keir Mierle8ebb0732012-04-30 23:09:08 -0700195
196} // namespace internal
197} // namespace ceres