1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
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/compressed_col_sparse_matrix_utils.h"
35 #include "ceres/internal/port.h"
36 #include "glog/logging.h"
43 void CompressedColumnScalarMatrixToBlockMatrix(
44 const int* scalar_rows,
45 const int* scalar_cols,
46 const vector<int>& row_blocks,
47 const vector<int>& col_blocks,
48 vector<int>* block_rows,
49 vector<int>* block_cols) {
50 CHECK_NOTNULL(block_rows)->clear();
51 CHECK_NOTNULL(block_cols)->clear();
52 const int num_row_blocks = row_blocks.size();
53 const int num_col_blocks = col_blocks.size();
55 vector<int> row_block_starts(num_row_blocks);
56 for (int i = 0, cursor = 0; i < num_row_blocks; ++i) {
57 row_block_starts[i] = cursor;
58 cursor += row_blocks[i];
61 // This loop extracts the block sparsity of the scalar sparse matrix
62 // It does so by iterating over the columns, but only considering
63 // the columns corresponding to the first element of each column
64 // block. Within each column, the inner loop iterates over the rows,
65 // and detects the presence of a row block by checking for the
66 // presence of a non-zero entry corresponding to its first element.
67 block_cols->push_back(0);
69 for (int col_block = 0; col_block < num_col_blocks; ++col_block) {
71 for (int idx = scalar_cols[c]; idx < scalar_cols[c + 1]; ++idx) {
72 vector<int>::const_iterator it =
73 std::lower_bound(row_block_starts.begin(),
74 row_block_starts.end(),
76 // Since we are using lower_bound, it will return the row id
77 // where the row block starts. For everything but the first row
78 // of the block, where these values will be the same, we can
79 // skip, as we only need the first row to detect the presence of
82 // For rows all but the first row in the last row block,
83 // lower_bound will return row_block_starts.end(), but those can
84 // be skipped like the rows in other row blocks too.
85 if (it == row_block_starts.end() || *it != scalar_rows[idx]) {
89 block_rows->push_back(it - row_block_starts.begin());
92 block_cols->push_back(block_cols->back() + column_size);
93 c += col_blocks[col_block];
97 void BlockOrderingToScalarOrdering(const vector<int>& blocks,
98 const vector<int>& block_ordering,
99 vector<int>* scalar_ordering) {
100 CHECK_EQ(blocks.size(), block_ordering.size());
101 const int num_blocks = blocks.size();
103 // block_starts = [0, block1, block1 + block2 ..]
104 vector<int> block_starts(num_blocks);
105 for (int i = 0, cursor = 0; i < num_blocks ; ++i) {
106 block_starts[i] = cursor;
110 scalar_ordering->resize(block_starts.back() + blocks.back());
112 for (int i = 0; i < num_blocks; ++i) {
113 const int block_id = block_ordering[i];
114 const int block_size = blocks[block_id];
115 int block_position = block_starts[block_id];
116 for (int j = 0; j < block_size; ++j) {
117 (*scalar_ordering)[cursor++] = block_position++;
121 } // namespace internal