Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2013 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 <limits> |
| 32 | #include <vector> |
| 33 | |
| 34 | #include "ceres/block_random_access_diagonal_matrix.h" |
| 35 | #include "ceres/internal/eigen.h" |
| 36 | #include "glog/logging.h" |
| 37 | #include "gtest/gtest.h" |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 38 | #include "Eigen/Cholesky" |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 39 | |
| 40 | namespace ceres { |
| 41 | namespace internal { |
| 42 | |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 43 | class BlockRandomAccessDiagonalMatrixTest : public ::testing::Test { |
| 44 | public: |
| 45 | void SetUp() { |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 46 | std::vector<int> blocks; |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 47 | blocks.push_back(3); |
| 48 | blocks.push_back(4); |
| 49 | blocks.push_back(5); |
| 50 | const int num_rows = 3 + 4 + 5; |
| 51 | num_nonzeros_ = 3 * 3 + 4 * 4 + 5 * 5; |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 52 | |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 53 | m_.reset(new BlockRandomAccessDiagonalMatrix(blocks)); |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 54 | |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 55 | EXPECT_EQ(m_->num_rows(), num_rows); |
| 56 | EXPECT_EQ(m_->num_cols(), num_rows); |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 57 | |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 58 | for (int i = 0; i < blocks.size(); ++i) { |
| 59 | const int row_block_id = i; |
| 60 | int col_block_id; |
| 61 | int row; |
| 62 | int col; |
| 63 | int row_stride; |
| 64 | int col_stride; |
| 65 | |
| 66 | for (int j = 0; j < blocks.size(); ++j) { |
| 67 | col_block_id = j; |
| 68 | CellInfo* cell = m_->GetCell(row_block_id, col_block_id, |
| 69 | &row, &col, |
| 70 | &row_stride, &col_stride); |
| 71 | // Off diagonal entries are not present. |
| 72 | if (i != j) { |
| 73 | EXPECT_TRUE(cell == NULL); |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | EXPECT_TRUE(cell != NULL); |
| 78 | EXPECT_EQ(row, 0); |
| 79 | EXPECT_EQ(col, 0); |
| 80 | EXPECT_EQ(row_stride, blocks[row_block_id]); |
| 81 | EXPECT_EQ(col_stride, blocks[col_block_id]); |
| 82 | |
| 83 | // Write into the block |
| 84 | MatrixRef(cell->values, row_stride, col_stride).block( |
| 85 | row, col, blocks[row_block_id], blocks[col_block_id]) = |
| 86 | (row_block_id + 1) * (col_block_id +1) * |
| 87 | Matrix::Ones(blocks[row_block_id], blocks[col_block_id]) |
| 88 | + Matrix::Identity(blocks[row_block_id], blocks[row_block_id]); |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 89 | } |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 93 | protected: |
| 94 | int num_nonzeros_; |
| 95 | scoped_ptr<BlockRandomAccessDiagonalMatrix> m_; |
| 96 | }; |
| 97 | |
| 98 | TEST_F(BlockRandomAccessDiagonalMatrixTest, MatrixContents) { |
| 99 | const TripletSparseMatrix* tsm = m_->matrix(); |
| 100 | EXPECT_EQ(tsm->num_nonzeros(), num_nonzeros_); |
| 101 | EXPECT_EQ(tsm->max_num_nonzeros(), num_nonzeros_); |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 102 | |
| 103 | Matrix dense; |
| 104 | tsm->ToDenseMatrix(&dense); |
| 105 | |
| 106 | double kTolerance = 1e-14; |
| 107 | |
| 108 | // (0,0) |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 109 | EXPECT_NEAR((dense.block(0, 0, 3, 3) - |
| 110 | (Matrix::Ones(3, 3) + Matrix::Identity(3, 3))).norm(), |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 111 | 0.0, |
| 112 | kTolerance); |
| 113 | |
| 114 | // (1,1) |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 115 | EXPECT_NEAR((dense.block(3, 3, 4, 4) - |
| 116 | (2 * 2 * Matrix::Ones(4, 4) + Matrix::Identity(4, 4))).norm(), |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 117 | 0.0, |
| 118 | kTolerance); |
| 119 | |
| 120 | // (1,1) |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 121 | EXPECT_NEAR((dense.block(7, 7, 5, 5) - |
| 122 | (3 * 3 * Matrix::Ones(5, 5) + Matrix::Identity(5, 5))).norm(), |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 123 | 0.0, |
| 124 | kTolerance); |
| 125 | |
| 126 | // There is nothing else in the matrix besides these four blocks. |
Sameer Agarwal | 4ad9149 | 2014-09-24 23:54:18 -0700 | [diff] [blame] | 127 | EXPECT_NEAR(dense.norm(), |
| 128 | sqrt(6 * 1.0 + 3 * 4.0 + |
| 129 | 12 * 16.0 + 4 * 25.0 + |
| 130 | 20 * 81.0 + 5 * 100.0), kTolerance); |
| 131 | } |
| 132 | |
| 133 | TEST_F(BlockRandomAccessDiagonalMatrixTest, RightMultiply) { |
| 134 | double kTolerance = 1e-14; |
| 135 | const TripletSparseMatrix* tsm = m_->matrix(); |
| 136 | Matrix dense; |
| 137 | tsm->ToDenseMatrix(&dense); |
| 138 | Vector x = Vector::Random(dense.rows()); |
| 139 | Vector expected_y = dense * x; |
| 140 | Vector actual_y = Vector::Zero(dense.rows()); |
| 141 | m_->RightMultiply(x.data(), actual_y.data()); |
| 142 | EXPECT_NEAR((expected_y - actual_y).norm(), 0, kTolerance); |
| 143 | } |
| 144 | |
| 145 | TEST_F(BlockRandomAccessDiagonalMatrixTest, Invert) { |
| 146 | double kTolerance = 1e-14; |
| 147 | const TripletSparseMatrix* tsm = m_->matrix(); |
| 148 | Matrix dense; |
| 149 | tsm->ToDenseMatrix(&dense); |
| 150 | Matrix expected_inverse = |
| 151 | dense.llt().solve(Matrix::Identity(dense.rows(), dense.rows())); |
| 152 | |
| 153 | m_->Invert(); |
| 154 | tsm->ToDenseMatrix(&dense); |
| 155 | |
| 156 | EXPECT_NEAR((expected_inverse - dense).norm(), 0.0, kTolerance); |
Sameer Agarwal | 0e2743e | 2013-10-23 14:51:07 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } // namespace internal |
| 160 | } // namespace ceres |