OR-Tools  9.6
formatters.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 #ifndef OR_TOOLS_MATH_OPT_CPP_FORMATTERS_H_
15 #define OR_TOOLS_MATH_OPT_CPP_FORMATTERS_H_
16 
17 #include <cmath>
18 #include <iostream>
19 #include <ostream>
20 
22 
24 
25 // Streaming formatter for a coefficient of a linear/quadratic term, along with
26 // any leading "+"/"-"'s to connect it with preceding terms in a sum, and
27 // potentially a "*" postfix. The `is_first` parameter specifies if the term is
28 // the first appearing in the sum, in which case the handling of the +/-
29 // connectors is different.
31  LeadingCoefficientFormatter(const double coeff, const bool is_first)
33  const double coeff;
34  const bool is_first;
35 };
36 
37 inline std::ostream& operator<<(std::ostream& out,
38  const LeadingCoefficientFormatter formatter) {
39  const double coeff = formatter.coeff;
40  if (formatter.is_first) {
41  if (coeff == 1.0) {
42  // Do nothing.
43  } else if (coeff == -1.0) {
44  out << "-";
45  } else {
46  out << RoundTripDoubleFormat(coeff) << "*";
47  }
48  } else {
49  if (coeff == 1.0) {
50  out << " + ";
51  } else if (coeff == -1.0) {
52  out << " - ";
53  } else if (std::isnan(coeff)) {
54  out << " + nan*";
55  } else if (coeff >= 0) {
56  out << " + " << RoundTripDoubleFormat(coeff) << "*";
57  } else {
58  out << " - " << RoundTripDoubleFormat(-coeff) << "*";
59  }
60  }
61  return out;
62 }
63 
64 // Streaming formatter for a constant in a linear/quadratic expression.
66  ConstantFormatter(const double constant, const bool is_first)
68  const double constant;
69  const bool is_first;
70 };
71 
72 inline std::ostream& operator<<(std::ostream& out,
73  const ConstantFormatter formatter) {
74  const double constant = formatter.constant;
75  if (formatter.is_first) {
76  out << RoundTripDoubleFormat(constant);
77  } else if (constant == 0) {
78  // Do nothing.
79  } else if (std::isnan(constant)) {
80  out << " + nan";
81  } else if (constant > 0) {
82  out << " + " << RoundTripDoubleFormat(constant);
83  } else {
84  out << " - " << RoundTripDoubleFormat(-constant);
85  }
86  return out;
87 }
88 
89 } // namespace operations_research::math_opt
90 
91 #endif // OR_TOOLS_MATH_OPT_CPP_FORMATTERS_H_
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
ConstantFormatter(const double constant, const bool is_first)
Definition: formatters.h:66
LeadingCoefficientFormatter(const double coeff, const bool is_first)
Definition: formatters.h:31