MLIR  20.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 
11 #include "llvm/ADT/SmallVector.h"
12 
13 namespace mlir {
14 namespace polynomial {
15 
16 template <typename PolyT, typename MonomialT>
17 FailureOr<PolyT> fromMonomialsImpl(ArrayRef<MonomialT> monomials) {
18  // A polynomial's terms are canonically stored in order of increasing degree.
19  auto monomialsCopy = llvm::SmallVector<MonomialT>(monomials);
20  std::sort(monomialsCopy.begin(), monomialsCopy.end());
21 
22  // Ensure non-unique exponents are not present. Since we sorted the list by
23  // exponent, a linear scan of adjancent monomials suffices.
24  if (std::adjacent_find(monomialsCopy.begin(), monomialsCopy.end(),
25  [](const MonomialT &lhs, const MonomialT &rhs) {
26  return lhs.getExponent() == rhs.getExponent();
27  }) != monomialsCopy.end()) {
28  return failure();
29  }
30 
31  return PolyT(monomialsCopy);
32 }
33 
34 FailureOr<IntPolynomial>
36  return fromMonomialsImpl<IntPolynomial, IntMonomial>(monomials);
37 }
38 
39 FailureOr<FloatPolynomial>
41  return fromMonomialsImpl<FloatPolynomial, FloatMonomial>(monomials);
42 }
43 
44 template <typename PolyT, typename MonomialT, typename CoeffT>
47  auto size = coeffs.size();
48  monomials.reserve(size);
49  for (size_t i = 0; i < size; i++) {
50  monomials.emplace_back(coeffs[i], i);
51  }
52  auto result = PolyT::fromMonomials(monomials);
53  // Construction guarantees unique exponents, so the failure mode of
54  // fromMonomials can be bypassed.
55  assert(succeeded(result));
56  return result.value();
57 }
58 
60  return fromCoefficientsImpl<IntPolynomial, IntMonomial, int64_t>(coeffs);
61 }
62 
64  return fromCoefficientsImpl<FloatPolynomial, FloatMonomial, double>(coeffs);
65 }
66 
67 } // namespace polynomial
68 } // namespace mlir
A single-variable polynomial with double coefficients.
Definition: Polynomial.h:245
static FailureOr< FloatPolynomial > fromMonomials(ArrayRef< FloatMonomial > monomials)
Definition: Polynomial.cpp:40
static FloatPolynomial fromCoefficients(ArrayRef< double > coeffs)
Returns a polynomial with coefficients given by coeffs.
Definition: Polynomial.cpp:63
A single-variable polynomial with integer coefficients.
Definition: Polynomial.h:228
static IntPolynomial fromCoefficients(ArrayRef< int64_t > coeffs)
Returns a polynomial with coefficients given by coeffs.
Definition: Polynomial.cpp:59
static FailureOr< IntPolynomial > fromMonomials(ArrayRef< IntMonomial > monomials)
Definition: Polynomial.cpp:35
FailureOr< PolyT > fromMonomialsImpl(ArrayRef< MonomialT > monomials)
Definition: Polynomial.cpp:17
PolyT fromCoefficientsImpl(ArrayRef< CoeffT > coeffs)
Definition: Polynomial.cpp:45
Include the generated interface declarations.