Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
| 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/block_sparse_matrix.h" |
| 32 | |
| 33 | #include <memory> |
| 34 | #include <string> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 35 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include "ceres/casts.h" |
| 37 | #include "ceres/internal/eigen.h" |
| 38 | #include "ceres/linear_least_squares_problems.h" |
| 39 | #include "ceres/triplet_sparse_matrix.h" |
| 40 | #include "glog/logging.h" |
| 41 | #include "gtest/gtest.h" |
| 42 | |
| 43 | namespace ceres { |
| 44 | namespace internal { |
| 45 | |
| 46 | class BlockSparseMatrixTest : public ::testing::Test { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 47 | protected: |
| 48 | void SetUp() final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 49 | std::unique_ptr<LinearLeastSquaresProblem> problem( |
| 50 | CreateLinearLeastSquaresProblemFromId(2)); |
| 51 | CHECK(problem != nullptr); |
| 52 | A_.reset(down_cast<BlockSparseMatrix*>(problem->A.release())); |
| 53 | |
| 54 | problem.reset(CreateLinearLeastSquaresProblemFromId(1)); |
| 55 | CHECK(problem != nullptr); |
| 56 | B_.reset(down_cast<TripletSparseMatrix*>(problem->A.release())); |
| 57 | |
| 58 | CHECK_EQ(A_->num_rows(), B_->num_rows()); |
| 59 | CHECK_EQ(A_->num_cols(), B_->num_cols()); |
| 60 | CHECK_EQ(A_->num_nonzeros(), B_->num_nonzeros()); |
| 61 | } |
| 62 | |
| 63 | std::unique_ptr<BlockSparseMatrix> A_; |
| 64 | std::unique_ptr<TripletSparseMatrix> B_; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(BlockSparseMatrixTest, SetZeroTest) { |
| 68 | A_->SetZero(); |
| 69 | EXPECT_EQ(13, A_->num_nonzeros()); |
| 70 | } |
| 71 | |
| 72 | TEST_F(BlockSparseMatrixTest, RightMultiplyTest) { |
| 73 | Vector y_a = Vector::Zero(A_->num_rows()); |
| 74 | Vector y_b = Vector::Zero(A_->num_rows()); |
| 75 | for (int i = 0; i < A_->num_cols(); ++i) { |
| 76 | Vector x = Vector::Zero(A_->num_cols()); |
| 77 | x[i] = 1.0; |
| 78 | A_->RightMultiply(x.data(), y_a.data()); |
| 79 | B_->RightMultiply(x.data(), y_b.data()); |
| 80 | EXPECT_LT((y_a - y_b).norm(), 1e-12); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | TEST_F(BlockSparseMatrixTest, LeftMultiplyTest) { |
| 85 | Vector y_a = Vector::Zero(A_->num_cols()); |
| 86 | Vector y_b = Vector::Zero(A_->num_cols()); |
| 87 | for (int i = 0; i < A_->num_rows(); ++i) { |
| 88 | Vector x = Vector::Zero(A_->num_rows()); |
| 89 | x[i] = 1.0; |
| 90 | A_->LeftMultiply(x.data(), y_a.data()); |
| 91 | B_->LeftMultiply(x.data(), y_b.data()); |
| 92 | EXPECT_LT((y_a - y_b).norm(), 1e-12); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | TEST_F(BlockSparseMatrixTest, SquaredColumnNormTest) { |
| 97 | Vector y_a = Vector::Zero(A_->num_cols()); |
| 98 | Vector y_b = Vector::Zero(A_->num_cols()); |
| 99 | A_->SquaredColumnNorm(y_a.data()); |
| 100 | B_->SquaredColumnNorm(y_b.data()); |
| 101 | EXPECT_LT((y_a - y_b).norm(), 1e-12); |
| 102 | } |
| 103 | |
| 104 | TEST_F(BlockSparseMatrixTest, ToDenseMatrixTest) { |
| 105 | Matrix m_a; |
| 106 | Matrix m_b; |
| 107 | A_->ToDenseMatrix(&m_a); |
| 108 | B_->ToDenseMatrix(&m_b); |
| 109 | EXPECT_LT((m_a - m_b).norm(), 1e-12); |
| 110 | } |
| 111 | |
| 112 | TEST_F(BlockSparseMatrixTest, AppendRows) { |
| 113 | std::unique_ptr<LinearLeastSquaresProblem> problem( |
| 114 | CreateLinearLeastSquaresProblemFromId(2)); |
| 115 | std::unique_ptr<BlockSparseMatrix> m( |
| 116 | down_cast<BlockSparseMatrix*>(problem->A.release())); |
| 117 | A_->AppendRows(*m); |
| 118 | EXPECT_EQ(A_->num_rows(), 2 * m->num_rows()); |
| 119 | EXPECT_EQ(A_->num_cols(), m->num_cols()); |
| 120 | |
| 121 | problem.reset(CreateLinearLeastSquaresProblemFromId(1)); |
| 122 | std::unique_ptr<TripletSparseMatrix> m2( |
| 123 | down_cast<TripletSparseMatrix*>(problem->A.release())); |
| 124 | B_->AppendRows(*m2); |
| 125 | |
| 126 | Vector y_a = Vector::Zero(A_->num_rows()); |
| 127 | Vector y_b = Vector::Zero(A_->num_rows()); |
| 128 | for (int i = 0; i < A_->num_cols(); ++i) { |
| 129 | Vector x = Vector::Zero(A_->num_cols()); |
| 130 | x[i] = 1.0; |
| 131 | y_a.setZero(); |
| 132 | y_b.setZero(); |
| 133 | |
| 134 | A_->RightMultiply(x.data(), y_a.data()); |
| 135 | B_->RightMultiply(x.data(), y_b.data()); |
| 136 | EXPECT_LT((y_a - y_b).norm(), 1e-12); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | TEST_F(BlockSparseMatrixTest, AppendAndDeleteBlockDiagonalMatrix) { |
| 141 | const std::vector<Block>& column_blocks = A_->block_structure()->cols; |
| 142 | const int num_cols = |
| 143 | column_blocks.back().size + column_blocks.back().position; |
| 144 | Vector diagonal(num_cols); |
| 145 | for (int i = 0; i < num_cols; ++i) { |
| 146 | diagonal(i) = 2 * i * i + 1; |
| 147 | } |
| 148 | std::unique_ptr<BlockSparseMatrix> appendage( |
| 149 | BlockSparseMatrix::CreateDiagonalMatrix(diagonal.data(), column_blocks)); |
| 150 | |
| 151 | A_->AppendRows(*appendage); |
| 152 | Vector y_a, y_b; |
| 153 | y_a.resize(A_->num_rows()); |
| 154 | y_b.resize(A_->num_rows()); |
| 155 | for (int i = 0; i < A_->num_cols(); ++i) { |
| 156 | Vector x = Vector::Zero(A_->num_cols()); |
| 157 | x[i] = 1.0; |
| 158 | y_a.setZero(); |
| 159 | y_b.setZero(); |
| 160 | |
| 161 | A_->RightMultiply(x.data(), y_a.data()); |
| 162 | B_->RightMultiply(x.data(), y_b.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 163 | EXPECT_LT((y_a.head(B_->num_rows()) - y_b.head(B_->num_rows())).norm(), |
| 164 | 1e-12); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 165 | Vector expected_tail = Vector::Zero(A_->num_cols()); |
| 166 | expected_tail(i) = diagonal(i); |
| 167 | EXPECT_LT((y_a.tail(A_->num_cols()) - expected_tail).norm(), 1e-12); |
| 168 | } |
| 169 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 170 | A_->DeleteRowBlocks(column_blocks.size()); |
| 171 | EXPECT_EQ(A_->num_rows(), B_->num_rows()); |
| 172 | EXPECT_EQ(A_->num_cols(), B_->num_cols()); |
| 173 | |
| 174 | y_a.resize(A_->num_rows()); |
| 175 | y_b.resize(A_->num_rows()); |
| 176 | for (int i = 0; i < A_->num_cols(); ++i) { |
| 177 | Vector x = Vector::Zero(A_->num_cols()); |
| 178 | x[i] = 1.0; |
| 179 | y_a.setZero(); |
| 180 | y_b.setZero(); |
| 181 | |
| 182 | A_->RightMultiply(x.data(), y_a.data()); |
| 183 | B_->RightMultiply(x.data(), y_b.data()); |
| 184 | EXPECT_LT((y_a - y_b).norm(), 1e-12); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | TEST(BlockSparseMatrix, CreateDiagonalMatrix) { |
| 189 | std::vector<Block> column_blocks; |
| 190 | column_blocks.push_back(Block(2, 0)); |
| 191 | column_blocks.push_back(Block(1, 2)); |
| 192 | column_blocks.push_back(Block(3, 3)); |
| 193 | const int num_cols = |
| 194 | column_blocks.back().size + column_blocks.back().position; |
| 195 | Vector diagonal(num_cols); |
| 196 | for (int i = 0; i < num_cols; ++i) { |
| 197 | diagonal(i) = 2 * i * i + 1; |
| 198 | } |
| 199 | |
| 200 | std::unique_ptr<BlockSparseMatrix> m( |
| 201 | BlockSparseMatrix::CreateDiagonalMatrix(diagonal.data(), column_blocks)); |
| 202 | const CompressedRowBlockStructure* bs = m->block_structure(); |
| 203 | EXPECT_EQ(bs->cols.size(), column_blocks.size()); |
| 204 | for (int i = 0; i < column_blocks.size(); ++i) { |
| 205 | EXPECT_EQ(bs->cols[i].size, column_blocks[i].size); |
| 206 | EXPECT_EQ(bs->cols[i].position, column_blocks[i].position); |
| 207 | } |
| 208 | EXPECT_EQ(m->num_rows(), m->num_cols()); |
| 209 | Vector x = Vector::Ones(num_cols); |
| 210 | Vector y = Vector::Zero(num_cols); |
| 211 | m->RightMultiply(x.data(), y.data()); |
| 212 | for (int i = 0; i < num_cols; ++i) { |
| 213 | EXPECT_NEAR(y[i], diagonal[i], std::numeric_limits<double>::epsilon()); |
| 214 | } |
| 215 | } |
| 216 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 217 | } // namespace internal |
| 218 | } // namespace ceres |