source: git/Singular/bbfan.cc @ 0fb34ba

spielwiese
Last change on this file since 0fb34ba was 0fb34ba, checked in by Hans Schoenemann <hannes@…>, 13 years ago
start with dir Singular: fix includes and makefile
  • Property mode set to 100644
File size: 6.4 KB
Line 
1#include <kernel/mod2.h>
2#ifdef HAVE_FANS
3
4#include <Singular/ipid.h>
5#include <Singular/blackbox.h>
6#include <omalloc/omalloc.h>
7#include <kernel/febase.h>
8#include <kernel/longrat.h>
9#include <Singular/subexpr.h>
10#include <kernel/bbfan.h>
11#include <Singular/bbcone.h>
12#include <ipshell.h>
13#include <misc/intvec.h>
14#include <sstream>
15#include <gfanlib/gfanlib.h>
16
17int fanID;
18
19void *bbfan_Init(blackbox *b)
20{
21  return (void*)(new gfan::ZFan(0));
22}
23
24void bbfan_destroy(blackbox *b, void *d)
25{
26  if (d!=NULL)
27  {
28    gfan::ZFan* zf = (gfan::ZFan*) d;
29    delete zf;
30  }
31}
32
33
34char * bbfan_String(blackbox *b, void *d)
35{ 
36  if (d==NULL) return omStrDup("invalid object");
37  else
38  {
39    gfan::ZFan* zf = (gfan::ZFan*)d;
40    std::string s = zf->toString();
41    char* ns = (char*) omAlloc(strlen(s.c_str()) + 10);
42    sprintf(ns, "%s", s.c_str());
43    omCheckAddr(ns);
44    return ns;
45  }
46}
47
48void * bbfan_Copy(blackbox*b, void *d)
49{       
50  gfan::ZFan* zf = (gfan::ZFan*)d;
51  gfan::ZFan* newZf = new gfan::ZFan(*zf);
52  return newZf;
53}
54
55BOOLEAN bbfan_Assign(leftv l, leftv r)
56{
57  gfan::ZFan* newZf;
58  if (r==NULL)
59  { 
60    if (l->Data()!=NULL) 
61    {   
62      gfan::ZFan* zd = (gfan::ZFan*)l->Data();
63      delete zd;
64    }
65    newZf = new gfan::ZFan(0);
66  }
67  else if (r->Typ()==l->Typ())
68  {
69    if (l->Data()!=NULL)
70    {   
71      gfan::ZFan* zd = (gfan::ZFan*)l->Data();
72      delete zd;
73    }
74    gfan::ZFan* zf = (gfan::ZFan*)r->Data();
75    newZf = new gfan::ZFan(*zf);
76  }
77  else if (r->Typ()==INT_CMD)
78  {
79    int ambientDim = (int)(long)r->Data();
80    if (ambientDim < 0)
81    {
82      Werror("expected an int >= 0, but got %d", ambientDim);
83      return TRUE;
84    }
85    if (l->Data()!=NULL)
86    {   
87      gfan::ZFan* zd = (gfan::ZFan*)l->Data();
88      delete zd;
89    }
90    newZf = new gfan::ZFan(ambientDim);
91  }
92  else
93  {
94    Werror("assign Type(%d) = Type(%d) not implemented",l->Typ(),r->Typ());
95    return TRUE;
96  }
97 
98  if (l->rtyp==IDHDL)
99  {
100    IDDATA((idhdl)l->data)=(char *)newZf;
101  }
102  else
103  {
104    l->data=(void *)newZf;
105  }
106  return FALSE;
107}
108
109/* returns 1 iff all rows consist of entries 1..n,
110   where n is the number of columns of the provided
111   intmat; 0 otherwise */
112static gfan::IntMatrix permutationIntMatrix(const intvec* iv)
113{
114        int cc = iv->cols();
115        int rr = iv->rows();
116        intvec* ivCopy = new intvec(rr, cc, 0);
117        for (int r = 1; r <= rr; r++)
118          for (int c = 1; c <= cc; c++)
119            IMATELEM(*ivCopy, r, c) = IMATELEM(*iv, r, c) - 1;
120        gfan::ZMatrix zm = intmat2ZMatrix(ivCopy);
121        gfan::IntMatrix* im = new gfan::IntMatrix(gfan::ZToIntMatrix(zm));
122        return *im;
123}
124static BOOLEAN jjFANEMPTY_I(leftv res, leftv v)
125{
126  int ambientDim = (int)(long)v->Data();
127  if (ambientDim < 0)
128  {
129    Werror("expected non-negative ambient dim but got %d", ambientDim);
130    return TRUE;
131  }
132  res->rtyp = fanID;
133  res->data = (char*)(new gfan::ZFan(ambientDim));
134  return FALSE;
135}
136static BOOLEAN jjFANEMPTY_IM(leftv res, leftv v)
137{
138  intvec* permutations = (intvec*)v->Data();
139  int ambientDim = permutations->cols();
140  gfan::IntMatrix im = permutationIntMatrix(permutations);
141  if (!gfan::Permutation::arePermutations(im))
142  {
143    Werror("provided intmat contains invalid permutations of {1, ..., %d}", ambientDim);
144    return TRUE;
145  }
146  gfan::SymmetryGroup sg = gfan::SymmetryGroup(ambientDim);
147  sg.computeClosure(im);
148  res->rtyp = fanID;
149  res->data = (char*)(new gfan::ZFan(sg));
150  return FALSE;
151}
152
153BOOLEAN fan_empty(leftv res, leftv args)
154{
155  leftv u = args;
156  if (u == NULL)
157  {
158    res->rtyp = fanID;
159    res->data = (void*)(new gfan::ZFan(0));
160    return FALSE;
161  }
162  if ((u != NULL) && (u->Typ() == INT_CMD))
163  {
164    if (u->next == NULL) return jjFANEMPTY_I(res, u);
165  }
166  if ((u != NULL) && (u->Typ() == INTMAT_CMD))
167  {
168    if (u->next == NULL) return jjFANEMPTY_IM(res, u);
169  }
170  WerrorS("fan_empty: unexpected parameters");
171  return TRUE;
172}
173
174static BOOLEAN jjFANFULL_I(leftv res, leftv v)
175{
176  int ambientDim = (int)(long)v->Data();
177  if (ambientDim < 0)
178  {
179    Werror("expected non-negative ambient dim but got %d", ambientDim);
180    return TRUE;
181  }
182  gfan::ZFan* zf = new gfan::ZFan(gfan::ZFan::fullFan(ambientDim));
183  res->rtyp = fanID;
184  res->data = (char*)zf;
185  return FALSE;
186}
187static BOOLEAN jjFANFULL_IM(leftv res, leftv v)
188{
189  intvec* permutations = (intvec*)v->Data();
190  int ambientDim = permutations->cols();
191  gfan::IntMatrix im = permutationIntMatrix(permutations);
192  if (!gfan::Permutation::arePermutations(im))
193  {
194    Werror("provided intmat contains invalid permutations of {1, ..., %d}", ambientDim);
195    return TRUE;
196  }
197  gfan::SymmetryGroup sg = gfan::SymmetryGroup(ambientDim);
198  sg.computeClosure(im);
199  gfan::ZFan* zf = new gfan::ZFan(gfan::ZFan::fullFan(sg));
200  res->rtyp = fanID;
201  res->data = (char*)zf;
202  return FALSE;
203}
204
205BOOLEAN fan_full(leftv res, leftv args)
206{
207  /*  {
208    gfan::ZFan f(2);
209    std::cout<<f.toString();
210    f.insert(gfan::ZCone::positiveOrthant(2));
211    std::cout<<f.toString();
212    }*/
213
214
215  leftv u = args;
216  if (u == NULL)
217  {
218    res->rtyp = fanID;
219    res->data = (void*)(new gfan::ZFan(0));
220    return FALSE;
221  }
222  if ((u != NULL) && (u->Typ() == INT_CMD))
223  {
224    if (u->next == NULL) return jjFANFULL_I(res, u);
225  }
226  if ((u != NULL) && (u->Typ() == INTMAT_CMD))
227  {
228    if (u->next == NULL) return jjFANFULL_IM(res, u);
229  }
230  WerrorS("fan_full: unexpected parameters");
231  return TRUE;
232}
233
234BOOLEAN insert_cone(leftv res, leftv args)
235{
236  leftv u=args;
237  if ((u != NULL) && (u->Typ() == fanID))
238  {
239    leftv v=u->next;
240    if ((v != NULL) && (v->Typ() == coneID))
241    {
242      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
243      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
244      zc->canonicalize();
245      zf->insert(*zc);
246      res->rtyp = NONE;
247      res->data = NULL;
248      return FALSE;
249    }
250  }
251  WerrorS("insert_cone: unexpected parameters");
252  return TRUE;
253}
254
255void bbfan_setup()
256{
257  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
258  // all undefined entries will be set to default in setBlackboxStuff
259  // the default Print is quite usefule,
260  // all other are simply error messages
261  b->blackbox_destroy=bbfan_destroy;
262  b->blackbox_String=bbfan_String;
263  //b->blackbox_Print=blackbox_default_Print;
264  b->blackbox_Init=bbfan_Init;
265  b->blackbox_Copy=bbfan_Copy;
266  b->blackbox_Assign=bbfan_Assign;
267  iiAddCproc("","fan_empty",FALSE,fan_empty);
268  iiAddCproc("","fan_full",FALSE,fan_full);
269  iiAddCproc("","insert_cone",FALSE,insert_cone);
270  fanID=setBlackboxStuff(b,"fan");
271  Print("created type %d (fan)\n",fanID); 
272}
273
274#endif
275/* HAVE_FANS */
Note: See TracBrowser for help on using the repository browser.