• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
44 namespace ceres {
45 namespace internal {
46 
47 const double kEpsilon = 1e-14;
48 
49 class PartitionedMatrixViewTest : public ::testing::Test {
50  protected :
SetUp()51   virtual void SetUp() {
52     srand(5);
53     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;
61     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())));
66   }
67 
68   int num_rows_;
69   int num_cols_;
70   int num_eliminate_blocks_;
71   scoped_ptr<SparseMatrix> A_;
72   scoped_ptr<PartitionedMatrixViewBase> pmv_;
73 };
74 
TEST_F(PartitionedMatrixViewTest,DimensionsTest)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());
82 }
83 
TEST_F(PartitionedMatrixViewTest,RightMultiplyE)84 TEST_F(PartitionedMatrixViewTest, RightMultiplyE) {
85   Vector x1(pmv_->num_cols_e());
86   Vector x2(pmv_->num_cols());
87   x2.setZero();
88 
89   for (int i = 0; i < pmv_->num_cols_e(); ++i) {
90     x1(i) = x2(i) = RandDouble();
91   }
92 
93   Vector y1 = Vector::Zero(pmv_->num_rows());
94   pmv_->RightMultiplyE(x1.data(), y1.data());
95 
96   Vector y2 = Vector::Zero(pmv_->num_rows());
97   A_->RightMultiply(x2.data(), y2.data());
98 
99   for (int i = 0; i < pmv_->num_rows(); ++i) {
100     EXPECT_NEAR(y1(i), y2(i), kEpsilon);
101   }
102 }
103 
TEST_F(PartitionedMatrixViewTest,RightMultiplyF)104 TEST_F(PartitionedMatrixViewTest, RightMultiplyF) {
105   Vector x1(pmv_->num_cols_f());
106   Vector x2 = Vector::Zero(pmv_->num_cols());
107 
108   for (int i = 0; i < pmv_->num_cols_f(); ++i) {
109     x1(i) = RandDouble();
110     x2(i + pmv_->num_cols_e()) = x1(i);
111   }
112 
113   Vector y1 = Vector::Zero(pmv_->num_rows());
114   pmv_->RightMultiplyF(x1.data(), y1.data());
115 
116   Vector y2 = Vector::Zero(pmv_->num_rows());
117   A_->RightMultiply(x2.data(), y2.data());
118 
119   for (int i = 0; i < pmv_->num_rows(); ++i) {
120     EXPECT_NEAR(y1(i), y2(i), kEpsilon);
121   }
122 }
123 
TEST_F(PartitionedMatrixViewTest,LeftMultiply)124 TEST_F(PartitionedMatrixViewTest, LeftMultiply) {
125   Vector x = Vector::Zero(pmv_->num_rows());
126   for (int i = 0; i < pmv_->num_rows(); ++i) {
127     x(i) = RandDouble();
128   }
129 
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());
133 
134   A_->LeftMultiply(x.data(), y.data());
135   pmv_->LeftMultiplyE(x.data(), y1.data());
136   pmv_->LeftMultiplyF(x.data(), y2.data());
137 
138   for (int i = 0; i < pmv_->num_cols(); ++i) {
139     EXPECT_NEAR(y(i),
140                 (i < pmv_->num_cols_e()) ? y1(i) : y2(i - pmv_->num_cols_e()),
141                 kEpsilon);
142   }
143 }
144 
TEST_F(PartitionedMatrixViewTest,BlockDiagonalEtE)145 TEST_F(PartitionedMatrixViewTest, BlockDiagonalEtE) {
146   scoped_ptr<BlockSparseMatrix>
147       block_diagonal_ee(pmv_->CreateBlockDiagonalEtE());
148   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 
TEST_F(PartitionedMatrixViewTest,BlockDiagonalFtF)159 TEST_F(PartitionedMatrixViewTest, BlockDiagonalFtF) {
160   scoped_ptr<BlockSparseMatrix>
161       block_diagonal_ff(pmv_->CreateBlockDiagonalFtF());
162   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
175