Debian Codegraph @

unit_test/general/sparse_block_matrix.cpp

raw symbols SCIP
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include "g2o/core/sparse_block_matrix.h"
28
29 #include <iostream>
30
31 #include "gtest/gtest.h"
32
33 using namespace std;
34 using namespace g2o;
35
36 using SparseBlockMatrixX = SparseBlockMatrix<MatrixX>;
37
38 std::ostream& operator<<(std::ostream& os,
39 const SparseBlockMatrixX::SparseMatrixBlock& m) {
40 for (int i = 0; i < m.rows(); ++i) {
41 for (int j = 0; j < m.cols(); ++j) os << m(i, j) << " ";
42 os << endl;
43 }
44 return os;
45 }
46
47 TEST(General, SparseBlockMatrix) {
48 auto fill_block = [](MatrixX& b) {
49 for (int i = 0; i < b.rows(); ++i)
50 for (int j = 0; j < b.cols(); ++j) {
51 b(i, j) = i * b.cols() + j;
52 }
53 };
54
55 int rcol[] = {3, 6, 8, 12};
56 int ccol[] = {2, 4, 13};
57 SparseBlockMatrixX* M = new SparseBlockMatrixX(rcol, ccol, 4, 3);
58
59 SparseBlockMatrixX::SparseMatrixBlock* b = M->block(0, 0, true);
60 EXPECT_EQ(3, b->rows());
61 EXPECT_EQ(2, b->cols());
62 fill_block(*b);
63
64 b = M->block(0, 2, true);
65 EXPECT_EQ(3, b->rows());
66 EXPECT_EQ(9, b->cols());
67 fill_block(*b);
68
69 b = M->block(3, 2, true);
70 EXPECT_EQ(4, b->rows());
71 EXPECT_EQ(9, b->cols());
72 fill_block(*b);
73
74 auto Ms = M->added();
75 bool addResult = M->add(*Ms);
76 EXPECT_TRUE(addResult);
77 EXPECT_EQ(Ms->rows(), M->rows());
78 EXPECT_EQ(Ms->cols(), M->cols());
79
80 auto Mt = M->transposed<MatrixX>();
81 EXPECT_EQ(M->rows(), Mt->cols());
82 EXPECT_EQ(M->cols(), Mt->rows());
83 // cerr << *Mt << endl;
84
85 SparseBlockMatrixX* Mp = nullptr;
86 M->multiply(Mp, Mt.get());
87 ASSERT_NE(Mp, nullptr);
88
89 int iperm[] = {3, 2, 1, 0};
90 SparseBlockMatrixX* PMp = nullptr;
91 bool symPermResult = Mp->symmPermutation(PMp, iperm, false);
92 ASSERT_TRUE(symPermResult);
93 // cerr << *PMp << endl;
94
95 PMp->clear(true);
96 Mp->block(3, 0)->fill(0.);
97 symPermResult = Mp->symmPermutation(PMp, iperm, true);
98 ASSERT_TRUE(symPermResult);
99 // cerr << *PMp << endl;
100 }