My Project
bbpolytope.cc
Go to the documentation of this file.
1#include "kernel/mod2.h"
2
3#if HAVE_GFANLIB
4
5#include "Singular/ipid.h"
6#include "Singular/ipshell.h"
7#include "Singular/blackbox.h"
8#include "misc/intvec.h"
9#include "coeffs/bigintmat.h"
10
12
13#include "gfanlib/gfanlib.h"
14#include "gfanlib/gfanlib_q.h"
15
17
18std::string bbpolytopeToString(gfan::ZCone const &c)
19{
20 std::stringstream s;
21 gfan::ZMatrix i=c.getInequalities();
22 gfan::ZMatrix e=c.getEquations();
23 s<<"AMBIENT_DIM"<<std::endl;
24 s<<c.ambientDimension()-1<<std::endl;
25 s<<"INEQUALITIES"<<std::endl;
26 s<<toString(i)<<std::endl;
27 s<<"EQUATIONS"<<std::endl;
28 s<<toString(e)<<std::endl;
29 return s.str();
30}
31
32void *bbpolytope_Init(blackbox* /*b*/)
33{
34 return (void*)(new gfan::ZCone());
35}
36
38{
39 gfan::ZCone* newZc;
40 if (r==NULL)
41 {
42 if (l->Data()!=NULL)
43 {
44 gfan::ZCone* zd = (gfan::ZCone*)l->Data();
45 delete zd;
46 }
47 newZc = new gfan::ZCone();
48 }
49 else if (r->Typ()==l->Typ())
50 {
51 if (l->Data()!=NULL)
52 {
53 gfan::ZCone* zd = (gfan::ZCone*)l->Data();
54 delete zd;
55 }
56 gfan::ZCone* zc = (gfan::ZCone*)r->Data();
57 newZc = new gfan::ZCone(*zc);
58 }
59 // else if (r->Typ()==INT_CMD) TODO:r->Typ()==BIGINTMAT_CMD
60 // {
61 // int ambientDim = (int)(long)r->Data();
62 // if (ambientDim < 0)
63 // {
64 // Werror("expected an int >= 0, but got %d", ambientDim);
65 // return TRUE;
66 // }
67 // if (l->Data()!=NULL)
68 // {
69 // gfan::ZCone* zd = (gfan::ZCone*)l->Data();
70 // delete zd;
71 // }
72 // newZc = new gfan::ZCone(ambientDim);
73 // }
74 else
75 {
76 Werror("assign Type(%d) = Type(%d) not implemented",l->Typ(),r->Typ());
77 return TRUE;
78 }
79
80 if (l->rtyp==IDHDL)
81 {
82 IDDATA((idhdl)l->data) = (char*) newZc;
83 }
84 else
85 {
86 l->data=(void *)newZc;
87 }
88 return FALSE;
89}
90
91char* bbpolytope_String(blackbox* /*b*/, void *d)
92{ if (d==NULL) return omStrDup("invalid object");
93 else
94 {
95 gfan::ZCone* zc = (gfan::ZCone*)d;
97 return omStrDup(s.c_str());
98 }
99}
100
101void bbpolytope_destroy(blackbox* /*b*/, void *d)
102{
103 if (d!=NULL)
104 {
105 gfan::ZCone* zc = (gfan::ZCone*) d;
106 delete zc;
107 }
108}
109
110void* bbpolytope_Copy(blackbox* /*b*/, void *d)
111{
112 gfan::ZCone* zc = (gfan::ZCone*)d;
113 gfan::ZCone* newZc = new gfan::ZCone(*zc);
114 return newZc;
115}
116
118{
119 /* method for generating a cone object from half-lines
120 (cone = convex hull of the half-lines; note: there may be
121 entire lines in the cone);
122 valid parametrizations: (bigintmat) */
124 if (v->Typ() == INTMAT_CMD)
125 {
126 intvec* rays0 = (intvec*) v->Data();
127 rays = iv2bim(rays0,coeffs_BIGINT);
128 }
129 else
130 rays = (bigintmat*) v->Data();
131
132 gfan::ZMatrix* zm = bigintmatToZMatrix(rays);
133 gfan::ZCone* zc = new gfan::ZCone();
134 *zc = gfan::ZCone::givenByRays(*zm, gfan::ZMatrix(0, zm->getWidth()));
135 res->rtyp = polytopeID;
136 res->data = (void*) zc;
137
138 delete zm;
139 if (v->Typ() == INTMAT_CMD)
140 delete rays;
141 return FALSE;
142}
143
145{
146 /* method for generating a cone object from half-lines
147 (any point in the cone being the sum of a point
148 in the convex hull of the half-lines and a point in the span
149 of the lines), and an integer k;
150 valid parametrizations: (bigintmat, int);
151 Errors will be invoked in the following cases:
152 - k not 0 or 1;
153 if the k=1, then the extreme rays are known:
154 each half-line spans a (different) extreme ray */
156 if (u->Typ() == INTMAT_CMD)
157 {
158 intvec* rays0 = (intvec*) u->Data();
159 rays = iv2bim(rays0,coeffs_BIGINT);
160 }
161 else
162 rays = (bigintmat*) u->Data();
163 int k = (int)(long)v->Data();
164
165 if ((k < 0) || (k > 1))
166 {
167 WerrorS("expected int argument in [0..1]");
168 return TRUE;
169 }
170 k=k*2;
171 gfan::ZMatrix* zm = bigintmatToZMatrix(rays);
172 gfan::ZCone* zc = new gfan::ZCone();
173 *zc = gfan::ZCone::givenByRays(*zm,gfan::ZMatrix(0, zm->getWidth()));
174 //k should be passed on to zc; not available yet
175 res->rtyp = polytopeID;
176 res->data = (void*) zc;
177
178 delete zm;
179 if (v->Typ() == INTMAT_CMD)
180 delete rays;
181 return FALSE;
182}
183
185{
186 leftv u = args;
187 if ((u != NULL) && ((u->Typ() == BIGINTMAT_CMD) || (u->Typ() == INTMAT_CMD)))
188 {
189 if (u->next == NULL)
190 {
191 gfan::initializeCddlibIfRequired();
192 BOOLEAN bo = ppCONERAYS1(res, u);
193 gfan::deinitializeCddlibIfRequired();
194 return bo;
195 }
196 leftv v = u->next;
197 if ((v != NULL) && (v->Typ() == INT_CMD))
198 {
199 if (v->next == NULL)
200 {
201 gfan::initializeCddlibIfRequired();
202 BOOLEAN bo = ppCONERAYS3(res, u, v);
203 gfan::deinitializeCddlibIfRequired();
204 return bo;
205 }
206 }
207 }
208 WerrorS("polytopeViaPoints: unexpected parameters");
209 return TRUE;
210}
211
213{
214 /* method for generating a cone object from inequalities;
215 valid parametrizations: (bigintmat) */
216 bigintmat* ineq = NULL;
217 if (v->Typ() == INTMAT_CMD)
218 {
219 intvec* ineq0 = (intvec*) v->Data();
220 ineq = iv2bim(ineq0,coeffs_BIGINT);
221 }
222 else
223 ineq = (bigintmat*) v->Data();
224 gfan::ZMatrix* zm = bigintmatToZMatrix(ineq);
225 gfan::ZCone* zc = new gfan::ZCone(*zm, gfan::ZMatrix(0, zm->getWidth()));
226 delete zm;
227 if (v->Typ() == INTMAT_CMD)
228 delete ineq;
229 res->rtyp = polytopeID;
230 res->data = (void*) zc;
231 return FALSE;
232}
233
235{
236 /* method for generating a cone object from iequalities,
237 and equations (...)
238 valid parametrizations: (bigintmat, bigintmat)
239 Errors will be invoked in the following cases:
240 - u and v have different numbers of columns */
241 bigintmat* ineq = NULL; bigintmat* eq = NULL;
242 if (u->Typ() == INTMAT_CMD)
243 {
244 intvec* ineq0 = (intvec*) u->Data();
245 ineq = iv2bim(ineq0,coeffs_BIGINT);
246 }
247 else
248 ineq = (bigintmat*) u->Data();
249 if (v->Typ() == INTMAT_CMD)
250 {
251 intvec* eq0 = (intvec*) v->Data();
252 eq = iv2bim(eq0,coeffs_BIGINT);
253 }
254 else
255 eq = (bigintmat*) v->Data();
256
257 if (ineq->cols() != eq->cols())
258 {
259 Werror("expected same number of columns but got %d vs. %d",
260 ineq->cols(), eq->cols());
261 return TRUE;
262 }
263 gfan::ZMatrix* zm1 = bigintmatToZMatrix(ineq);
264 gfan::ZMatrix* zm2 = bigintmatToZMatrix(eq);
265 gfan::ZCone* zc = new gfan::ZCone(*zm1, *zm2);
266 delete zm1;
267 delete zm2;
268 if (u->Typ() == INTMAT_CMD)
269 delete ineq;
270 if (v->Typ() == INTMAT_CMD)
271 delete eq;
272
273 res->rtyp = polytopeID;
274 res->data = (void*) zc;
275 return FALSE;
276}
277
279{
280 /* method for generating a cone object from inequalities, equations,
281 and an integer k;
282 valid parametrizations: (bigintmat, bigintmat, int);
283 Errors will be invoked in the following cases:
284 - u and v have different numbers of columns,
285 - k not in [0..3];
286 if the 2^0-bit of k is set, then ... */
287 bigintmat* ineq = NULL; bigintmat* eq = NULL;
288 if (u->Typ() == INTMAT_CMD)
289 {
290 intvec* ineq0 = (intvec*) u->Data();
291 ineq = iv2bim(ineq0,coeffs_BIGINT);
292 }
293 else
294 ineq = (bigintmat*) u->Data();
295 if (v->Typ() == INTMAT_CMD)
296 {
297 intvec* eq0 = (intvec*) v->Data();
298 eq = iv2bim(eq0,coeffs_BIGINT);
299 }
300 else
301 eq = (bigintmat*) v->Data();
302
303 if (ineq->cols() != eq->cols())
304 {
305 Werror("expected same number of columns but got %d vs. %d",
306 ineq->cols(), eq->cols());
307 return TRUE;
308 }
309 int k = (int)(long)w->Data();
310 if ((k < 0) || (k > 3))
311 {
312 WerrorS("expected int argument in [0..3]");
313 return TRUE;
314 }
315 gfan::ZMatrix* zm1 = bigintmatToZMatrix(ineq);
316 gfan::ZMatrix* zm2 = bigintmatToZMatrix(eq);
317 gfan::ZCone* zc = new gfan::ZCone(*zm1, *zm2, k);
318 delete zm1;
319 delete zm2;
320 if (u->Typ() == INTMAT_CMD)
321 delete ineq;
322 if (v->Typ() == INTMAT_CMD)
323 delete eq;
324
325 res->rtyp = polytopeID;
326 res->data = (void*) zc;
327 return FALSE;
328}
329
331{
332 leftv u = args;
333 if ((u != NULL) && ((u->Typ() == BIGINTMAT_CMD) || (u->Typ() == INTMAT_CMD)))
334 {
335 if (u->next == NULL)