Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 34 | #include "ceres/triplet_sparse_matrix.h" |
| 35 | #include "ceres/internal/eigen.h" |
| 36 | #include "ceres/internal/port.h" |
Sameer Agarwal | a1eaa26 | 2013-05-09 10:02:24 -0700 | [diff] [blame] | 37 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 38 | |
| 39 | namespace ceres { |
| 40 | namespace internal { |
| 41 | |
| 42 | DenseSparseMatrix::DenseSparseMatrix(int num_rows, int num_cols) |
| 43 | : has_diagonal_appended_(false), |
| 44 | has_diagonal_reserved_(false) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 45 | m_.resize(num_rows, num_cols); |
| 46 | m_.setZero(); |
| 47 | } |
| 48 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 49 | DenseSparseMatrix::DenseSparseMatrix(int num_rows, |
| 50 | int num_cols, |
| 51 | bool reserve_diagonal) |
Sameer Agarwal | 5bfa7e4 | 2012-10-05 10:06:27 -0700 | [diff] [blame] | 52 | : has_diagonal_appended_(false), |
| 53 | has_diagonal_reserved_(reserve_diagonal) { |
Sameer Agarwal | 5bfa7e4 | 2012-10-05 10:06:27 -0700 | [diff] [blame] | 54 | if (reserve_diagonal) { |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 55 | // Allocate enough space for the diagonal. |
Sameer Agarwal | 5bfa7e4 | 2012-10-05 10:06:27 -0700 | [diff] [blame] | 56 | m_.resize(num_rows + num_cols, num_cols); |
| 57 | } else { |
| 58 | m_.resize(num_rows, num_cols); |
| 59 | } |
| 60 | m_.setZero(); |
| 61 | } |
| 62 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 63 | DenseSparseMatrix::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 | |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 77 | DenseSparseMatrix::DenseSparseMatrix(const ColMajorMatrix& m) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 78 | : m_(m), |
| 79 | has_diagonal_appended_(false), |
| 80 | has_diagonal_reserved_(false) { |
| 81 | } |
| 82 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 83 | void DenseSparseMatrix::SetZero() { |
| 84 | m_.setZero(); |
| 85 | } |
| 86 | |
| 87 | void DenseSparseMatrix::RightMultiply(const double* x, double* y) const { |
| 88 | VectorRef(y, num_rows()) += matrix() * ConstVectorRef(x, num_cols()); |
| 89 | } |
| 90 | |
| 91 | void DenseSparseMatrix::LeftMultiply(const double* x, double* y) const { |
| 92 | VectorRef(y, num_cols()) += |
| 93 | matrix().transpose() * ConstVectorRef(x, num_rows()); |
| 94 | } |
| 95 | |
| 96 | void DenseSparseMatrix::SquaredColumnNorm(double* x) const { |
| 97 | VectorRef(x, num_cols()) = m_.colwise().squaredNorm(); |
| 98 | } |
| 99 | |
| 100 | void DenseSparseMatrix::ScaleColumns(const double* scale) { |
| 101 | m_ *= ConstVectorRef(scale, num_cols()).asDiagonal(); |
| 102 | } |
| 103 | |
| 104 | void DenseSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const { |
Keir Mierle | 00c8a06 | 2012-12-01 13:22:59 -0800 | [diff] [blame] | 105 | *dense_matrix = m_.block(0, 0, num_rows(), num_cols()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 108 | void DenseSparseMatrix::AppendDiagonal(double *d) { |
| 109 | CHECK(!has_diagonal_appended_); |
| 110 | if (!has_diagonal_reserved_) { |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 111 | ColMajorMatrix tmp = m_; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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 | |
| 123 | void DenseSparseMatrix::RemoveDiagonal() { |
| 124 | CHECK(has_diagonal_appended_); |
| 125 | has_diagonal_appended_ = false; |
| 126 | // Leave the diagonal reserved. |
| 127 | } |
| 128 | |
| 129 | int 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 | |
| 136 | int DenseSparseMatrix::num_cols() const { |
| 137 | return m_.cols(); |
| 138 | } |
| 139 | |
| 140 | int 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 | |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 147 | ConstColMajorMatrixRef 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)); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 157 | ColMajorMatrixRef 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)); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 167 | |
Sameer Agarwal | 82f4b88 | 2012-05-06 21:05:28 -0700 | [diff] [blame] | 168 | void DenseSparseMatrix::ToTextFile(FILE* file) const { |
| 169 | CHECK_NOTNULL(file); |
| 170 | const int active_rows = |
| 171 | (has_diagonal_reserved_ && !has_diagonal_appended_) |
Sameer Agarwal | dd2b17d | 2012-08-16 19:34:57 -0700 | [diff] [blame] | 172 | ? (m_.rows() - m_.cols()) |
Sameer Agarwal | 82f4b88 | 2012-05-06 21:05:28 -0700 | [diff] [blame] | 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 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 181 | |
| 182 | } // namespace internal |
| 183 | } // namespace ceres |