cleaning variable names polynomial.h

parent 9ba42e2d
......@@ -177,7 +177,7 @@ void F4_reductors_selection(const list<critpair<T> > &vCrit_step,
set<Monomial> lM;
for (auto& it : std::as_const(vCrit_step)) {
tmp = it.eval();
for (auto& it2 : std::as_const(tmp.get_vp()))
for (auto& it2 : std::as_const(tmp.terms()))
lM.insert(it2.first);
}
......
......@@ -292,8 +292,8 @@ sparse_uptriang_matrix<T>::sparse_uptriang_matrix(
it->second.to_polynomial(tmp);
tmp = tmp*(it->first);
for (typename std::list<std::pair<Monomial, T> >::
const_iterator it2 = tmp.get_vp().begin();
it2 != --(tmp.get_vp().end()); ++it2)
const_iterator it2 = tmp.terms().begin();
it2 != --(tmp.terms().end()); ++it2)
if (map_mon.count(it2->first)) {
list_elts.push_back(sparsemat_elt<T>(
(unsigned)num_pol, map_mon[it2->first], it2->second));
......@@ -340,8 +340,8 @@ matrix_rowmajor<T>::matrix_rowmajor(
it->second.to_polynomial(tmp);
tmp = tmp*(it->first);
for (typename std::list<std::pair<Monomial, T> >
::const_iterator it2 = tmp.get_vp().begin();
it2 != tmp.get_vp().end(); ++it2)
::const_iterator it2 = tmp.terms().begin();
it2 != tmp.terms().end(); ++it2)
if (map_mon.count(it2->first))
set_elt(num_pol, map_mon[it2->first], it2->second);
num_pol++;
......@@ -367,8 +367,8 @@ matrix_rowmajor<T>::matrix_rowmajor(const std::list<critpair<T> > &critpairP,
it != critpairP.end(); ++it) {
tmp = it->eval();
for (typename std::list<std::pair<Monomial, T> >
::const_iterator it2 = tmp.get_vp().begin();
it2 != tmp.get_vp().end(); ++it2)
::const_iterator it2 = tmp.terms().begin();
it2 != tmp.terms().end(); ++it2)
if (map_mon.count(it2->first))
set_elt(num_pol, map_mon[it2->first], it2->second);
num_pol++;
......@@ -515,8 +515,8 @@ matrixF4<T>::matrixF4(const std::list<Polynomial<T> > &_PP) {
for (unsigned i = 0; i < PP.size(); ++i)
for (typename std::list<std::pair<Monomial, T> >
::const_iterator it = PP[i].get_vp().begin();
it != PP[i].get_vp().end(); ++it)
::const_iterator it = PP[i].terms().begin();
it != PP[i].terms().end(); ++it)
lv.insert(it->first);
v1.clear(); v2.clear();
......@@ -546,8 +546,8 @@ matrix_rowmajor<T>::matrix_rowmajor(const std::vector<Polynomial<T> > &PP,
val.shrink_to_fit();
for (unsigned num_pol = 0; num_pol < rsize; ++num_pol)
for (typename std::list<std::pair<Monomial, T> >
::const_iterator it = PP[num_pol].get_vp().begin();
it != PP[num_pol].get_vp().end(); ++it)
::const_iterator it = PP[num_pol].terms().begin();
it != PP[num_pol].terms().end(); ++it)
if (map_mon.count(it->first))
set_elt(num_pol, map_mon[it->first], it->second);
}
......
......@@ -93,7 +93,7 @@ unsigned polymat<T>::degree_bilin() const {
template<class T>
void polymat<T>::to_polynomial(Polynomial<T> &P) const {
P.clear();
P.SetToZero();
if (M == NULL) return;
if (row < M->get_Asize()) {
P.insert(M->get_v1()[row], 1);
......
......@@ -16,52 +16,42 @@ std::ostream& operator<<(std::ostream &o, const Polynomial<T> &p);
template<class T>
class Polynomial {
void combine_LM();
std::list<std::pair<Monomial, T> > vp;
public:
typedef std::pair<Monomial, T> Term;
Polynomial() {
vp = std::list<std::pair<Monomial, T> >();
terms_ = std::list<std::pair<Monomial, T> >();
}
explicit Polynomial(const std::list<Term>& terms_)
: terms_(terms_) {}
explicit Polynomial(const std::list<std::pair<Monomial, T> >& _vp)
: vp(_vp) {}
const std::list<std::pair<Monomial, T> >& get_vp() const {
return vp;
const std::list<std::pair<Monomial, T> >& terms() const {
return terms_;
}
void normalize();
void clear() {
vp.clear();
void SetToZero() {
terms_.clear();
}
void insert(Monomial m, T e) {
vp.push_back(std::pair<Monomial, T>(m, e));
terms_.push_back(std::pair<Monomial, T>(m, e));
}
const Monomial& LM() const {
assert(!vp.back().second.is_zero());
return vp.back().first;
assert(!terms_.back().second.IsZero());
return terms_.back().first;
}
const T& LC() const {
return vp.back().second;
return terms_.back().second;
}
void set_monic();
unsigned size() const {
return vp.size();
}
bool is_zero() const {
return vp.empty();
}
unsigned degree() const {
// TODO(pj): extends for non-degree orderings
// TODO(pj): extend for non-degree orderings
return LM().degree();
}
......@@ -83,6 +73,20 @@ class Polynomial {
inline bool operator<(const Polynomial<T>& P) const;
friend std::ostream& operator<< <T>(std::ostream&, const Polynomial<T>&);
private:
// Adds coefficients of the two leading terms if they share the same
// monomials. Useful for adding polynomials.
void CombineLeadingMonomials();
bool IsEmpty() const {
return terms_.empty();
}
// Removes terms with coefficient 0
void RemoveZeroes();
std::list<Term> terms_;
};
#ifdef BILIN
......@@ -90,32 +94,34 @@ template<class T>
unsigned Polynomial<T>::degree_bilin() const {
unsigned R = 0;
for (typename std::list<std::pair<Monomial, T> >::const_iterator it = \
vp.begin(); it != vp.end(); ++it)
terms_.begin(); it != terms_.end(); ++it)
if (it->first.degree_bilin() > R)
R = it->first.degree_bilin();
return R;
}
#endif
template<class T>
void Polynomial<T>::combine_LM() {
if (vp.empty())
return;
typename std::list<std::pair<Monomial, T> >::iterator it2;
for (typename std::list<std::pair<Monomial, T> >::iterator it = vp.begin();
it != vp.end();) {
void Polynomial<T>::RemoveZeroes() {
for (auto it = terms_.begin(); it != terms_.end(); ++it) {
if (it->second == T(0)) {
it = vp.erase(it);
it = terms_.erase(it);
continue;
}
}
}
template<class T>
void Polynomial<T>::CombineLeadingMonomials() {
typename std::list<std::pair<Monomial, T> >::iterator it2;
for (auto it = terms_.begin(); it != terms_.end();) {
it2 = it;
++it;
if (it == vp.end()) {
if (it == terms_.end()) {
break;
} else if (it->first == it2->first) {
it->second += it2->second;
vp.erase(it2);
terms_.erase(it2);
}
}
}
......@@ -123,7 +129,7 @@ void Polynomial<T>::combine_LM() {
template<class T>
T Polynomial<T>::coeff(const Monomial& m) const {
for (typename std::list<std::pair<Monomial, T> >::const_iterator it = \
vp.begin(); it != vp.end(); ++it)
terms_.begin(); it != terms_.end(); ++it)
if (it->first == m)
return it->second;
return T(0);
......@@ -131,23 +137,25 @@ T Polynomial<T>::coeff(const Monomial& m) const {
template<class T>
void Polynomial<T>::set_monic() {
combine_LM();
T leading_coeff = vp.back().second;
RemoveZeroes();
CombineLeadingMonomials();
T leading_coeff = terms_.back().second;
leading_coeff.inv();
// TODO(pj): test if 1
for (auto& it : vp)
for (auto& it : terms_)
it.second *= leading_coeff;
}
template<class T>
void Polynomial<T>::normalize() {
vp.sort();
combine_LM();
terms_.sort();
RemoveZeroes();
CombineLeadingMonomials();
}
template<class T>
bool Polynomial<T>::topred(const Polynomial &g) {
if (g.is_zero() || is_zero())
if (g.IsEmpty() || IsEmpty())
return false;
if (!LM().isDivisibleBy(g.LM()))
return false;
......@@ -160,52 +168,53 @@ bool Polynomial<T>::topred(const Polynomial &g) {
template<class T>
bool Polynomial<T>::istopred(const Polynomial<T> &g) const {
if (g.is_zero() || is_zero()) return false;
if (g.IsEmpty() || IsEmpty()) return false;
return LM().isDivisibleBy(g.LM());
}
template<class T>
Polynomial<T> Polynomial<T>::operator*(const Monomial &m) const {
Polynomial R;
R.vp.insert(R.vp.begin(), this->vp.begin(), this->vp.end());
R.terms_.insert(R.terms_.begin(), this->terms_.begin(), this->terms_.end());
for (typename std::list<std::pair<Monomial, T> >::iterator it = \
R.vp.begin(); it != R.vp.end(); ++it)
R.terms_.begin(); it != R.terms_.end(); ++it)
(it->first) *= m;
return R;
}
template<class T>
void Polynomial<T>::operator+=(const Polynomial<T> &p2) {
vp.merge(p2.vp);
combine_LM();
terms_.merge(p2.terms_);
RemoveZeroes();
CombineLeadingMonomials();
}
template<class T>
void Polynomial<T>::operator*=(const T &e) {
for (typename std::list<std::pair<Monomial, T> >::iterator it = vp.begin(); \
it != vp.end(); ++it)
for (typename std::list<std::pair<Monomial, T> >::iterator it = terms_.begin(); \
it != terms_.end(); ++it)
it->second *= e;
}
template<class T>
void Polynomial<T>::operator/=(const T& e) {
for (typename std::list<std::pair<Monomial, T> >::iterator it = vp.begin(); \
it != vp.end(); ++it)
for (typename std::list<std::pair<Monomial, T> >::iterator it = terms_.begin(); \
it != terms_.end(); ++it)
it->second /= e;
}
template <class T>
void Polynomial<T>::operator-=(const Polynomial<T>& p2) {
typename std::list<std::pair<Monomial, T> >::iterator it = vp.begin();
typename std::list<std::pair<Monomial, T> >::iterator it = terms_.begin();
typename std::list<std::pair<Monomial, T> >::const_iterator it2 = \
p2.vp.begin();
p2.terms_.begin();
typename std::list<std::pair<Monomial, T> >::const_iterator it2end = \
p2.vp.end();
p2.terms_.end();
while (it2 != p2.vp.end()) {
if (it == vp.end()) {
vp.insert(it, it2, it2end);
while (it != vp.end()) {
while (it2 != p2.terms_.end()) {
if (it == terms_.end()) {
terms_.insert(it, it2, it2end);
while (it != terms_.end()) {
it->second.neg(); ++it;
}
break;
......@@ -213,14 +222,14 @@ void Polynomial<T>::operator-=(const Polynomial<T>& p2) {
++it;
} else if (it->first == it2->first) {
if (it->second == it2->second) {
it = vp.erase(it);
it = terms_.erase(it);
++it2;
} else {
it->second -= it2->second;
++it2;
}
} else {
it = vp.insert(it, *it2);
it = terms_.insert(it, *it2);
it->second.neg();
++it2;
}
......@@ -229,7 +238,7 @@ void Polynomial<T>::operator-=(const Polynomial<T>& p2) {
template <class T>
void Polynomial<T>::print(std::ostream& o) const {
for (auto& it : std::as_const(vp)) {
for (auto& it : std::as_const(terms_)) {
o << it.second << " ";
it.first.print(o);
o << std::endl;
......@@ -239,17 +248,17 @@ void Polynomial<T>::print(std::ostream& o) const {
template <class T>
std::ostream& operator<<(std::ostream& o, const Polynomial<T>& p) {
if (p.is_zero()) {
if (p.IsEmpty()) {
o << "0";
return o;
}
typename std::list<std::pair<Monomial, T> >::const_iterator it = p.vp.begin();
if (it == p.vp.end())
typename std::list<std::pair<Monomial, T> >::const_iterator it = p.terms_.begin();
if (it == p.terms_.end())
return o;
if (it->second != T(0))
o << it->second << it->first;
++it;
for (; it != p.vp.end() ; ++it)
for (; it != p.terms_.end() ; ++it)
if (it->second != T(0))
o << "+" << it->second << it->first;
return o;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment