9 #ifndef MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_
10 #define MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/Hashing.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/raw_ostream.h"
25 namespace polynomial {
32 template <
class Derived,
typename CoefficientType>
57 Derived
add(
const Derived &other) {
59 CoefficientType newCoeff =
coefficient + other.coefficient;
61 result.setCoefficient(newCoeff);
70 template <
class D,
typename T>
115 template <
class Derived,
typename Monomial>
122 explicit operator bool()
const {
return !terms.empty(); }
124 return other.terms == terms;
127 return !(other.terms == terms);
130 void print(raw_ostream &os, ::llvm::StringRef separator,
131 ::llvm::StringRef exponentiation)
const {
133 for (
const Monomial &term :
getTerms()) {
139 std::string coeffToPrint;
140 if (term.isMonic() && term.getExponent().uge(1)) {
144 term.coefficientToString(coeffString);
145 coeffToPrint = coeffString.str();
148 if (term.getExponent() == 0) {
150 }
else if (term.getExponent() == 1) {
151 os << coeffToPrint <<
"x";
154 term.getExponent().toStringSigned(expString);
155 os << coeffToPrint <<
"x" << exponentiation << expString;
160 Derived
add(
const Derived &other) {
162 auto it1 = terms.begin();
163 auto it2 = other.terms.begin();
164 while (it1 != terms.end() || it2 != other.terms.end()) {
165 if (it1 == terms.end()) {
166 newTerms.emplace_back(*it2);
171 if (it2 == other.terms.end()) {
172 newTerms.emplace_back(*it1);
177 while (it1->getExponent().ult(it2->getExponent())) {
178 newTerms.emplace_back(*it1);
180 if (it1 == terms.end())
184 while (it2->getExponent().ult(it1->getExponent())) {
185 newTerms.emplace_back(*it2);
187 if (it2 == terms.end())
191 newTerms.emplace_back(it1->add(*it2));
195 return Derived(newTerms);
199 void print(raw_ostream &os)
const {
print(os,
" + ",
"**"); }
206 llvm::raw_string_ostream os(result);
212 return terms.back().getExponent().getZExtValue();
217 template <
class D,
typename T>
234 static FailureOr<IntPolynomial>
252 static FailureOr<FloatPolynomial>
261 template <
class D,
typename T>
263 return ::llvm::hash_combine_range(arg.terms.begin(), arg.terms.end());
266 template <
class D,
typename T>
272 template <
class D,
typename T>
275 polynomial.
print(os);
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.