10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer 20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/ 40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without 60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met: 70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice, 90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer. 100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice, 110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer in the documentation 120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and/or other materials provided with the distribution. 130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be 140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used to endorse or promote products derived from this software without 150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// specific prior written permission. 160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE. 280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal) 300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// For generalized bi-partite Jacobian matrices that arise in 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Structure from Motion related problems, it is sometimes useful to 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// have access to the two parts of the matrix as linear operators 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// themselves. This class provides that functionality. 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_ 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_ 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 3979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include <algorithm> 4079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include <cstring> 4179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include <vector> 4279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez 4379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/block_structure.h" 4479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/internal/eigen.h" 4579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/linear_solver.h" 4679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/small_blas.h" 4779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "glog/logging.h" 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres { 500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal { 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Given generalized bi-partite matrix A = [E F], with the same block 530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// structure as required by the Schur complement based solver, found 540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// in explicit_schur_complement_solver.h, provide access to the 550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// matrices E and F and their outer products E'E and F'F with 560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// themselves. 570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Lack of BlockStructure object will result in a crash and if the 590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// block structure of the matrix does not satisfy the requirements of 600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// the Schur complement solver it will result in unpredictable and 610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// wrong output. 6279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass PartitionedMatrixViewBase { 630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public: 6479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual ~PartitionedMatrixViewBase() {} 650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // y += E'x 6779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void LeftMultiplyE(const double* x, double* y) const = 0; 680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // y += F'x 7079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void LeftMultiplyF(const double* x, double* y) const = 0; 710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // y += Ex 7379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void RightMultiplyE(const double* x, double* y) const = 0; 740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // y += Fx 7679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void RightMultiplyF(const double* x, double* y) const = 0; 770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Create and return the block diagonal of the matrix E'E. 7979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual BlockSparseMatrix* CreateBlockDiagonalEtE() const = 0; 800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 8179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // Create and return the block diagonal of the matrix F'F. Caller 8279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // owns the result. 8379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual BlockSparseMatrix* CreateBlockDiagonalFtF() const = 0; 840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Compute the block diagonal of the matrix E'E and store it in 860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // block_diagonal. The matrix block_diagonal is expected to have a 870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // BlockStructure (preferably created using 880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // CreateBlockDiagonalMatrixEtE) which is has the same structure as 890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // the block diagonal of E'E. 9079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void UpdateBlockDiagonalEtE( 9179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez BlockSparseMatrix* block_diagonal) const = 0; 920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Compute the block diagonal of the matrix F'F and store it in 940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // block_diagonal. The matrix block_diagonal is expected to have a 950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // BlockStructure (preferably created using 960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // CreateBlockDiagonalMatrixFtF) which is has the same structure as 970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // the block diagonal of F'F. 9879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void UpdateBlockDiagonalFtF( 9979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez BlockSparseMatrix* block_diagonal) const = 0; 10079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez 10179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_col_blocks_e() const = 0; 10279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_col_blocks_f() const = 0; 10379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_cols_e() const = 0; 10479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_cols_f() const = 0; 10579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_rows() const = 0; 10679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_cols() const = 0; 10779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez 10879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez static PartitionedMatrixViewBase* Create(const LinearSolver::Options& options, 10979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez const BlockSparseMatrix& matrix); 11079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez}; 11179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez 11279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandeztemplate <int kRowBlockSize = Eigen::Dynamic, 11379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez int kEBlockSize = Eigen::Dynamic, 11479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez int kFBlockSize = Eigen::Dynamic > 11579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass PartitionedMatrixView : public PartitionedMatrixViewBase { 11679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez public: 11779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // matrix = [E F], where the matrix E contains the first 11879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // num_col_blocks_a column blocks. 11979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez PartitionedMatrixView(const BlockSparseMatrix& matrix, int num_col_blocks_e); 12079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez 12179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual ~PartitionedMatrixView(); 12279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void LeftMultiplyE(const double* x, double* y) const; 12379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void LeftMultiplyF(const double* x, double* y) const; 12479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void RightMultiplyE(const double* x, double* y) const; 12579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void RightMultiplyF(const double* x, double* y) const; 12679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual BlockSparseMatrix* CreateBlockDiagonalEtE() const; 12779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual BlockSparseMatrix* CreateBlockDiagonalFtF() const; 12879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void UpdateBlockDiagonalEtE(BlockSparseMatrix* block_diagonal) const; 12979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual void UpdateBlockDiagonalFtF(BlockSparseMatrix* block_diagonal) const; 13079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_col_blocks_e() const { return num_col_blocks_e_; } 13179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_col_blocks_f() const { return num_col_blocks_f_; } 13279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_cols_e() const { return num_cols_e_; } 13379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_cols_f() const { return num_cols_f_; } 13479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_rows() const { return matrix_.num_rows(); } 13579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez virtual int num_cols() const { return matrix_.num_cols(); } 1360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong private: 1380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong BlockSparseMatrix* CreateBlockDiagonalMatrixLayout(int start_col_block, 1390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int end_col_block) const; 1400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1411d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling const BlockSparseMatrix& matrix_; 1420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_row_blocks_e_; 1430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_col_blocks_e_; 1440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_col_blocks_f_; 1450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_cols_e_; 1460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_cols_f_; 1470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}; 1480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace internal 1500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace ceres 1510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif // CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_ 153