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/block_sparse_matrix.h"
32
33 #include <string>
34 #include "ceres/casts.h"
35 #include "ceres/internal/eigen.h"
36 #include "ceres/internal/scoped_ptr.h"
37 #include "ceres/linear_least_squares_problems.h"
38 #include "ceres/triplet_sparse_matrix.h"
39 #include "glog/logging.h"
40 #include "gtest/gtest.h"
41
42 namespace ceres {
43 namespace internal {
44
45 class BlockSparseMatrixTest : public ::testing::Test {
46 protected :
SetUp()47 virtual void SetUp() {
48 scoped_ptr<LinearLeastSquaresProblem> problem(
49 CreateLinearLeastSquaresProblemFromId(2));
50 CHECK_NOTNULL(problem.get());
51 A_.reset(down_cast<BlockSparseMatrix*>(problem->A.release()));
52
53 problem.reset(CreateLinearLeastSquaresProblemFromId(1));
54 CHECK_NOTNULL(problem.get());
55 B_.reset(down_cast<TripletSparseMatrix*>(problem->A.release()));
56
57 CHECK_EQ(A_->num_rows(), B_->num_rows());
58 CHECK_EQ(A_->num_cols(), B_->num_cols());
59 CHECK_EQ(A_->num_nonzeros(), B_->num_nonzeros());
60 }
61
62 scoped_ptr<BlockSparseMatrix> A_;
63 scoped_ptr<TripletSparseMatrix> B_;
64 };
65
TEST_F(BlockSparseMatrixTest,SetZeroTest)66 TEST_F(BlockSparseMatrixTest, SetZeroTest) {
67 A_->SetZero();
68 EXPECT_EQ(13, A_->num_nonzeros());
69 }
70
TEST_F(BlockSparseMatrixTest,RightMultiplyTest)71 TEST_F(BlockSparseMatrixTest, RightMultiplyTest) {
72 Vector y_a = Vector::Zero(A_->num_rows());
73 Vector y_b = Vector::Zero(A_->num_rows());
74 for (int i = 0; i < A_->num_cols(); ++i) {
75 Vector x = Vector::Zero(A_->num_cols());
76 x[i] = 1.0;
77 A_->RightMultiply(x.data(), y_a.data());
78 B_->RightMultiply(x.data(), y_b.data());
79 EXPECT_LT((y_a - y_b).norm(), 1e-12);
80 }
81 }
82
TEST_F(BlockSparseMatrixTest,LeftMultiplyTest)83 TEST_F(BlockSparseMatrixTest, LeftMultiplyTest) {
84 Vector y_a = Vector::Zero(A_->num_cols());
85 Vector y_b = Vector::Zero(A_->num_cols());
86 for (int i = 0; i < A_->num_rows(); ++i) {
87 Vector x = Vector::Zero(A_->num_rows());
88 x[i] = 1.0;
89 A_->LeftMultiply(x.data(), y_a.data());
90 B_->LeftMultiply(x.data(), y_b.data());
91 EXPECT_LT((y_a - y_b).norm(), 1e-12);
92 }
93 }
94
TEST_F(BlockSparseMatrixTest,SquaredColumnNormTest)95 TEST_F(BlockSparseMatrixTest, SquaredColumnNormTest) {
96 Vector y_a = Vector::Zero(A_->num_cols());
97 Vector y_b = Vector::Zero(A_->num_cols());
98 A_->SquaredColumnNorm(y_a.data());
99 B_->SquaredColumnNorm(y_b.data());
100 EXPECT_LT((y_a - y_b).norm(), 1e-12);
101 }
102
TEST_F(BlockSparseMatrixTest,ToDenseMatrixTest)103 TEST_F(BlockSparseMatrixTest, ToDenseMatrixTest) {
104 Matrix m_a;
105 Matrix m_b;
106 A_->ToDenseMatrix(&m_a);
107 B_->ToDenseMatrix(&m_b);
108 EXPECT_LT((m_a - m_b).norm(), 1e-12);
109 }
110
111 } // namespace internal
112 } // namespace ceres
113