Skip to content

Commit b29771b

Browse files
committed
kernel: pass GAP matrices directly, not as strings
1 parent a094b73 commit b29771b

File tree

3 files changed

+59
-95
lines changed

3 files changed

+59
-95
lines changed

gap/tools.gd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
DeclareOperation("PTM", [ IsMatrix ] );
32
DeclareOperation( "IsCompatiblePolyhedronList", [IsList] );
43
DeclareOperation( "GiveGeneratingVerticesAndGeneratingRays", [ IsList, IsList ] );
@@ -10,5 +9,4 @@ DeclareAttribute( "PolyToList", IsCddPolyhedron );
109
DeclareOperation( "GetRidOfLinearity", [ IsCddPolyhedron ] );
1110
DeclareOperation( "LinearProgramToList", [ IsCddLinearProgram ] );
1211
DeclareGlobalFunction( "NumberOfDigitsOfTheNumber" );
13-
DeclareGlobalFunction( "ListToString" );
14-
DeclareGlobalFunction( "CanonicalizeListOfFacesAndInteriorPoints" );
12+
DeclareGlobalFunction( "CanonicalizeListOfFacesAndInteriorPoints" );

gap/tools.gi

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,6 @@ InstallMethod( PolyToList,
232232
L[1] := 2;
233233
fi;
234234

235-
#L[2] := 2; # unused
236-
237-
if Length( poly!.linearity ) = 0 then
238-
L[3] := 0;
239-
else
240-
L[3] := 1;
241-
fi;
242-
243235
matrix := poly!.matrix;
244236

245237
if poly!.rep_type= "V-rep" and IsZero( matrix ) then
@@ -249,15 +241,8 @@ InstallMethod( PolyToList,
249241

250242
L[4] := NrRows( matrix );
251243
L[5] := NrCols( matrix );
252-
253-
lin := poly!.linearity;
254-
255-
temp:= [ Length( lin ) ];
256-
Append( temp, lin );
257-
L[6] := ListToString( [ temp ] );
258-
259-
L[7] := ReplacedString( String( matrix ), ",", "" );
260-
244+
L[6] := poly!.linearity;
245+
L[7] := matrix;
261246
L[8] := 0;
262247
L[9] := [ ];
263248

@@ -335,7 +320,7 @@ InstallMethod( LinearProgramToList,
335320

336321
fi;
337322

338-
result[ 9 ] := ListToString( [ lp!.rowvector ] );
323+
result[ 9 ] := lp!.rowvector;
339324

340325
return result;
341326

@@ -463,28 +448,6 @@ function( poly )
463448

464449
end );
465450

466-
##
467-
InstallGlobalFunction( ListToString,
468-
[ IsList ],
469-
function( l )
470-
local i, j, s;
471-
472-
s := " ";
473-
474-
for i in [ 1 .. Length( l ) ] do
475-
476-
for j in [ 1 .. Length( l[ 1 ] ) ] do
477-
478-
s := Concatenation( [ s, String( l[i][j] ), " " ] );
479-
480-
od;
481-
482-
od;
483-
484-
return s;
485-
486-
end );
487-
488451
##
489452
InstallGlobalFunction( CanonicalizeListOfFacesAndInteriorPoints,
490453
function( L )

src/CddInterface.c

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@
1919

2020
extern void dd_SetLinearity(dd_MatrixPtr, char *);
2121

22-
// Old implementation
23-
// Obj MPZ_TO_GAPOBJ(mpz_t x)
24-
// {
25-
// //gmp_printf ("%s is an mpz %Zd\n", "here", x);
26-
// return INTOBJ_INT(mpz_get_si(x));
27-
// }
28-
2922
// The following conversion has been taken from
3023
// https://github.com/gap-packages/NormalizInterface
3124
// Thanks to Max Horn
32-
static Obj MPZ_TO_GAPOBJ( const mpz_t x)
25+
static Obj MPZ_TO_GAPOBJ(const mpz_t x)
3326
{
3427
Obj res;
3528
Int size = x->_mp_size;
@@ -65,6 +58,40 @@ static Obj MPQ_TO_GAPOBJ(const mpq_t x)
6558
return QUO(num, den);
6659
}
6760

61+
static void GAPOBJ_TO_MPZ(mpz_t out, Obj x)
62+
{
63+
if (IS_INTOBJ(x)) {
64+
mpz_set_si(out, INT_INTOBJ(x));
65+
}
66+
else if (TNUM_OBJ(x) == T_INTPOS || TNUM_OBJ(x) == T_INTNEG) {
67+
UInt size = SIZE_INT(x);
68+
mpz_realloc2(out, size * GMP_NUMB_BITS);
69+
memcpy(out->_mp_d, ADDR_INT(x), sizeof(mp_limb_t) * size);
70+
out->_mp_size = (TNUM_OBJ(x) == T_INTPOS) ? (Int)size : -(Int)size;
71+
}
72+
else {
73+
ErrorMayQuit("expected a GAP integer object", 0, 0);
74+
}
75+
}
76+
77+
static void GAPOBJ_TO_MPQ(mpq_t out, Obj x)
78+
{
79+
if (IS_INTOBJ(x)) {
80+
mpq_set_si(out, INT_INTOBJ(x), 1);
81+
}
82+
else if (TNUM_OBJ(x) == T_INTPOS || TNUM_OBJ(x) == T_INTNEG) {
83+
GAPOBJ_TO_MPZ(mpq_numref(out), x);
84+
mpz_set_si(mpq_denref(out), 1);
85+
}
86+
else if (TNUM_OBJ(x) == T_RAT) {
87+
GAPOBJ_TO_MPZ(mpq_numref(out), NUM_RAT(x));
88+
GAPOBJ_TO_MPZ(mpq_denref(out), DEN_RAT(x));
89+
}
90+
else {
91+
ErrorMayQuit("expected a GAP integer or rational object", 0, 0);
92+
}
93+
}
94+
6895
/**********************************************************
6996
*
7097
* Converting functions
@@ -181,36 +208,24 @@ static Obj MatPtrToGapObj(dd_MatrixPtr M)
181208

182209
static dd_MatrixPtr GapInputToMatrixPtr(Obj input)
183210
{
184-
185-
int k_rep, k_linearity, k_rowrange, k_colrange, k_LPobject;
186-
char k_linearity_array[dd_linelenmax], k_rowvec[dd_linelenmax];
211+
int k_rep, k_rowrange, k_colrange, k_LPobject;
212+
Obj k_linearity_array, k_rowvec, k_matrix;
187213

188214
// reset the global variable, before defining it again to be used in the current session.
189215
dd_set_global_constants();
190216

191217
k_rep = INT_INTOBJ(ELM_PLIST(input, 1));
192-
k_linearity = INT_INTOBJ(ELM_PLIST(input, 3));
193218
k_rowrange = INT_INTOBJ(ELM_PLIST(input, 4));
194219
k_colrange = INT_INTOBJ(ELM_PLIST(input, 5));
220+
k_linearity_array = ELM_PLIST(input, 6);
221+
k_matrix = ELM_PLIST(input, 7);
195222
k_LPobject = INT_INTOBJ(ELM_PLIST(input, 8));
196-
Obj string = ELM_PLIST(input, 7);
223+
k_rowvec = ELM_PLIST(input, 9);
224+
197225
if (k_colrange == 0)
198226
ErrorMayQuit("k_colrange == 0 should not happen, please report this!", 0, 0);
199227

200-
int str_len = GET_LEN_STRING(string);
201-
//fprintf(stdout, "%d: ", str_len);
202-
//ErrorMayQuit( "j", 0, 0 );
203-
204-
char k_matrix[str_len];
205-
strcpy(k_linearity_array, CSTR_STRING(ELM_PLIST(input, 6)));
206-
strcpy(k_matrix, CSTR_STRING(ELM_PLIST(input, 7)));
207-
strcpy(k_rowvec, CSTR_STRING(ELM_PLIST(input, 9)));
208-
209-
char k_value[dd_linelenmax];
210-
char *pch;
211-
int u;
212228
dd_MatrixPtr M = NULL;
213-
mytype rational_value;
214229

215230
// // creating the matrix with these two dimesnions
216231
M = dd_CreateMatrix(k_rowrange, k_colrange);
@@ -228,30 +243,23 @@ static dd_MatrixPtr GapInputToMatrixPtr(Obj input)
228243

229244
//
230245
// controling the linearity of the given polygon.
231-
if (k_linearity == 1)
246+
const Int len = LEN_LIST(k_linearity_array);
247+
for (int i = 1; i <= len; i++)
232248
{
233-
dd_SetLinearity(M, k_linearity_array);
249+
Obj val = ELM_LIST(k_linearity_array, i);
250+
set_addelem(M->linset, INT_INTOBJ(val));
234251
}
252+
235253
//
236254
// // filling the matrix with elements scanned from the string k_matrix
237255
//
238-
239-
pch = strtok(k_matrix, " ,.{}][");
240-
int uu,vv;
241-
242-
for (uu = 0; uu < k_rowrange; uu++){
243-
for (vv = 0; vv < k_colrange; vv++){
244-
//fprintf(stdout, "uu:%d: ", uu );
245-
//fprintf(stdout, "vv:%d: ", vv );
246-
247-
strcpy(k_value, pch);
248-
dd_init(rational_value);
249-
dd_sread_rational_value(k_value, rational_value);
250-
dd_set(M->matrix[uu][vv], rational_value);
251-
dd_clear(rational_value);
252-
pch = strtok(NULL, " ,.{}][");
256+
for (int uu = 0; uu < k_rowrange; uu++){
257+
Obj row = ELM_LIST(k_matrix, uu + 1);
258+
for (int vv = 0; vv < k_colrange; vv++){
259+
Obj val = ELM_LIST(row, vv + 1);
260+
GAPOBJ_TO_MPQ(M->matrix[uu][vv], val);
261+
}
253262
}
254-
}
255263

256264
if (k_LPobject == 0)
257265
M->objective = dd_LPnone;
@@ -262,15 +270,10 @@ static dd_MatrixPtr GapInputToMatrixPtr(Obj input)
262270

263271
if (M->objective == dd_LPmax || M->objective == dd_LPmin)
264272
{
265-
pch = strtok(k_rowvec, " ,.{}][");
266-
for (u = 0; u < M->colsize; u++)
273+
for (int u = 0; u < M->colsize; u++)
267274
{
268-
strcpy(k_value, pch);
269-
dd_init(rational_value);
270-
dd_sread_rational_value(k_value, rational_value);
271-
dd_set(M->rowvec[u], rational_value);
272-
dd_clear(rational_value);
273-
pch = strtok(NULL, " ,.{}][");
275+
Obj val = ELM_LIST(k_rowvec, u + 1);
276+
GAPOBJ_TO_MPQ(M->rowvec[u], val);
274277
}
275278
}
276279

0 commit comments

Comments
 (0)