|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: strandmark@google.com (Petter Strandmark)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_INTERNAL_CXSPARSE_H_
|
|
Packit |
ea1746 |
#define CERES_INTERNAL_CXSPARSE_H_
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This include must come before any #ifndef check on Ceres compile options.
|
|
Packit |
ea1746 |
#include "ceres/internal/port.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_NO_CXSPARSE
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <string>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/linear_solver.h"
|
|
Packit |
ea1746 |
#include "ceres/sparse_cholesky.h"
|
|
Packit |
ea1746 |
#include "cs.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class CompressedRowSparseMatrix;
|
|
Packit |
ea1746 |
class TripletSparseMatrix;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This object provides access to solving linear systems using Cholesky
|
|
Packit |
ea1746 |
// factorization with a known symbolic factorization. This features does not
|
|
Packit |
ea1746 |
// explicity exist in CXSparse. The methods in the class are nonstatic because
|
|
Packit |
ea1746 |
// the class manages internal scratch space.
|
|
Packit |
ea1746 |
class CXSparse {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
CXSparse();
|
|
Packit |
ea1746 |
~CXSparse();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Solve the system lhs * solution = rhs in place by using an
|
|
Packit |
ea1746 |
// approximate minimum degree fill reducing ordering.
|
|
Packit |
ea1746 |
bool SolveCholesky(cs_di* lhs, double* rhs_and_solution);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Solves a linear system given its symbolic and numeric factorization.
|
|
Packit |
ea1746 |
void Solve(cs_dis* symbolic_factor,
|
|
Packit |
ea1746 |
csn* numeric_factor,
|
|
Packit |
ea1746 |
double* rhs_and_solution);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Compute the numeric Cholesky factorization of A, given its
|
|
Packit |
ea1746 |
// symbolic factorization.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Caller owns the result.
|
|
Packit |
ea1746 |
csn* Cholesky(cs_di* A, cs_dis* symbolic_factor);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Creates a sparse matrix from a compressed-column form. No memory is
|
|
Packit |
ea1746 |
// allocated or copied; the structure A is filled out with info from the
|
|
Packit |
ea1746 |
// argument.
|
|
Packit |
ea1746 |
cs_di CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Creates a new matrix from a triplet form. Deallocate the returned matrix
|
|
Packit |
ea1746 |
// with Free. May return NULL if the compression or allocation fails.
|
|
Packit |
ea1746 |
cs_di* CreateSparseMatrix(TripletSparseMatrix* A);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// B = A'
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The returned matrix should be deallocated with Free when not used
|
|
Packit |
ea1746 |
// anymore.
|
|
Packit |
ea1746 |
cs_di* TransposeMatrix(cs_di* A);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// C = A * B
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The returned matrix should be deallocated with Free when not used
|
|
Packit |
ea1746 |
// anymore.
|
|
Packit |
ea1746 |
cs_di* MatrixMatrixMultiply(cs_di* A, cs_di* B);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Computes a symbolic factorization of A that can be used in SolveCholesky.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The returned matrix should be deallocated with Free when not used anymore.
|
|
Packit |
ea1746 |
cs_dis* AnalyzeCholesky(cs_di* A);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Computes a symbolic factorization of A that can be used in
|
|
Packit |
ea1746 |
// SolveCholesky, but does not compute a fill-reducing ordering.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The returned matrix should be deallocated with Free when not used anymore.
|
|
Packit |
ea1746 |
cs_dis* AnalyzeCholeskyWithNaturalOrdering(cs_di* A);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Computes a symbolic factorization of A that can be used in
|
|
Packit |
ea1746 |
// SolveCholesky. The difference from AnalyzeCholesky is that this
|
|
Packit |
ea1746 |
// function first detects the block sparsity of the matrix using
|
|
Packit |
ea1746 |
// information about the row and column blocks and uses this block
|
|
Packit |
ea1746 |
// sparse matrix to find a fill-reducing ordering. This ordering is
|
|
Packit |
ea1746 |
// then used to find a symbolic factorization. This can result in a
|
|
Packit |
ea1746 |
// significant performance improvement AnalyzeCholesky on block
|
|
Packit |
ea1746 |
// sparse matrices.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The returned matrix should be deallocated with Free when not used
|
|
Packit |
ea1746 |
// anymore.
|
|
Packit |
ea1746 |
cs_dis* BlockAnalyzeCholesky(cs_di* A,
|
|
Packit |
ea1746 |
const std::vector<int>& row_blocks,
|
|
Packit |
ea1746 |
const std::vector<int>& col_blocks);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Compute an fill-reducing approximate minimum degree ordering of
|
|
Packit |
ea1746 |
// the matrix A. ordering should be non-NULL and should point to
|
|
Packit |
ea1746 |
// enough memory to hold the ordering for the rows of A.
|
|
Packit |
ea1746 |
void ApproximateMinimumDegreeOrdering(cs_di* A, int* ordering);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void Free(cs_di* sparse_matrix);
|
|
Packit |
ea1746 |
void Free(cs_dis* symbolic_factorization);
|
|
Packit |
ea1746 |
void Free(csn* numeric_factorization);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
// Cached scratch space
|
|
Packit |
ea1746 |
CS_ENTRY* scratch_;
|
|
Packit |
ea1746 |
int scratch_size_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// An implementation of SparseCholesky interface using the CXSparse
|
|
Packit |
ea1746 |
// library.
|
|
Packit |
ea1746 |
class CXSparseCholesky : public SparseCholesky {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
// Factory
|
|
Packit |
ea1746 |
static CXSparseCholesky* Create(const OrderingType ordering_type);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// SparseCholesky interface.
|
|
Packit |
ea1746 |
virtual ~CXSparseCholesky();
|
|
Packit |
ea1746 |
virtual CompressedRowSparseMatrix::StorageType StorageType() const;
|
|
Packit |
ea1746 |
virtual LinearSolverTerminationType Factorize(CompressedRowSparseMatrix* lhs,
|
|
Packit |
ea1746 |
std::string* message);
|
|
Packit |
ea1746 |
virtual LinearSolverTerminationType Solve(const double* rhs,
|
|
Packit |
ea1746 |
double* solution,
|
|
Packit |
ea1746 |
std::string* message);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
CXSparseCholesky(const OrderingType ordering_type);
|
|
Packit |
ea1746 |
void FreeSymbolicFactorization();
|
|
Packit |
ea1746 |
void FreeNumericFactorization();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const OrderingType ordering_type_;
|
|
Packit |
ea1746 |
CXSparse cs_;
|
|
Packit |
ea1746 |
cs_dis* symbolic_factor_;
|
|
Packit |
ea1746 |
csn* numeric_factor_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#else // CERES_NO_CXSPARSE
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
typedef void cs_dis;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class CXSparse {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
void Free(void* arg) {}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
#endif // CERES_NO_CXSPARSE
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif // CERES_INTERNAL_CXSPARSE_H_
|