blob: 60043018b15dd40087bebfe36d0d1686a13df2b5 [file] [log] [blame]
Petter Strandmark1e3cbd92012-08-29 09:39:56 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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: strandmark@google.com (Petter Strandmark)
30
31#ifndef CERES_INTERNAL_CXSPARSE_H_
32#define CERES_INTERNAL_CXSPARSE_H_
33
34#ifndef CERES_NO_CXSPARSE
35
Sameer Agarwal344c09f2013-04-20 16:07:56 -070036#include <vector>
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070037#include "cs.h"
Sameer Agarwal344c09f2013-04-20 16:07:56 -070038#include "ceres/internal/port.h"
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070039
40namespace ceres {
41namespace internal {
42
43class CompressedRowSparseMatrix;
44class TripletSparseMatrix;
45
46// This object provides access to solving linear systems using Cholesky
47// factorization with a known symbolic factorization. This features does not
48// explicity exist in CXSparse. The methods in the class are nonstatic because
49// the class manages internal scratch space.
50class CXSparse {
51 public:
52 CXSparse();
53 ~CXSparse();
54
55 // Solves a symmetric linear system A * x = b using Cholesky factorization.
Sameer Agarwal98bf14d2012-08-30 10:26:44 -070056 // A - The system matrix.
57 // symbolic_factorization - The symbolic factorization of A. This is obtained
58 // from AnalyzeCholesky.
59 // b - The right hand size of the linear equation. This
60 // array will also recieve the solution.
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070061 // Returns false if Cholesky factorization of A fails.
Sameer Agarwal98bf14d2012-08-30 10:26:44 -070062 bool SolveCholesky(cs_di* A, cs_dis* symbolic_factorization, double* b);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070063
64 // Creates a sparse matrix from a compressed-column form. No memory is
65 // allocated or copied; the structure A is filled out with info from the
66 // argument.
67 cs_di CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A);
68
69 // Creates a new matrix from a triplet form. Deallocate the returned matrix
70 // with Free. May return NULL if the compression or allocation fails.
71 cs_di* CreateSparseMatrix(TripletSparseMatrix* A);
72
Sameer Agarwald5b93bf2013-04-26 21:17:49 -070073 // B = A'
74 //
75 // The returned matrix should be deallocated with Free when not used
76 // anymore.
77 cs_di* TransposeMatrix(cs_di* A);
78
79 // C = A * B
80 //
81 // The returned matrix should be deallocated with Free when not used
82 // anymore.
83 cs_di* MatrixMatrixMultiply(cs_di* A, cs_di* B);
84
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070085 // Computes a symbolic factorization of A that can be used in SolveCholesky.
Sameer Agarwald5b93bf2013-04-26 21:17:49 -070086 //
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070087 // The returned matrix should be deallocated with Free when not used anymore.
88 cs_dis* AnalyzeCholesky(cs_di* A);
89
Sameer Agarwald5b93bf2013-04-26 21:17:49 -070090 // Computes a symbolic factorization of A that can be used in
91 // SolveCholesky, but does not compute a fill-reducing ordering.
92 //
93 // The returned matrix should be deallocated with Free when not used anymore.
94 cs_dis* AnalyzeCholeskyWithNaturalOrdering(cs_di* A);
95
96 // Computes a symbolic factorization of A that can be used in
97 // SolveCholesky. The difference from AnalyzeCholesky is that this
98 // function first detects the block sparsity of the matrix using
99 // information about the row and column blocks and uses this block
100 // sparse matrix to find a fill-reducing ordering. This ordering is
101 // then used to find a symbolic factorization. This can result in a
102 // significant performance improvement AnalyzeCholesky on block
103 // sparse matrices.
104 //
105 // The returned matrix should be deallocated with Free when not used
106 // anymore.
Sameer Agarwal344c09f2013-04-20 16:07:56 -0700107 cs_dis* BlockAnalyzeCholesky(cs_di* A,
108 const vector<int>& row_blocks,
109 const vector<int>& col_blocks);
110
Sameer Agarwald5b93bf2013-04-26 21:17:49 -0700111 // Compute an fill-reducing approximate minimum degree ordering of
112 // the matrix A. ordering should be non-NULL and should point to
113 // enough memory to hold the ordering for the rows of A.
114 void ApproximateMinimumDegreeOrdering(cs_di* A, int* ordering);
115
Sameer Agarwal05937472013-02-11 13:57:12 -0800116 void Free(cs_di* sparse_matrix);
117 void Free(cs_dis* symbolic_factorization);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700118
119 private:
120 // Cached scratch space
121 CS_ENTRY* scratch_;
122 int scratch_size_;
123};
124
125} // namespace internal
126} // namespace ceres
127
128#endif // CERES_NO_CXSPARSE
129
130#endif // CERES_INTERNAL_CXSPARSE_H_