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 | |
| 35 | #include "ceres/compressed_row_sparse_matrix.h" |
| 36 | #include "ceres/triplet_sparse_matrix.h" |
| 37 | #include "glog/logging.h" |
| 38 | |
| 39 | namespace ceres { |
| 40 | namespace internal { |
| 41 | |
Keir Mierle | 97ca0fb | 2012-09-18 15:52:36 -0700 | [diff] [blame] | 42 | CXSparse::CXSparse() : scratch_(NULL), scratch_size_(0) { |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | CXSparse::~CXSparse() { |
| 46 | if (scratch_size_ > 0) { |
| 47 | cs_free(scratch_); |
| 48 | } |
| 49 | } |
| 50 | |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 51 | bool CXSparse::SolveCholesky(cs_di* A, |
| 52 | cs_dis* symbolic_factorization, |
| 53 | double* b) { |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 54 | // Make sure we have enough scratch space available. |
| 55 | if (scratch_size_ < A->n) { |
| 56 | if (scratch_size_ > 0) { |
| 57 | cs_free(scratch_); |
| 58 | } |
| 59 | scratch_ = reinterpret_cast<CS_ENTRY*>(cs_malloc(A->n, sizeof(CS_ENTRY))); |
| 60 | } |
| 61 | |
| 62 | // Solve using Cholesky factorization |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 63 | csn* numeric_factorization = cs_chol(A, symbolic_factorization); |
| 64 | if (numeric_factorization == NULL) { |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 65 | LOG(WARNING) << "Cholesky factorization failed."; |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // When the Cholesky factorization succeeded, these methods are guaranteed to |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 70 | // succeeded as well. In the comments below, "x" refers to the scratch space. |
| 71 | // |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 72 | // Set x = P * b. |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 73 | cs_ipvec(symbolic_factorization->pinv, b, scratch_, A->n); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 74 | |
| 75 | // Set x = L \ x. |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 76 | cs_lsolve(numeric_factorization->L, scratch_); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 77 | |
| 78 | // Set x = L' \ x. |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 79 | cs_ltsolve(numeric_factorization->L, scratch_); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 80 | |
| 81 | // Set b = P' * x. |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 82 | cs_pvec(symbolic_factorization->pinv, scratch_, b, A->n); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 83 | |
| 84 | // Free Cholesky factorization. |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 85 | cs_nfree(numeric_factorization); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | |
| 89 | cs_dis* CXSparse::AnalyzeCholesky(cs_di* A) { |
| 90 | // order = 1 for Cholesky factorization. |
| 91 | return cs_schol(1, A); |
| 92 | } |
| 93 | |
| 94 | cs_di CXSparse::CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A) { |
| 95 | cs_di At; |
| 96 | At.m = A->num_cols(); |
| 97 | At.n = A->num_rows(); |
| 98 | At.nz = -1; |
| 99 | At.nzmax = A->num_nonzeros(); |
| 100 | At.p = A->mutable_rows(); |
| 101 | At.i = A->mutable_cols(); |
| 102 | At.x = A->mutable_values(); |
| 103 | return At; |
| 104 | } |
| 105 | |
| 106 | cs_di* CXSparse::CreateSparseMatrix(TripletSparseMatrix* tsm) { |
| 107 | cs_di_sparse tsm_wrapper; |
| 108 | tsm_wrapper.nzmax = tsm->num_nonzeros();; |
| 109 | tsm_wrapper.nz = tsm->num_nonzeros();; |
| 110 | tsm_wrapper.m = tsm->num_rows(); |
| 111 | tsm_wrapper.n = tsm->num_cols(); |
| 112 | tsm_wrapper.p = tsm->mutable_cols(); |
| 113 | tsm_wrapper.i = tsm->mutable_rows(); |
| 114 | tsm_wrapper.x = tsm->mutable_values(); |
| 115 | |
| 116 | return cs_compress(&tsm_wrapper); |
| 117 | } |
| 118 | |
| 119 | void CXSparse::Free(cs_di* factor) { |
| 120 | cs_free(factor); |
| 121 | } |
| 122 | |
| 123 | void CXSparse::Free(cs_dis* factor) { |
| 124 | cs_sfree(factor); |
| 125 | } |
| 126 | |
| 127 | } // namespace internal |
| 128 | } // namespace ceres |
| 129 | |
| 130 | #endif // CERES_NO_CXSPARSE |