blob: ef5dae9d87d7489d1118218835dd434944ddf766 [file] [log] [blame]
Angus Kong0ae28bd2013-02-13 14:56:04 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
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.
16//
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.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30
31#include "ceres/partitioned_matrix_view.h"
32
33#include <vector>
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"
43
44namespace ceres {
45namespace internal {
46
47const double kEpsilon = 1e-14;
48
49class PartitionedMatrixViewTest : public ::testing::Test {
50 protected :
51 virtual void SetUp() {
Carlos Hernandez79397c22014-08-07 17:51:38 -070052 srand(5);
Angus Kong0ae28bd2013-02-13 14:56:04 -080053 scoped_ptr<LinearLeastSquaresProblem> problem(
54 CreateLinearLeastSquaresProblemFromId(2));
55 CHECK_NOTNULL(problem.get());
56 A_.reset(problem->A.release());
57
58 num_cols_ = A_->num_cols();
59 num_rows_ = A_->num_rows();
60 num_eliminate_blocks_ = problem->num_eliminate_blocks;
Carlos Hernandez79397c22014-08-07 17:51:38 -070061 LinearSolver::Options options;
62 options.elimination_groups.push_back(num_eliminate_blocks_);
63 pmv_.reset(PartitionedMatrixViewBase::Create(
64 options,
65 *down_cast<BlockSparseMatrix*>(A_.get())));
Angus Kong0ae28bd2013-02-13 14:56:04 -080066 }
67
68 int num_rows_;
69 int num_cols_;
70 int num_eliminate_blocks_;
Angus Kong0ae28bd2013-02-13 14:56:04 -080071 scoped_ptr<SparseMatrix> A_;
Carlos Hernandez79397c22014-08-07 17:51:38 -070072 scoped_ptr<PartitionedMatrixViewBase> pmv_;
Angus Kong0ae28bd2013-02-13 14:56:04 -080073};
74
75TEST_F(PartitionedMatrixViewTest, DimensionsTest) {
Carlos Hernandez79397c22014-08-07 17:51:38 -070076 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());
Angus Kong0ae28bd2013-02-13 14:56:04 -080082}
83
84TEST_F(PartitionedMatrixViewTest, RightMultiplyE) {
Carlos Hernandez79397c22014-08-07 17:51:38 -070085 Vector x1(pmv_->num_cols_e());
86 Vector x2(pmv_->num_cols());
Angus Kong0ae28bd2013-02-13 14:56:04 -080087 x2.setZero();
88
Carlos Hernandez79397c22014-08-07 17:51:38 -070089 for (int i = 0; i < pmv_->num_cols_e(); ++i) {
Angus Kong0ae28bd2013-02-13 14:56:04 -080090 x1(i) = x2(i) = RandDouble();
91 }
92
Carlos Hernandez79397c22014-08-07 17:51:38 -070093 Vector y1 = Vector::Zero(pmv_->num_rows());
94 pmv_->RightMultiplyE(x1.data(), y1.data());
Angus Kong0ae28bd2013-02-13 14:56:04 -080095
Carlos Hernandez79397c22014-08-07 17:51:38 -070096 Vector y2 = Vector::Zero(pmv_->num_rows());
Angus Kong0ae28bd2013-02-13 14:56:04 -080097 A_->RightMultiply(x2.data(), y2.data());
98
Carlos Hernandez79397c22014-08-07 17:51:38 -070099 for (int i = 0; i < pmv_->num_rows(); ++i) {
Angus Kong0ae28bd2013-02-13 14:56:04 -0800100 EXPECT_NEAR(y1(i), y2(i), kEpsilon);
101 }
102}
103
104TEST_F(PartitionedMatrixViewTest, RightMultiplyF) {
Carlos Hernandez79397c22014-08-07 17:51:38 -0700105 Vector x1(pmv_->num_cols_f());
106 Vector x2 = Vector::Zero(pmv_->num_cols());
Angus Kong0ae28bd2013-02-13 14:56:04 -0800107
Carlos Hernandez79397c22014-08-07 17:51:38 -0700108 for (int i = 0; i < pmv_->num_cols_f(); ++i) {
Angus Kong0ae28bd2013-02-13 14:56:04 -0800109 x1(i) = RandDouble();
Carlos Hernandez79397c22014-08-07 17:51:38 -0700110 x2(i + pmv_->num_cols_e()) = x1(i);
Angus Kong0ae28bd2013-02-13 14:56:04 -0800111 }
112
Carlos Hernandez79397c22014-08-07 17:51:38 -0700113 Vector y1 = Vector::Zero(pmv_->num_rows());
114 pmv_->RightMultiplyF(x1.data(), y1.data());
Angus Kong0ae28bd2013-02-13 14:56:04 -0800115
Carlos Hernandez79397c22014-08-07 17:51:38 -0700116 Vector y2 = Vector::Zero(pmv_->num_rows());
Angus Kong0ae28bd2013-02-13 14:56:04 -0800117 A_->RightMultiply(x2.data(), y2.data());
118
Carlos Hernandez79397c22014-08-07 17:51:38 -0700119 for (int i = 0; i < pmv_->num_rows(); ++i) {
Angus Kong0ae28bd2013-02-13 14:56:04 -0800120 EXPECT_NEAR(y1(i), y2(i), kEpsilon);
121 }
122}
123
124TEST_F(PartitionedMatrixViewTest, LeftMultiply) {
Carlos Hernandez79397c22014-08-07 17:51:38 -0700125 Vector x = Vector::Zero(pmv_->num_rows());
126 for (int i = 0; i < pmv_->num_rows(); ++i) {
Angus Kong0ae28bd2013-02-13 14:56:04 -0800127 x(i) = RandDouble();
128 }
129
Carlos Hernandez79397c22014-08-07 17:51:38 -0700130 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());
Angus Kong0ae28bd2013-02-13 14:56:04 -0800133
134 A_->LeftMultiply(x.data(), y.data());
Carlos Hernandez79397c22014-08-07 17:51:38 -0700135 pmv_->LeftMultiplyE(x.data(), y1.data());
136 pmv_->LeftMultiplyF(x.data(), y2.data());
Angus Kong0ae28bd2013-02-13 14:56:04 -0800137
Carlos Hernandez79397c22014-08-07 17:51:38 -0700138 for (int i = 0; i < pmv_->num_cols(); ++i) {
Angus Kong0ae28bd2013-02-13 14:56:04 -0800139 EXPECT_NEAR(y(i),
Carlos Hernandez79397c22014-08-07 17:51:38 -0700140 (i < pmv_->num_cols_e()) ? y1(i) : y2(i - pmv_->num_cols_e()),
Angus Kong0ae28bd2013-02-13 14:56:04 -0800141 kEpsilon);
142 }
143}
144
145TEST_F(PartitionedMatrixViewTest, BlockDiagonalEtE) {
Angus Kong0ae28bd2013-02-13 14:56:04 -0800146 scoped_ptr<BlockSparseMatrix>
Carlos Hernandez79397c22014-08-07 17:51:38 -0700147 block_diagonal_ee(pmv_->CreateBlockDiagonalEtE());
Angus Kong0ae28bd2013-02-13 14:56:04 -0800148 const CompressedRowBlockStructure* bs = block_diagonal_ee->block_structure();
149
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);
154
155 EXPECT_NEAR(block_diagonal_ee->values()[0], 10.0, kEpsilon);
156 EXPECT_NEAR(block_diagonal_ee->values()[1], 155.0, kEpsilon);
157}
158
159TEST_F(PartitionedMatrixViewTest, BlockDiagonalFtF) {
Angus Kong0ae28bd2013-02-13 14:56:04 -0800160 scoped_ptr<BlockSparseMatrix>
Carlos Hernandez79397c22014-08-07 17:51:38 -0700161 block_diagonal_ff(pmv_->CreateBlockDiagonalFtF());
Angus Kong0ae28bd2013-02-13 14:56:04 -0800162 const CompressedRowBlockStructure* bs = block_diagonal_ff->block_structure();
163
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);
171}
172
173} // namespace internal
174} // namespace ceres