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/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are met:
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.
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.
29
// Author: sameeragarwal@google.com (Sameer Agarwal)
31
#include "ceres/block_sparse_matrix.h"
34
#include <glog/logging.h>
35
#include "gtest/gtest.h"
36
#include "ceres/casts.h"
37
#include "ceres/linear_least_squares_problems.h"
38
#include "ceres/matrix_proto.h"
39
#include "ceres/triplet_sparse_matrix.h"
40
#include "ceres/internal/eigen.h"
41
#include "ceres/internal/scoped_ptr.h"
46
class BlockSparseMatrixTest : public ::testing::Test {
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()));
54
problem.reset(CreateLinearLeastSquaresProblemFromId(1));
55
CHECK_NOTNULL(problem.get());
56
B_.reset(down_cast<TripletSparseMatrix*>(problem->A.release()));
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());
63
scoped_ptr<BlockSparseMatrix> A_;
64
scoped_ptr<TripletSparseMatrix> B_;
67
TEST_F(BlockSparseMatrixTest, SetZeroTest) {
69
EXPECT_EQ(13, A_->num_nonzeros());
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());
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);
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());
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);
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);
104
TEST_F(BlockSparseMatrixTest, ToDenseMatrixTest) {
107
A_->ToDenseMatrix(&m_a);
108
B_->ToDenseMatrix(&m_b);
109
EXPECT_LT((m_a - m_b).norm(), 1e-12);
112
#ifndef CERES_DONT_HAVE_PROTOCOL_BUFFERS
113
TEST_F(BlockSparseMatrixTest, Serialization) {
114
// Roundtrip through serialization and check for equality.
115
SparseMatrixProto proto;
118
LOG(INFO) << proto.DebugString();
120
BlockSparseMatrix A2(proto);
124
A_->ToDenseMatrix(&m_a);
125
A2.ToDenseMatrix(&m_b);
127
LOG(INFO) << "\n" << m_a;
128
LOG(INFO) << "\n" << m_b;
130
EXPECT_LT((m_a - m_b).norm(), 1e-12);
134
} // namespace internal