OR-Tools  9.6
sparse_submatrix.h
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // Tools to extract some sub-components of sparse matrices.
15 #ifndef OR_TOOLS_MATH_OPT_CORE_SPARSE_SUBMATRIX_H_
16 #define OR_TOOLS_MATH_OPT_CORE_SPARSE_SUBMATRIX_H_
17 
18 #include <cstdint>
19 #include <optional>
20 #include <utility>
21 #include <vector>
22 
25 #include "ortools/math_opt/sparse_containers.pb.h"
26 
28 
29 // A vector that contains one pair (row_id, columns_coefficients) per row,
30 // sorted by row_id. The columns_coefficients are views.
32  std::vector<std::pair<int64_t, SparseVectorView<double>>>;
33 
34 // Returns the coefficients of columns in the range [start_col_id, end_col_id)
35 // for each row in the range [start_row_id, end_row_id).
36 //
37 // Returns a vector that contains one pair (row_id, columns_coefficients) per
38 // row. It CHECKs that the input matrix is valid. The coefficients are returned
39 // in a views that points to the input matrix's data. Therefore they should not
40 // be used after the proto is modified/deleted.
41 //
42 // When end_(col|row)_id is nullopt, includes all indices greater or equal to
43 // start_(col|row)_id.
44 //
45 // This functions runs in O(size of matrix).
46 //
47 // Use TransposeSparseSubmatrix() to transpose the submatrix and get the
48 // columns instead of the rows.
49 //
50 // Usage example:
51 //
52 // // With this input sparse matrix:
53 // // |0 1 2 3 4 5 6
54 // // -+-------------
55 // // 0|2 - - - 3 4 -
56 // // 1|- - - - - - -
57 // // 2|- 5 - 1 - - 3
58 // // 3|9 - - 8 - - 7
59 // const SparseDoubleMatrixProto matrix = ...;
60 //
61 // // Keeping coefficients of lines >= 1 and columns in [1, 6).
62 // const auto rows = SparseSubmatrixByRows(
63 // matrix,
64 // /*start_row_id=*/1, /*end_row_id=*/std::nullopt,
65 // /*start_col_id=*/1, /*end_col_id=*/6);
66 //
67 // // The returned rows and coefficients will be:
68 // // {2, {{1, 5.0}, {3, 1.0}}}
69 // // {3, { {3, 8.0}}}
70 //
72  const SparseDoubleMatrixProto& matrix, int64_t start_row_id,
73  std::optional<int64_t> end_row_id, int64_t start_col_id,
74  std::optional<int64_t> end_col_id);
75 
76 // Returns a vector that contains one pair (row_id, rows_coefficients) per
77 // column.
78 //
79 // The coefficients are returned as copies of the input views.
80 //
81 // This functions runs in:
82 // O(num_non_zeros + num_non_empty_cols * lg(num_non_empty_cols)).
83 std::vector<std::pair<int64_t, SparseVector<double>>> TransposeSparseSubmatrix(
84  const SparseSubmatrixRowsView& submatrix_by_rows);
85 
86 } // namespace operations_research::math_opt
87 
88 #endif // OR_TOOLS_MATH_OPT_CORE_SPARSE_SUBMATRIX_H_
std::vector< std::pair< int64_t, SparseVector< double > > > TransposeSparseSubmatrix(const SparseSubmatrixRowsView &submatrix_by_rows)
std::vector< std::pair< int64_t, SparseVectorView< double > >> SparseSubmatrixRowsView
SparseSubmatrixRowsView SparseSubmatrixByRows(const SparseDoubleMatrixProto &matrix, const int64_t start_row_id, const std::optional< int64_t > end_row_id, const int64_t start_col_id, const std::optional< int64_t > end_col_id)