1
// Ceres Solver - A fast non-linear least squares minimizer
2
// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3
// http://code.google.com/p/ceres-solver/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are met:
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.
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.
29
// Author: sameeragarwal@google.com (Sameer Agarwal)
31
// Interface definition for sparse matrices.
33
#ifndef CERES_INTERNAL_SPARSE_MATRIX_H_
34
#define CERES_INTERNAL_SPARSE_MATRIX_H_
37
#include "ceres/linear_operator.h"
38
#include "ceres/internal/eigen.h"
39
#include "ceres/types.h"
44
class SparseMatrixProto;
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.
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.
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
67
class SparseMatrix : public LinearOperator {
69
virtual ~SparseMatrix();
72
virtual void RightMultiply(const double* x, double* y) const = 0;
74
virtual void LeftMultiply(const double* x, double* y) const = 0;
76
// In MATLAB notation sum(A.*A, 1)
77
virtual void SquaredColumnNorm(double* x) const = 0;
78
// A = A * diag(scale)
79
virtual void ScaleColumns(const double* scale) = 0;
81
// A = 0. A->num_nonzeros() == 0 is true after this call. The
82
// sparsity pattern is preserved.
83
virtual void SetZero() = 0;
85
// Resize and populate dense_matrix with a dense version of the
87
virtual void ToDenseMatrix(Matrix* dense_matrix) const = 0;
89
#ifndef CERES_DONT_HAVE_PROTOCOL_BUFFERS
90
// Dump the sparse matrix to a proto. Destroys the contents of proto.
91
virtual void ToProto(SparseMatrixProto* proto) const = 0;
94
// Write out the matrix as a sequence of (i,j,s) triplets. This
95
// format is useful for loading the matrix into MATLAB/octave as a
97
virtual void ToTextFile(FILE* file) const = 0;
99
// Accessors for the values array that stores the entries of the
100
// sparse matrix. The exact interpreptation of the values of this
101
// array depends on the particular kind of SparseMatrix being
103
virtual double* mutable_values() = 0;
104
virtual const double* values() const = 0;
106
virtual int num_rows() const = 0;
107
virtual int num_cols() const = 0;
108
virtual int num_nonzeros() const = 0;
111
} // namespace internal
114
#endif // CERES_INTERNAL_SPARSE_MATRIX_H_