~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to internal/ceres/block_random_access_sparse_matrix_test.cc

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <vector>
 
32
#include <glog/logging.h>
 
33
#include "gtest/gtest.h"
 
34
#include "ceres/block_random_access_sparse_matrix.h"
 
35
#include "ceres/internal/eigen.h"
 
36
 
 
37
namespace ceres {
 
38
namespace internal {
 
39
 
 
40
TEST(BlockRandomAccessSparseMatrix, GetCell) {
 
41
  vector<int> blocks;
 
42
  blocks.push_back(3);
 
43
  blocks.push_back(4);
 
44
  blocks.push_back(5);
 
45
  const int num_rows = 3 + 4 + 5;
 
46
 
 
47
  set< pair<int, int> > block_pairs;
 
48
  int num_nonzeros = 0;
 
49
  block_pairs.insert(make_pair(0, 0));
 
50
  num_nonzeros += blocks[0] * blocks[0];
 
51
 
 
52
  block_pairs.insert(make_pair(1, 1));
 
53
  num_nonzeros += blocks[1] * blocks[1];
 
54
 
 
55
  block_pairs.insert(make_pair(1, 2));
 
56
  num_nonzeros += blocks[1] * blocks[2];
 
57
 
 
58
  block_pairs.insert(make_pair(2, 0));
 
59
  num_nonzeros += blocks[2] * blocks[0];
 
60
 
 
61
  BlockRandomAccessSparseMatrix m(blocks, block_pairs);
 
62
  EXPECT_EQ(m.num_rows(), num_rows);
 
63
  EXPECT_EQ(m.num_cols(), num_rows);
 
64
 
 
65
  for (set<pair<int, int> >::const_iterator it = block_pairs.begin();
 
66
       it != block_pairs.end();
 
67
       ++it) {
 
68
    const int row_block_id = it->first;
 
69
    const int col_block_id = it->second;
 
70
    int row;
 
71
    int col;
 
72
    int row_stride;
 
73
    int col_stride;
 
74
    CellInfo* cell =  m.GetCell(row_block_id, col_block_id,
 
75
                                &row, &col,
 
76
                                &row_stride, &col_stride);
 
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
  }
 
89
 
 
90
  const TripletSparseMatrix* tsm = m.matrix();
 
91
  EXPECT_EQ(tsm->num_nonzeros(), num_nonzeros);
 
92
  EXPECT_EQ(tsm->max_num_nonzeros(), num_nonzeros);
 
93
 
 
94
  Matrix dense;
 
95
  tsm->ToDenseMatrix(&dense);
 
96
 
 
97
  double kTolerance = 1e-14;
 
98
 
 
99
  // (0,0)
 
100
  EXPECT_NEAR((dense.block(0, 0, 3, 3) - Matrix::Ones(3, 3)).norm(),
 
101
              0.0,
 
102
              kTolerance);
 
103
  // (1,1)
 
104
  EXPECT_NEAR((dense.block(3, 3, 4, 4) - 2 * 2 * Matrix::Ones(4, 4)).norm(),
 
105
              0.0,
 
106
              kTolerance);
 
107
  // (1,2)
 
108
  EXPECT_NEAR((dense.block(3, 3 + 4, 4, 5) - 2 * 3 * Matrix::Ones(4, 5)).norm(),
 
109
              0.0,
 
110
              kTolerance);
 
111
  // (2,0)
 
112
  EXPECT_NEAR((dense.block(3 + 4, 0, 5, 3) - 3 * 1 * Matrix::Ones(5, 3)).norm(),
 
113
              0.0,
 
114
              kTolerance);
 
115
 
 
116
  // There is nothing else in the matrix besides these four blocks.
 
117
  EXPECT_NEAR(dense.norm(), sqrt(9 + 16 * 16 + 36 * 20 + 9 * 15), kTolerance);
 
118
}
 
119
 
 
120
}  // namespace internal
 
121
}  // namespace ceres