Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 34 | #include <utility> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include "ceres/internal/eigen.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 37 | #include "ceres/internal/export.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 38 | #include "ceres/triplet_sparse_matrix.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 39 | #include "glog/logging.h" |
| 40 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 41 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 42 | |
| 43 | DenseSparseMatrix::DenseSparseMatrix(int num_rows, int num_cols) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 44 | : m_(Matrix(num_rows, num_cols)) {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 45 | |
| 46 | DenseSparseMatrix::DenseSparseMatrix(const TripletSparseMatrix& m) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 47 | : m_(Matrix::Zero(m.num_rows(), m.num_cols())) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 48 | const double* values = m.values(); |
| 49 | const int* rows = m.rows(); |
| 50 | const int* cols = m.cols(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 51 | int num_nonzeros = m.num_nonzeros(); |
| 52 | |
| 53 | for (int i = 0; i < num_nonzeros; ++i) { |
| 54 | m_(rows[i], cols[i]) += values[i]; |
| 55 | } |
| 56 | } |
| 57 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 58 | DenseSparseMatrix::DenseSparseMatrix(Matrix m) : m_(std::move(m)) {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 59 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 60 | void DenseSparseMatrix::SetZero() { m_.setZero(); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 61 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 62 | void DenseSparseMatrix::RightMultiplyAndAccumulate(const double* x, |
| 63 | double* y) const { |
| 64 | VectorRef(y, num_rows()).noalias() += m_ * ConstVectorRef(x, num_cols()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 67 | void DenseSparseMatrix::LeftMultiplyAndAccumulate(const double* x, |
| 68 | double* y) const { |
| 69 | VectorRef(y, num_cols()).noalias() += |
| 70 | m_.transpose() * ConstVectorRef(x, num_rows()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void DenseSparseMatrix::SquaredColumnNorm(double* x) const { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 74 | // This implementation is 3x faster than the naive version |
| 75 | // x = m_.colwise().square().sum(), likely because m_ |
| 76 | // is a row major matrix. |
| 77 | |
| 78 | const int num_rows = m_.rows(); |
| 79 | const int num_cols = m_.cols(); |
| 80 | std::fill_n(x, num_cols, 0.0); |
| 81 | const double* m = m_.data(); |
| 82 | for (int i = 0; i < num_rows; ++i) { |
| 83 | for (int j = 0; j < num_cols; ++j, ++m) { |
| 84 | x[j] += (*m) * (*m); |
| 85 | } |
| 86 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void DenseSparseMatrix::ScaleColumns(const double* scale) { |
| 90 | m_ *= ConstVectorRef(scale, num_cols()).asDiagonal(); |
| 91 | } |
| 92 | |
| 93 | void DenseSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 94 | *dense_matrix = m_; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 97 | int DenseSparseMatrix::num_rows() const { return m_.rows(); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 98 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 99 | int DenseSparseMatrix::num_cols() const { return m_.cols(); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 100 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 101 | int DenseSparseMatrix::num_nonzeros() const { return m_.rows() * m_.cols(); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 102 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 103 | const Matrix& DenseSparseMatrix::matrix() const { return m_; } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 104 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 105 | Matrix* DenseSparseMatrix::mutable_matrix() { return &m_; } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 106 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 107 | void DenseSparseMatrix::ToTextFile(FILE* file) const { |
| 108 | CHECK(file != nullptr); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 109 | for (int r = 0; r < m_.rows(); ++r) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 110 | for (int c = 0; c < m_.cols(); ++c) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 111 | fprintf(file, "% 10d % 10d %17f\n", r, c, m_(r, c)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 116 | } // namespace ceres::internal |