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
// A simple C++ interface to the SuiteSparse and CHOLMOD libraries.
33
#ifndef CERES_INTERNAL_SUITESPARSE_H_
34
#define CERES_INTERNAL_SUITESPARSE_H_
36
#ifndef CERES_NO_SUITESPARSE
41
#include <glog/logging.h>
43
#include "ceres/internal/port.h"
48
class CompressedRowSparseMatrix;
49
class TripletSparseMatrix;
51
// The raw CHOLMOD and SuiteSparseQR libraries have a slightly
52
// cumbersome c like calling format. This object abstracts it away and
53
// provides the user with a simpler interface. The methods here cannot
54
// be static as a cholmod_common object serves as a global variable
55
// for all cholmod function calls.
58
SuiteSparse() { cholmod_start(&cc_); }
59
~SuiteSparse() { cholmod_finish(&cc_); }
61
// Functions for building cholmod_sparse objects from sparse
62
// matrices stored in triplet form. The matrix A is not
63
// modifed. Called owns the result.
64
cholmod_sparse* CreateSparseMatrix(TripletSparseMatrix* A);
66
// This function works like CreateSparseMatrix, except that the
67
// return value corresponds to A' rather than A.
68
cholmod_sparse* CreateSparseMatrixTranspose(TripletSparseMatrix* A);
70
// Create a cholmod_sparse wrapper around the contents of A. This is
71
// a shallow object, which refers to the contents of A and does not
72
// use the SuiteSparse machinery to allocate memory, this object
73
// should be disposed off with a delete and not a call to Free as is
74
// the case for objects returned by CreateSparseMatrixTranspose.
75
cholmod_sparse* CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A);
77
// Given a vector x, build a cholmod_dense vector of size out_size
78
// with the first in_size entries copied from x. If x is NULL, then
79
// an all zeros vector is returned. Caller owns the result.
80
cholmod_dense* CreateDenseVector(const double* x, int in_size, int out_size);
82
// The matrix A is scaled using the matrix whose diagonal is the
83
// vector scale. mode describes how scaling is applied. Possible
84
// values are CHOLMOD_ROW for row scaling - diag(scale) * A,
85
// CHOLMOD_COL for column scaling - A * diag(scale) and CHOLMOD_SYM
86
// for symmetric scaling which scales both the rows and the columns
87
// - diag(scale) * A * diag(scale).
88
void Scale(cholmod_dense* scale, int mode, cholmod_sparse* A) {
89
cholmod_scale(scale, mode, A, &cc_);
92
// Create and return a matrix m = A * A'. Caller owns the
93
// result. The matrix A is not modified.
94
cholmod_sparse* AATranspose(cholmod_sparse* A) {
95
cholmod_sparse*m = cholmod_aat(A, NULL, A->nrow, 1, &cc_);
96
m->stype = 1; // Pay attention to the upper triangular part.
100
// y = alpha * A * x + beta * y. Only y is modified.
101
void SparseDenseMultiply(cholmod_sparse* A, double alpha, double beta,
102
cholmod_dense* x, cholmod_dense* y) {
103
double alpha_[2] = {alpha, 0};
104
double beta_[2] = {beta, 0};
105
cholmod_sdmult(A, 0, alpha_, beta_, x, y, &cc_);
108
// Analyze the sparsity structure of the matrix A compute the
109
// symbolic factorization of A. A is not modified, only the pattern
110
// of non-zeros of A is used, the actual numerical values in A are
111
// of no consequence. Caller owns the result.
112
cholmod_factor* AnalyzeCholesky(cholmod_sparse* A);
114
// Use the symbolic factorization in L, to find the numerical
115
// factorization for the matrix A or AA^T. Return true if
116
// successful, false otherwise. L contains the numeric factorization
118
bool Cholesky(cholmod_sparse* A, cholmod_factor* L);
120
// Given a Cholesky factorization of a matrix A = LL^T, solve the
121
// linear system Ax = b, and return the result. If the Solve fails
122
// NULL is returned. Caller owns the result.
123
cholmod_dense* Solve(cholmod_factor* L, cholmod_dense* b);
125
// Combine the calls to Cholesky and Solve into a single call. If
126
// the cholesky factorization or the solve fails, return
127
// NULL. Caller owns the result.
128
cholmod_dense* SolveCholesky(cholmod_sparse* A,
132
void Free(cholmod_sparse* m) { cholmod_free_sparse(&m, &cc_); }
133
void Free(cholmod_dense* m) { cholmod_free_dense(&m, &cc_); }
134
void Free(cholmod_factor* m) { cholmod_free_factor(&m, &cc_); }
136
void Print(cholmod_sparse* m, const string& name) {
137
cholmod_print_sparse(m, const_cast<char*>(name.c_str()), &cc_);
140
void Print(cholmod_dense* m, const string& name) {
141
cholmod_print_dense(m, const_cast<char*>(name.c_str()), &cc_);
144
void Print(cholmod_triplet* m, const string& name) {
145
cholmod_print_triplet(m, const_cast<char*>(name.c_str()), &cc_);
148
cholmod_common* mutable_cc() { return &cc_; }
154
} // namespace internal
157
#endif // CERES_NO_SUITESPARSE
159
#endif // CERES_INTERNAL_SUITESPARSE_H_