Gröbner basis project
Codebase for research into Gröbner basis computation
weighted_sugar_strategy.cpp
1 #ifndef __SUGAR_STRATEGY_CPP_
2 #define __SUGAR_STRATEGY_CPP_
3 
4 /*****************************************************************************\
5 * This file is part of DynGB. *
6 * *
7 * DynGB is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Foobar is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
19 \*****************************************************************************/
20 
21 #include "polynomial.hpp"
22 #include "strategies.hpp"
23 #include "critical_pair.hpp"
24 #include "weighted_sugar_strategy.hpp"
25 #include "particular_orderings.hpp"
26 
28  const Abstract_Polynomial * poly, const WT_TYPE *w
29 ) {
30  p = poly;
31  weights = w;
32  sugar = p->weighted_degree(w);
33 }
34 
36  const Poly_WSugar_Data * psd = static_cast<const Poly_WSugar_Data *>(&sd);
37  return sugar == psd->sugar;
38 }
39 
41  const Poly_WSugar_Data * psd = static_cast<const Poly_WSugar_Data *>(&sd);
42  return sugar > psd->sugar;
43 }
44 
46  sugar = 0;
47  for (
49  not pi->fellOff(); pi->moveRight()
50  ) {
51  if (pi->currMonomial().total_degree() > sugar)
52  sugar = pi->currMonomial().weighted_degree(weights);
53  }
54 }
55 
57  sugar = 0;
59  for ( /* already initialized */ ; not pi->fellOff(); pi->moveRight()) {
60  if (
63  ) {
66  }
67  }
68  delete pi;
69 }
70 
72  const EXP_TYPE * u, const Abstract_Polynomial & g
73 ) {
74  Poly_WSugar_Data * sd = static_cast<Poly_WSugar_Data *>(g.strategy());
75  DEG_TYPE d = sd->sugar;
76  const Monomial & t = g.leading_monomial();
77  for (NVAR_TYPE i = 0; i < t.num_vars(); ++i) d += weights[i]*u[i];
78  if (sugar < d) sugar = d;
79 }
80 
81 DEG_TYPE Poly_WSugar_Data::poly_sugar() const { return sugar; }
82 
83 const WT_TYPE * Poly_WSugar_Data::weighting() const { return weights; }
84 
85 ostream & operator <<(ostream &os, const Poly_WSugar_Data &sd) {
86  os << "Weighted sugar strategy recording sugar " << sd.sugar;
87  return os;
88 }
89 
91 {
92  cp = &cpb;
93  if (cpb.first()->strategy() == nullptr) {
94  const CachedWGrevlex_Ordering * mord
95  = dynamic_cast<const CachedWGrevlex_Ordering *>(cpb.first()->monomial_ordering());
96  const WT_TYPE * w = (mord == nullptr) ? nullptr : mord->order_weights();
97  sugar = cpb.first()->weighted_degree(w);
98  }
99  else {
100  Poly_WSugar_Data * wdata
101  = dynamic_cast<Poly_WSugar_Data *>(cpb.first()->strategy());
102  const WT_TYPE * w = (wdata == nullptr) ? nullptr : wdata->weighting();
103  sugar = wdata->poly_sugar() + cpb.first_multiplier().weighted_degree(w);
104  }
105  if (cpb.second() != nullptr) {
106  const Abstract_Polynomial * q = cpb.second();
107  if (false) cout << "second: " << *q << endl;
108  Poly_WSugar_Data * wdata
109  = dynamic_cast<Poly_WSugar_Data *>(q->strategy());
110  const WT_TYPE * w = (wdata == nullptr) ? nullptr : wdata->weighting();
111  DEG_TYPE second_sugar = wdata->poly_sugar()
113  if (second_sugar > sugar) sugar = second_sugar;
114  }
115 }
116 
118  const Pair_WSugar_Strategy * nsd = static_cast<const Pair_WSugar_Strategy *>(&sd);
119  return (sugar == nsd->sugar)
120  and (cp->lcm() == nsd->cp->lcm())
121  and (cp->first()->leading_monomial() == nsd->cp->first()->leading_monomial())
122  and (cp->second()->leading_monomial()
123  == nsd->cp->second()->leading_monomial());
124 }
125 
127  const Pair_WSugar_Strategy * nsd = static_cast<const Pair_WSugar_Strategy *>(&sd);
128  bool result = false;
129  if (sugar > nsd->sugar)
130  result = true;
131  else if (sugar == nsd->sugar and cp->lcm() > nsd->cp->lcm())
132  result = true;
133  else if (sugar == nsd->sugar and cp->lcm().is_like(nsd->cp->lcm())) {
134  if (cp->first()->leading_monomial() > nsd->cp->first()->leading_monomial())
135  result = true;
136  else if (
137  cp->first()->leading_monomial().is_like(
138  nsd->cp->first()->leading_monomial()
139  )
140  ) {
141  if (
142  cp->second()->leading_monomial() > nsd->cp->second()->leading_monomial()
143  ) {
144  result = true;
145  }
146  }
147  }
148  return result;
149 }
150 
152 
153 #endif
virtual const WT_TYPE * order_weights() const override
the weights that define this ordering
The general class of a polynomial.
Definition: polynomial.hpp:101
const Abstract_Polynomial * p
the polynomial to which this strategy applies
Definition: strategies.hpp:135
const Monomial & second_multiplier() const
monomial needed to multiply second polynomial to lcm()
const Monomial_Ordering * monomial_ordering() const
reports leading monomial’s monomial ordering
Definition: polynomial.hpp:130
DEG_TYPE weighted_degree(const WT_TYPE *weights, NVAR_TYPE m=0) const
Definition: monomial.cpp:199
polynomial-related strategy data
Definition: strategies.hpp:49
virtual Polynomial_Iterator * new_iterator() const =0
An iterator that poses no risk of modifying the polynomial.
virtual void moveRight()=0
Moves right in the polynomial, to the next smaller monomial.
Critical_Pair_Basic * cp
the critical pair to which this Normal_Strategy belongs
const Abstract_Polynomial * second() const
second polynomial in pair
ordering critical pairs using the weighted sugar strategy
DEG_TYPE poly_sugar() const
the sugar itself
virtual Monomial & leading_monomial() const =0
leading monomial – call after sort_by_order()!
DEG_TYPE sugar
the polynomial’s sugar
const Monomial & lcm() const
lcm of leading monomials of polynomials
Structure for sorting critical pairs.
Definition: strategies.hpp:145
virtual bool first_larger(const Poly_Strategy_Data &sd) const
returns true iff this sugar is larger than sd ’s
DEG_TYPE sugar
the pair*rsquo;s sugar
virtual void pre_reduction_tasks(const EXP_TYPE *u, const Abstract_Polynomial &g)
re-evaluates the sugar, if need be
virtual DEG_TYPE weighted_degree(const WT_TYPE *w=nullptr) const
Definition: polynomial.cpp:108
NVAR_TYPE num_vars() const
number of variables
Definition: monomial.hpp:130
virtual bool fellOff() const =0
const Abstract_Polynomial * first() const
first polynomial in pair
virtual void at_generation_tasks()
sets the sugar to the largest weighted degree of a monomial in the assigned previously polynomial ...
virtual bool first_larger(const Pair_Strategy_Data &sd) const
implementation of first_larger()
polynomial-related data for a weighted sugar strategy
Implementation of monomials.
Definition: monomial.hpp:69
Poly_WSugar_Data(const Abstract_Polynomial *poly, const WT_TYPE *w)
records poly as the reference for this
const WT_TYPE * weights
pointer to the weights
const Monomial & first_multiplier() const
monomial needed to multiply first polynomial to lcm()
virtual bool equivalent(const Pair_Strategy_Data &sd) const
implementation of equivalent()
Used to iterate through a polynomial.
Definition: polynomial.hpp:224
DEG_TYPE pair_sugar()
the weighted sugar
const WT_TYPE * weighting() const
the weights used to compute the sugar
virtual const Monomial & currMonomial() const =0
Reports the monomial at the current position.
the weighted grevlex ordering for a specified number of variables, with cached weights for each monom...
Controls the creation of s-polynomials.
Pair_WSugar_Strategy(Critical_Pair_Basic &)
creates a pair whose weighted sugar is the maximum of that of the products of the polynomials in the ...
virtual bool equivalent(const Poly_Strategy_Data &sd) const
returns true iff the sugars are equal
virtual Poly_Strategy_Data * strategy() const
strategy related information
Definition: polynomial.hpp:144