• 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/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/matrix_proto.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 {
47  protected :
SetUp()48   virtual void SetUp() {
49     scoped_ptr<LinearLeastSquaresProblem> problem(
50         CreateLinearLeastSquaresProblemFromId(2));
51     CHECK_NOTNULL(problem.get());
52     A_.reset(down_cast<BlockSparseMatrix*>(problem->A.release()));
53 
54     problem.reset(CreateLinearLeastSquaresProblemFromId(1));
55     CHECK_NOTNULL(problem.get());
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   scoped_ptr<BlockSparseMatrix> A_;
64   scoped_ptr<TripletSparseMatrix> B_;
65 };
66 
TEST_F(BlockSparseMatrixTest,SetZeroTest)67 TEST_F(BlockSparseMatrixTest, SetZeroTest) {
68   A_->SetZero();
69   EXPECT_EQ(13, A_->num_nonzeros());
70 }
71 
TEST_F(BlockSparseMatrixTest,RightMultiplyTest)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 
TEST_F(BlockSparseMatrixTest,LeftMultiplyTest)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 
TEST_F(BlockSparseMatrixTest,SquaredColumnNormTest)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 
TEST_F(BlockSparseMatrixTest,ToDenseMatrixTest)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 #ifndef CERES_NO_PROTOCOL_BUFFERS
TEST_F(BlockSparseMatrixTest,Serialization)113 TEST_F(BlockSparseMatrixTest, Serialization) {
114   // Roundtrip through serialization and check for equality.
115   SparseMatrixProto proto;
116   A_->ToProto(&proto);
117 
118   LOG(INFO) << proto.DebugString();
119 
120   BlockSparseMatrix A2(proto);
121 
122   Matrix m_a;
123   Matrix m_b;
124   A_->ToDenseMatrix(&m_a);
125   A2.ToDenseMatrix(&m_b);
126 
127   LOG(INFO) << "\n" << m_a;
128   LOG(INFO) << "\n" << m_b;
129 
130   EXPECT_LT((m_a - m_b).norm(), 1e-12);
131 }
132 #endif
133 
134 }  // namespace internal
135 }  // namespace ceres
136