MLIR  19.0.0git
Polynomial.cpp
Go to the documentation of this file.
1 //===- Polynomial.cpp - MLIR storage type for static Polynomial -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
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"
17 
18 namespace mlir {
19 namespace polynomial {
20 
22  // A polynomial's terms are canonically stored in order of increasing degree.
23  auto monomialsCopy = llvm::SmallVector<Monomial>(monomials);
24  std::sort(monomialsCopy.begin(), monomialsCopy.end());
25 
26  // Ensure non-unique exponents are not present. Since we sorted the list by
27  // exponent, a linear scan of adjancent monomials suffices.
28  if (std::adjacent_find(monomialsCopy.begin(), monomialsCopy.end(),
29  [](const Monomial &lhs, const Monomial &rhs) {
30  return lhs.exponent == rhs.exponent;
31  }) != monomialsCopy.end()) {
32  return failure();
33  }
34 
35  return Polynomial(monomialsCopy);
36 }
37 
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);
44  }
45  auto result = Polynomial::fromMonomials(monomials);
46  // Construction guarantees unique exponents, so the failure mode of
47  // fromMonomials can be bypassed.
48  assert(succeeded(result));
49  return result.value();
50 }
51 
52 void Polynomial::print(raw_ostream &os, ::llvm::StringRef separator,
53  ::llvm::StringRef exponentiation) const {
54  bool first = true;
55  for (const Monomial &term : terms) {
56  if (first) {
57  first = false;
58  } else {
59  os << separator;
60  }
61  std::string coeffToPrint;
62  if (term.coefficient == 1 && term.exponent.uge(1)) {
63  coeffToPrint = "";
64  } else {
65  llvm::SmallString<16> coeffString;
66  term.coefficient.toStringSigned(coeffString);
67  coeffToPrint = coeffString.str();
68  }
69 
70  if (term.exponent == 0) {
71  os << coeffToPrint;
72  } else if (term.exponent == 1) {
73  os << coeffToPrint << "x";
74  } else {
75  llvm::SmallString<16> expString;
76  term.exponent.toStringSigned(expString);
77  os << coeffToPrint << "x" << exponentiation << expString;
78  }
79  }
80 }
81 
82 void Polynomial::print(raw_ostream &os) const { print(os, " + ", "**"); }
83 
84 std::string Polynomial::toIdentifier() const {
85  std::string result;
86  llvm::raw_string_ostream os(result);
87  print(os, "_", "");
88  return os.str();
89 }
90 
91 unsigned Polynomial::getDegree() const {
92  return terms.back().exponent.getZExtValue();
93 }
94 
95 } // namespace polynomial
96 } // namespace mlir
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
A class representing a monomial of a single-variable polynomial with integer coefficients.
Definition: Polynomial.h:32
A single-variable polynomial with integer coefficients.
Definition: Polynomial.h:69
void print(raw_ostream &os) const
Definition: Polynomial.cpp:82
unsigned getDegree() const
Definition: Polynomial.cpp:91
std::string toIdentifier() const
Definition: Polynomial.cpp:84
static Polynomial fromCoefficients(ArrayRef< int64_t > coeffs)
Returns a polynomial with coefficients given by coeffs.
Definition: Polynomial.cpp:38
static FailureOr< Polynomial > fromMonomials(ArrayRef< Monomial > monomials)
Definition: Polynomial.cpp:21
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
Definition: LogicalResult.h:68