1 | #include <kernel/polys.h> |
---|
2 | #include <libpolys/coeffs/longrat.h> |
---|
3 | #include <bbcone.h> |
---|
4 | |
---|
5 | |
---|
6 | poly initial(poly p) |
---|
7 | { |
---|
8 | poly g = p; |
---|
9 | poly h = p_Head(g,currRing); |
---|
10 | poly f = h; |
---|
11 | long d = p_Deg(g,currRing); |
---|
12 | pIter(g); |
---|
13 | while ((g != NULL) && (p_Deg(g,currRing) == d)) |
---|
14 | { |
---|
15 | pNext(h) = p_Head(g,currRing); |
---|
16 | pIter(h); |
---|
17 | pIter(g); |
---|
18 | } |
---|
19 | return(f); |
---|
20 | } |
---|
21 | |
---|
22 | |
---|
23 | BOOLEAN initial(leftv res, leftv args) |
---|
24 | { |
---|
25 | leftv u = args; |
---|
26 | if ((u != NULL) && (u->Typ() == POLY_CMD)) |
---|
27 | { |
---|
28 | leftv v = u->next; |
---|
29 | if (v == NULL) |
---|
30 | { |
---|
31 | poly p = (poly) u->Data(); |
---|
32 | res->rtyp = POLY_CMD; |
---|
33 | res->data = (void*) initial(p); |
---|
34 | return FALSE; |
---|
35 | } |
---|
36 | } |
---|
37 | if ((u != NULL) && (u->Typ() == IDEAL_CMD)) |
---|
38 | { |
---|
39 | leftv v = u->next; |
---|
40 | if (v == NULL) |
---|
41 | { |
---|
42 | ideal I = (ideal) u->Data(); |
---|
43 | ideal inI = idInit(IDELEMS(I)); |
---|
44 | for (int i=0; i<IDELEMS(I); i++) |
---|
45 | inI->m[i]=initial(I->m[i]); |
---|
46 | res->rtyp = IDEAL_CMD; |
---|
47 | res->data = (void*) inI; |
---|
48 | return FALSE; |
---|
49 | } |
---|
50 | } |
---|
51 | WerrorS("initial: unexpected parameters"); |
---|
52 | return TRUE; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | BOOLEAN homogeneitySpace(leftv res, leftv args) |
---|
57 | { |
---|
58 | leftv u = args; |
---|
59 | if ((u != NULL) && (u->Typ() == IDEAL_CMD)) |
---|
60 | { |
---|
61 | leftv v = u->next; |
---|
62 | if (v == NULL) |
---|
63 | { |
---|
64 | int n = currRing->N; |
---|
65 | ideal I = (ideal) u->Data(); |
---|
66 | poly g; |
---|
67 | int* leadexpv = (int*) omAlloc((n+1)*sizeof(int)); |
---|
68 | int* tailexpv = (int*) omAlloc((n+1)*sizeof(int)); |
---|
69 | gfan::ZVector leadexpw = gfan::ZVector(n); |
---|
70 | gfan::ZVector tailexpw = gfan::ZVector(n); |
---|
71 | gfan::ZMatrix equations = gfan::ZMatrix(0,n); |
---|
72 | for (int i=0; i<IDELEMS(I); i++) |
---|
73 | { |
---|
74 | g = (poly) I->m[i]; pGetExpV(g,leadexpv); |
---|
75 | leadexpw = intStar2ZVector(n, leadexpv); |
---|
76 | pIter(g); |
---|
77 | while (g != NULL) |
---|
78 | { |
---|
79 | pGetExpV(g,tailexpv); |
---|
80 | tailexpw = intStar2ZVector(n, tailexpv); |
---|
81 | equations.appendRow(leadexpw-tailexpw); |
---|
82 | pIter(g); |
---|
83 | } |
---|
84 | } |
---|
85 | gfan::ZCone* gCone = new gfan::ZCone(gfan::ZMatrix(0, equations.getWidth()),equations); |
---|
86 | omFreeSize(leadexpv,(n+1)*sizeof(int)); |
---|
87 | omFreeSize(tailexpv,(n+1)*sizeof(int)); |
---|
88 | |
---|
89 | res->rtyp = coneID; |
---|
90 | res->data = (void*) gCone; |
---|
91 | return FALSE; |
---|
92 | } |
---|
93 | } |
---|
94 | WerrorS("homogeneitySpace: unexpected parameters"); |
---|
95 | return TRUE; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | BOOLEAN groebnerCone(leftv res, leftv args) |
---|
100 | { |
---|
101 | leftv u = args; |
---|
102 | if ((u != NULL) && (u->Typ() == IDEAL_CMD)) |
---|
103 | { |
---|
104 | leftv v = u->next; |
---|
105 | if (v == NULL) |
---|
106 | { |
---|
107 | int n = currRing->N; |
---|
108 | ideal I = (ideal) u->Data(); |
---|
109 | poly g = NULL; |
---|
110 | int* leadexpv = (int*) omAlloc((n+1)*sizeof(int)); |
---|
111 | int* tailexpv = (int*) omAlloc((n+1)*sizeof(int)); |
---|
112 | gfan::ZVector leadexpw = gfan::ZVector(n); |
---|
113 | gfan::ZVector tailexpw = gfan::ZVector(n); |
---|
114 | gfan::ZMatrix inequalities = gfan::ZMatrix(0,n); |
---|
115 | gfan::ZMatrix equations = gfan::ZMatrix(0,n); |
---|
116 | long d; |
---|
117 | for (int i=0; i<IDELEMS(I); i++) |
---|
118 | { |
---|
119 | g = (poly) I->m[i]; pGetExpV(g,leadexpv); |
---|
120 | leadexpw = intStar2ZVector(n, leadexpv); |
---|
121 | pIter(g); |
---|
122 | d = p_Deg(g,currRing); |
---|
123 | while ((g != NULL) && (p_Deg(g,currRing) == d)) |
---|
124 | { |
---|
125 | pGetExpV(g,tailexpv); |
---|
126 | tailexpw = intStar2ZVector(n, tailexpv); |
---|
127 | equations.appendRow(leadexpw-tailexpw); |
---|
128 | pIter(g); |
---|
129 | } |
---|
130 | |
---|
131 | if (g != NULL) |
---|
132 | { |
---|
133 | while (g != NULL) |
---|
134 | { |
---|
135 | pGetExpV(g,tailexpv); |
---|
136 | tailexpw = intStar2ZVector(n, tailexpv); |
---|
137 | inequalities.appendRow(leadexpw-tailexpw); |
---|
138 | pIter(g); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | gfan::ZCone* gCone = new gfan::ZCone(inequalities,equations); |
---|
143 | omFreeSize(leadexpv,(n+1)*sizeof(int)); |
---|
144 | omFreeSize(tailexpv,(n+1)*sizeof(int)); |
---|
145 | |
---|
146 | res->rtyp = coneID; |
---|
147 | res->data = (void*) gCone; |
---|
148 | return FALSE; |
---|
149 | } |
---|
150 | } |
---|
151 | WerrorS("groebnerCone: unexpected parameters"); |
---|
152 | return TRUE; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | BOOLEAN maximalGroebnerCone(leftv res, leftv args) |
---|
157 | { |
---|
158 | leftv u = args; |
---|
159 | if ((u != NULL) && (u->Typ() == IDEAL_CMD)) |
---|
160 | { |
---|
161 | leftv v = u->next; |
---|
162 | if (v == NULL) |
---|
163 | { |
---|
164 | int n = currRing->N; |
---|
165 | ideal I = (ideal) u->Data(); |
---|
166 | poly g = NULL; |
---|
167 | int* leadexpv = (int*) omAlloc((n+1)*sizeof(int)); |
---|
168 | int* tailexpv = (int*) omAlloc((n+1)*sizeof(int)); |
---|
169 | gfan::ZVector leadexpw = gfan::ZVector(n); |
---|
170 | gfan::ZVector tailexpw = gfan::ZVector(n); |
---|
171 | gfan::ZMatrix inequalities = gfan::ZMatrix(0,n); |
---|
172 | for (int i=0; i<IDELEMS(I); i++) |
---|
173 | { |
---|
174 | g = (poly) I->m[i]; pGetExpV(g,leadexpv); |
---|
175 | leadexpw = intStar2ZVector(n, leadexpv); |
---|
176 | pIter(g); |
---|
177 | while (g != NULL) |
---|
178 | { |
---|
179 | pGetExpV(g,tailexpv); |
---|
180 | tailexpw = intStar2ZVector(n, tailexpv); |
---|
181 | inequalities.appendRow(leadexpw-tailexpw); |
---|
182 | pIter(g); |
---|
183 | } |
---|
184 | } |
---|
185 | gfan::ZCone* gCone = new gfan::ZCone(inequalities,gfan::ZMatrix(0, inequalities.getWidth())); |
---|
186 | omFreeSize(leadexpv,(n+1)*sizeof(int)); |
---|
187 | omFreeSize(tailexpv,(n+1)*sizeof(int)); |
---|
188 | |
---|
189 | res->rtyp = coneID; |
---|
190 | res->data = (void*) gCone; |
---|
191 | return FALSE; |
---|
192 | } |
---|
193 | } |
---|
194 | WerrorS("maximalGroebnerCone: unexpected parameters"); |
---|
195 | return TRUE; |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | void tropical_setup(SModulFunctions* p) |
---|
200 | { |
---|
201 | p->iiAddCproc("","groebnerCone",FALSE,groebnerCone); |
---|
202 | p->iiAddCproc("","maximalGroebnerCone",FALSE,maximalGroebnerCone); |
---|
203 | p->iiAddCproc("","initial",FALSE,initial); |
---|
204 | p->iiAddCproc("","homogeneitySpace",FALSE,homogeneitySpace); |
---|
205 | } |
---|