1 | #include <gfanlib/gfanlib.h> |
---|
2 | |
---|
3 | #include <kernel/ideals.h> |
---|
4 | #include <Singular/subexpr.h> |
---|
5 | #include <libpolys/polys/monomials/p_polys.h> |
---|
6 | #include <libpolys/polys/simpleideals.h> |
---|
7 | |
---|
8 | #include <callgfanlib_conversion.h> |
---|
9 | #include <gfanlib_exceptions.h> |
---|
10 | |
---|
11 | #include <exception> |
---|
12 | |
---|
13 | /*** |
---|
14 | * Computes the weighted degree of the leading term of p with respect to w |
---|
15 | **/ |
---|
16 | long wDeg(const poly p, const ring r, const gfan::ZVector w) |
---|
17 | { |
---|
18 | long d=0; |
---|
19 | for (unsigned i=0; i<w.size(); i++) |
---|
20 | { |
---|
21 | if (!w[i].fitsInInt()) |
---|
22 | throw 0; //weightOverflow; |
---|
23 | d += p_GetExp(p,i+1,r)*w[i].toInt(); |
---|
24 | } |
---|
25 | return d; |
---|
26 | } |
---|
27 | |
---|
28 | /*** |
---|
29 | * Checks if p is sorted with respect to w. |
---|
30 | **/ |
---|
31 | static bool checkSloppyInput(const poly p, const ring r, const gfan::ZVector w) |
---|
32 | { |
---|
33 | long d = wDeg(p,r,w); |
---|
34 | for (poly currentTerm = p->next; currentTerm; pIter(currentTerm)) |
---|
35 | { |
---|
36 | long e = wDeg(currentTerm,r,w); |
---|
37 | if (e>d) |
---|
38 | return false; |
---|
39 | } |
---|
40 | return true; |
---|
41 | } |
---|
42 | |
---|
43 | /*** |
---|
44 | * Returns the terms of p of same weighted degree under w as the leading term. |
---|
45 | * Coincides with the initial form of p with respect to w if and only if p was already |
---|
46 | * sorted with respect to w in the sense that the leading term is of highest w-degree. |
---|
47 | **/ |
---|
48 | poly sloppyInitial(const poly p, const ring r, const gfan::ZVector w) |
---|
49 | { |
---|
50 | assume(checkSloppyInput(p,r,w)); |
---|
51 | int n = rVar(r); |
---|
52 | int* expv = (int*) omAlloc(n*sizeof(int)); |
---|
53 | poly q0 = p_Head(p,r); |
---|
54 | poly q1 = q0; |
---|
55 | long d = wDeg(p,r,w); |
---|
56 | for (poly currentTerm = p->next; currentTerm; pIter(currentTerm)) |
---|
57 | { |
---|
58 | if (wDeg(currentTerm,r,w) == d) |
---|
59 | { |
---|
60 | pNext(q1) = p_Head(currentTerm,r); |
---|
61 | pIter(q1); |
---|
62 | } |
---|
63 | } |
---|
64 | omFreeSize(expv,n*sizeof(int)); |
---|
65 | return q0; |
---|
66 | } |
---|
67 | |
---|
68 | /*** |
---|
69 | * Runs the above procedure over all generators of an ideal. |
---|
70 | * Coincides with the initial ideal of I with respect to w if and only if |
---|
71 | * the elements of I were already sorted with respect to w and |
---|
72 | * I is a standard basis form with respect to w. |
---|
73 | **/ |
---|
74 | ideal sloppyInitial(const ideal I, const ring r, const gfan::ZVector w) |
---|
75 | { |
---|
76 | int k = idSize(I); ideal inI = idInit(k); |
---|
77 | for (int i=0; i<k; i++) |
---|
78 | inI->m[i] = sloppyInitial(I->m[i],r,w); |
---|
79 | return inI; |
---|
80 | } |
---|
81 | |
---|
82 | /*** |
---|
83 | * Returns the first terms of p of same weighted degree under w, |
---|
84 | * this is not necessarily the initial form of p with respect to w! |
---|
85 | **/ |
---|
86 | poly initial(const poly p, const ring r, const gfan::ZVector w) |
---|
87 | { |
---|
88 | int n = r->N; |
---|
89 | int* expv = (int*) omAlloc(n*sizeof(int)); |
---|
90 | poly q0 = p_Head(p,r); |
---|
91 | poly q1 = q0; |
---|
92 | long d = wDeg(p,r,w); |
---|
93 | for (poly currentTerm = p->next; currentTerm; pIter(currentTerm)) |
---|
94 | { |
---|
95 | if (wDeg(currentTerm,r,w)==d) |
---|
96 | { |
---|
97 | pNext(q1) = p_Head(currentTerm,r); |
---|
98 | pIter(q1); |
---|
99 | } |
---|
100 | } |
---|
101 | omFreeSize(expv,n*sizeof(int)); |
---|
102 | return q0; |
---|
103 | } |
---|
104 | |
---|
105 | /*** |
---|
106 | * Runs the above procedure over all generators of an ideal. |
---|
107 | **/ |
---|
108 | ideal initial(const ideal I, const ring r, const gfan::ZVector w) |
---|
109 | { |
---|
110 | int k = idSize(I); ideal inI = idInit(k); |
---|
111 | for (int i=0; i<k; i++) |
---|
112 | inI->m[i] = initial(I->m[i],r,w); |
---|
113 | return inI; |
---|
114 | } |
---|
115 | |
---|
116 | #ifndef NDEBUG |
---|
117 | BOOLEAN initial0(leftv res, leftv args) |
---|
118 | { |
---|
119 | leftv u = args; |
---|
120 | ideal I = (ideal) u->CopyD(); |
---|
121 | leftv v = u->next; |
---|
122 | bigintmat* w0 = (bigintmat*) v->Data(); |
---|
123 | gfan::ZVector* w = bigintmatToZVector(w0); |
---|
124 | omUpdateInfo(); |
---|
125 | Print("usedBytesBefore=%ld\n",om_Info.UsedBytes); |
---|
126 | ideal inI = initial(I,currRing,*w); |
---|
127 | id_Delete(&I,currRing); |
---|
128 | delete w; |
---|
129 | res->rtyp = IDEAL_CMD; |
---|
130 | res->data = (char*) inI; |
---|
131 | return FALSE; |
---|
132 | } |
---|
133 | #endif |
---|
134 | |
---|
135 | |
---|
136 | /*** |
---|
137 | * Computes the initial form of p with respect to the first row in the order matrix |
---|
138 | **/ |
---|
139 | poly initial(const poly p, const ring r) |
---|
140 | { |
---|
141 | long d = p_Deg(p,r); |
---|
142 | poly initialForm0 = p_Head(p,r); |
---|
143 | poly initialForm1 = initialForm0; |
---|
144 | poly currentTerm = p->next; |
---|
145 | while (currentTerm && p_Deg(currentTerm,r)==d) |
---|
146 | { |
---|
147 | pNext(initialForm1) = p_Head(currentTerm,r); |
---|
148 | pIter(currentTerm); pIter(initialForm1); |
---|
149 | } |
---|
150 | return initialForm0; |
---|
151 | } |
---|
152 | |
---|
153 | /*** |
---|
154 | * Computes the initial form of all generators of I. |
---|
155 | * If I is a standard basis, then this is a standard basis |
---|
156 | * of the initial ideal. |
---|
157 | **/ |
---|
158 | ideal initial(const ideal I, const ring r) |
---|
159 | { |
---|
160 | int k = idSize(I); ideal inI = idInit(k); |
---|
161 | for (int i=0; i<k; i++) |
---|
162 | inI->m[i] = initial(I->m[i],r); |
---|
163 | return inI; |
---|
164 | } |
---|
165 | |
---|
166 | BOOLEAN initial(leftv res, leftv args) |
---|
167 | { |
---|
168 | leftv u = args; |
---|
169 | if ((u != NULL) && (u->Typ() == POLY_CMD) && (u->next == NULL)) |
---|
170 | { |
---|
171 | poly p = (poly) u->Data(); |
---|
172 | res->rtyp = POLY_CMD; |
---|
173 | res->data = (void*) initial(p, currRing); |
---|
174 | return FALSE; |
---|
175 | } |
---|
176 | if ((u != NULL) && (u->Typ() == IDEAL_CMD) && (u->next == NULL)) |
---|
177 | { |
---|
178 | ideal I = (ideal) u->Data(); |
---|
179 | res->rtyp = IDEAL_CMD; |
---|
180 | res->data = (void*) initial(I, currRing); |
---|
181 | return FALSE; |
---|
182 | } |
---|
183 | WerrorS("initial: unexpected parameters"); |
---|
184 | return TRUE; |
---|
185 | } |
---|