Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 34 | #include "ceres/block_structure.h" |
| 35 | #include "ceres/casts.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 36 | #include "ceres/internal/eigen.h" |
| 37 | #include "ceres/internal/scoped_ptr.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 38 | #include "ceres/linear_least_squares_problems.h" |
| 39 | #include "ceres/random.h" |
| 40 | #include "ceres/sparse_matrix.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 41 | #include "glog/logging.h" |
| 42 | #include "gtest/gtest.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 43 | |
| 44 | namespace ceres { |
| 45 | namespace internal { |
| 46 | |
| 47 | const double kEpsilon = 1e-14; |
| 48 | |
| 49 | class PartitionedMatrixViewTest : public ::testing::Test { |
| 50 | protected : |
| 51 | virtual void SetUp() { |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 52 | srand(5); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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; |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 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()))); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int num_rows_; |
| 69 | int num_cols_; |
| 70 | int num_eliminate_blocks_; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 71 | scoped_ptr<SparseMatrix> A_; |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 72 | scoped_ptr<PartitionedMatrixViewBase> pmv_; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | TEST_F(PartitionedMatrixViewTest, DimensionsTest) { |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 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()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST_F(PartitionedMatrixViewTest, RightMultiplyE) { |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 85 | Vector x1(pmv_->num_cols_e()); |
| 86 | Vector x2(pmv_->num_cols()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 87 | x2.setZero(); |
| 88 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 89 | for (int i = 0; i < pmv_->num_cols_e(); ++i) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 90 | x1(i) = x2(i) = RandDouble(); |
| 91 | } |
| 92 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 93 | Vector y1 = Vector::Zero(pmv_->num_rows()); |
| 94 | pmv_->RightMultiplyE(x1.data(), y1.data()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 95 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 96 | Vector y2 = Vector::Zero(pmv_->num_rows()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 97 | A_->RightMultiply(x2.data(), y2.data()); |
| 98 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 99 | for (int i = 0; i < pmv_->num_rows(); ++i) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 100 | EXPECT_NEAR(y1(i), y2(i), kEpsilon); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | TEST_F(PartitionedMatrixViewTest, RightMultiplyF) { |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 105 | Vector x1(pmv_->num_cols_f()); |
| 106 | Vector x2 = Vector::Zero(pmv_->num_cols()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 107 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 108 | for (int i = 0; i < pmv_->num_cols_f(); ++i) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 109 | x1(i) = RandDouble(); |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 110 | x2(i + pmv_->num_cols_e()) = x1(i); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 113 | Vector y1 = Vector::Zero(pmv_->num_rows()); |
| 114 | pmv_->RightMultiplyF(x1.data(), y1.data()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 115 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 116 | Vector y2 = Vector::Zero(pmv_->num_rows()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 117 | A_->RightMultiply(x2.data(), y2.data()); |
| 118 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 119 | for (int i = 0; i < pmv_->num_rows(); ++i) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 120 | EXPECT_NEAR(y1(i), y2(i), kEpsilon); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | TEST_F(PartitionedMatrixViewTest, LeftMultiply) { |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 125 | Vector x = Vector::Zero(pmv_->num_rows()); |
| 126 | for (int i = 0; i < pmv_->num_rows(); ++i) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 127 | x(i) = RandDouble(); |
| 128 | } |
| 129 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 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()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 133 | |
| 134 | A_->LeftMultiply(x.data(), y.data()); |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 135 | pmv_->LeftMultiplyE(x.data(), y1.data()); |
| 136 | pmv_->LeftMultiplyF(x.data(), y2.data()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 137 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 138 | for (int i = 0; i < pmv_->num_cols(); ++i) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 139 | EXPECT_NEAR(y(i), |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 140 | (i < pmv_->num_cols_e()) ? y1(i) : y2(i - pmv_->num_cols_e()), |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 141 | kEpsilon); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | TEST_F(PartitionedMatrixViewTest, BlockDiagonalEtE) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 146 | scoped_ptr<BlockSparseMatrix> |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 147 | block_diagonal_ee(pmv_->CreateBlockDiagonalEtE()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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 | |
| 159 | TEST_F(PartitionedMatrixViewTest, BlockDiagonalFtF) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 160 | scoped_ptr<BlockSparseMatrix> |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 161 | block_diagonal_ff(pmv_->CreateBlockDiagonalFtF()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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 |