blob: c6d77439653a89d8580d9c0821e5db790653d6d7 [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_NO_CXSPARSE
32
33#include "ceres/cxsparse.h"
34
Sameer Agarwal344c09f2013-04-20 16:07:56 -070035#include <vector>
36#include "ceres/compressed_col_sparse_matrix_utils.h"
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070037#include "ceres/compressed_row_sparse_matrix.h"
Sameer Agarwal344c09f2013-04-20 16:07:56 -070038#include "ceres/internal/port.h"
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070039#include "ceres/triplet_sparse_matrix.h"
40#include "glog/logging.h"
41
42namespace ceres {
43namespace internal {
44
Keir Mierle97ca0fb2012-09-18 15:52:36 -070045CXSparse::CXSparse() : scratch_(NULL), scratch_size_(0) {
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070046}
47
48CXSparse::~CXSparse() {
49 if (scratch_size_ > 0) {
Sameer Agarwal344c09f2013-04-20 16:07:56 -070050 cs_di_free(scratch_);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070051 }
52}
53
Sameer Agarwal344c09f2013-04-20 16:07:56 -070054
Sameer Agarwal98bf14d2012-08-30 10:26:44 -070055bool CXSparse::SolveCholesky(cs_di* A,
56 cs_dis* symbolic_factorization,
57 double* b) {
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070058 // Make sure we have enough scratch space available.
59 if (scratch_size_ < A->n) {
60 if (scratch_size_ > 0) {
Sameer Agarwal344c09f2013-04-20 16:07:56 -070061 cs_di_free(scratch_);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070062 }
Sameer Agarwalac626962013-05-06 07:04:26 -070063 scratch_ =
64 reinterpret_cast<CS_ENTRY*>(cs_di_malloc(A->n, sizeof(CS_ENTRY)));
Sameer Agarwale7148792013-03-04 10:17:30 -080065 scratch_size_ = A->n;
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070066 }
67
68 // Solve using Cholesky factorization
Sameer Agarwal344c09f2013-04-20 16:07:56 -070069 csn* numeric_factorization = cs_di_chol(A, symbolic_factorization);
Sameer Agarwal98bf14d2012-08-30 10:26:44 -070070 if (numeric_factorization == NULL) {
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070071 LOG(WARNING) << "Cholesky factorization failed.";
72 return false;
73 }
74
Sameer Agarwalac626962013-05-06 07:04:26 -070075 // When the Cholesky factorization succeeded, these methods are
76 // guaranteed to succeeded as well. In the comments below, "x"
77 // refers to the scratch space.
Sameer Agarwal98bf14d2012-08-30 10:26:44 -070078 //
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070079 // Set x = P * b.
Sameer Agarwal344c09f2013-04-20 16:07:56 -070080 cs_di_ipvec(symbolic_factorization->pinv, b, scratch_, A->n);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070081 // Set x = L \ x.
Sameer Agarwal344c09f2013-04-20 16:07:56 -070082 cs_di_lsolve(numeric_factorization->L, scratch_);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070083 // Set x = L' \ x.
Sameer Agarwal344c09f2013-04-20 16:07:56 -070084 cs_di_ltsolve(numeric_factorization->L, scratch_);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070085 // Set b = P' * x.
Sameer Agarwal344c09f2013-04-20 16:07:56 -070086 cs_di_pvec(symbolic_factorization->pinv, scratch_, b, A->n);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070087
88 // Free Cholesky factorization.
Sameer Agarwal344c09f2013-04-20 16:07:56 -070089 cs_di_nfree(numeric_factorization);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070090 return true;
91}
92
93cs_dis* CXSparse::AnalyzeCholesky(cs_di* A) {
94 // order = 1 for Cholesky factorization.
95 return cs_schol(1, A);
96}
97
Sameer Agarwald5b93bf2013-04-26 21:17:49 -070098cs_dis* CXSparse::AnalyzeCholeskyWithNaturalOrdering(cs_di* A) {
99 // order = 0 for Natural ordering.
100 return cs_schol(0, A);
101}
102
Sameer Agarwal344c09f2013-04-20 16:07:56 -0700103cs_dis* CXSparse::BlockAnalyzeCholesky(cs_di* A,
104 const vector<int>& row_blocks,
105 const vector<int>& col_blocks) {
106 const int num_row_blocks = row_blocks.size();
107 const int num_col_blocks = col_blocks.size();
108
109 vector<int> block_rows;
110 vector<int> block_cols;
111 CompressedColumnScalarMatrixToBlockMatrix(A->i,
112 A->p,
113 row_blocks,
114 col_blocks,
115 &block_rows,
116 &block_cols);
117 cs_di block_matrix;
118 block_matrix.m = num_row_blocks;
119 block_matrix.n = num_col_blocks;
120 block_matrix.nz = -1;
121 block_matrix.nzmax = block_rows.size();
122 block_matrix.p = &block_cols[0];
123 block_matrix.i = &block_rows[0];
124 block_matrix.x = NULL;
125
126 int* ordering = cs_amd(1, &block_matrix);
127 vector<int> block_ordering(num_row_blocks, -1);
128 copy(ordering, ordering + num_row_blocks, &block_ordering[0]);
129 cs_free(ordering);
130
131 vector<int> scalar_ordering;
132 BlockOrderingToScalarOrdering(row_blocks, block_ordering, &scalar_ordering);
133
Sameer Agarwalac626962013-05-06 07:04:26 -0700134 cs_dis* symbolic_factorization =
135 reinterpret_cast<cs_dis*>(cs_calloc(1, sizeof(cs_dis)));
Sameer Agarwal344c09f2013-04-20 16:07:56 -0700136 symbolic_factorization->pinv = cs_pinv(&scalar_ordering[0], A->n);
137 cs* permuted_A = cs_symperm(A, symbolic_factorization->pinv, 0);
138
139 symbolic_factorization->parent = cs_etree(permuted_A, 0);
140 int* postordering = cs_post(symbolic_factorization->parent, A->n);
Sameer Agarwalac626962013-05-06 07:04:26 -0700141 int* column_counts = cs_counts(permuted_A,
142 symbolic_factorization->parent,
143 postordering,
144 0);
Sameer Agarwal344c09f2013-04-20 16:07:56 -0700145 cs_free(postordering);
146 cs_spfree(permuted_A);
147
148 symbolic_factorization->cp = (int*) cs_malloc(A->n+1, sizeof(int));
Sameer Agarwalac626962013-05-06 07:04:26 -0700149 symbolic_factorization->lnz = cs_cumsum(symbolic_factorization->cp,
150 column_counts,
151 A->n);
Sameer Agarwal344c09f2013-04-20 16:07:56 -0700152 symbolic_factorization->unz = symbolic_factorization->lnz;
153
154 cs_free(column_counts);
155
156 if (symbolic_factorization->lnz < 0) {
157 cs_sfree(symbolic_factorization);
158 symbolic_factorization = NULL;
159 }
160
161 return symbolic_factorization;
162}
163
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700164cs_di CXSparse::CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A) {
165 cs_di At;
166 At.m = A->num_cols();
167 At.n = A->num_rows();
168 At.nz = -1;
169 At.nzmax = A->num_nonzeros();
170 At.p = A->mutable_rows();
171 At.i = A->mutable_cols();
172 At.x = A->mutable_values();
173 return At;
174}
175
176cs_di* CXSparse::CreateSparseMatrix(TripletSparseMatrix* tsm) {
177 cs_di_sparse tsm_wrapper;
178 tsm_wrapper.nzmax = tsm->num_nonzeros();;
179 tsm_wrapper.nz = tsm->num_nonzeros();;
180 tsm_wrapper.m = tsm->num_rows();
181 tsm_wrapper.n = tsm->num_cols();
182 tsm_wrapper.p = tsm->mutable_cols();
183 tsm_wrapper.i = tsm->mutable_rows();
184 tsm_wrapper.x = tsm->mutable_values();
185
186 return cs_compress(&tsm_wrapper);
187}
188
Sameer Agarwald5b93bf2013-04-26 21:17:49 -0700189void CXSparse::ApproximateMinimumDegreeOrdering(cs_di* A, int* ordering) {
190 int* cs_ordering = cs_amd(1, A);
191 copy(cs_ordering, cs_ordering + A->m, ordering);
192 cs_free(cs_ordering);
193}
194
195cs_di* CXSparse::TransposeMatrix(cs_di* A) {
196 return cs_di_transpose(A, 1);
197}
198
199cs_di* CXSparse::MatrixMatrixMultiply(cs_di* A, cs_di* B) {
200 return cs_di_multiply(A, B);
201}
202
Sameer Agarwal05937472013-02-11 13:57:12 -0800203void CXSparse::Free(cs_di* sparse_matrix) {
204 cs_di_spfree(sparse_matrix);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700205}
206
Sameer Agarwal05937472013-02-11 13:57:12 -0800207void CXSparse::Free(cs_dis* symbolic_factorization) {
208 cs_di_sfree(symbolic_factorization);
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700209}
210
211} // namespace internal
212} // namespace ceres
213
214#endif // CERES_NO_CXSPARSE