1// 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/triplet_sparse_matrix.h"
35#include "ceres/internal/eigen.h"
36#include "ceres/internal/port.h"
37#include "glog/logging.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  m_.resize(num_rows, num_cols);
46  m_.setZero();
47}
48
49DenseSparseMatrix::DenseSparseMatrix(int num_rows,
50                                     int num_cols,
51                                     bool reserve_diagonal)
52    : has_diagonal_appended_(false),
53      has_diagonal_reserved_(reserve_diagonal) {
54  if (reserve_diagonal) {
55    // Allocate enough space for the diagonal.
56    m_.resize(num_rows +  num_cols, num_cols);
57  } else {
58    m_.resize(num_rows, num_cols);
59  }
60  m_.setZero();
61}
62
63DenseSparseMatrix::DenseSparseMatrix(const TripletSparseMatrix& m)
64    : m_(Eigen::MatrixXd::Zero(m.num_rows(), m.num_cols())),
65      has_diagonal_appended_(false),
66      has_diagonal_reserved_(false) {
67  const double *values = m.values();
68  const int *rows = m.rows();
69  const int *cols = m.cols();
70  int num_nonzeros = m.num_nonzeros();
71
72  for (int i = 0; i < num_nonzeros; ++i) {
73    m_(rows[i], cols[i]) += values[i];
74  }
75}
76
77DenseSparseMatrix::DenseSparseMatrix(const ColMajorMatrix& m)
78    : m_(m),
79      has_diagonal_appended_(false),
80      has_diagonal_reserved_(false) {
81}
82
83void DenseSparseMatrix::SetZero() {
84  m_.setZero();
85}
86
87void DenseSparseMatrix::RightMultiply(const double* x, double* y) const {
88  VectorRef(y, num_rows()) += matrix() * ConstVectorRef(x, num_cols());
89}
90
91void DenseSparseMatrix::LeftMultiply(const double* x, double* y) const {
92  VectorRef(y, num_cols()) +=
93      matrix().transpose() * ConstVectorRef(x, num_rows());
94}
95
96void DenseSparseMatrix::SquaredColumnNorm(double* x) const {
97  VectorRef(x, num_cols()) = m_.colwise().squaredNorm();
98}
99
100void DenseSparseMatrix::ScaleColumns(const double* scale) {
101  m_ *= ConstVectorRef(scale, num_cols()).asDiagonal();
102}
103
104void DenseSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
105  *dense_matrix = m_.block(0, 0, num_rows(), num_cols());
106}
107
108void DenseSparseMatrix::AppendDiagonal(double *d) {
109  CHECK(!has_diagonal_appended_);
110  if (!has_diagonal_reserved_) {
111    ColMajorMatrix tmp = m_;
112    m_.resize(m_.rows() + m_.cols(), m_.cols());
113    m_.setZero();
114    m_.block(0, 0, tmp.rows(), tmp.cols()) = tmp;
115    has_diagonal_reserved_ = true;
116  }
117
118  m_.bottomLeftCorner(m_.cols(), m_.cols()) =
119      ConstVectorRef(d, m_.cols()).asDiagonal();
120  has_diagonal_appended_ = true;
121}
122
123void DenseSparseMatrix::RemoveDiagonal() {
124  CHECK(has_diagonal_appended_);
125  has_diagonal_appended_ = false;
126  // Leave the diagonal reserved.
127}
128
129int DenseSparseMatrix::num_rows() const {
130  if (has_diagonal_reserved_ && !has_diagonal_appended_) {
131    return m_.rows() - m_.cols();
132  }
133  return m_.rows();
134}
135
136int DenseSparseMatrix::num_cols() const {
137  return m_.cols();
138}
139
140int DenseSparseMatrix::num_nonzeros() const {
141  if (has_diagonal_reserved_ && !has_diagonal_appended_) {
142    return (m_.rows() - m_.cols()) * m_.cols();
143  }
144  return m_.rows() * m_.cols();
145}
146
147ConstColMajorMatrixRef DenseSparseMatrix::matrix() const {
148  return ConstColMajorMatrixRef(
149      m_.data(),
150      ((has_diagonal_reserved_ && !has_diagonal_appended_)
151       ? m_.rows() - m_.cols()
152       : m_.rows()),
153      m_.cols(),
154      Eigen::Stride<Eigen::Dynamic, 1>(m_.rows(), 1));
155}
156
157ColMajorMatrixRef DenseSparseMatrix::mutable_matrix() {
158  return ColMajorMatrixRef(
159      m_.data(),
160      ((has_diagonal_reserved_ && !has_diagonal_appended_)
161       ? m_.rows() - m_.cols()
162       : m_.rows()),
163      m_.cols(),
164      Eigen::Stride<Eigen::Dynamic, 1>(m_.rows(), 1));
165}
166
167
168void DenseSparseMatrix::ToTextFile(FILE* file) const {
169  CHECK_NOTNULL(file);
170  const int active_rows =
171      (has_diagonal_reserved_ && !has_diagonal_appended_)
172      ? (m_.rows() - m_.cols())
173      : m_.rows();
174
175  for (int r = 0; r < active_rows; ++r) {
176    for (int c = 0; c < m_.cols(); ++c) {
177      fprintf(file,  "% 10d % 10d %17f\n", r, c, m_(r, c));
178    }
179  }
180}
181
182}  // namespace internal
183}  // namespace ceres
184