dense_sparse_matrix.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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. #include "ceres/dense_sparse_matrix.h"
  31. #include <algorithm>
  32. #include <utility>
  33. #include "ceres/internal/eigen.h"
  34. #include "ceres/internal/export.h"
  35. #include "ceres/triplet_sparse_matrix.h"
  36. #include "glog/logging.h"
  37. namespace ceres::internal {
  38. DenseSparseMatrix::DenseSparseMatrix(int num_rows, int num_cols)
  39. : m_(Matrix(num_rows, num_cols)) {}
  40. DenseSparseMatrix::DenseSparseMatrix(const TripletSparseMatrix& m)
  41. : m_(Matrix::Zero(m.num_rows(), m.num_cols())) {
  42. const double* values = m.values();
  43. const int* rows = m.rows();
  44. const int* cols = m.cols();
  45. int num_nonzeros = m.num_nonzeros();
  46. for (int i = 0; i < num_nonzeros; ++i) {
  47. m_(rows[i], cols[i]) += values[i];
  48. }
  49. }
  50. DenseSparseMatrix::DenseSparseMatrix(Matrix m) : m_(std::move(m)) {}
  51. void DenseSparseMatrix::SetZero() { m_.setZero(); }
  52. void DenseSparseMatrix::RightMultiplyAndAccumulate(const double* x,
  53. double* y) const {
  54. VectorRef(y, num_rows()).noalias() += m_ * ConstVectorRef(x, num_cols());
  55. }
  56. void DenseSparseMatrix::LeftMultiplyAndAccumulate(const double* x,
  57. double* y) const {
  58. VectorRef(y, num_cols()).noalias() +=
  59. m_.transpose() * ConstVectorRef(x, num_rows());
  60. }
  61. void DenseSparseMatrix::SquaredColumnNorm(double* x) const {
  62. // This implementation is 3x faster than the naive version
  63. // x = m_.colwise().square().sum(), likely because m_
  64. // is a row major matrix.
  65. const int num_rows = m_.rows();
  66. const int num_cols = m_.cols();
  67. std::fill_n(x, num_cols, 0.0);
  68. const double* m = m_.data();
  69. for (int i = 0; i < num_rows; ++i) {
  70. for (int j = 0; j < num_cols; ++j, ++m) {
  71. x[j] += (*m) * (*m);
  72. }
  73. }
  74. }
  75. void DenseSparseMatrix::ScaleColumns(const double* scale) {
  76. m_ *= ConstVectorRef(scale, num_cols()).asDiagonal();
  77. }
  78. void DenseSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
  79. *dense_matrix = m_;
  80. }
  81. int DenseSparseMatrix::num_rows() const { return m_.rows(); }
  82. int DenseSparseMatrix::num_cols() const { return m_.cols(); }
  83. int DenseSparseMatrix::num_nonzeros() const { return m_.rows() * m_.cols(); }
  84. const Matrix& DenseSparseMatrix::matrix() const { return m_; }
  85. Matrix* DenseSparseMatrix::mutable_matrix() { return &m_; }
  86. void DenseSparseMatrix::ToTextFile(FILE* file) const {
  87. CHECK(file != nullptr);
  88. for (int r = 0; r < m_.rows(); ++r) {
  89. for (int c = 0; c < m_.cols(); ++c) {
  90. fprintf(file, "% 10d % 10d %17f\n", r, c, m_(r, c));
  91. }
  92. }
  93. }
  94. } // namespace ceres::internal