9 #ifndef MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_
10 #define MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/raw_ostream.h"
26 namespace polynomial {
33 template <
class Derived,
typename CoefficientType>
58 Derived
add(
const Derived &other) {
60 CoefficientType newCoeff =
coefficient + other.coefficient;
62 result.setCoefficient(newCoeff);
71 template <
class D,
typename T>
116 template <
class Derived,
typename Monomial>
123 explicit operator bool()
const {
return !terms.empty(); }
125 return other.terms == terms;
128 return !(other.terms == terms);
131 void print(raw_ostream &os, ::llvm::StringRef separator,
132 ::llvm::StringRef exponentiation)
const {
134 for (
const Monomial &term :
getTerms()) {
140 std::string coeffToPrint;
141 if (term.isMonic() && term.getExponent().uge(1)) {
145 term.coefficientToString(coeffString);
146 coeffToPrint = coeffString.str();
149 if (term.getExponent() == 0) {
151 }
else if (term.getExponent() == 1) {
152 os << coeffToPrint <<
"x";
155 term.getExponent().toStringSigned(expString);
156 os << coeffToPrint <<
"x" << exponentiation << expString;
161 Derived
add(
const Derived &other) {
163 auto it1 = terms.begin();
164 auto it2 = other.terms.begin();
165 while (it1 != terms.end() || it2 != other.terms.end()) {
166 if (it1 == terms.end()) {
167 newTerms.emplace_back(*it2);
172 if (it2 == other.terms.end()) {
173 newTerms.emplace_back(*it1);
178 while (it1->getExponent().ult(it2->getExponent())) {
179 newTerms.emplace_back(*it1);
181 if (it1 == terms.end())
185 while (it2->getExponent().ult(it1->getExponent())) {
186 newTerms.emplace_back(*it2);
188 if (it2 == terms.end())
192 newTerms.emplace_back(it1->add(*it2));
196 return Derived(newTerms);
200 void print(raw_ostream &os)
const {
print(os,
" + ",
"**"); }
207 llvm::raw_string_ostream os(result);
213 return terms.back().getExponent().getZExtValue();
218 template <
class D,
typename T>
262 template <
class D,
typename T>
264 return ::llvm::hash_combine_range(arg.terms.begin(), arg.terms.end());
267 template <
class D,
typename T>
273 template <
class D,
typename T>
276 polynomial.
print(os);
This class provides support for representing a failure result, or a valid value of type T.
A class representing a monomial of a single-variable polynomial with integer coefficients.
bool isMonic() const override
~FloatMonomial() override=default
FloatMonomial(double coeff, uint64_t expo)
void coefficientToString(llvm::SmallString< 16 > &coeffString) const override
A single-variable polynomial with double coefficients.
FloatPolynomial(ArrayRef< FloatMonomial > terms)
static FailureOr< FloatPolynomial > fromMonomials(ArrayRef< FloatMonomial > monomials)
static FloatPolynomial fromCoefficients(ArrayRef< double > coeffs)
Returns a polynomial with coefficients given by coeffs.
A class representing a monomial of a single-variable polynomial with integer coefficients.
void coefficientToString(llvm::SmallString< 16 > &coeffString) const override
IntMonomial(int64_t coeff, uint64_t expo)
bool isMonic() const override
~IntMonomial() override=default
A single-variable polynomial with integer coefficients.
static IntPolynomial fromCoefficients(ArrayRef< int64_t > coeffs)
Returns a polynomial with coefficients given by coeffs.
IntPolynomial(ArrayRef< IntMonomial > terms)
static FailureOr< IntPolynomial > fromMonomials(ArrayRef< IntMonomial > monomials)
bool operator<(const MonomialBase &other) const
Monomials are ordered by exponent.
bool operator!=(const MonomialBase &other) const
void setExponent(const APInt &exp)
virtual void coefficientToString(llvm::SmallString< 16 > &coeffString) const =0
MonomialBase(const CoefficientType &coeff, const APInt &expo)
const CoefficientType & getCoefficient() const
CoefficientType & getMutableCoefficient()
Derived add(const Derived &other)
void setCoefficient(const CoefficientType &coeff)
virtual bool isMonic() const =0
bool operator==(const MonomialBase &other) const
CoefficientType coefficient
const APInt & getExponent() const
friend ::llvm::hash_code hash_value(const MonomialBase< D, T > &arg)
virtual ~MonomialBase()=default
void print(raw_ostream &os) const
Derived add(const Derived &other)
std::string toIdentifier() const
friend ::llvm::hash_code hash_value(const PolynomialBase< D, T > &arg)
ArrayRef< Monomial > getTerms() const
bool operator==(const PolynomialBase &other) const
void print(raw_ostream &os, ::llvm::StringRef separator, ::llvm::StringRef exponentiation) const
PolynomialBase(ArrayRef< Monomial > terms)
bool operator!=(const PolynomialBase &other) const
unsigned getDegree() const
inline ::llvm::hash_code hash_value(const MonomialBase< D, T > &arg)
raw_ostream & operator<<(raw_ostream &os, const PolynomialBase< D, T > &polynomial)
inline ::llvm::hash_code hash_value(const PolynomialBase< D, T > &arg)
constexpr unsigned apintBitWidth
This restricts statically defined polynomials to have at most 64-bit coefficients.
Include the generated interface declarations.