Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Keir Mierle | 7492b0d | 2015-03-17 22:30:16 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 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_INTERNAL_CXSPARSE_H_ |
| 32 | #define CERES_INTERNAL_CXSPARSE_H_ |
| 33 | |
Alex Stewart | ea76585 | 2014-05-07 20:46:17 +0100 | [diff] [blame] | 34 | // This include must come before any #ifndef check on Ceres compile options. |
| 35 | #include "ceres/internal/port.h" |
| 36 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 37 | #ifndef CERES_NO_CXSPARSE |
| 38 | |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 39 | #include <vector> |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 40 | #include "cs.h" |
| 41 | |
| 42 | namespace ceres { |
| 43 | namespace internal { |
| 44 | |
| 45 | class CompressedRowSparseMatrix; |
| 46 | class TripletSparseMatrix; |
| 47 | |
| 48 | // This object provides access to solving linear systems using Cholesky |
| 49 | // factorization with a known symbolic factorization. This features does not |
| 50 | // explicity exist in CXSparse. The methods in the class are nonstatic because |
| 51 | // the class manages internal scratch space. |
| 52 | class CXSparse { |
| 53 | public: |
| 54 | CXSparse(); |
| 55 | ~CXSparse(); |
| 56 | |
| 57 | // Solves a symmetric linear system A * x = b using Cholesky factorization. |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 58 | // A - The system matrix. |
| 59 | // symbolic_factorization - The symbolic factorization of A. This is obtained |
| 60 | // from AnalyzeCholesky. |
| 61 | // b - The right hand size of the linear equation. This |
| 62 | // array will also recieve the solution. |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 63 | // Returns false if Cholesky factorization of A fails. |
Sameer Agarwal | 98bf14d | 2012-08-30 10:26:44 -0700 | [diff] [blame] | 64 | bool SolveCholesky(cs_di* A, cs_dis* symbolic_factorization, double* b); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 65 | |
| 66 | // Creates a sparse matrix from a compressed-column form. No memory is |
| 67 | // allocated or copied; the structure A is filled out with info from the |
| 68 | // argument. |
| 69 | cs_di CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A); |
| 70 | |
| 71 | // Creates a new matrix from a triplet form. Deallocate the returned matrix |
| 72 | // with Free. May return NULL if the compression or allocation fails. |
| 73 | cs_di* CreateSparseMatrix(TripletSparseMatrix* A); |
| 74 | |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 75 | // B = A' |
| 76 | // |
| 77 | // The returned matrix should be deallocated with Free when not used |
| 78 | // anymore. |
| 79 | cs_di* TransposeMatrix(cs_di* A); |
| 80 | |
| 81 | // C = A * B |
| 82 | // |
| 83 | // The returned matrix should be deallocated with Free when not used |
| 84 | // anymore. |
| 85 | cs_di* MatrixMatrixMultiply(cs_di* A, cs_di* B); |
| 86 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 87 | // Computes a symbolic factorization of A that can be used in SolveCholesky. |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 88 | // |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 89 | // The returned matrix should be deallocated with Free when not used anymore. |
| 90 | cs_dis* AnalyzeCholesky(cs_di* A); |
| 91 | |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 92 | // Computes a symbolic factorization of A that can be used in |
| 93 | // SolveCholesky, but does not compute a fill-reducing ordering. |
| 94 | // |
| 95 | // The returned matrix should be deallocated with Free when not used anymore. |
| 96 | cs_dis* AnalyzeCholeskyWithNaturalOrdering(cs_di* A); |
| 97 | |
| 98 | // Computes a symbolic factorization of A that can be used in |
| 99 | // SolveCholesky. The difference from AnalyzeCholesky is that this |
| 100 | // function first detects the block sparsity of the matrix using |
| 101 | // information about the row and column blocks and uses this block |
| 102 | // sparse matrix to find a fill-reducing ordering. This ordering is |
| 103 | // then used to find a symbolic factorization. This can result in a |
| 104 | // significant performance improvement AnalyzeCholesky on block |
| 105 | // sparse matrices. |
| 106 | // |
| 107 | // The returned matrix should be deallocated with Free when not used |
| 108 | // anymore. |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 109 | cs_dis* BlockAnalyzeCholesky(cs_di* A, |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 110 | const std::vector<int>& row_blocks, |
| 111 | const std::vector<int>& col_blocks); |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 112 | |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 113 | // Compute an fill-reducing approximate minimum degree ordering of |
| 114 | // the matrix A. ordering should be non-NULL and should point to |
| 115 | // enough memory to hold the ordering for the rows of A. |
| 116 | void ApproximateMinimumDegreeOrdering(cs_di* A, int* ordering); |
| 117 | |
Sameer Agarwal | 0593747 | 2013-02-11 13:57:12 -0800 | [diff] [blame] | 118 | void Free(cs_di* sparse_matrix); |
| 119 | void Free(cs_dis* symbolic_factorization); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | // Cached scratch space |
| 123 | CS_ENTRY* scratch_; |
| 124 | int scratch_size_; |
| 125 | }; |
| 126 | |
| 127 | } // namespace internal |
| 128 | } // namespace ceres |
| 129 | |
Sergey Sharybin | f258e46 | 2013-08-15 14:50:08 +0600 | [diff] [blame] | 130 | #else // CERES_NO_CXSPARSE |
| 131 | |
Sergey Sharybin | f258e46 | 2013-08-15 14:50:08 +0600 | [diff] [blame] | 132 | typedef void cs_dis; |
| 133 | |
Sameer Agarwal | 0315982 | 2014-07-17 14:35:18 -0700 | [diff] [blame] | 134 | class CXSparse { |
| 135 | public: |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 136 | void Free(void* arg) {} |
Sameer Agarwal | 0315982 | 2014-07-17 14:35:18 -0700 | [diff] [blame] | 137 | }; |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 138 | #endif // CERES_NO_CXSPARSE |
| 139 | |
| 140 | #endif // CERES_INTERNAL_CXSPARSE_H_ |