Macaulay2 Engine
Loading...
Searching...
No Matches
franzi-brp.cpp
Go to the documentation of this file.
1/* This code written by Franziska Hinkelmann is in the public domain */
2
3#include "franzi-brp.hpp"
4
6{
7 for (monomials_set::iterator it = other.begin(); it != other.end(); ++it)
8 {
9 m.push_back(*it);
10 }
11}
12
13bool BRP::isZero() const { return m.empty(); }
14bool BRP::operator==(const brMonomial &val) const
15{
16 if (val == 0)
17 {
18 return m.empty();
19 }
20 else if (val == 1)
21 {
22 return m.size() == 1 && *(m.begin()) == 0;
23 }
24 return false;
25}
26
27void BRP::addition(const BRP &other, monomials::iterator pos)
28{
29 // merge m with other.m, while removing doubles
30 monomials::const_iterator it = other.m.begin();
31 monomials::iterator pos_end = m.end();
32 monomials::const_iterator other_end = other.m.end();
33 while (it != other_end && pos != pos_end)
34 {
35 // if ( funccompGRL(*it, *pos) ) {
36 if (*it > *pos)
37 {
38 m.insert(pos, *it);
39 ++it;
40 }
41 else if (*it == *pos)
42 {
43 pos = m.erase(pos);
44 ++it;
45 }
46 else
47 {
48 ++pos;
49 }
50 }
51 if (pos == pos_end)
52 {
53 m.insert(pos, it, other_end);
54 }
55}
56
57BRP &BRP::operator+(const BRP &other)
58{ // careful here!
59 addition(other, m.begin());
60 return *this;
61 // a+b changes a, and return a reference to a ( which is now equal to a+b)
62}
63
64BRP BRP::operator*(const BRP &other) const
65{
66 // other _must_ be a monomial
67 if (other == 0)
68 {
69 std::cout << "Multiplication by 0" << std::endl;
70 return BRP();
71 }
72 else
73 {
74 brMonomial mono = *(other.m.begin());
75 return (*this) * mono;
76 }
77}
78
79inline bool funccomp(const brMonomial &a, const brMonomial &b) { return b < a; }
80BRP BRP::operator*(const brMonomial &other) const
81{
82 BRP tmp;
83 monomials::const_iterator end = m.end();
84 brMonomial last = -1; // set this to -1 so it never is the same as mono, if
85 // we set it to 0, we have problems when multiplying
86 // 1*1
87 for (monomials::const_iterator it = m.begin(); it != end; it++)
88 {
89 brMonomial mono = other | *it;
90 if (last == mono)
91 {
92 tmp.m.pop_back();
93 last = -1;
94 }
95 else
96 {
97 tmp.m.push_back(mono);
98 last = mono;
99 }
100 }
101 tmp.m.sort(funccomp);
102 monomials::iterator it = tmp.m.begin();
103 monomials::iterator lastIt = tmp.m.begin();
104 monomials::iterator tmpEnd = tmp.m.end();
105 it++;
106 while (it != tmpEnd)
107 {
108 if (*lastIt == *it)
109 {
110 lastIt = tmp.m.erase(lastIt, ++it);
111 if (lastIt != end)
112 {
113 ++it;
114 }
115 }
116 else
117 {
118 ++it;
119 ++lastIt;
120 }
121 }
122 return tmp;
123}
124
125bool BRP::isLeadingReducibleBy(const BRP &other) const
126{
127 // if ( (*this) == 0 ) cout << "This is 0 " << endl;
128 // if ( other == 0 ) cout << "Other is 0 " << endl;
129 return isDivisibleBy(LT(), other.LT());
130}
131
132bool BRP::isLeadingReducibleBy(const brMonomial &other) const
133{
134 return isDivisibleBy(LT(), other);
135}
136
137// write f as f = ax+b, return b
138BRP BRP::remainder(const BRP &x) const
139{
140 monomials tmp;
141 monomials::const_iterator end = m.end();
142 brMonomial xx = x.LT();
143 for (monomials::const_iterator it = m.begin(); it != end; it++)
144 {
145 brMonomial mono = *it;
146 if (!isDivisibleBy(mono, xx))
147 {
148 tmp.push_back(mono); // don't remove doubles, there shouldn't be any
149 }
150 }
151 return BRP(tmp);
152}
153
154// reduce all terms of f with leading term of g until no term of f can be
155// cancelled
156// return true if a change happened, otherwise false
157// f is being changed to its reduction
158//
159bool BRP::reduceTail(const BRP &g)
160{
161 // cout << "reduceTail: this = " << (*this) << ", by g = " << g << endl;
162 // if ( (*this) == 0 ) {
163 // cerr << "Called reduceTail on 0 brp" << endl;
164 // return false;
165 // }
166 brMonomial lt = g.LT();
167 bool ret = false;
168 monomials::iterator it = m.begin();
169 ++it; // really only reduce tail
170 monomials::iterator end = m.end();
171 for (; it != end;)
172 {
173 brMonomial mono = *it;
174 if (isDivisibleBy(mono, lt))
175 {
176 // cout << mono << " is divisible by " << lt << endl;
177 addition(g * (mono ^ lt), it--);
178 // cout << *this << endl;
179 ++it;
180 ret = true;
181 }
182 if (lt > mono)
183 { // stop iterating because smaller are never divisible by larger
184 return ret;
185 }
186 ++it;
187 }
188 // cout << "at the end of reduceTail, this = " << (*this) << endl;
189 return ret;
190}
191
192// Local Variables:
193// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
194// indent-tabs-mode: nil
195// End:
static bool isDivisibleBy(const brMonomial &a, const brMonomial &b)
BRP operator*(const BRP &other) const
bool reduceTail(const BRP &g)
void addition(const BRP &other, monomials::iterator pos)
bool isLeadingReducibleBy(const BRP &other) const
brMonomial LT() const
BRP remainder(const BRP &x) const
monomials m
bool isZero() const
BRP & operator+(const BRP &other)
bool operator==(const brMonomial &val) const
bool funccomp(const brMonomial &a, const brMonomial &b)
unsigned long brMonomial
std::set< brMonomial, lex > monomials_set
std::list< brMonomial > monomials
brMonomial — bit-packed Boolean-ring monomials for the Hinkelmann GB engine.
volatile int x
TermIterator< Nterm > end(Nterm *)
Definition ringelem.cpp:5