|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2017 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: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/inner_product_computer.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <numeric>
|
|
Packit |
ea1746 |
#include "ceres/block_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/eigen.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/scoped_ptr.h"
|
|
Packit |
ea1746 |
#include "ceres/random.h"
|
|
Packit |
ea1746 |
#include "ceres/triplet_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "glog/logging.h"
|
|
Packit |
ea1746 |
#include "gtest/gtest.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "Eigen/SparseCore"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#define COMPUTE_AND_COMPARE \
|
|
Packit |
ea1746 |
{ \
|
|
Packit |
ea1746 |
inner_product_computer->Compute(); \
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* actual_product_crsm = \
|
|
Packit |
ea1746 |
inner_product_computer->mutable_result(); \
|
|
Packit |
ea1746 |
Matrix actual_inner_product = \
|
|
Packit |
ea1746 |
Eigen::MappedSparseMatrix<double, Eigen::ColMajor>( \
|
|
Packit |
ea1746 |
actual_product_crsm->num_rows(), \
|
|
Packit |
ea1746 |
actual_product_crsm->num_rows(), \
|
|
Packit |
ea1746 |
actual_product_crsm->num_nonzeros(), \
|
|
Packit |
ea1746 |
actual_product_crsm->mutable_rows(), \
|
|
Packit |
ea1746 |
actual_product_crsm->mutable_cols(), \
|
|
Packit |
ea1746 |
actual_product_crsm->mutable_values()); \
|
|
Packit |
ea1746 |
EXPECT_EQ(actual_inner_product.rows(), actual_inner_product.cols()); \
|
|
Packit |
ea1746 |
EXPECT_EQ(expected_inner_product.rows(), expected_inner_product.cols()); \
|
|
Packit |
ea1746 |
EXPECT_EQ(actual_inner_product.rows(), expected_inner_product.rows()); \
|
|
Packit |
ea1746 |
Matrix expected_t, actual_t; \
|
|
Packit |
ea1746 |
if (actual_product_crsm->storage_type() == \
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::LOWER_TRIANGULAR) { \
|
|
Packit |
ea1746 |
expected_t = expected_inner_product.triangularView<Eigen::Upper>(); \
|
|
Packit |
ea1746 |
actual_t = actual_inner_product.triangularView<Eigen::Upper>(); \
|
|
Packit |
ea1746 |
} else { \
|
|
Packit |
ea1746 |
expected_t = expected_inner_product.triangularView<Eigen::Lower>(); \
|
|
Packit |
ea1746 |
actual_t = actual_inner_product.triangularView<Eigen::Lower>(); \
|
|
Packit |
ea1746 |
} \
|
|
Packit |
ea1746 |
EXPECT_LE((expected_t - actual_t).norm() / actual_t.norm(), \
|
|
Packit |
ea1746 |
100 * std::numeric_limits<double>::epsilon()) \
|
|
Packit |
ea1746 |
<< "expected: \n" \
|
|
Packit |
ea1746 |
<< expected_t << "\nactual: \n" \
|
|
Packit |
ea1746 |
<< actual_t; \
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST(InnerProductComputer, NormalOperation) {
|
|
Packit |
ea1746 |
// "Randomly generated seed."
|
|
Packit |
ea1746 |
SetRandomState(29823);
|
|
Packit |
ea1746 |
const int kMaxNumRowBlocks = 10;
|
|
Packit |
ea1746 |
const int kMaxNumColBlocks = 10;
|
|
Packit |
ea1746 |
const int kNumTrials = 10;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Create a random matrix, compute its outer product using Eigen and
|
|
Packit |
ea1746 |
// ComputeOuterProduct. Convert both matrices to dense matrices and
|
|
Packit |
ea1746 |
// compare their upper triangular parts.
|
|
Packit |
ea1746 |
for (int num_row_blocks = 1; num_row_blocks < kMaxNumRowBlocks;
|
|
Packit |
ea1746 |
++num_row_blocks) {
|
|
Packit |
ea1746 |
for (int num_col_blocks = 1; num_col_blocks < kMaxNumColBlocks;
|
|
Packit |
ea1746 |
++num_col_blocks) {
|
|
Packit |
ea1746 |
for (int trial = 0; trial < kNumTrials; ++trial) {
|
|
Packit |
ea1746 |
BlockSparseMatrix::RandomMatrixOptions options;
|
|
Packit |
ea1746 |
options.num_row_blocks = num_row_blocks;
|
|
Packit |
ea1746 |
options.num_col_blocks = num_col_blocks;
|
|
Packit |
ea1746 |
options.min_row_block_size = 1;
|
|
Packit |
ea1746 |
options.max_row_block_size = 5;
|
|
Packit |
ea1746 |
options.min_col_block_size = 1;
|
|
Packit |
ea1746 |
options.max_col_block_size = 10;
|
|
Packit |
ea1746 |
options.block_density = std::max(0.1, RandDouble());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
VLOG(2) << "num row blocks: " << options.num_row_blocks;
|
|
Packit |
ea1746 |
VLOG(2) << "num col blocks: " << options.num_col_blocks;
|
|
Packit |
ea1746 |
VLOG(2) << "min row block size: " << options.min_row_block_size;
|
|
Packit |
ea1746 |
VLOG(2) << "max row block size: " << options.max_row_block_size;
|
|
Packit |
ea1746 |
VLOG(2) << "min col block size: " << options.min_col_block_size;
|
|
Packit |
ea1746 |
VLOG(2) << "max col block size: " << options.max_col_block_size;
|
|
Packit |
ea1746 |
VLOG(2) << "block density: " << options.block_density;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<BlockSparseMatrix> random_matrix(
|
|
Packit |
ea1746 |
BlockSparseMatrix::CreateRandomMatrix(options));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TripletSparseMatrix tsm(random_matrix->num_rows(),
|
|
Packit |
ea1746 |
random_matrix->num_cols(),
|
|
Packit |
ea1746 |
random_matrix->num_nonzeros());
|
|
Packit |
ea1746 |
random_matrix->ToTripletSparseMatrix(&tsm;;
|
|
Packit |
ea1746 |
std::vector<Eigen::Triplet<double> > triplets;
|
|
Packit |
ea1746 |
for (int i = 0; i < tsm.num_nonzeros(); ++i) {
|
|
Packit |
ea1746 |
triplets.push_back(Eigen::Triplet<double>(
|
|
Packit |
ea1746 |
tsm.rows()[i], tsm.cols()[i], tsm.values()[i]));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
Eigen::SparseMatrix<double> eigen_random_matrix(
|
|
Packit |
ea1746 |
random_matrix->num_rows(), random_matrix->num_cols());
|
|
Packit |
ea1746 |
eigen_random_matrix.setFromTriplets(triplets.begin(), triplets.end());
|
|
Packit |
ea1746 |
Matrix expected_inner_product =
|
|
Packit |
ea1746 |
eigen_random_matrix.transpose() * eigen_random_matrix;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<InnerProductComputer> inner_product_computer;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
inner_product_computer.reset(InnerProductComputer::Create(
|
|
Packit |
ea1746 |
*random_matrix, CompressedRowSparseMatrix::LOWER_TRIANGULAR));
|
|
Packit |
ea1746 |
COMPUTE_AND_COMPARE;
|
|
Packit |
ea1746 |
inner_product_computer.reset(InnerProductComputer::Create(
|
|
Packit |
ea1746 |
*random_matrix, CompressedRowSparseMatrix::UPPER_TRIANGULAR));
|
|
Packit |
ea1746 |
COMPUTE_AND_COMPARE;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST(InnerProductComputer, SubMatrix) {
|
|
Packit |
ea1746 |
// "Randomly generated seed."
|
|
Packit |
ea1746 |
SetRandomState(29823);
|
|
Packit |
ea1746 |
const int kNumRowBlocks = 10;
|
|
Packit |
ea1746 |
const int kNumColBlocks = 20;
|
|
Packit |
ea1746 |
const int kNumTrials = 5;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Create a random matrix, compute its outer product using Eigen and
|
|
Packit |
ea1746 |
// ComputeInnerProductComputer. Convert both matrices to dense matrices and
|
|
Packit |
ea1746 |
// compare their upper triangular parts.
|
|
Packit |
ea1746 |
for (int trial = 0; trial < kNumTrials; ++trial) {
|
|
Packit |
ea1746 |
BlockSparseMatrix::RandomMatrixOptions options;
|
|
Packit |
ea1746 |
options.num_row_blocks = kNumRowBlocks;
|
|
Packit |
ea1746 |
options.num_col_blocks = kNumColBlocks;
|
|
Packit |
ea1746 |
options.min_row_block_size = 1;
|
|
Packit |
ea1746 |
options.max_row_block_size = 5;
|
|
Packit |
ea1746 |
options.min_col_block_size = 1;
|
|
Packit |
ea1746 |
options.max_col_block_size = 10;
|
|
Packit |
ea1746 |
options.block_density = std::max(0.1, RandDouble());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
VLOG(2) << "num row blocks: " << options.num_row_blocks;
|
|
Packit |
ea1746 |
VLOG(2) << "num col blocks: " << options.num_col_blocks;
|
|
Packit |
ea1746 |
VLOG(2) << "min row block size: " << options.min_row_block_size;
|
|
Packit |
ea1746 |
VLOG(2) << "max row block size: " << options.max_row_block_size;
|
|
Packit |
ea1746 |
VLOG(2) << "min col block size: " << options.min_col_block_size;
|
|
Packit |
ea1746 |
VLOG(2) << "max col block size: " << options.max_col_block_size;
|
|
Packit |
ea1746 |
VLOG(2) << "block density: " << options.block_density;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<BlockSparseMatrix> random_matrix(
|
|
Packit |
ea1746 |
BlockSparseMatrix::CreateRandomMatrix(options));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const std::vector<CompressedRow>& row_blocks =
|
|
Packit |
ea1746 |
random_matrix->block_structure()->rows;
|
|
Packit |
ea1746 |
const int num_row_blocks = row_blocks.size();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int start_row_block = 0; start_row_block < num_row_blocks - 1;
|
|
Packit |
ea1746 |
++start_row_block) {
|
|
Packit |
ea1746 |
for (int end_row_block = start_row_block + 1;
|
|
Packit |
ea1746 |
end_row_block < num_row_blocks;
|
|
Packit |
ea1746 |
++end_row_block) {
|
|
Packit |
ea1746 |
const int start_row = row_blocks[start_row_block].block.position;
|
|
Packit |
ea1746 |
const int end_row = row_blocks[end_row_block].block.position;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TripletSparseMatrix tsm(random_matrix->num_rows(),
|
|
Packit |
ea1746 |
random_matrix->num_cols(),
|
|
Packit |
ea1746 |
random_matrix->num_nonzeros());
|
|
Packit |
ea1746 |
random_matrix->ToTripletSparseMatrix(&tsm;;
|
|
Packit |
ea1746 |
std::vector<Eigen::Triplet<double> > triplets;
|
|
Packit |
ea1746 |
for (int i = 0; i < tsm.num_nonzeros(); ++i) {
|
|
Packit |
ea1746 |
if (tsm.rows()[i] >= start_row && tsm.rows()[i] < end_row) {
|
|
Packit |
ea1746 |
triplets.push_back(Eigen::Triplet<double>(
|
|
Packit |
ea1746 |
tsm.rows()[i], tsm.cols()[i], tsm.values()[i]));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Eigen::SparseMatrix<double> eigen_random_matrix(
|
|
Packit |
ea1746 |
random_matrix->num_rows(), random_matrix->num_cols());
|
|
Packit |
ea1746 |
eigen_random_matrix.setFromTriplets(triplets.begin(), triplets.end());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Matrix expected_inner_product =
|
|
Packit |
ea1746 |
eigen_random_matrix.transpose() * eigen_random_matrix;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<InnerProductComputer> inner_product_computer;
|
|
Packit |
ea1746 |
inner_product_computer.reset(InnerProductComputer::Create(
|
|
Packit |
ea1746 |
*random_matrix,
|
|
Packit |
ea1746 |
start_row_block,
|
|
Packit |
ea1746 |
end_row_block,
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::LOWER_TRIANGULAR));
|
|
Packit |
ea1746 |
COMPUTE_AND_COMPARE;
|
|
Packit |
ea1746 |
inner_product_computer.reset(InnerProductComputer::Create(
|
|
Packit |
ea1746 |
*random_matrix,
|
|
Packit |
ea1746 |
start_row_block,
|
|
Packit |
ea1746 |
end_row_block,
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::UPPER_TRIANGULAR));
|
|
Packit |
ea1746 |
COMPUTE_AND_COMPARE;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#undef COMPUTE_AND_COMPARE
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|