1 | #include "Singular/libsingular.h" |
---|
2 | |
---|
3 | static void makepartition(int vars,int actvar,int deg,int monomdeg, lists L, int &idpowerpoint) |
---|
4 | { |
---|
5 | poly p; |
---|
6 | int i=0; |
---|
7 | |
---|
8 | if ((idpowerpoint == 0) && (actvar ==1)) |
---|
9 | { |
---|
10 | L->m[idpowerpoint].rtyp=INTVEC_CMD; |
---|
11 | L->m[idpowerpoint].data=(char *)new intvec(vars); |
---|
12 | monomdeg = 0; |
---|
13 | } |
---|
14 | while (i<=deg) |
---|
15 | { |
---|
16 | if (deg == monomdeg) |
---|
17 | { |
---|
18 | idpowerpoint++; |
---|
19 | return; |
---|
20 | } |
---|
21 | if (actvar == vars) |
---|
22 | { |
---|
23 | intvec *v=(intvec*)L->m[idpowerpoint].data; |
---|
24 | (*v)[actvar-1]=deg-monomdeg; |
---|
25 | idpowerpoint++; |
---|
26 | return; |
---|
27 | } |
---|
28 | else |
---|
29 | { |
---|
30 | intvec *v=(intvec*)L->m[idpowerpoint].data; |
---|
31 | intvec *vv=ivCopy(v); |
---|
32 | makepartition(vars,actvar+1,deg,monomdeg,L,idpowerpoint); |
---|
33 | L->m[idpowerpoint].data=(char *)vv; |
---|
34 | L->m[idpowerpoint].rtyp=INTVEC_CMD; |
---|
35 | } |
---|
36 | monomdeg++; |
---|
37 | intvec *v=(intvec*)L->m[idpowerpoint].data; |
---|
38 | (*v)[actvar-1]=(*v)[actvar-1]+1; |
---|
39 | i++; |
---|
40 | } |
---|
41 | } |
---|
42 | lists lPartition(int sum, int elem) |
---|
43 | { |
---|
44 | lists L=(lists)omAlloc0Bin(slists_bin); |
---|
45 | if (sum < 0) |
---|
46 | { |
---|
47 | WarnS("partition: power must be non-negative"); |
---|
48 | } |
---|
49 | int i = binom(elem+sum-1,sum); |
---|
50 | if ((sum < 1)||(i<1)) |
---|
51 | { |
---|
52 | L->Init(); |
---|
53 | return L; |
---|
54 | } |
---|
55 | L->Init(i); |
---|
56 | int idpowerpoint = 0; |
---|
57 | makepartition(elem,1,sum,0,L,idpowerpoint); |
---|
58 | return L; |
---|
59 | } |
---|
60 | |
---|
61 | static BOOLEAN partition(leftv res, leftv arg) |
---|
62 | { |
---|
63 | if ((arg!=NULL)&&(arg->Typ()==INT_CMD) |
---|
64 | && (arg->next!=NULL) &&(arg->next->Typ()==INT_CMD) |
---|
65 | && (arg->next->next==NULL)) |
---|
66 | { |
---|
67 | res->data=(void*)lPartition((int)(long)arg->Data(),(int)(long)arg->next->Data()); |
---|
68 | res->rtyp=LIST_CMD; |
---|
69 | return FALSE; |
---|
70 | } |
---|
71 | return TRUE; |
---|
72 | } |
---|
73 | extern "C" int mod_init(SModulFunctions* psModulFunctions) |
---|
74 | { |
---|
75 | // this is the initialization routine of the module |
---|
76 | // adding the routine hello: |
---|
77 | psModulFunctions->iiAddCproc( |
---|
78 | (currPack->libname? currPack->libname: ""),// the library name, |
---|
79 | // rely on the loader to set it in currPack->libname |
---|
80 | "partition",// the name for the singular interpreter |
---|
81 | FALSE, // should not be static |
---|
82 | partition); // the C/C++ routine |
---|
83 | const char* partition_help="USAGE: partition(int a, int b)\n" |
---|
84 | "RETURN: list of intvec with b entries and sum a"; |
---|
85 | module_help_proc( |
---|
86 | (currPack->libname? currPack->libname: ""),// the library name, |
---|
87 | "partition", // the name of the procedure |
---|
88 | partition_help); // the help string |
---|
89 | module_help_main( |
---|
90 | (currPack->libname? currPack->libname: ""),// the library name, |
---|
91 | "partition"); // the help string for the module |
---|
92 | VAR return MAX_TOK; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|