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

« back to all changes in this revision

Viewing changes to internal/ceres/suitesparse.cc

  • 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
#ifndef CERES_NO_SUITESPARSE
 
32
 
 
33
#include "ceres/suitesparse.h"
 
34
 
 
35
#include "cholmod.h"
 
36
#include "ceres/compressed_row_sparse_matrix.h"
 
37
#include "ceres/triplet_sparse_matrix.h"
 
38
namespace ceres {
 
39
namespace internal {
 
40
 
 
41
cholmod_sparse* SuiteSparse::CreateSparseMatrix(TripletSparseMatrix* A) {
 
42
  cholmod_triplet triplet;
 
43
 
 
44
  triplet.nrow = A->num_rows();
 
45
  triplet.ncol = A->num_cols();
 
46
  triplet.nzmax = A->max_num_nonzeros();
 
47
  triplet.nnz = A->num_nonzeros();
 
48
  triplet.i = reinterpret_cast<void*>(A->mutable_rows());
 
49
  triplet.j = reinterpret_cast<void*>(A->mutable_cols());
 
50
  triplet.x = reinterpret_cast<void*>(A->mutable_values());
 
51
  triplet.stype = 0;  // Matrix is not symmetric.
 
52
  triplet.itype = CHOLMOD_INT;
 
53
  triplet.xtype = CHOLMOD_REAL;
 
54
  triplet.dtype = CHOLMOD_DOUBLE;
 
55
 
 
56
  return cholmod_triplet_to_sparse(&triplet, triplet.nnz, &cc_);
 
57
}
 
58
 
 
59
 
 
60
cholmod_sparse* SuiteSparse::CreateSparseMatrixTranspose(
 
61
    TripletSparseMatrix* A) {
 
62
  cholmod_triplet triplet;
 
63
 
 
64
  triplet.ncol = A->num_rows();  // swap row and columns
 
65
  triplet.nrow = A->num_cols();
 
66
  triplet.nzmax = A->max_num_nonzeros();
 
67
  triplet.nnz = A->num_nonzeros();
 
68
 
 
69
  // swap rows and columns
 
70
  triplet.j = reinterpret_cast<void*>(A->mutable_rows());
 
71
  triplet.i = reinterpret_cast<void*>(A->mutable_cols());
 
72
  triplet.x = reinterpret_cast<void*>(A->mutable_values());
 
73
  triplet.stype = 0;  // Matrix is not symmetric.
 
74
  triplet.itype = CHOLMOD_INT;
 
75
  triplet.xtype = CHOLMOD_REAL;
 
76
  triplet.dtype = CHOLMOD_DOUBLE;
 
77
 
 
78
  return cholmod_triplet_to_sparse(&triplet, triplet.nnz, &cc_);
 
79
}
 
80
 
 
81
cholmod_sparse* SuiteSparse::CreateSparseMatrixTransposeView(
 
82
    CompressedRowSparseMatrix* A) {
 
83
  cholmod_sparse* m = new cholmod_sparse_struct;
 
84
  m->nrow = A->num_cols();
 
85
  m->ncol = A->num_rows();
 
86
  m->nzmax = A->num_nonzeros();
 
87
 
 
88
  m->p = reinterpret_cast<void*>(A->mutable_rows());
 
89
  m->i = reinterpret_cast<void*>(A->mutable_cols());
 
90
  m->x = reinterpret_cast<void*>(A->mutable_values());
 
91
 
 
92
  m->stype = 0;  // Matrix is not symmetric.
 
93
  m->itype = CHOLMOD_INT;
 
94
  m->xtype = CHOLMOD_REAL;
 
95
  m->dtype = CHOLMOD_DOUBLE;
 
96
  m->sorted = 1;
 
97
  m->packed = 1;
 
98
 
 
99
  return m;
 
100
}
 
101
 
 
102
cholmod_dense* SuiteSparse::CreateDenseVector(const double* x,
 
103
                                              int in_size,
 
104
                                              int out_size) {
 
105
    CHECK_LE(in_size, out_size);
 
106
    cholmod_dense* v = cholmod_zeros(out_size, 1, CHOLMOD_REAL, &cc_);
 
107
    if (x != NULL) {
 
108
      memcpy(v->x, x, in_size*sizeof(*x));
 
109
    }
 
110
    return v;
 
111
}
 
112
 
 
113
cholmod_factor* SuiteSparse::AnalyzeCholesky(cholmod_sparse* A) {
 
114
  cholmod_factor* factor = cholmod_analyze(A, &cc_);
 
115
  CHECK_EQ(cc_.status, CHOLMOD_OK)
 
116
      << "Cholmod symbolic analysis failed " << cc_.status;
 
117
  CHECK_NOTNULL(factor);
 
118
  return factor;
 
119
}
 
120
 
 
121
bool SuiteSparse::Cholesky(cholmod_sparse* A, cholmod_factor* L) {
 
122
  CHECK_NOTNULL(A);
 
123
  CHECK_NOTNULL(L);
 
124
 
 
125
  cc_.quick_return_if_not_posdef = 1;
 
126
  int status = cholmod_factorize(A, L, &cc_);
 
127
  switch (cc_.status) {
 
128
    case CHOLMOD_NOT_INSTALLED:
 
129
      LOG(WARNING) << "Cholmod failure: method not installed.";
 
130
      return false;
 
131
    case CHOLMOD_OUT_OF_MEMORY:
 
132
      LOG(WARNING) << "Cholmod failure: out of memory.";
 
133
      return false;
 
134
    case CHOLMOD_TOO_LARGE:
 
135
      LOG(WARNING) << "Cholmod failure: integer overflow occured.";
 
136
      return false;
 
137
    case CHOLMOD_INVALID:
 
138
      LOG(WARNING) << "Cholmod failure: invalid input.";
 
139
      return false;
 
140
    case CHOLMOD_NOT_POSDEF:
 
141
      // TODO(sameeragarwal): These two warnings require more
 
142
      // sophisticated handling going forward. For now we will be
 
143
      // strict and treat them as failures.
 
144
      LOG(WARNING) << "Cholmod warning: matrix not positive definite.";
 
145
      return false;
 
146
    case CHOLMOD_DSMALL:
 
147
      LOG(WARNING) << "Cholmod warning: D for LDL' or diag(L) or "
 
148
                   << "LL' has tiny absolute value.";
 
149
      return false;
 
150
    case CHOLMOD_OK:
 
151
      if (status != 0) {
 
152
        return true;
 
153
      }
 
154
      LOG(WARNING) << "Cholmod failure: cholmod_factorize returned zero "
 
155
                   << "but cholmod_common::status is CHOLMOD_OK."
 
156
                   << "Please report this to ceres-solver@googlegroups.com.";
 
157
      return false;
 
158
    default:
 
159
      LOG(WARNING) << "Unknown cholmod return code. "
 
160
                   << "Please report this to ceres-solver@googlegroups.com.";
 
161
      return false;
 
162
  }
 
163
  return false;
 
164
}
 
165
 
 
166
cholmod_dense* SuiteSparse::Solve(cholmod_factor* L,
 
167
                                  cholmod_dense* b) {
 
168
  if (cc_.status != CHOLMOD_OK) {
 
169
    LOG(WARNING) << "CHOLMOD status NOT OK";
 
170
    return NULL;
 
171
  }
 
172
 
 
173
  return cholmod_solve(CHOLMOD_A, L, b, &cc_);
 
174
}
 
175
 
 
176
cholmod_dense* SuiteSparse::SolveCholesky(cholmod_sparse* A,
 
177
                                          cholmod_factor* L,
 
178
                                          cholmod_dense* b) {
 
179
  CHECK_NOTNULL(A);
 
180
  CHECK_NOTNULL(L);
 
181
  CHECK_NOTNULL(b);
 
182
 
 
183
  if (Cholesky(A, L)) {
 
184
    return Solve(L, b);
 
185
  }
 
186
 
 
187
  return NULL;
 
188
}
 
189
 
 
190
}  // namespace internal
 
191
}  // namespace ceres
 
192
 
 
193
#endif  // CERES_NO_SUITESPARSE