• 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     scoped_ptr<LinearLeastSquaresProblem> problem(
53         CreateLinearLeastSquaresProblemFromId(2));
54     CHECK_NOTNULL(problem.get());
55     A_.reset(problem->A.release());
56 
57     num_cols_ = A_->num_cols();
58     num_rows_ = A_->num_rows();
59     num_eliminate_blocks_ = problem->num_eliminate_blocks;
60   }
61 
62   int num_rows_;
63   int num_cols_;
64   int num_eliminate_blocks_;
65 
66   scoped_ptr<SparseMatrix> A_;
67 };
68 
TEST_F(PartitionedMatrixViewTest,DimensionsTest)69 TEST_F(PartitionedMatrixViewTest, DimensionsTest) {
70   PartitionedMatrixView m(*down_cast<BlockSparseMatrix*>(A_.get()),
71                           num_eliminate_blocks_);
72   EXPECT_EQ(m.num_col_blocks_e(), num_eliminate_blocks_);
73   EXPECT_EQ(m.num_col_blocks_f(), num_cols_ - num_eliminate_blocks_);
74   EXPECT_EQ(m.num_cols_e(), num_eliminate_blocks_);
75   EXPECT_EQ(m.num_cols_f(), num_cols_ - num_eliminate_blocks_);
76   EXPECT_EQ(m.num_cols(), A_->num_cols());
77   EXPECT_EQ(m.num_rows(), A_->num_rows());
78 }
79 
TEST_F(PartitionedMatrixViewTest,RightMultiplyE)80 TEST_F(PartitionedMatrixViewTest, RightMultiplyE) {
81   PartitionedMatrixView m(*down_cast<BlockSparseMatrix*>(A_.get()),
82                           num_eliminate_blocks_);
83 
84   srand(5);
85 
86   Vector x1(m.num_cols_e());
87   Vector x2(m.num_cols());
88   x2.setZero();
89 
90   for (int i = 0; i < m.num_cols_e(); ++i) {
91     x1(i) = x2(i) = RandDouble();
92   }
93 
94   Vector y1 = Vector::Zero(m.num_rows());
95   m.RightMultiplyE(x1.data(), y1.data());
96 
97   Vector y2 = Vector::Zero(m.num_rows());
98   A_->RightMultiply(x2.data(), y2.data());
99 
100   for (int i = 0; i < m.num_rows(); ++i) {
101     EXPECT_NEAR(y1(i), y2(i), kEpsilon);
102   }
103 }
104 
TEST_F(PartitionedMatrixViewTest,RightMultiplyF)105 TEST_F(PartitionedMatrixViewTest, RightMultiplyF) {
106   PartitionedMatrixView m(*down_cast<BlockSparseMatrix*>(A_.get()),
107                           num_eliminate_blocks_);
108 
109   srand(5);
110 
111   Vector x1(m.num_cols_f());
112   Vector x2 = Vector::Zero(m.num_cols());
113 
114   for (int i = 0; i < m.num_cols_f(); ++i) {
115     x1(i) = RandDouble();
116     x2(i + m.num_cols_e()) = x1(i);
117   }
118 
119   Vector y1 = Vector::Zero(m.num_rows());
120   m.RightMultiplyF(x1.data(), y1.data());
121 
122   Vector y2 = Vector::Zero(m.num_rows());
123   A_->RightMultiply(x2.data(), y2.data());
124 
125   for (int i = 0; i < m.num_rows(); ++i) {
126     EXPECT_NEAR(y1(i), y2(i), kEpsilon);
127   }
128 }
129 
TEST_F(PartitionedMatrixViewTest,LeftMultiply)130 TEST_F(PartitionedMatrixViewTest, LeftMultiply) {
131   PartitionedMatrixView m(*down_cast<BlockSparseMatrix*>(A_.get()),
132                           num_eliminate_blocks_);
133 
134   srand(5);
135 
136   Vector x = Vector::Zero(m.num_rows());
137   for (int i = 0; i < m.num_rows(); ++i) {
138     x(i) = RandDouble();
139   }
140 
141   Vector y = Vector::Zero(m.num_cols());
142   Vector y1 = Vector::Zero(m.num_cols_e());
143   Vector y2 = Vector::Zero(m.num_cols_f());
144 
145   A_->LeftMultiply(x.data(), y.data());
146   m.LeftMultiplyE(x.data(), y1.data());
147   m.LeftMultiplyF(x.data(), y2.data());
148 
149   for (int i = 0; i < m.num_cols(); ++i) {
150     EXPECT_NEAR(y(i),
151                 (i < m.num_cols_e()) ? y1(i) : y2(i - m.num_cols_e()),
152                 kEpsilon);
153   }
154 }
155 
TEST_F(PartitionedMatrixViewTest,BlockDiagonalEtE)156 TEST_F(PartitionedMatrixViewTest, BlockDiagonalEtE) {
157   PartitionedMatrixView m(*down_cast<BlockSparseMatrix*>(A_.get()),
158                           num_eliminate_blocks_);
159 
160   scoped_ptr<BlockSparseMatrix>
161       block_diagonal_ee(m.CreateBlockDiagonalEtE());
162   const CompressedRowBlockStructure* bs  = block_diagonal_ee->block_structure();
163 
164   EXPECT_EQ(block_diagonal_ee->num_rows(), 2);
165   EXPECT_EQ(block_diagonal_ee->num_cols(), 2);
166   EXPECT_EQ(bs->cols.size(), 2);
167   EXPECT_EQ(bs->rows.size(), 2);
168 
169   EXPECT_NEAR(block_diagonal_ee->values()[0], 10.0, kEpsilon);
170   EXPECT_NEAR(block_diagonal_ee->values()[1], 155.0, kEpsilon);
171 }
172 
TEST_F(PartitionedMatrixViewTest,BlockDiagonalFtF)173 TEST_F(PartitionedMatrixViewTest, BlockDiagonalFtF) {
174   PartitionedMatrixView m(*down_cast<BlockSparseMatrix*>(A_.get()),
175                           num_eliminate_blocks_);
176 
177   scoped_ptr<BlockSparseMatrix>
178       block_diagonal_ff(m.CreateBlockDiagonalFtF());
179   const CompressedRowBlockStructure* bs  = block_diagonal_ff->block_structure();
180 
181   EXPECT_EQ(block_diagonal_ff->num_rows(), 3);
182   EXPECT_EQ(block_diagonal_ff->num_cols(), 3);
183   EXPECT_EQ(bs->cols.size(), 3);
184   EXPECT_EQ(bs->rows.size(), 3);
185   EXPECT_NEAR(block_diagonal_ff->values()[0], 70.0, kEpsilon);
186   EXPECT_NEAR(block_diagonal_ff->values()[1], 17.0, kEpsilon);
187   EXPECT_NEAR(block_diagonal_ff->values()[2], 37.0, kEpsilon);
188 }
189 
190 }  // namespace internal
191 }  // namespace ceres
192