|
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: keir@google.com (Keir Mierle)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_INTERNAL_CGNR_LINEAR_OPERATOR_H_
|
|
Packit |
ea1746 |
#define CERES_INTERNAL_CGNR_LINEAR_OPERATOR_H_
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <algorithm>
|
|
Packit |
ea1746 |
#include "ceres/linear_operator.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/scoped_ptr.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/eigen.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class SparseMatrix;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// A linear operator which takes a matrix A and a diagonal vector D and
|
|
Packit |
ea1746 |
// performs products of the form
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// (A^T A + D^T D)x
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// This is used to implement iterative general sparse linear solving with
|
|
Packit |
ea1746 |
// conjugate gradients, where A is the Jacobian and D is a regularizing
|
|
Packit |
ea1746 |
// parameter. A brief proof that D^T D is the correct regularizer:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Given a regularized least squares problem:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// min ||Ax - b||^2 + ||Dx||^2
|
|
Packit |
ea1746 |
// x
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// First expand into matrix notation:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// (Ax - b)^T (Ax - b) + xD^TDx
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Then multiply out to get:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// = xA^TAx - 2b^T Ax + b^Tb + xD^TDx
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Take the derivative:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// 0 = 2A^TAx - 2A^T b + 2 D^TDx
|
|
Packit |
ea1746 |
// 0 = A^TAx - A^T b + D^TDx
|
|
Packit |
ea1746 |
// 0 = (A^TA + D^TD)x - A^T b
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Thus, the symmetric system we need to solve for CGNR is
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Sx = z
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// with S = A^TA + D^TD
|
|
Packit |
ea1746 |
// and z = A^T b
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Note: This class is not thread safe, since it uses some temporary storage.
|
|
Packit |
ea1746 |
class CgnrLinearOperator : public LinearOperator {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
CgnrLinearOperator(const LinearOperator& A, const double *D)
|
|
Packit |
ea1746 |
: A_(A), D_(D), z_(new double[A.num_rows()]) {
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
virtual ~CgnrLinearOperator() {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual void RightMultiply(const double* x, double* y) const {
|
|
Packit |
ea1746 |
std::fill(z_.get(), z_.get() + A_.num_rows(), 0.0);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// z = Ax
|
|
Packit |
ea1746 |
A_.RightMultiply(x, z_.get());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// y = y + Atz
|
|
Packit |
ea1746 |
A_.LeftMultiply(z_.get(), y);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// y = y + DtDx
|
|
Packit |
ea1746 |
if (D_ != NULL) {
|
|
Packit |
ea1746 |
int n = A_.num_cols();
|
|
Packit |
ea1746 |
VectorRef(y, n).array() += ConstVectorRef(D_, n).array().square() *
|
|
Packit |
ea1746 |
ConstVectorRef(x, n).array();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual void LeftMultiply(const double* x, double* y) const {
|
|
Packit |
ea1746 |
RightMultiply(x, y);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual int num_rows() const { return A_.num_cols(); }
|
|
Packit |
ea1746 |
virtual int num_cols() const { return A_.num_cols(); }
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
const LinearOperator& A_;
|
|
Packit |
ea1746 |
const double* D_;
|
|
Packit |
ea1746 |
scoped_array<double> z_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif // CERES_INTERNAL_CGNR_LINEAR_OPERATOR_H_
|