|
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: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <limits>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
#include "ceres/block_random_access_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/eigen.h"
|
|
Packit |
ea1746 |
#include "glog/logging.h"
|
|
Packit |
ea1746 |
#include "gtest/gtest.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
using std::make_pair;
|
|
Packit |
ea1746 |
using std::pair;
|
|
Packit |
ea1746 |
using std::set;
|
|
Packit |
ea1746 |
using std::vector;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST(BlockRandomAccessSparseMatrix, GetCell) {
|
|
Packit |
ea1746 |
vector<int> blocks;
|
|
Packit |
ea1746 |
blocks.push_back(3);
|
|
Packit |
ea1746 |
blocks.push_back(4);
|
|
Packit |
ea1746 |
blocks.push_back(5);
|
|
Packit |
ea1746 |
const int num_rows = 3 + 4 + 5;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
set< pair<int, int> > block_pairs;
|
|
Packit |
ea1746 |
int num_nonzeros = 0;
|
|
Packit |
ea1746 |
block_pairs.insert(make_pair(0, 0));
|
|
Packit |
ea1746 |
num_nonzeros += blocks[0] * blocks[0];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
block_pairs.insert(make_pair(1, 1));
|
|
Packit |
ea1746 |
num_nonzeros += blocks[1] * blocks[1];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
block_pairs.insert(make_pair(1, 2));
|
|
Packit |
ea1746 |
num_nonzeros += blocks[1] * blocks[2];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
block_pairs.insert(make_pair(0, 2));
|
|
Packit |
ea1746 |
num_nonzeros += blocks[2] * blocks[0];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
BlockRandomAccessSparseMatrix m(blocks, block_pairs);
|
|
Packit |
ea1746 |
EXPECT_EQ(m.num_rows(), num_rows);
|
|
Packit |
ea1746 |
EXPECT_EQ(m.num_cols(), num_rows);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (set<pair<int, int> >::const_iterator it = block_pairs.begin();
|
|
Packit |
ea1746 |
it != block_pairs.end();
|
|
Packit |
ea1746 |
++it) {
|
|
Packit |
ea1746 |
const int row_block_id = it->first;
|
|
Packit |
ea1746 |
const int col_block_id = it->second;
|
|
Packit |
ea1746 |
int row;
|
|
Packit |
ea1746 |
int col;
|
|
Packit |
ea1746 |
int row_stride;
|
|
Packit |
ea1746 |
int col_stride;
|
|
Packit |
ea1746 |
CellInfo* cell = m.GetCell(row_block_id, col_block_id,
|
|
Packit |
ea1746 |
&row, &col,
|
|
Packit |
ea1746 |
&row_stride, &col_stride);
|
|
Packit |
ea1746 |
EXPECT_TRUE(cell != NULL);
|
|
Packit |
ea1746 |
EXPECT_EQ(row, 0);
|
|
Packit |
ea1746 |
EXPECT_EQ(col, 0);
|
|
Packit |
ea1746 |
EXPECT_EQ(row_stride, blocks[row_block_id]);
|
|
Packit |
ea1746 |
EXPECT_EQ(col_stride, blocks[col_block_id]);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Write into the block
|
|
Packit |
ea1746 |
MatrixRef(cell->values, row_stride, col_stride).block(
|
|
Packit |
ea1746 |
row, col, blocks[row_block_id], blocks[col_block_id]) =
|
|
Packit |
ea1746 |
(row_block_id + 1) * (col_block_id +1) *
|
|
Packit |
ea1746 |
Matrix::Ones(blocks[row_block_id], blocks[col_block_id]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const TripletSparseMatrix* tsm = m.matrix();
|
|
Packit |
ea1746 |
EXPECT_EQ(tsm->num_nonzeros(), num_nonzeros);
|
|
Packit |
ea1746 |
EXPECT_EQ(tsm->max_num_nonzeros(), num_nonzeros);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Matrix dense;
|
|
Packit |
ea1746 |
tsm->ToDenseMatrix(&dense);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double kTolerance = 1e-14;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// (0, 0)
|
|
Packit |
ea1746 |
EXPECT_NEAR((dense.block(0, 0, 3, 3) - Matrix::Ones(3, 3)).norm(),
|
|
Packit |
ea1746 |
0.0,
|
|
Packit |
ea1746 |
kTolerance);
|
|
Packit |
ea1746 |
// (1, 1)
|
|
Packit |
ea1746 |
EXPECT_NEAR((dense.block(3, 3, 4, 4) - 2 * 2 * Matrix::Ones(4, 4)).norm(),
|
|
Packit |
ea1746 |
0.0,
|
|
Packit |
ea1746 |
kTolerance);
|
|
Packit |
ea1746 |
// (1, 2)
|
|
Packit |
ea1746 |
EXPECT_NEAR((dense.block(3, 3 + 4, 4, 5) - 2 * 3 * Matrix::Ones(4, 5)).norm(),
|
|
Packit |
ea1746 |
0.0,
|
|
Packit |
ea1746 |
kTolerance);
|
|
Packit |
ea1746 |
// (0, 2)
|
|
Packit |
ea1746 |
EXPECT_NEAR((dense.block(0, 3 + 4, 3, 5) - 3 * 1 * Matrix::Ones(3, 5)).norm(),
|
|
Packit |
ea1746 |
0.0,
|
|
Packit |
ea1746 |
kTolerance);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// There is nothing else in the matrix besides these four blocks.
|
|
Packit |
ea1746 |
EXPECT_NEAR(dense.norm(), sqrt(9. + 16. * 16. + 36. * 20. + 9. * 15.),
|
|
Packit |
ea1746 |
kTolerance);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Vector x = Vector::Ones(dense.rows());
|
|
Packit |
ea1746 |
Vector actual_y = Vector::Zero(dense.rows());
|
|
Packit |
ea1746 |
Vector expected_y = Vector::Zero(dense.rows());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
expected_y += dense.selfadjointView<Eigen::Upper>() * x;
|
|
Packit |
ea1746 |
m.SymmetricRightMultiply(x.data(), actual_y.data());
|
|
Packit |
ea1746 |
EXPECT_NEAR((expected_y - actual_y).norm(), 0.0, kTolerance)
|
|
Packit |
ea1746 |
<< "actual: " << actual_y.transpose() << "\n"
|
|
Packit |
ea1746 |
<< "expected: " << expected_y.transpose()
|
|
Packit |
ea1746 |
<< "matrix: \n " << dense;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// IntPairToLong is private, thus this fixture is needed to access and
|
|
Packit |
ea1746 |
// test it.
|
|
Packit |
ea1746 |
class BlockRandomAccessSparseMatrixTest : public ::testing::Test {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
virtual void SetUp() {
|
|
Packit |
ea1746 |
vector<int> blocks;
|
|
Packit |
ea1746 |
blocks.push_back(1);
|
|
Packit |
ea1746 |
set< pair<int, int> > block_pairs;
|
|
Packit |
ea1746 |
block_pairs.insert(make_pair(0, 0));
|
|
Packit |
ea1746 |
m_.reset(new BlockRandomAccessSparseMatrix(blocks, block_pairs));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CheckIntPairToLong(int a, int b) {
|
|
Packit |
ea1746 |
int64 value = m_->IntPairToLong(a, b);
|
|
Packit |
ea1746 |
EXPECT_GT(value, 0) << "Overflow a = " << a << " b = " << b;
|
|
Packit |
ea1746 |
EXPECT_GT(value, a) << "Overflow a = " << a << " b = " << b;
|
|
Packit |
ea1746 |
EXPECT_GT(value, b) << "Overflow a = " << a << " b = " << b;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CheckLongToIntPair() {
|
|
Packit |
ea1746 |
uint64 max_rows = m_->kMaxRowBlocks;
|
|
Packit |
ea1746 |
for (int row = max_rows - 10; row < max_rows; ++row) {
|
|
Packit |
ea1746 |
for (int col = 0; col < 10; ++col) {
|
|
Packit |
ea1746 |
int row_computed;
|
|
Packit |
ea1746 |
int col_computed;
|
|
Packit |
ea1746 |
m_->LongToIntPair(m_->IntPairToLong(row, col),
|
|
Packit |
ea1746 |
&row_computed,
|
|
Packit |
ea1746 |
&col_computed);
|
|
Packit |
ea1746 |
EXPECT_EQ(row, row_computed);
|
|
Packit |
ea1746 |
EXPECT_EQ(col, col_computed);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
scoped_ptr<BlockRandomAccessSparseMatrix> m_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_F(BlockRandomAccessSparseMatrixTest, IntPairToLongOverflow) {
|
|
Packit |
ea1746 |
CheckIntPairToLong(std::numeric_limits<int>::max(),
|
|
Packit |
ea1746 |
std::numeric_limits<int>::max());
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_F(BlockRandomAccessSparseMatrixTest, LongToIntPair) {
|
|
Packit |
ea1746 |
CheckLongToIntPair();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|