sparse_matrix.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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: sameeragarwal@google.com (Sameer Agarwal)
  30. //
  31. // Interface definition for sparse matrices.
  32. #ifndef CERES_INTERNAL_SPARSE_MATRIX_H_
  33. #define CERES_INTERNAL_SPARSE_MATRIX_H_
  34. #include <cstdio>
  35. #include "ceres/internal/eigen.h"
  36. #include "ceres/internal/export.h"
  37. #include "ceres/linear_operator.h"
  38. #include "ceres/types.h"
  39. namespace ceres::internal {
  40. class ContextImpl;
  41. // This class defines the interface for storing and manipulating
  42. // sparse matrices. The key property that differentiates different
  43. // sparse matrices is how they are organized in memory and how the
  44. // information about the sparsity structure of the matrix is
  45. // stored. This has significant implications for linear solvers
  46. // operating on these matrices.
  47. //
  48. // To deal with the different kinds of layouts, we will assume that a
  49. // sparse matrix will have a two part representation. A values array
  50. // that will be used to store the entries of the sparse matrix and
  51. // some sort of a layout object that tells the user the sparsity
  52. // structure and layout of the values array. For example in case of
  53. // the TripletSparseMatrix, this information is carried in the rows
  54. // and cols arrays and for the BlockSparseMatrix, this information is
  55. // carried in the CompressedRowBlockStructure object.
  56. //
  57. // This interface deliberately does not contain any information about
  58. // the structure of the sparse matrix as that seems to be highly
  59. // matrix type dependent and we are at this stage unable to come up
  60. // with an efficient high level interface that spans multiple sparse
  61. // matrix types.
  62. class CERES_NO_EXPORT SparseMatrix : public LinearOperator {
  63. public:
  64. ~SparseMatrix() override;
  65. // y += Ax;
  66. using LinearOperator::RightMultiplyAndAccumulate;
  67. void RightMultiplyAndAccumulate(const double* x,
  68. double* y) const override = 0;
  69. // y += A'x;
  70. void LeftMultiplyAndAccumulate(const double* x, double* y) const override = 0;
  71. // In MATLAB notation sum(A.*A, 1)
  72. virtual void SquaredColumnNorm(double* x) const = 0;
  73. virtual void SquaredColumnNorm(double* x,
  74. ContextImpl* context,
  75. int num_threads) const;
  76. // A = A * diag(scale)
  77. virtual void ScaleColumns(const double* scale) = 0;
  78. virtual void ScaleColumns(const double* scale,
  79. ContextImpl* context,
  80. int num_threads);
  81. // A = 0. A->num_nonzeros() == 0 is true after this call. The
  82. // sparsity pattern is preserved.
  83. virtual void SetZero() = 0;
  84. virtual void SetZero(ContextImpl* /*context*/, int /*num_threads*/) {
  85. SetZero();
  86. }
  87. // Resize and populate dense_matrix with a dense version of the
  88. // sparse matrix.
  89. virtual void ToDenseMatrix(Matrix* dense_matrix) const = 0;
  90. // Write out the matrix as a sequence of (i,j,s) triplets. This
  91. // format is useful for loading the matrix into MATLAB/octave as a
  92. // sparse matrix.
  93. virtual void ToTextFile(FILE* file) const = 0;
  94. // Accessors for the values array that stores the entries of the
  95. // sparse matrix. The exact interpretation of the values of this
  96. // array depends on the particular kind of SparseMatrix being
  97. // accessed.
  98. virtual double* mutable_values() = 0;
  99. virtual const double* values() const = 0;
  100. int num_rows() const override = 0;
  101. int num_cols() const override = 0;
  102. virtual int num_nonzeros() const = 0;
  103. };
  104. } // namespace ceres::internal
  105. #endif // CERES_INTERNAL_SPARSE_MATRIX_H_