12 #include "llvm/ADT/APInt.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/raw_ostream.h"
19 namespace polynomial {
24 std::sort(monomialsCopy.begin(), monomialsCopy.end());
28 if (std::adjacent_find(monomialsCopy.begin(), monomialsCopy.end(),
30 return lhs.exponent == rhs.exponent;
31 }) != monomialsCopy.end()) {
40 auto size = coeffs.size();
41 monomials.reserve(size);
42 for (
size_t i = 0; i < size; i++) {
43 monomials.emplace_back(coeffs[i], i);
49 return result.value();
53 ::llvm::StringRef exponentiation)
const {
61 std::string coeffToPrint;
62 if (term.coefficient == 1 && term.exponent.uge(1)) {
66 term.coefficient.toStringSigned(coeffString);
67 coeffToPrint = coeffString.str();
70 if (term.exponent == 0) {
72 }
else if (term.exponent == 1) {
73 os << coeffToPrint <<
"x";
76 term.exponent.toStringSigned(expString);
77 os << coeffToPrint <<
"x" << exponentiation << expString;
86 llvm::raw_string_ostream os(result);
92 return terms.back().exponent.getZExtValue();
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.
A single-variable polynomial with integer coefficients.
void print(raw_ostream &os) const
unsigned getDegree() const
std::string toIdentifier() const
static Polynomial fromCoefficients(ArrayRef< int64_t > coeffs)
Returns a polynomial with coefficients given by coeffs.
static FailureOr< Polynomial > fromMonomials(ArrayRef< Monomial > monomials)
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.