20#include "absl/status/status.h"
21#include "absl/strings/str_cat.h"
22#include "absl/types/span.h"
25#include "ortools/math_opt/sparse_containers.pb.h"
31 const bool enforce_upper_triangular) {
32 const int nnz = matrix.row_ids_size();
33 if (nnz != matrix.column_ids_size()) {
35 <<
"Expected row_id.size=" << nnz
36 <<
" equal to column_ids.size=" << matrix.column_ids_size();
38 if (nnz != matrix.coefficients_size()) {
40 <<
"Expected row_id.size=" << nnz
41 <<
" equal to coefficients.size=" << matrix.coefficients_size();
43 int64_t previous_row = -1;
44 int64_t previous_col = -1;
45 for (
int i = 0; i < nnz; ++i) {
46 const int64_t
row = matrix.row_ids(i);
49 <<
"row_ids should be nonnegative, but found id: " <<
row
50 <<
" (at index: " << i <<
")";
52 const int64_t
col = matrix.column_ids(i);
55 <<
"column_ids should be nonnegative, but found id: " <<
col
56 <<
" (at index: " << i <<
")";
58 if (enforce_upper_triangular &&
row >
col) {
60 <<
"lower triangular entry at [" <<
row <<
", " <<
col
61 <<
"] (at index: " << i <<
")";
63 if (
row < previous_row) {
65 <<
"row_ids should be nondecreasing, but found ids ["
66 << previous_row <<
", " <<
row <<
"] at indices [" << i - 1 <<
", "
68 }
else if (
row == previous_row) {
69 if (previous_col >=
col) {
71 <<
"column_ids should be strictly increasing within a row, but "
73 <<
row <<
" found [" << previous_col <<
", " <<
col
74 <<
"] at indices, [" << i - 1 <<
", " << i <<
"]";
78 if (!std::isfinite(matrix.coefficients(i))) {
80 <<
"Expected finite coefficients without NaN, but at row_id: "
81 <<
row <<
", column_id: " <<
col
82 <<
" found coefficient: " << matrix.coefficients(i)
83 <<
" (at index: " << i <<
")";
88 return absl::OkStatus();
92 const SparseDoubleMatrixProto& matrix,
93 const absl::Span<const int64_t> row_ids,
94 const absl::Span<const int64_t> column_ids) {
98 <<
"Unknown column_id";
99 return absl::OkStatus();
#define RETURN_IF_ERROR(expr)
absl::Status CheckUnsortedIdsSubset(const absl::Span< const int64_t > ids, const absl::Span< const int64_t > universe)
absl::Status SparseMatrixValid(const SparseDoubleMatrixProto &matrix, const bool enforce_upper_triangular)
absl::Status CheckSortedIdsSubset(const absl::Span< const int64_t > ids, const absl::Span< const int64_t > universe)
absl::Status SparseMatrixIdsAreKnown(const SparseDoubleMatrixProto &matrix, const absl::Span< const int64_t > row_ids, const absl::Span< const int64_t > column_ids)
StatusBuilder InvalidArgumentErrorBuilder()