1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
31 #include "ceres/partitioned_matrix_view.h"
34 #include "ceres/block_structure.h"
35 #include "ceres/casts.h"
36 #include "ceres/internal/eigen.h"
37 #include "ceres/internal/scoped_ptr.h"
38 #include "ceres/linear_least_squares_problems.h"
39 #include "ceres/random.h"
40 #include "ceres/sparse_matrix.h"
41 #include "glog/logging.h"
42 #include "gtest/gtest.h"
47 const double kEpsilon = 1e-14;
49 class PartitionedMatrixViewTest : public ::testing::Test {
51 virtual void SetUp() {
53 scoped_ptr<LinearLeastSquaresProblem> problem(
54 CreateLinearLeastSquaresProblemFromId(2));
55 CHECK_NOTNULL(problem.get());
56 A_.reset(problem->A.release());
58 num_cols_ = A_->num_cols();
59 num_rows_ = A_->num_rows();
60 num_eliminate_blocks_ = problem->num_eliminate_blocks;
61 LinearSolver::Options options;
62 options.elimination_groups.push_back(num_eliminate_blocks_);
63 pmv_.reset(PartitionedMatrixViewBase::Create(
65 *down_cast<BlockSparseMatrix*>(A_.get())));
70 int num_eliminate_blocks_;
71 scoped_ptr<SparseMatrix> A_;
72 scoped_ptr<PartitionedMatrixViewBase> pmv_;
75 TEST_F(PartitionedMatrixViewTest, DimensionsTest) {
76 EXPECT_EQ(pmv_->num_col_blocks_e(), num_eliminate_blocks_);
77 EXPECT_EQ(pmv_->num_col_blocks_f(), num_cols_ - num_eliminate_blocks_);
78 EXPECT_EQ(pmv_->num_cols_e(), num_eliminate_blocks_);
79 EXPECT_EQ(pmv_->num_cols_f(), num_cols_ - num_eliminate_blocks_);
80 EXPECT_EQ(pmv_->num_cols(), A_->num_cols());
81 EXPECT_EQ(pmv_->num_rows(), A_->num_rows());
84 TEST_F(PartitionedMatrixViewTest, RightMultiplyE) {
85 Vector x1(pmv_->num_cols_e());
86 Vector x2(pmv_->num_cols());
89 for (int i = 0; i < pmv_->num_cols_e(); ++i) {
90 x1(i) = x2(i) = RandDouble();
93 Vector y1 = Vector::Zero(pmv_->num_rows());
94 pmv_->RightMultiplyE(x1.data(), y1.data());
96 Vector y2 = Vector::Zero(pmv_->num_rows());
97 A_->RightMultiply(x2.data(), y2.data());
99 for (int i = 0; i < pmv_->num_rows(); ++i) {
100 EXPECT_NEAR(y1(i), y2(i), kEpsilon);
104 TEST_F(PartitionedMatrixViewTest, RightMultiplyF) {
105 Vector x1(pmv_->num_cols_f());
106 Vector x2 = Vector::Zero(pmv_->num_cols());
108 for (int i = 0; i < pmv_->num_cols_f(); ++i) {
109 x1(i) = RandDouble();
110 x2(i + pmv_->num_cols_e()) = x1(i);
113 Vector y1 = Vector::Zero(pmv_->num_rows());
114 pmv_->RightMultiplyF(x1.data(), y1.data());
116 Vector y2 = Vector::Zero(pmv_->num_rows());
117 A_->RightMultiply(x2.data(), y2.data());
119 for (int i = 0; i < pmv_->num_rows(); ++i) {
120 EXPECT_NEAR(y1(i), y2(i), kEpsilon);
124 TEST_F(PartitionedMatrixViewTest, LeftMultiply) {
125 Vector x = Vector::Zero(pmv_->num_rows());
126 for (int i = 0; i < pmv_->num_rows(); ++i) {
130 Vector y = Vector::Zero(pmv_->num_cols());
131 Vector y1 = Vector::Zero(pmv_->num_cols_e());
132 Vector y2 = Vector::Zero(pmv_->num_cols_f());
134 A_->LeftMultiply(x.data(), y.data());
135 pmv_->LeftMultiplyE(x.data(), y1.data());
136 pmv_->LeftMultiplyF(x.data(), y2.data());
138 for (int i = 0; i < pmv_->num_cols(); ++i) {
140 (i < pmv_->num_cols_e()) ? y1(i) : y2(i - pmv_->num_cols_e()),
145 TEST_F(PartitionedMatrixViewTest, BlockDiagonalEtE) {
146 scoped_ptr<BlockSparseMatrix>
147 block_diagonal_ee(pmv_->CreateBlockDiagonalEtE());
148 const CompressedRowBlockStructure* bs = block_diagonal_ee->block_structure();
150 EXPECT_EQ(block_diagonal_ee->num_rows(), 2);
151 EXPECT_EQ(block_diagonal_ee->num_cols(), 2);
152 EXPECT_EQ(bs->cols.size(), 2);
153 EXPECT_EQ(bs->rows.size(), 2);
155 EXPECT_NEAR(block_diagonal_ee->values()[0], 10.0, kEpsilon);
156 EXPECT_NEAR(block_diagonal_ee->values()[1], 155.0, kEpsilon);
159 TEST_F(PartitionedMatrixViewTest, BlockDiagonalFtF) {
160 scoped_ptr<BlockSparseMatrix>
161 block_diagonal_ff(pmv_->CreateBlockDiagonalFtF());
162 const CompressedRowBlockStructure* bs = block_diagonal_ff->block_structure();
164 EXPECT_EQ(block_diagonal_ff->num_rows(), 3);
165 EXPECT_EQ(block_diagonal_ff->num_cols(), 3);
166 EXPECT_EQ(bs->cols.size(), 3);
167 EXPECT_EQ(bs->rows.size(), 3);
168 EXPECT_NEAR(block_diagonal_ff->values()[0], 70.0, kEpsilon);
169 EXPECT_NEAR(block_diagonal_ff->values()[1], 17.0, kEpsilon);
170 EXPECT_NEAR(block_diagonal_ff->values()[2], 37.0, kEpsilon);
173 } // namespace internal