1919
2020extern 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
182209static 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