blob: 9c79417a7f442254b5b23e12740992feedcedb21 [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: sameeragarwal@google.com (Sameer Agarwal)
30//
31// Interface definition for sparse matrices.
32
33#ifndef CERES_INTERNAL_SPARSE_MATRIX_H_
34#define CERES_INTERNAL_SPARSE_MATRIX_H_
35
36#include <cstdio>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080037
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "ceres/internal/eigen.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070039#include "ceres/internal/export.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080040#include "ceres/linear_operator.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080041#include "ceres/types.h"
42
Austin Schuh3de38b02024-06-25 18:25:10 -070043namespace ceres::internal {
44class ContextImpl;
Austin Schuh70cc9552019-01-21 19:46:48 -080045
46// This class defines the interface for storing and manipulating
47// sparse matrices. The key property that differentiates different
48// sparse matrices is how they are organized in memory and how the
49// information about the sparsity structure of the matrix is
50// stored. This has significant implications for linear solvers
51// operating on these matrices.
52//
53// To deal with the different kinds of layouts, we will assume that a
54// sparse matrix will have a two part representation. A values array
55// that will be used to store the entries of the sparse matrix and
56// some sort of a layout object that tells the user the sparsity
57// structure and layout of the values array. For example in case of
58// the TripletSparseMatrix, this information is carried in the rows
59// and cols arrays and for the BlockSparseMatrix, this information is
60// carried in the CompressedRowBlockStructure object.
61//
62// This interface deliberately does not contain any information about
63// the structure of the sparse matrix as that seems to be highly
64// matrix type dependent and we are at this stage unable to come up
65// with an efficient high level interface that spans multiple sparse
66// matrix types.
Austin Schuh3de38b02024-06-25 18:25:10 -070067class CERES_NO_EXPORT SparseMatrix : public LinearOperator {
Austin Schuh70cc9552019-01-21 19:46:48 -080068 public:
Austin Schuh3de38b02024-06-25 18:25:10 -070069 ~SparseMatrix() override;
Austin Schuh70cc9552019-01-21 19:46:48 -080070
71 // y += Ax;
Austin Schuh3de38b02024-06-25 18:25:10 -070072 using LinearOperator::RightMultiplyAndAccumulate;
73 void RightMultiplyAndAccumulate(const double* x,
74 double* y) const override = 0;
75
Austin Schuh70cc9552019-01-21 19:46:48 -080076 // y += A'x;
Austin Schuh3de38b02024-06-25 18:25:10 -070077 void LeftMultiplyAndAccumulate(const double* x, double* y) const override = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -080078
79 // In MATLAB notation sum(A.*A, 1)
80 virtual void SquaredColumnNorm(double* x) const = 0;
Austin Schuh3de38b02024-06-25 18:25:10 -070081 virtual void SquaredColumnNorm(double* x,
82 ContextImpl* context,
83 int num_threads) const;
Austin Schuh70cc9552019-01-21 19:46:48 -080084 // A = A * diag(scale)
85 virtual void ScaleColumns(const double* scale) = 0;
Austin Schuh3de38b02024-06-25 18:25:10 -070086 virtual void ScaleColumns(const double* scale,
87 ContextImpl* context,
88 int num_threads);
Austin Schuh70cc9552019-01-21 19:46:48 -080089
90 // A = 0. A->num_nonzeros() == 0 is true after this call. The
91 // sparsity pattern is preserved.
92 virtual void SetZero() = 0;
Austin Schuh3de38b02024-06-25 18:25:10 -070093 virtual void SetZero(ContextImpl* /*context*/, int /*num_threads*/) {
94 SetZero();
95 }
Austin Schuh70cc9552019-01-21 19:46:48 -080096
97 // Resize and populate dense_matrix with a dense version of the
98 // sparse matrix.
99 virtual void ToDenseMatrix(Matrix* dense_matrix) const = 0;
100
101 // Write out the matrix as a sequence of (i,j,s) triplets. This
102 // format is useful for loading the matrix into MATLAB/octave as a
103 // sparse matrix.
104 virtual void ToTextFile(FILE* file) const = 0;
105
106 // Accessors for the values array that stores the entries of the
107 // sparse matrix. The exact interpretation of the values of this
108 // array depends on the particular kind of SparseMatrix being
109 // accessed.
110 virtual double* mutable_values() = 0;
111 virtual const double* values() const = 0;
112
Austin Schuh3de38b02024-06-25 18:25:10 -0700113 int num_rows() const override = 0;
114 int num_cols() const override = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800115 virtual int num_nonzeros() const = 0;
116};
117
Austin Schuh3de38b02024-06-25 18:25:10 -0700118} // namespace ceres::internal
Austin Schuh70cc9552019-01-21 19:46:48 -0800119
120#endif // CERES_INTERNAL_SPARSE_MATRIX_H_