1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | /**************************************** |
---|
3 | * Computer Algebra System SINGULAR * |
---|
4 | ****************************************/ |
---|
5 | // $Id: clapconv.cc,v 1.13 1998-01-12 17:32:44 Singular Exp $ |
---|
6 | /* |
---|
7 | * ABSTRACT: convert data between Singular and factory |
---|
8 | */ |
---|
9 | |
---|
10 | |
---|
11 | #include "mod2.h" |
---|
12 | #ifdef HAVE_FACTORY |
---|
13 | #include "tok.h" |
---|
14 | #define SI_DONT_HAVE_GLOBAL_VARS |
---|
15 | #include "clapconv.h" |
---|
16 | #include "numbers.h" |
---|
17 | #include "longalg.h" |
---|
18 | #include "polys.h" |
---|
19 | #include "modulop.h" |
---|
20 | #include "mmemory.h" |
---|
21 | #include "febase.h" |
---|
22 | #include "ipid.h" |
---|
23 | #include "ring.h" |
---|
24 | |
---|
25 | static void convRec( const CanonicalForm & f, int * exp, poly & result ); |
---|
26 | |
---|
27 | static void convRecAlg( const CanonicalForm & f, int * exp, alg & result ); |
---|
28 | |
---|
29 | static void convRecPP ( const CanonicalForm & f, int * exp, poly & result ); |
---|
30 | |
---|
31 | static void convRecPTr ( const CanonicalForm & f, int * exp, alg & result ); |
---|
32 | |
---|
33 | static void convRecAP ( const CanonicalForm & f, int * exp, poly & result ); |
---|
34 | |
---|
35 | static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs ); |
---|
36 | |
---|
37 | static number convClapNSingAN( const CanonicalForm &f); |
---|
38 | |
---|
39 | CanonicalForm |
---|
40 | convSingPClapP( poly p ) |
---|
41 | { |
---|
42 | CanonicalForm result = 0; |
---|
43 | int e, n = pVariables; |
---|
44 | |
---|
45 | while ( p != NULL ) |
---|
46 | { |
---|
47 | CanonicalForm term; |
---|
48 | /* does only work for finite fields */ |
---|
49 | if ( getCharacteristic() != 0 ) |
---|
50 | { |
---|
51 | Off(SW_USE_EZGCD); |
---|
52 | term = npInt( pGetCoeff( p ) ); |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | if ( (int)(pGetCoeff( p )) & 1 ) |
---|
57 | { |
---|
58 | term = ((int)( pGetCoeff( p ) )>>2); |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | if ( pGetCoeff( p )->s == 3 ) |
---|
63 | { |
---|
64 | MP_INT dummy; |
---|
65 | mpz_init_set( &dummy, &(pGetCoeff( p )->z) ); |
---|
66 | term = make_cf( dummy ); |
---|
67 | } |
---|
68 | else if ( pGetCoeff( p )->s == 1 ) |
---|
69 | { |
---|
70 | MP_INT num, den; |
---|
71 | On(SW_RATIONAL); |
---|
72 | mpz_init_set( &num, &(pGetCoeff( p )->z) ); |
---|
73 | mpz_init_set( &den, &(pGetCoeff( p )->n) ); |
---|
74 | term = make_cf( num, den, false ); |
---|
75 | } |
---|
76 | else |
---|
77 | { // assume s == 0 |
---|
78 | MP_INT num, den; |
---|
79 | mpz_init_set( &num, &(pGetCoeff( p )->z) ); |
---|
80 | mpz_init_set( &den, &(pGetCoeff( p )->n) ); |
---|
81 | term = make_cf( num, den, true ); |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | for ( int i = 1; i <= n; i++ ) |
---|
86 | { |
---|
87 | if ( (e = pGetExp( p, i )) != 0 ) |
---|
88 | term *= power( Variable( i ), e ); |
---|
89 | } |
---|
90 | result += term; |
---|
91 | p = pNext( p ); |
---|
92 | } |
---|
93 | return result; |
---|
94 | } |
---|
95 | |
---|
96 | poly |
---|
97 | convClapPSingP ( const CanonicalForm & f ) |
---|
98 | { |
---|
99 | // cerr << " f = " << f << endl; |
---|
100 | int n = pVariables+1; |
---|
101 | /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */ |
---|
102 | int * exp = new int[n]; |
---|
103 | //for ( int i = 0; i < n; i++ ) exp[i] = 0; |
---|
104 | memset(exp,0,n*sizeof(int)); |
---|
105 | poly result = NULL; |
---|
106 | convRecPP( f, exp, result ); |
---|
107 | delete [] exp; |
---|
108 | return result; |
---|
109 | } |
---|
110 | |
---|
111 | static void |
---|
112 | convRecPP ( const CanonicalForm & f, int * exp, poly & result ) |
---|
113 | { |
---|
114 | if ( ! f.inCoeffDomain() ) |
---|
115 | { |
---|
116 | int l = f.level(); |
---|
117 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
118 | { |
---|
119 | exp[l] = i.exp(); |
---|
120 | convRecPP( i.coeff(), exp, result ); |
---|
121 | } |
---|
122 | exp[l] = 0; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | poly term = pNew(); |
---|
127 | pNext( term ) = NULL; |
---|
128 | for ( int i = 1; i <= pVariables; i++ ) |
---|
129 | pSetExp( term, i, exp[i]); |
---|
130 | pSetComp(term, 0); |
---|
131 | if ( getCharacteristic() != 0 ) |
---|
132 | { |
---|
133 | pGetCoeff( term ) = nInit( f.intval() ); |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | if ( f.isImm() ) |
---|
138 | pGetCoeff( term ) = nInit( f.intval() ); |
---|
139 | else |
---|
140 | { |
---|
141 | number z=(number)Alloc(sizeof(rnumber)); |
---|
142 | #ifdef LDEBUG |
---|
143 | z->debug=123456; |
---|
144 | #endif |
---|
145 | z->z = gmp_numerator( f ); |
---|
146 | if ( f.den() == 1 ) |
---|
147 | z->s = 3; |
---|
148 | else |
---|
149 | { |
---|
150 | z->n = gmp_denominator( f ); |
---|
151 | z->s = 0; |
---|
152 | } |
---|
153 | pGetCoeff( term ) = z; |
---|
154 | } |
---|
155 | } |
---|
156 | pSetm( term ); |
---|
157 | result = pAdd( result, term ); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | CanonicalForm |
---|
163 | convSingTrClapP( alg p ) |
---|
164 | { |
---|
165 | CanonicalForm result = 0; |
---|
166 | int e, n = napVariables; |
---|
167 | |
---|
168 | while ( p!=NULL) |
---|
169 | { |
---|
170 | CanonicalForm term; |
---|
171 | /* does only work for finite fields */ |
---|
172 | if ( getCharacteristic() != 0 ) |
---|
173 | { |
---|
174 | Off(SW_USE_EZGCD); |
---|
175 | term = npInt( napGetCoeff( p ) ); |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | On(SW_USE_EZGCD); |
---|
180 | //if ( (!(int)(napGetCoeff( p )) & 1 ) |
---|
181 | //&& ( napGetCoeff( p )->s == 0)) |
---|
182 | // naNormalize( naGetCoeff( p ) ); |
---|
183 | if ( (int)(napGetCoeff( p )) & 1 ) |
---|
184 | term = nlInt( napGetCoeff( p ) ); |
---|
185 | else |
---|
186 | { |
---|
187 | if ( napGetCoeff( p )->s == 3 ) |
---|
188 | { |
---|
189 | MP_INT dummy; |
---|
190 | mpz_init_set( &dummy, &(napGetCoeff( p )->z) ); |
---|
191 | term = make_cf( dummy ); |
---|
192 | } |
---|
193 | else if ( napGetCoeff( p )->s == 1 ) |
---|
194 | { |
---|
195 | MP_INT num, den; |
---|
196 | On(SW_RATIONAL); |
---|
197 | mpz_init_set( &num, &(napGetCoeff( p )->z) ); |
---|
198 | mpz_init_set( &den, &(napGetCoeff( p )->n) ); |
---|
199 | term = make_cf( num, den, false ); |
---|
200 | } |
---|
201 | else |
---|
202 | { // assume s == 0 |
---|
203 | MP_INT num, den; |
---|
204 | On(SW_RATIONAL); |
---|
205 | mpz_init_set( &num, &(napGetCoeff( p )->z) ); |
---|
206 | mpz_init_set( &den, &(napGetCoeff( p )->n) ); |
---|
207 | term = make_cf( num, den, true ); |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | for ( int i = 1; i <= n; i++ ) |
---|
212 | { |
---|
213 | if ( (e = napGetExp( p, i )) != 0 ) |
---|
214 | term *= power( Variable( i ), e ); |
---|
215 | } |
---|
216 | result += term; |
---|
217 | p = napNext( p ); |
---|
218 | } |
---|
219 | return result; |
---|
220 | } |
---|
221 | |
---|
222 | alg |
---|
223 | convClapPSingTr ( const CanonicalForm & f ) |
---|
224 | { |
---|
225 | // cerr << " f = " << f << endl; |
---|
226 | int n = napVariables+1; |
---|
227 | /* ASSERT( level( f ) <= napVariables, "illegal number of variables" ); */ |
---|
228 | int * exp = new int[n]; |
---|
229 | // for ( int i = 0; i < n; i++ ) exp[i] = 0; |
---|
230 | memset(exp,0,n*sizeof(int)); |
---|
231 | alg result = NULL; |
---|
232 | convRecPTr( f, exp, result ); |
---|
233 | delete [] exp; |
---|
234 | return result; |
---|
235 | } |
---|
236 | |
---|
237 | static void |
---|
238 | convRecPTr ( const CanonicalForm & f, int * exp, alg & result ) |
---|
239 | { |
---|
240 | if ( ! f.inCoeffDomain() ) |
---|
241 | { |
---|
242 | int l = f.level(); |
---|
243 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
244 | { |
---|
245 | exp[l] = i.exp(); |
---|
246 | convRecPTr( i.coeff(), exp, result ); |
---|
247 | } |
---|
248 | exp[l] = 0; |
---|
249 | } |
---|
250 | else |
---|
251 | { |
---|
252 | alg term = napNew(); |
---|
253 | // napNext( term ) = NULL; //already done by napNew |
---|
254 | for ( int i = 1; i <= napVariables; i++ ) |
---|
255 | napGetExp( term, i ) = exp[i]; |
---|
256 | if ( getCharacteristic() != 0 ) |
---|
257 | { |
---|
258 | napGetCoeff( term ) = npInit( f.intval() ); |
---|
259 | } |
---|
260 | else |
---|
261 | { |
---|
262 | if ( f.isImm() ) |
---|
263 | napGetCoeff( term ) = nlInit( f.intval() ); |
---|
264 | else |
---|
265 | { |
---|
266 | number z=(number)Alloc(sizeof(rnumber)); |
---|
267 | #ifdef LDEBUG |
---|
268 | z->debug=123456; |
---|
269 | #endif |
---|
270 | z->z = gmp_numerator( f ); |
---|
271 | if ( f.den() == 1 ) |
---|
272 | { |
---|
273 | z->s = 3; |
---|
274 | } |
---|
275 | else |
---|
276 | { |
---|
277 | z->n = gmp_denominator( f ); |
---|
278 | if (mpz_cmp_si(&z->n,(long)1)==0) |
---|
279 | { |
---|
280 | mpz_clear(&z->n); |
---|
281 | z->s=3; |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | z->s = 0; |
---|
286 | } |
---|
287 | nacNormalize(z); |
---|
288 | } |
---|
289 | napGetCoeff( term ) = z; |
---|
290 | } |
---|
291 | } |
---|
292 | result = napAdd( result, term ); |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | CanonicalForm convSingAPClapAP ( poly p , const Variable & a) |
---|
297 | { |
---|
298 | CanonicalForm result = 0; |
---|
299 | int e, n = pVariables; |
---|
300 | |
---|
301 | while ( p!=NULL) |
---|
302 | { |
---|
303 | CanonicalForm term=convSingAClapA(((lnumber)pGetCoeff(p))->z,a); |
---|
304 | for ( int i = 1; i <= n; i++ ) |
---|
305 | { |
---|
306 | if ( (e = pGetExp( p, i )) != 0 ) |
---|
307 | term *= power( Variable( i ), e ); |
---|
308 | } |
---|
309 | result += term; |
---|
310 | p = pNext( p ); |
---|
311 | } |
---|
312 | return result; |
---|
313 | } |
---|
314 | |
---|
315 | poly convClapAPSingAP ( const CanonicalForm & f ) |
---|
316 | { |
---|
317 | int n = pVariables+1; |
---|
318 | /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */ |
---|
319 | int * exp = new int[n]; |
---|
320 | // for ( int i = 0; i < n; i++ ) exp[i] = 0; |
---|
321 | memset(exp,0,n*sizeof(int)); |
---|
322 | poly result = NULL; |
---|
323 | convRecAP( f, exp, result ); |
---|
324 | delete [] exp; |
---|
325 | return result; |
---|
326 | } |
---|
327 | |
---|
328 | static void |
---|
329 | convRecAP ( const CanonicalForm & f, int * exp, poly & result ) |
---|
330 | { |
---|
331 | if ( ! f.inCoeffDomain() ) |
---|
332 | { |
---|
333 | int l = f.level(); |
---|
334 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
335 | { |
---|
336 | exp[l] = i.exp(); |
---|
337 | convRecAP( i.coeff(), exp, result ); |
---|
338 | } |
---|
339 | exp[l] = 0; |
---|
340 | } |
---|
341 | else |
---|
342 | { |
---|
343 | alg z=(alg)convClapASingA( f ); |
---|
344 | if (z!=NULL) |
---|
345 | { |
---|
346 | poly term = pNew(); |
---|
347 | pNext( term ) = NULL; |
---|
348 | for ( int i = 1; i <= pVariables; i++ ) |
---|
349 | pSetExp( term, i , exp[i]); |
---|
350 | pSetComp(term, 0); |
---|
351 | pGetCoeff(term)=(number)Alloc0(sizeof(rnumber)); |
---|
352 | ((lnumber)pGetCoeff(term))->z=z; |
---|
353 | pSetm( term ); |
---|
354 | result = pAdd( result, term ); |
---|
355 | } |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | CanonicalForm convSingAClapA ( alg p , const Variable & a ) |
---|
360 | { |
---|
361 | CanonicalForm result = 0; |
---|
362 | int e; |
---|
363 | |
---|
364 | while ( p!=NULL ) |
---|
365 | { |
---|
366 | CanonicalForm term; |
---|
367 | /* does only work for finite fields */ |
---|
368 | if ( getCharacteristic() != 0 ) |
---|
369 | { |
---|
370 | Off(SW_USE_EZGCD); |
---|
371 | term = npInt( napGetCoeff( p ) ); |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | On(SW_USE_EZGCD); |
---|
376 | //if ( (!(int)(napGetCoeff( p )) & 1 ) |
---|
377 | //&& ( napGetCoeff( p )->s == 0)) |
---|
378 | // naNormalize( naGetCoeff( p ) ); |
---|
379 | if ( (int)(napGetCoeff( p )) & 1 ) |
---|
380 | term = nlInt( napGetCoeff( p ) ); |
---|
381 | else |
---|
382 | { |
---|
383 | if ( napGetCoeff( p )->s == 3 ) |
---|
384 | { |
---|
385 | MP_INT dummy; |
---|
386 | mpz_init_set( &dummy, &(napGetCoeff( p )->z) ); |
---|
387 | term = make_cf( dummy ); |
---|
388 | } |
---|
389 | else if ( napGetCoeff( p )->s == 1 ) |
---|
390 | { |
---|
391 | MP_INT num, den; |
---|
392 | On(SW_RATIONAL); |
---|
393 | mpz_init_set( &num, &(napGetCoeff( p )->z) ); |
---|
394 | mpz_init_set( &den, &(napGetCoeff( p )->n) ); |
---|
395 | term = make_cf( num, den, false ); |
---|
396 | } |
---|
397 | else |
---|
398 | { // assume s == 0 |
---|
399 | MP_INT num, den; |
---|
400 | On(SW_RATIONAL); |
---|
401 | mpz_init_set( &num, &(napGetCoeff( p )->z) ); |
---|
402 | mpz_init_set( &den, &(napGetCoeff( p )->n) ); |
---|
403 | term = make_cf( num, den, true ); |
---|
404 | } |
---|
405 | } |
---|
406 | } |
---|
407 | if ( (e = napGetExp( p, 1 )) != 0 ) |
---|
408 | term *= power( a , e ); |
---|
409 | result += term; |
---|
410 | p = napNext( p ); |
---|
411 | } |
---|
412 | return result; |
---|
413 | } |
---|
414 | |
---|
415 | static number convClapNSingAN( const CanonicalForm &f) |
---|
416 | { |
---|
417 | if ((getCharacteristic() != 0) || ( f.isImm() )) |
---|
418 | return nacInit( f.intval() ); |
---|
419 | else |
---|
420 | { |
---|
421 | number z=(number)Alloc(sizeof(rnumber)); |
---|
422 | #ifdef LDEBUG |
---|
423 | z->debug=123456; |
---|
424 | #endif |
---|
425 | z->z = gmp_numerator( f ); |
---|
426 | if ( f.den() == 1 ) |
---|
427 | { |
---|
428 | z->s = 3; |
---|
429 | } |
---|
430 | else |
---|
431 | { |
---|
432 | z->n = gmp_denominator( f ); |
---|
433 | z->s = 0; |
---|
434 | } |
---|
435 | #ifdef LDEBUG |
---|
436 | nlDBTest(z,__FILE__,__LINE__); |
---|
437 | #endif |
---|
438 | return z; |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | alg convClapASingA ( const CanonicalForm & f ) |
---|
443 | { |
---|
444 | alg a=NULL; |
---|
445 | alg t; |
---|
446 | for( CFIterator i=f; i.hasTerms(); i++) |
---|
447 | { |
---|
448 | t=napNew(); |
---|
449 | // napNext( t ) = NULL; //already done by napNew |
---|
450 | napGetCoeff(t)=convClapNSingAN( i.coeff() ); |
---|
451 | if (nacIsZero(napGetCoeff(t))) |
---|
452 | { |
---|
453 | napDelete(&t); |
---|
454 | } |
---|
455 | else |
---|
456 | { |
---|
457 | napGetExp(t,1)=i.exp(); |
---|
458 | a=napAdd(a,t); |
---|
459 | } |
---|
460 | } |
---|
461 | return a; |
---|
462 | } |
---|
463 | |
---|
464 | CanonicalForm convSingTrPClapP ( poly p ) |
---|
465 | { |
---|
466 | CanonicalForm result = 0; |
---|
467 | int e, n = pVariables; |
---|
468 | int offs = rPar(currRing); |
---|
469 | |
---|
470 | while ( p!=NULL ) |
---|
471 | { |
---|
472 | nNormalize(pGetCoeff(p)); |
---|
473 | CanonicalForm term=convSingTrClapP(((lnumber)pGetCoeff(p))->z); |
---|
474 | for ( int i = 1; i <= n; i++ ) |
---|
475 | { |
---|
476 | if ( (e = pGetExp( p, i )) != 0 ) |
---|
477 | term = term * power( Variable( i + offs ), e ); |
---|
478 | } |
---|
479 | result += term; |
---|
480 | p = pNext( p ); |
---|
481 | } |
---|
482 | return result; |
---|
483 | } |
---|
484 | |
---|
485 | poly convClapPSingTrP ( const CanonicalForm & f ) |
---|
486 | { |
---|
487 | int n = pVariables+1; |
---|
488 | /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */ |
---|
489 | int * exp = new int[n]; |
---|
490 | // for ( int i = 0; i < n; i++ ) exp[i] = 0; |
---|
491 | memset(exp,0,n*sizeof(int)); |
---|
492 | poly result = NULL; |
---|
493 | convRecTrP( f, exp, result , rPar(currRing) ); |
---|
494 | delete [] exp; |
---|
495 | return result; |
---|
496 | } |
---|
497 | |
---|
498 | static void |
---|
499 | convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs) |
---|
500 | { |
---|
501 | if ( f.level() > offs ) |
---|
502 | { |
---|
503 | int l = f.level(); |
---|
504 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
505 | { |
---|
506 | exp[l-offs] = i.exp(); |
---|
507 | convRecTrP( i.coeff(), exp, result, offs ); |
---|
508 | } |
---|
509 | exp[l-offs] = 0; |
---|
510 | } |
---|
511 | else |
---|
512 | { |
---|
513 | poly term = pNew(); |
---|
514 | pNext( term ) = NULL; |
---|
515 | for ( int i = 1; i <= pVariables; i++ ) |
---|
516 | pSetExp( term, i ,exp[i]); |
---|
517 | pSetComp(term, 0); |
---|
518 | pGetCoeff(term)=(number)Alloc0(sizeof(rnumber)); |
---|
519 | ((lnumber)pGetCoeff(term))->z=convClapPSingTr( f ); |
---|
520 | pSetm( term ); |
---|
521 | result = pAdd( result, term ); |
---|
522 | } |
---|
523 | } |
---|
524 | |
---|
525 | int convClapISingI( const CanonicalForm & f) |
---|
526 | { |
---|
527 | if (!f.isImm()) WerrorS("int overflow in det"); |
---|
528 | return f.intval(); |
---|
529 | } |
---|
530 | #endif |
---|