16 #ifndef OR_TOOLS_LP_DATA_LP_UTILS_H_
17 #define OR_TOOLS_LP_DATA_LP_UTILS_H_
43 return std::abs(f - std::round(f));
48 template <
class DenseRowOrColumn1,
class DenseRowOrColumn2>
50 const DenseRowOrColumn2& v) {
51 DCHECK_EQ(u.size().value(), v.size().value());
53 typename DenseRowOrColumn1::IndexType i(0);
54 typename DenseRowOrColumn2::IndexType j(0);
55 const size_t num_blocks = u.size().value() / 4;
56 for (
size_t block = 0; block < num_blocks; ++block) {
70 sum += (u[i++] * v[j++]) + (u[i++] * v[j++]) + (u[i++] * v[j++]) +
73 while (i < u.size()) {
74 sum += u[i++] * v[j++];
83 template <
class DenseRowOrColumn>
87 sum += u[
typename DenseRowOrColumn::IndexType(e.row().value())] *
93 template <
class DenseRowOrColumn>
95 DCHECK_EQ(u.size().value(), v.
values.
size().value());
100 for (
const auto e : v) {
101 sum += (u[
typename DenseRowOrColumn::IndexType(e.row().value())] *
107 template <
class DenseRowOrColumn,
class DenseRowOrColumn2>
109 const DenseRowOrColumn2& v) {
110 DCHECK_EQ(u.size().value(), v.size().value());
112 for (
typename DenseRowOrColumn::IndexType i(0); i < u.size(); ++i) {
113 sum.
Add(u[i] * v[
typename DenseRowOrColumn2::IndexType(i.value())]);
118 template <
class DenseRowOrColumn>
123 sum.
Add(u[
typename DenseRowOrColumn::IndexType(e.row().value())] *
130 template <
class DenseRowOrColumn>
135 if (e.row().value() >= max_index) {
138 sum += u[
typename DenseRowOrColumn::IndexType(e.row().value())] *
180 RowIndex* row_index);
197 DCHECK(
col.empty() || (&(
col[RowIndex(0)]) == &(
row[ColIndex(0)])));
205 DCHECK(
col.empty() || (&(
col[RowIndex(0)]) == &(
row[ColIndex(0)])));
210 template <
typename IndexType>
212 std::vector<IndexType>* non_zeros) {
217 non_zeros->push_back(
index);
223 template <
typename Container>
226 if (
value != 0.0)
return false;
232 template <
typename BoolVector>
234 return std::all_of(v.begin(), v.end(), [](
bool value) { return !value; });
238 template <
typename IndexType,
typename PermutationIndexType>
244 const IndexType size = input_output->
size();
245 zero_scratchpad->
swap(*input_output);
246 input_output->
resize(size, 0.0);
250 const IndexType permuted_index(
251 permutation[PermutationIndexType(
index.value())].value());
252 (*input_output)[permuted_index] =
value;
255 zero_scratchpad->
assign(size, 0.0);
260 template <
typename IndexType>
265 std::vector<IndexType>* non_zeros) {
267 zero_scratchpad->
swap(*output);
269 for (IndexType& index_ref : *non_zeros) {
271 (*zero_scratchpad)[index_ref] = 0.0;
272 const IndexType permuted_index(permutation[index_ref]);
273 (*output)[permuted_index] =
value;
274 index_ref = permuted_index;
279 template <
typename IndexType,
typename ScatteredRowOrCol>
281 ScatteredRowOrCol* v) {
285 const double kSparseThreshold = 0.05;
286 if (!v->non_zeros.empty() &&
287 v->non_zeros.size() < kSparseThreshold * size.value()) {
288 for (
const IndexType
index : v->non_zeros) {
289 DCHECK_LT(
index, v->values.size());
292 v->values.resize(size, 0.0);
295 v->values.AssignToZero(size);
297 v->non_zeros.clear();
301 template <
typename IndexType>
303 const IndexType
end = data->
size();
304 for (IndexType i(0); i <
end; ++i) {
305 (*data)[i] = -(*data)[i];
322 template <
bool supported_infinity_is_positive>
328 DCHECK(!std::isnan(x));
331 DCHECK_EQ(x, Infinity());
345 DCHECK_GE(num_infinities_, 1);
350 if (num_infinities_ > 0)
return Infinity();
356 if (num_infinities_ > 0)
return Infinity();
357 return sum_.
Value() - x;
359 DCHECK_EQ(Infinity(), x);
360 if (num_infinities_ > 1)
return Infinity();
void swap(StrongVector &x)
void Add(const FpNumber &value)
typename Iterator::Entry Entry
void resize(IntType size)
void assign(IntType size, const T &v)
Fractional SumWithoutUb(Fractional c) const
Fractional SumWithoutLb(Fractional c) const
Fractional SumWithout(Fractional x) const
void PermuteWithScratchpad(const Permutation< PermutationIndexType > &permutation, StrictITIVector< IndexType, Fractional > *zero_scratchpad, StrictITIVector< IndexType, Fractional > *input_output)
Fractional Square(Fractional f)
Fractional PreciseSquaredNorm(const SparseColumn &v)
Fractional InfinityNorm(const DenseColumn &v)
Fractional SquaredNorm(const SparseColumn &v)
bool IsAllZero(const Container &input)
AccurateSum< Fractional > KahanSum
Fractional ScalarProduct(const DenseRowOrColumn1 &u, const DenseRowOrColumn2 &v)
void ComputeNonZeros(const StrictITIVector< IndexType, Fractional > &input, std::vector< IndexType > *non_zeros)
Fractional PreciseScalarProduct(const DenseRowOrColumn &u, const DenseRowOrColumn2 &v)
StrictITIVector< ColIndex, Fractional > DenseRow
void RemoveNearZeroEntries(Fractional threshold, DenseRow *row)
constexpr double kInfinity
SumWithOneMissing< false > SumWithNegativeInfiniteAndOneMissing
bool IsAllFalse(const BoolVector &v)
void PermuteWithKnownNonZeros(const Permutation< IndexType > &permutation, StrictITIVector< IndexType, Fractional > *zero_scratchpad, StrictITIVector< IndexType, Fractional > *output, std::vector< IndexType > *non_zeros)
double Density(const DenseRow &row)
void SetSupportToFalse(const ColumnView &column, DenseBooleanColumn *b)
bool IsFinite(Fractional value)
bool IsDominated(const ColumnView &column, const DenseColumn &radius)
void ClearAndResizeVectorWithNonZeros(IndexType size, ScatteredRowOrCol *v)
const DenseRow & Transpose(const DenseColumn &col)
RowIndex ColToRowIndex(ColIndex col)
void ChangeSign(StrictITIVector< IndexType, Fractional > *data)
static Fractional Fractionality(Fractional f)
Fractional PartialScalarProduct(const DenseRowOrColumn &u, const SparseColumn &v, int max_index)
Fractional RestrictedInfinityNorm(const ColumnView &column, const DenseBooleanColumn &rows_to_consider, RowIndex *row_index)
StrictITIVector< RowIndex, Fractional > DenseColumn
SumWithOneMissing< true > SumWithPositiveInfiniteAndOneMissing
StrictITIVector< RowIndex, bool > DenseBooleanColumn
Collection of objects used to extend the Constraint Solver library.
static int input(yyscan_t yyscanner)
std::optional< int64_t > end
bool ShouldUseDenseIteration(double ratio_for_using_dense_representation) const
StrictITIVector< Index, Fractional > values