CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Views: 418346
1
#include "polymake_tropical.h"
2
3
4
Obj REAL_TROPICAL_HYPERSURFACE_BY_MONOMS_AND_COEFFICIENTS( Polymake_Data* data, Obj monomials, Obj coefficients ){
5
6
#ifdef MORE_TESTS
7
if( ! IS_PLIST( monomials ) ){
8
ErrorMayQuit( "not a plain list", 0, 0);
9
return NULL;
10
}
11
#endif
12
13
int len = LEN_PLIST( monomials );
14
Obj akt = ELM_PLIST( monomials, 1 );
15
Obj elem;
16
17
#ifdef MORE_TESTS
18
if( !IS_PLIST( akt ) ){
19
ErrorMayQuit( "not a plain list", 0, 0);
20
return NULL;
21
}
22
#endif
23
24
#ifdef MORE_TESTS
25
if( !IS_PLIST( coefficients ) ){
26
ErrorMayQuit( "coefficients not a plain list", 0, 0);
27
return NULL;
28
}
29
#endif
30
31
32
33
int len_elem = LEN_PLIST( akt );
34
data->main_polymake_session->set_application("tropical");
35
36
pm::Integer* ratarray;
37
ratarray = new pm::Integer[(len)*(len_elem)];
38
39
pm::Integer* coeffarray;
40
coeffarray = new pm::Integer[len];
41
42
for(int i=1;i<=len;i++){
43
akt = ELM_PLIST( monomials, i );
44
#ifdef MORE_TESTS
45
if( !IS_PLIST( akt ) ){
46
delete [] ratarray;
47
delete [] coeffarray;
48
ErrorMayQuit( "not a plain list", 0, 0);
49
return NULL;
50
}
51
if( LEN_PLIST( akt ) != len_elem ){
52
delete [] ratarray;
53
delete [] coeffarray;
54
ErrorMayQuit( "monomials are not of the same lenght", 0, 0);
55
return NULL;
56
}
57
#endif
58
59
for(int j = 1; j <= len_elem; j++){
60
elem = ELM_PLIST( akt, j);
61
62
#ifdef MORE_TESTS
63
if( ! IS_INTOBJ( elem ) ){
64
delete [] ratarray;
65
delete [] coeffarray;
66
ErrorMayQuit( "some entries are not integers", 0, 0);
67
return NULL;
68
}
69
#endif
70
71
ratarray[(i-1)*(len_elem)+j-1] = INT_INTOBJ( elem );
72
}
73
74
elem = ELM_PLIST( coefficients, i );
75
76
#ifdef MORE_TESTS
77
if( ! IS_INTOBJ( elem ) ){
78
delete [] ratarray;
79
delete [] coeffarray;
80
ErrorMayQuit( "some entries are not integers", 0, 0);
81
return NULL;
82
}
83
#endif
84
85
coeffarray[ i - 1 ] = INT_INTOBJ( elem );
86
87
}
88
89
pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(len,len_elem,ratarray);
90
delete [] ratarray;
91
pm::Vector<pm::Integer>* coeff = new pm::Vector<pm::Integer>(len,coeffarray);
92
delete [] coeffarray;
93
perlobj* p = new perlobj("TropicalHypersurface");
94
p->take("MONOMIALS") << *matr;
95
p->take("COEFFICIENTS") << *coeff;
96
delete matr;
97
delete coeff;
98
elem = NewPolymakeExternalObject( T_POLYMAKE_EXTERNAL_TROPICAL_HYPERSURFACE );
99
POLYMAKEOBJ_SET_PERLOBJ( elem, p );
100
return elem;
101
}
102
103
104
Obj REAL_MONOMIALS_OF_HYPERSURFACE( Polymake_Data* data, Obj hypersurf){
105
106
#ifdef MORE_TESTS
107
if(! IS_POLYMAKE_TROPICAL_HYPERSURFACE(hypersurf) ){
108
ErrorMayQuit(" parameter is not a hypersurface.",0,0);
109
return NULL;
110
}
111
#endif
112
113
perlobj* polyobj = PERLOBJ_POLYMAKEOBJ( hypersurf );
114
data->main_polymake_session->set_application_of(*polyobj);
115
pm::Matrix<pm::Rational> matr;
116
try{
117
pm::Matrix<pm::Rational> matr_temp = polyobj->give("MONOMIALS");
118
matr = matr_temp;
119
}
120
121
POLYMAKE_GAP_CATCH
122
123
UInt l = 10;
124
Obj RETLI = NEW_PLIST( T_PLIST , l );
125
SET_LEN_PLIST(RETLI, l );
126
UInt k = 0;
127
Obj LIZeil;
128
UInt matr_cols = matr.cols();
129
for(int i = 0;i<matr.rows();i++){
130
if( ++k > l){
131
GROW_PLIST(RETLI,l*=2);
132
SET_LEN_PLIST(RETLI, l );
133
}
134
LIZeil = NEW_PLIST( T_PLIST, matr.cols());
135
SET_LEN_PLIST( LIZeil , matr_cols );
136
for(int j = 0;j<matr.cols();j++){
137
SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT((matr(i,j)).to_int()));
138
}
139
SET_ELM_PLIST(RETLI,k,LIZeil);
140
CHANGED_BAG(RETLI);
141
}
142
SHRINK_PLIST(RETLI,k);
143
SET_LEN_PLIST(RETLI, k );
144
return RETLI;
145
146
}
147
148
Obj REAL_TROPICAL_POLYTOPE_BY_POINTS( Polymake_Data* data, Obj points ){
149
if( ! IS_PLIST( points ) ){
150
ErrorMayQuit( "not a plain list", 0, 0);
151
return NULL;
152
}
153
154
int len = LEN_PLIST( points );
155
Obj akt = ELM_PLIST( points, 1 );
156
Obj elem;
157
158
#ifdef MORE_TESTS
159
if( !IS_PLIST( akt ) ){
160
ErrorMayQuit( "first ray is not a plain list", 0, 0);
161
return NULL;
162
}
163
#endif
164
165
int len_elem = LEN_PLIST( akt );
166
data->main_polymake_session->set_application("tropical");
167
168
pm::Integer* ratarray;
169
ratarray = new pm::Integer[(len)*(len_elem)];
170
171
for(int i=0;i<len;i++){
172
akt = ELM_PLIST( points, i+1 );
173
#ifdef MORE_TESTS
174
if( !IS_PLIST( akt ) ){
175
delete [] ratarray;
176
ErrorMayQuit( "one point is not a plain list", 0, 0);
177
return NULL;
178
}
179
if( LEN_PLIST( akt ) != len_elem ){
180
delete [] ratarray;
181
ErrorMayQuit( "points are not of the same lenght", 0, 0);
182
return NULL;
183
}
184
#endif
185
for(int j = 0; j < len_elem; j++){
186
elem = ELM_PLIST( akt, j+1);
187
#ifdef MORE_TESTS
188
if( ! IS_INTOBJ( elem) ){
189
delete [] ratarray;
190
ErrorMayQuit( "some entries are not integers", 0, 0);
191
return NULL;
192
}
193
#endif
194
ratarray[ ( i * len_elem ) + j] = INT_INTOBJ( elem );
195
}
196
197
}
198
199
pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(len,len_elem,ratarray);
200
delete [] ratarray;
201
perlobj* p = new perlobj("TropicalPolytope");
202
p->take("POINTS") << *matr;
203
delete matr;
204
elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_POLYTOPE);
205
206
POLYMAKEOBJ_SET_PERLOBJ(elem, p);
207
208
return elem;
209
}
210
211
212