blob: e0c917cc5b05204281729056562abe5859493886 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh3de38b02024-06-25 18:25:10 -070034#include <utility>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035
Austin Schuh70cc9552019-01-21 19:46:48 -080036#include "ceres/internal/eigen.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070037#include "ceres/internal/export.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038#include "ceres/triplet_sparse_matrix.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include "glog/logging.h"
40
Austin Schuh3de38b02024-06-25 18:25:10 -070041namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080042
43DenseSparseMatrix::DenseSparseMatrix(int num_rows, int num_cols)
Austin Schuh3de38b02024-06-25 18:25:10 -070044 : m_(Matrix(num_rows, num_cols)) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080045
46DenseSparseMatrix::DenseSparseMatrix(const TripletSparseMatrix& m)
Austin Schuh3de38b02024-06-25 18:25:10 -070047 : m_(Matrix::Zero(m.num_rows(), m.num_cols())) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048 const double* values = m.values();
49 const int* rows = m.rows();
50 const int* cols = m.cols();
Austin Schuh70cc9552019-01-21 19:46:48 -080051 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 Schuh3de38b02024-06-25 18:25:10 -070058DenseSparseMatrix::DenseSparseMatrix(Matrix m) : m_(std::move(m)) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080059
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080060void DenseSparseMatrix::SetZero() { m_.setZero(); }
Austin Schuh70cc9552019-01-21 19:46:48 -080061
Austin Schuh3de38b02024-06-25 18:25:10 -070062void DenseSparseMatrix::RightMultiplyAndAccumulate(const double* x,
63 double* y) const {
64 VectorRef(y, num_rows()).noalias() += m_ * ConstVectorRef(x, num_cols());
Austin Schuh70cc9552019-01-21 19:46:48 -080065}
66
Austin Schuh3de38b02024-06-25 18:25:10 -070067void DenseSparseMatrix::LeftMultiplyAndAccumulate(const double* x,
68 double* y) const {
69 VectorRef(y, num_cols()).noalias() +=
70 m_.transpose() * ConstVectorRef(x, num_rows());
Austin Schuh70cc9552019-01-21 19:46:48 -080071}
72
73void DenseSparseMatrix::SquaredColumnNorm(double* x) const {
Austin Schuh3de38b02024-06-25 18:25:10 -070074 // 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 Schuh70cc9552019-01-21 19:46:48 -080087}
88
89void DenseSparseMatrix::ScaleColumns(const double* scale) {
90 m_ *= ConstVectorRef(scale, num_cols()).asDiagonal();
91}
92
93void DenseSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
Austin Schuh3de38b02024-06-25 18:25:10 -070094 *dense_matrix = m_;
Austin Schuh70cc9552019-01-21 19:46:48 -080095}
96
Austin Schuh3de38b02024-06-25 18:25:10 -070097int DenseSparseMatrix::num_rows() const { return m_.rows(); }
Austin Schuh70cc9552019-01-21 19:46:48 -080098
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080099int DenseSparseMatrix::num_cols() const { return m_.cols(); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800100
Austin Schuh3de38b02024-06-25 18:25:10 -0700101int DenseSparseMatrix::num_nonzeros() const { return m_.rows() * m_.cols(); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800102
Austin Schuh3de38b02024-06-25 18:25:10 -0700103const Matrix& DenseSparseMatrix::matrix() const { return m_; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800104
Austin Schuh3de38b02024-06-25 18:25:10 -0700105Matrix* DenseSparseMatrix::mutable_matrix() { return &m_; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800106
Austin Schuh70cc9552019-01-21 19:46:48 -0800107void DenseSparseMatrix::ToTextFile(FILE* file) const {
108 CHECK(file != nullptr);
Austin Schuh3de38b02024-06-25 18:25:10 -0700109 for (int r = 0; r < m_.rows(); ++r) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800110 for (int c = 0; c < m_.cols(); ++c) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800111 fprintf(file, "% 10d % 10d %17f\n", r, c, m_(r, c));
Austin Schuh70cc9552019-01-21 19:46:48 -0800112 }
113 }
114}
115
Austin Schuh3de38b02024-06-25 18:25:10 -0700116} // namespace ceres::internal