~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to internal/ceres/suitesparse.h

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/
 
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
// A simple C++ interface to the SuiteSparse and CHOLMOD libraries.
 
32
 
 
33
#ifndef CERES_INTERNAL_SUITESPARSE_H_
 
34
#define CERES_INTERNAL_SUITESPARSE_H_
 
35
 
 
36
#ifndef CERES_NO_SUITESPARSE
 
37
 
 
38
#include <cstring>
 
39
#include <string>
 
40
 
 
41
#include <glog/logging.h>
 
42
#include "cholmod.h"
 
43
#include "ceres/internal/port.h"
 
44
 
 
45
namespace ceres {
 
46
namespace internal {
 
47
 
 
48
class CompressedRowSparseMatrix;
 
49
class TripletSparseMatrix;
 
50
 
 
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.
 
56
class SuiteSparse {
 
57
 public:
 
58
  SuiteSparse()  { cholmod_start(&cc_);  }
 
59
  ~SuiteSparse() { cholmod_finish(&cc_); }
 
60
 
 
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);
 
65
 
 
66
  // This function works like CreateSparseMatrix, except that the
 
67
  // return value corresponds to A' rather than A.
 
68
  cholmod_sparse* CreateSparseMatrixTranspose(TripletSparseMatrix* A);
 
69
 
 
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);
 
76
 
 
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);
 
81
 
 
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_);
 
90
  }
 
91
 
 
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.
 
97
    return m;
 
98
  }
 
99
 
 
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_);
 
106
  }
 
107
 
 
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);
 
113
 
 
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
 
117
  // on return.
 
118
  bool Cholesky(cholmod_sparse* A, cholmod_factor* L);
 
119
 
 
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);
 
124
 
 
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,
 
129
                               cholmod_factor* L,
 
130
                               cholmod_dense* b);
 
131
 
 
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_); }
 
135
 
 
136
  void Print(cholmod_sparse* m, const string& name) {
 
137
    cholmod_print_sparse(m, const_cast<char*>(name.c_str()), &cc_);
 
138
  }
 
139
 
 
140
  void Print(cholmod_dense* m, const string& name) {
 
141
    cholmod_print_dense(m, const_cast<char*>(name.c_str()), &cc_);
 
142
  }
 
143
 
 
144
  void Print(cholmod_triplet* m, const string& name) {
 
145
    cholmod_print_triplet(m, const_cast<char*>(name.c_str()), &cc_);
 
146
  }
 
147
 
 
148
  cholmod_common* mutable_cc() { return &cc_; }
 
149
 
 
150
 private:
 
151
  cholmod_common cc_;
 
152
};
 
153
 
 
154
}  // namespace internal
 
155
}  // namespace ceres
 
156
 
 
157
#endif  // CERES_NO_SUITESPARSE
 
158
 
 
159
#endif  // CERES_INTERNAL_SUITESPARSE_H_