source: git/Singular/bbfan.cc @ c90500

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