1 | #include <callgfanlib_conversion.h> |
---|
2 | #include <groebnerCone.h> |
---|
3 | #include <tropicalVarietyOfPolynomials.h> |
---|
4 | #include <tropicalVarietyOfIdeals.h> |
---|
5 | #include <libpolys/coeffs/numbers.h> |
---|
6 | #include <kernel/structs.h> |
---|
7 | |
---|
8 | static poly adjustPoly(const poly g, const number p, const ring r) |
---|
9 | { |
---|
10 | poly currentTerm = g; |
---|
11 | poly fBeginning = p_Head(currentTerm,r); |
---|
12 | poly f = fBeginning; |
---|
13 | number coeff0 = p_GetCoeff(currentTerm,r); |
---|
14 | unsigned power = 0; |
---|
15 | while (n_DivBy(coeff0,p,r->cf)) |
---|
16 | { |
---|
17 | power++; |
---|
18 | number coeff1 = n_Div(coeff0,p,r->cf); |
---|
19 | n_Delete(&coeff0,r->cf); |
---|
20 | coeff0 = coeff1; |
---|
21 | } |
---|
22 | p_SetCoeff(f,coeff0,r); |
---|
23 | p_AddExp(f,1,power,r); p_Setm(f,r); |
---|
24 | |
---|
25 | for (pIter(currentTerm); currentTerm; pIter(currentTerm)) |
---|
26 | { |
---|
27 | f->next = p_Head(currentTerm,r); |
---|
28 | pIter(f); |
---|
29 | coeff0 = p_GetCoeff(currentTerm,r); |
---|
30 | power = 0; |
---|
31 | while (n_DivBy(coeff0,p,r->cf)) |
---|
32 | { |
---|
33 | power++; |
---|
34 | number coeff1 = n_Div(coeff0,p,r->cf); |
---|
35 | n_Delete(&coeff0,r->cf); |
---|
36 | coeff0 = coeff1; |
---|
37 | } |
---|
38 | p_SetCoeff(f,coeff0,r); |
---|
39 | p_AddExp(f,1,power,r); p_Setm(f,r); |
---|
40 | } |
---|
41 | return fBeginning; |
---|
42 | } |
---|
43 | |
---|
44 | static ideal adjustIdeal(const ideal I, const number p, const ring r) |
---|
45 | { |
---|
46 | int k = idSize(I); |
---|
47 | ideal J = idInit(k+1); |
---|
48 | for (int i = 0; i<k; i++) |
---|
49 | J->m[i+1] = adjustPoly(I->m[i],p,r); |
---|
50 | poly h0 = p_One(r); |
---|
51 | p_SetCoeff(h0,n_Copy(p,r->cf),r); |
---|
52 | poly h1 = p_One(r); |
---|
53 | p_SetExp(h1,1,1,r); |
---|
54 | p_Setm(h1,r); |
---|
55 | J->m[0] = p_Add_q(h1,p_Neg(h0,r),r); |
---|
56 | return J; |
---|
57 | } |
---|
58 | |
---|
59 | BOOLEAN tropicalVariety(leftv res, leftv args) |
---|
60 | { |
---|
61 | ring origin = currRing; |
---|
62 | ring r = rCopy(currRing); |
---|
63 | rChangeCurrRing(r); |
---|
64 | leftv u = args; |
---|
65 | if ((u!=NULL) && (u->Typ()==POLY_CMD)) |
---|
66 | { |
---|
67 | poly g = (poly) u->Data(); |
---|
68 | leftv v = u->next; |
---|
69 | if (v==NULL) |
---|
70 | { |
---|
71 | std::set<gfan::ZCone> setOfCones = fullTropicalVarietyOfPolynomial(g,currRing); |
---|
72 | gfan::ZFan* tropG = toFanStar(setOfCones); |
---|
73 | res->rtyp = fanID; |
---|
74 | res->data = (char*) tropG; |
---|
75 | rChangeCurrRing(origin); |
---|
76 | return FALSE; |
---|
77 | } |
---|
78 | if ((v!=NULL) && (v->Typ()==NUMBER_CMD)) |
---|
79 | { |
---|
80 | number p = (number) v->CopyD(); |
---|
81 | poly f = adjustPoly(g,p,currRing); |
---|
82 | std::set<gfan::ZCone> setOfCones = lowerTropicalVarietyOfPolynomial(f,currRing); |
---|
83 | gfan::ZFan* tropG = toFanStar(setOfCones); |
---|
84 | res->rtyp = fanID; |
---|
85 | res->data = (char*) tropG; |
---|
86 | rChangeCurrRing(origin); |
---|
87 | return FALSE; |
---|
88 | } |
---|
89 | if ((v!=NULL) && (v->Typ()==POLY_CMD)) |
---|
90 | { |
---|
91 | std::set<gfan::ZCone> setOfCones = lowerTropicalVarietyOfPolynomial(g,currRing); |
---|
92 | gfan::ZFan* tropG = toFanStar(setOfCones); |
---|
93 | res->rtyp = fanID; |
---|
94 | res->data = (char*) tropG; |
---|
95 | rChangeCurrRing(origin); |
---|
96 | return FALSE; |
---|
97 | } |
---|
98 | } |
---|
99 | if ((u!=NULL) && (u->Typ()==IDEAL_CMD)) |
---|
100 | { |
---|
101 | ideal I = (ideal) u->CopyD(); |
---|
102 | leftv v = u->next; |
---|
103 | if (v==NULL) |
---|
104 | { |
---|
105 | setOfGroebnerConeData TropI = fullTropicalVariety(I,currRing); |
---|
106 | gfan::ZFan* tropI = toFanStar(TropI); |
---|
107 | res->rtyp = fanID; |
---|
108 | res->data = (char*) tropI; |
---|
109 | rChangeCurrRing(origin); |
---|
110 | return FALSE; |
---|
111 | } |
---|
112 | if ((v!=NULL) && (v->Typ()==NUMBER_CMD)) |
---|
113 | { |
---|
114 | number p = (number) v->CopyD(); |
---|
115 | ideal J = adjustIdeal(I,p,currRing); |
---|
116 | setOfGroebnerConeData TropI = lowerTropicalVariety(J,currRing); |
---|
117 | gfan::ZFan* tropI = toFanStar(TropI); |
---|
118 | res->rtyp = fanID; |
---|
119 | res->data = (char*) tropI; |
---|
120 | rChangeCurrRing(origin); |
---|
121 | return FALSE; |
---|
122 | } |
---|
123 | if ((v!=NULL) && (v->Typ()==POLY_CMD)) |
---|
124 | { |
---|
125 | setOfGroebnerConeData TropI = lowerTropicalVariety(I,currRing); |
---|
126 | gfan::ZFan* tropI = toFanStar(TropI); |
---|
127 | res->rtyp = fanID; |
---|
128 | res->data = (char*) tropI; |
---|
129 | rChangeCurrRing(origin); |
---|
130 | return FALSE; |
---|
131 | } |
---|
132 | rChangeCurrRing(origin); |
---|
133 | return FALSE; |
---|
134 | } |
---|
135 | rChangeCurrRing(origin); |
---|
136 | WerrorS("tropicalVariety: unexpected parameters"); |
---|
137 | return TRUE; |
---|
138 | } |
---|