Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 1 | // 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 Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 35 | #include <vector> |
| 36 | #include "ceres/compressed_col_sparse_matrix_utils.h" |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 37 | #include "ceres/compressed_row_sparse_matrix.h" |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 38 | #include "ceres/internal/port.h" |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 39 | #include "ceres/triplet_sparse_matrix.h" |
| 40 | #include "glog/logging.h" |
| 41 | |
| 42 | namespace ceres { |
| 43 | namespace internal { |
| 44 | |
Keir Mierle | 97ca0fb | 2012-09-18 15:52:36 -0700 | [diff] [blame] | 45 | CXSparse::CXSparse() : scratch_(NULL), scratch_size_(0) { |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | CXSparse::~CXSparse() { |
| 49 | if (scratch_size_ > 0) { |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 50 | cs_di_free(scratch_); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 54 | |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 55 | bool CXSparse::SolveCholesky(cs_di* A, |
| 56 | cs_dis* symbolic_factorization, |
| 57 | double* b) { |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 58 | // Make sure we have enough scratch space available. |
| 59 | if (scratch_size_ < A->n) { |
| 60 | if (scratch_size_ > 0) { |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 61 | cs_di_free(scratch_); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 62 | } |
Sameer Agarwal | ac62696 | 2013-05-06 07:04:26 -0700 | [diff] [blame] | 63 | scratch_ = |
| 64 | reinterpret_cast<CS_ENTRY*>(cs_di_malloc(A->n, sizeof(CS_ENTRY))); |
Sameer Agarwal | e714879 | 2013-03-04 10:17:30 -0800 | [diff] [blame] | 65 | scratch_size_ = A->n; |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Solve using Cholesky factorization |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 69 | csn* numeric_factorization = cs_di_chol(A, symbolic_factorization); |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 70 | if (numeric_factorization == NULL) { |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 71 | LOG(WARNING) << "Cholesky factorization failed."; |
| 72 | return false; |
| 73 | } |
| 74 | |
Sameer Agarwal | ac62696 | 2013-05-06 07:04:26 -0700 | [diff] [blame] | 75 | // 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 Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 78 | // |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 79 | // Set x = P * b. |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 80 | cs_di_ipvec(symbolic_factorization->pinv, b, scratch_, A->n); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 81 | // Set x = L \ x. |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 82 | cs_di_lsolve(numeric_factorization->L, scratch_); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 83 | // Set x = L' \ x. |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 84 | cs_di_ltsolve(numeric_factorization->L, scratch_); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 85 | // Set b = P' * x. |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 86 | cs_di_pvec(symbolic_factorization->pinv, scratch_, b, A->n); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 87 | |
| 88 | // Free Cholesky factorization. |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 89 | cs_di_nfree(numeric_factorization); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | |
| 93 | cs_dis* CXSparse::AnalyzeCholesky(cs_di* A) { |
| 94 | // order = 1 for Cholesky factorization. |
| 95 | return cs_schol(1, A); |
| 96 | } |
| 97 | |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 98 | cs_dis* CXSparse::AnalyzeCholeskyWithNaturalOrdering(cs_di* A) { |
| 99 | // order = 0 for Natural ordering. |
| 100 | return cs_schol(0, A); |
| 101 | } |
| 102 | |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 103 | cs_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 Agarwal | ac62696 | 2013-05-06 07:04:26 -0700 | [diff] [blame] | 134 | cs_dis* symbolic_factorization = |
| 135 | reinterpret_cast<cs_dis*>(cs_calloc(1, sizeof(cs_dis))); |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 136 | 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 Agarwal | ac62696 | 2013-05-06 07:04:26 -0700 | [diff] [blame] | 141 | int* column_counts = cs_counts(permuted_A, |
| 142 | symbolic_factorization->parent, |
| 143 | postordering, |
| 144 | 0); |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 145 | cs_free(postordering); |
| 146 | cs_spfree(permuted_A); |
| 147 | |
| 148 | symbolic_factorization->cp = (int*) cs_malloc(A->n+1, sizeof(int)); |
Sameer Agarwal | ac62696 | 2013-05-06 07:04:26 -0700 | [diff] [blame] | 149 | symbolic_factorization->lnz = cs_cumsum(symbolic_factorization->cp, |
| 150 | column_counts, |
| 151 | A->n); |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 152 | 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 Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 164 | cs_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 | |
| 176 | cs_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 Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 189 | void 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 | |
| 195 | cs_di* CXSparse::TransposeMatrix(cs_di* A) { |
| 196 | return cs_di_transpose(A, 1); |
| 197 | } |
| 198 | |
| 199 | cs_di* CXSparse::MatrixMatrixMultiply(cs_di* A, cs_di* B) { |
| 200 | return cs_di_multiply(A, B); |
| 201 | } |
| 202 | |
Sameer Agarwal | 0593747 | 2013-02-11 13:57:12 -0800 | [diff] [blame] | 203 | void CXSparse::Free(cs_di* sparse_matrix) { |
| 204 | cs_di_spfree(sparse_matrix); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Sameer Agarwal | 0593747 | 2013-02-11 13:57:12 -0800 | [diff] [blame] | 207 | void CXSparse::Free(cs_dis* symbolic_factorization) { |
| 208 | cs_di_sfree(symbolic_factorization); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | } // namespace internal |
| 212 | } // namespace ceres |
| 213 | |
| 214 | #endif // CERES_NO_CXSPARSE |