Skip to content

Commit

Permalink
Simplify polynomial ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Jan 7, 2025
1 parent b123460 commit bcdc2b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
13 changes: 3 additions & 10 deletions src/solvers/enumpoly/poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ class ExponentVector {
}
ExponentVector(const ExponentVector &) = default;
~ExponentVector() = default;

// Operators
ExponentVector &operator=(const ExponentVector &) = default;

std::shared_ptr<VariableSpace> GetSpace() const { return m_space; }
int operator[](int index) const { return m_components[index]; }

bool operator==(const ExponentVector &y) const
Expand Down Expand Up @@ -165,7 +164,7 @@ template <class T> class Monomial {
}
Monomial<T> operator-() const { return {-coef, exps}; }

// information
std::shared_ptr<VariableSpace> GetSpace() const { return exps.GetSpace(); }
const T &Coef() const { return coef; }
int TotalDegree() const { return exps.TotalDegree(); }
bool IsMultiaffine() const { return exps.IsMultiaffine(); }
Expand Down Expand Up @@ -211,14 +210,8 @@ template <class T> class Polynomial {
{
m_terms.push_back(Monomial<T>(static_cast<T>(1), ExponentVector(p, var_no, exp)));
}
// Constructs a polynomial that is the monomial coeff*vars^exps
Polynomial(std::shared_ptr<VariableSpace> p, const ExponentVector &exps, const T &coeff)
: m_space(p)
{
m_terms.push_back(Monomial<T>(coeff, exps));
}
// Constructs a polynomial with single monomial
Polynomial(std::shared_ptr<VariableSpace> p, const Monomial<T> &mono) : m_space(p)
explicit Polynomial(const Monomial<T> &mono) : m_space(mono.GetSpace())
{
m_terms.push_back(mono);
}
Expand Down
15 changes: 7 additions & 8 deletions src/solvers/enumpoly/poly.imp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ template <class T> Polynomial<T> Polynomial<T>::PartialDerivative(int varnumber)
Polynomial<T> newPoly(*this);

for (size_t i = 1; i <= newPoly.m_terms.size(); i++) {
newPoly.m_terms[i] = Monomial<T>(newPoly.m_terms[i].Coef() * (T)newPoly.m_terms[i].ExpV()[varnumber],
newPoly.m_terms[i].ExpV().WithZeroExponent(varnumber));
newPoly.m_terms[i] =
Monomial<T>(newPoly.m_terms[i].Coef() * (T)newPoly.m_terms[i].ExpV()[varnumber],
newPoly.m_terms[i].ExpV().WithZeroExponent(varnumber));
}

size_t j = 1;
Expand Down Expand Up @@ -246,8 +247,7 @@ Polynomial<T> Polynomial<T>::MonoInNewCoordinates(const Monomial<T> &m,
if (m.ExpV()[i] > 0) {
Polynomial<T> linearform(m_space, static_cast<T>(0));
for (int j = 1; j <= GetDimension(); j++) {
ExponentVector exps(GetSpace(), j, 1);
linearform += Polynomial<T>(GetSpace(), exps, M(i, j));
linearform += Polynomial<T>(Monomial<T>(M(i, j), ExponentVector(GetSpace(), j, 1)));
}
for (int k = 1; k <= m.ExpV()[i]; k++) {
answer *= linearform;
Expand Down Expand Up @@ -281,10 +281,9 @@ template <class T> T Polynomial<T>::MaximalValueOfNonlinearPart(const T &radius)

template <class T> Polynomial<T> Polynomial<T>::Normalize() const
{
auto maxcoeff =
std::max_element(m_terms.begin(), m_terms.end(), [](const Monomial<T> &a, const Monomial<T> &b) {
return a.Coef() < b.Coef();
});
auto maxcoeff = std::max_element(
m_terms.begin(), m_terms.end(),
[](const Monomial<T> &a, const Monomial<T> &b) { return a.Coef() < b.Coef(); });
if (maxcoeff->Coef() < static_cast<T>(0.000000001)) {
return *this;
}
Expand Down

0 comments on commit bcdc2b3

Please sign in to comment.