Macaulay2 Engine
Loading...
Searching...
No Matches
geovec.hpp
Go to the documentation of this file.
1// Copyright 1996 Michael E. Stillman
2
32
33// This should probably be done by:
34// (a) making a type const FreeModule, that FreeModule, and res_poly
35// both can inherit from: but this is a bit of a kludge...
36// (b) making a vector type with a next and coeff field, that
37// is then inherited by vecterm, resterm.
38// Redefine:
39// const FreeModule
40// routines that should be implemented in this class:
41// add_to, compare, get_ring, remove
42// vecterm *
43// fields of this structure type should include:
44// next, coeff
45
46// GEOHEAP_SIZE: defined in style.hpp
47// heap_size: defined in object.cpp
48
50{
51 const FreeModule *F; // Our elements will be vectors in here
52 const Ring *K; // The coefficient ring
55 int mLead; // set after a call to get_lead_term.
56 // set negative after each call to add,
57 // or remove_lead_term
58 public:
59 vecHeap(const FreeModule *F);
60 ~vecHeap();
61
62 void add(vecterm *p);
63 const vecterm *get_lead_term(); // Returns NULL if none.
64 vecterm *remove_lead_term(); // Returns NULL if none.
65
66 const FreeModule *get_target() const { return F; }
67 vecterm *value(); // Returns the linearized value, and resets the vecHeap.
68
70 {
71 return heap[i];
72 } // DO NOT USE, except for debugging purposes!
74 const; // Adds up all the elements and returns this value
75 // Mainly used for debugging.
76};
77
79 : F(FF), K(FF->get_ring()), top_of_heap(-1), mLead(-1)
80{
81 // set K
82 int i;
83 for (i = 0; i < GEOHEAP_SIZE; i++) heap[i] = NULL;
84}
85
87{
88 // The user of this class must insure that all 'vecterm's
89 // have been removed first. Thus, we don't need to
90 // do anything here.
91}
92
93inline void vecHeap::add(vecterm *p)
94{
95 mLead = -1;
96 int len = K->n_nonzero_terms(p);
97 int i = 0;
98 while (len >= heap_size[i]) i++;
99 K->add_vec_to(heap[i], p);
100 len = K->n_nonzero_terms(heap[i]);
101 p = NULL;
102 while (len >= heap_size[i])
103 {
104 i++;
105 K->add_vec_to(heap[i], heap[i - 1]);
106 len = K->n_nonzero_terms(heap[i]);
107 heap[i - 1] = NULL;
108 }
109 if (i > top_of_heap) top_of_heap = i;
110}
111
112static int compare(const vecterm *t, const vecterm *s)
113{
114 int cmp = t->comp - s->comp;
115 if (cmp < 0) return LT;
116 if (cmp > 0) return GT;
117 return EQ;
118}
119
121{
122 int lead_so_far = -1;
123 for (int i = 0; i <= top_of_heap; i++)
124 {
125 if (heap[i] == NULL) continue;
126 if (lead_so_far < 0)
127 {
128 lead_so_far = i;
129 continue;
130 }
131 int cmp = compare(heap[lead_so_far], heap[i]);
132 if (cmp == GT) continue;
133 if (cmp == LT)
134 {
135 lead_so_far = i;
136 continue;
137 }
138 // At this point we have equality
139 K->add_to(heap[lead_so_far]->coeff, heap[i]->coeff);
140 vecterm *tmp = heap[i];
141 heap[i] = tmp->next;
142 tmp->next = NULL;
143 K->remove_vec(tmp);
144
145 if (K->is_zero(heap[lead_so_far]->coeff))
146 {
147 // Remove, and start over
148 tmp = heap[lead_so_far];
149 heap[lead_so_far] = tmp->next;
150 tmp->next = NULL;
151 K->remove_vec(tmp);
152 lead_so_far = -1;
153 i = -1;
154 }
155 }
156 mLead = lead_so_far;
157 if (lead_so_far < 0) return NULL;
158 vecterm *result = heap[lead_so_far];
159 return result;
160}
162{
163 if (mLead < 0) get_lead_term();
164 if (mLead < 0) return NULL;
166 heap[mLead] = result->next;
167 result->next = NULL;
168 mLead = -1;
169 return result;
170}
171
173{
174 vecterm *result = NULL;
175 for (int i = 0; i <= top_of_heap; i++)
176 {
177 if (heap[i] == NULL) continue;
178 K->add_vec_to(result, heap[i]);
179 heap[i] = NULL;
180 }
181 top_of_heap = -1;
182 return result;
183}
185{
186 vecterm *result = NULL;
187 for (int i = 0; i <= top_of_heap; i++)
188 {
189 if (heap[i] == NULL) continue;
190 vecterm *tmp = K->copy_vec(heap[i]);
191 K->add_vec_to(result, tmp);
192 }
193 return result;
194}
195
196// Local Variables:
197// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
198// indent-tabs-mode: nil
199// End:
Engine-side free module R^n over a Ring.
Definition freemod.hpp:66
xxx xxx xxx
Definition ring.hpp:102
~vecHeap()
Definition geovec.hpp:86
const FreeModule * F
Definition geovec.hpp:51
vecterm * value()
Definition geovec.hpp:172
const vecterm * get_lead_term()
Definition geovec.hpp:120
vecterm * debug_list(int i)
Definition geovec.hpp:69
int mLead
Definition geovec.hpp:55
vecterm * current_value() const
Definition geovec.hpp:184
const FreeModule * get_target() const
Definition geovec.hpp:66
const Ring * K
Definition geovec.hpp:52
vecterm * heap[GEOHEAP_SIZE]
Definition geovec.hpp:53
vecHeap(const FreeModule *F)
Definition geovec.hpp:78
vecterm * remove_lead_term()
Definition geovec.hpp:161
int top_of_heap
Definition geovec.hpp:54
void add(vecterm *p)
Definition geovec.hpp:93
const int heap_size[GEOHEAP_SIZE]
Definition engine.cpp:53
static int compare(const vecterm *t, const vecterm *s)
Definition geovec.hpp:112
int p
void size_t s
Definition m2-mem.cpp:271
VALGRIND_MAKE_MEM_DEFINED & result(result)
const int EQ
Definition style.hpp:40
const int GT
Definition style.hpp:41
#define GEOHEAP_SIZE
Definition style.hpp:46
const int LT
Definition style.hpp:39