1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: modulop.cc 14402 2011-09-29 17:16:19Z hannes $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: numbers modulo p (<=32003) |
---|
7 | */ |
---|
8 | |
---|
9 | #include <string.h> |
---|
10 | #include "config.h" |
---|
11 | #include <omalloc.h> |
---|
12 | #include "coeffs.h" |
---|
13 | #include "output.h" |
---|
14 | #include "numbers.h" |
---|
15 | #include "longrat.h" |
---|
16 | #include "mpr_complex.h" |
---|
17 | #include "mylimits.h" |
---|
18 | #include "modulop.h" |
---|
19 | |
---|
20 | int npGen=0; |
---|
21 | |
---|
22 | #ifdef HAVE_DIV_MOD |
---|
23 | unsigned short *npInvTable=NULL; |
---|
24 | #endif |
---|
25 | |
---|
26 | #if !defined(HAVE_DIV_MOD) || !defined(HAVE_MULT_MOD) |
---|
27 | unsigned short *npExpTable=NULL; |
---|
28 | unsigned short *npLogTable=NULL; |
---|
29 | #endif |
---|
30 | |
---|
31 | |
---|
32 | BOOLEAN npGreaterZero (number k, const coeffs r) |
---|
33 | { |
---|
34 | int h = (int)((long) k); |
---|
35 | return ((int)h !=0) && (h <= (r->npPrimeM>>1)); |
---|
36 | } |
---|
37 | |
---|
38 | //unsigned long npMultMod(unsigned long a, unsigned long b) |
---|
39 | //{ |
---|
40 | // unsigned long c = a*b; |
---|
41 | // c = c % npPrimeM; |
---|
42 | // assume(c == (unsigned long) npMultM((number) a, (number) b)); |
---|
43 | // return c; |
---|
44 | //} |
---|
45 | |
---|
46 | number npMult (number a,number b, const coeffs r) |
---|
47 | { |
---|
48 | if (((long)a == 0) || ((long)b == 0)) |
---|
49 | return (number)0; |
---|
50 | else |
---|
51 | return npMultM(a,b, r); |
---|
52 | } |
---|
53 | |
---|
54 | /*2 |
---|
55 | * create a number from int |
---|
56 | */ |
---|
57 | number npInit (int i, const coeffs r) |
---|
58 | { |
---|
59 | long ii=i; |
---|
60 | long p=(long)ABS(r->ch); |
---|
61 | while (ii < 0L) ii += p; |
---|
62 | while ((ii>1L) && (ii >= p)) ii -= p; |
---|
63 | return (number)ii; |
---|
64 | } |
---|
65 | |
---|
66 | /*2 |
---|
67 | * convert a number to int (-p/2 .. p/2) |
---|
68 | */ |
---|
69 | int npInt(number &n, const coeffs r) |
---|
70 | { |
---|
71 | if ((long)n > (((long)r->ch) >>1)) return (int)((long)n -((long)r->ch)); |
---|
72 | else return (int)((long)n); |
---|
73 | } |
---|
74 | |
---|
75 | number npAdd (number a, number b, const coeffs r) |
---|
76 | { |
---|
77 | return npAddM(a,b, r); |
---|
78 | } |
---|
79 | |
---|
80 | number npSub (number a, number b, const coeffs r) |
---|
81 | { |
---|
82 | return npSubM(a,b,r); |
---|
83 | } |
---|
84 | |
---|
85 | BOOLEAN npIsZero (number a, const coeffs r) |
---|
86 | { |
---|
87 | return 0 == (long)a; |
---|
88 | } |
---|
89 | |
---|
90 | BOOLEAN npIsOne (number a, const coeffs r) |
---|
91 | { |
---|
92 | return 1 == (long)a; |
---|
93 | } |
---|
94 | |
---|
95 | BOOLEAN npIsMOne (number a, const coeffs r) |
---|
96 | { |
---|
97 | return ((r->npPminus1M == (long)a)&&((long)1!=(long)a)); |
---|
98 | } |
---|
99 | |
---|
100 | #ifdef HAVE_DIV_MOD |
---|
101 | #ifdef USE_NTL_XGCD |
---|
102 | //ifdef HAVE_NTL // in ntl.a |
---|
103 | //extern void XGCD(long& d, long& s, long& t, long a, long b); |
---|
104 | #include <NTL/ZZ.h> |
---|
105 | #ifdef NTL_CLIENT |
---|
106 | NTL_CLIENT |
---|
107 | #endif |
---|
108 | #endif |
---|
109 | |
---|
110 | long InvMod(long a, const coeffs R) |
---|
111 | { |
---|
112 | long d, s, t; |
---|
113 | |
---|
114 | #ifdef USE_NTL_XGCD |
---|
115 | XGCD(d, s, t, a, R->npPrimeM); |
---|
116 | assume (d == 1); |
---|
117 | #else |
---|
118 | long u, v, u0, v0, u1, v1, u2, v2, q, r; |
---|
119 | |
---|
120 | assume(a>0); |
---|
121 | u1=1; u2=0; |
---|
122 | u = a; v = R->npPrimeM; |
---|
123 | |
---|
124 | while (v != 0) |
---|
125 | { |
---|
126 | q = u / v; |
---|
127 | r = u % v; |
---|
128 | u = v; |
---|
129 | v = r; |
---|
130 | u0 = u2; |
---|
131 | u2 = u1 - q*u2; |
---|
132 | u1 = u0; |
---|
133 | } |
---|
134 | |
---|
135 | assume(u==1); |
---|
136 | s = u1; |
---|
137 | #endif |
---|
138 | if (s < 0) |
---|
139 | return s + R->npPrimeM; |
---|
140 | else |
---|
141 | return s; |
---|
142 | } |
---|
143 | #endif |
---|
144 | |
---|
145 | inline number npInversM (number c, const coeffs r) |
---|
146 | { |
---|
147 | #ifndef HAVE_DIV_MOD |
---|
148 | return (number)(long)r->npExpTable[r->npPminus1M - r->npLogTable[(long)c]]; |
---|
149 | #else |
---|
150 | long inv=(long)r->npInvTable[(long)c]; |
---|
151 | if (inv==0) |
---|
152 | { |
---|
153 | inv=InvMod((long)c,r); |
---|
154 | r->npInvTable[(long)c]=inv; |
---|
155 | } |
---|
156 | return (number)inv; |
---|
157 | #endif |
---|
158 | } |
---|
159 | |
---|
160 | number npDiv (number a,number b, const coeffs r) |
---|
161 | { |
---|
162 | //#ifdef NV_OPS |
---|
163 | // if (npPrimeM>NV_MAX_PRIME) |
---|
164 | // return nvDiv(a,b); |
---|
165 | //#endif |
---|
166 | if ((long)a==0) |
---|
167 | return (number)0; |
---|
168 | #ifndef HAVE_DIV_MOD |
---|
169 | if ((long)b==0) |
---|
170 | { |
---|
171 | WerrorS(nDivBy0); |
---|
172 | return (number)0; |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | int s = r->npLogTable[(long)a] - r->npLogTable[(long)b]; |
---|
177 | if (s < 0) |
---|
178 | s += r->npPminus1M; |
---|
179 | return (number)(long)r->npExpTable[s]; |
---|
180 | } |
---|
181 | #else |
---|
182 | number inv=npInversM(b,r); |
---|
183 | return npMultM(a,inv,r); |
---|
184 | #endif |
---|
185 | } |
---|
186 | number npInvers (number c, const coeffs r) |
---|
187 | { |
---|
188 | if ((long)c==0) |
---|
189 | { |
---|
190 | WerrorS("1/0"); |
---|
191 | return (number)0; |
---|
192 | } |
---|
193 | return npInversM(c,r); |
---|
194 | } |
---|
195 | |
---|
196 | number npNeg (number c, const coeffs r) |
---|
197 | { |
---|
198 | if ((long)c==0) return c; |
---|
199 | return npNegM(c,r); |
---|
200 | } |
---|
201 | |
---|
202 | BOOLEAN npGreater (number a,number b, const coeffs r) |
---|
203 | { |
---|
204 | //return (long)a != (long)b; |
---|
205 | return (long)a > (long)b; |
---|
206 | } |
---|
207 | |
---|
208 | BOOLEAN npEqual (number a,number b, const coeffs r) |
---|
209 | { |
---|
210 | // return (long)a == (long)b; |
---|
211 | return npEqualM(a,b,r); |
---|
212 | } |
---|
213 | |
---|
214 | void npWrite (number &a, const coeffs r) |
---|
215 | { |
---|
216 | if ((long)a>(((long)r->ch) >>1)) StringAppend("-%d",(int)(((long)r->ch)-((long)a))); |
---|
217 | else StringAppend("%d",(int)((long)a)); |
---|
218 | } |
---|
219 | |
---|
220 | void npPower (number a, int i, number * result, const coeffs r) |
---|
221 | { |
---|
222 | if (i==0) |
---|
223 | { |
---|
224 | //npInit(1,result); |
---|
225 | *(long *)result = 1; |
---|
226 | } |
---|
227 | else if (i==1) |
---|
228 | { |
---|
229 | *result = a; |
---|
230 | } |
---|
231 | else |
---|
232 | { |
---|
233 | npPower(a,i-1,result,r); |
---|
234 | *result = npMultM(a,*result,r); |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | static const char* npEati(const char *s, int *i, const coeffs r) |
---|
239 | { |
---|
240 | |
---|
241 | if (((*s) >= '0') && ((*s) <= '9')) |
---|
242 | { |
---|
243 | unsigned long ii=0L; |
---|
244 | do |
---|
245 | { |
---|
246 | ii *= 10; |
---|
247 | ii += *s++ - '0'; |
---|
248 | if (ii >= (MAX_INT / 10)) ii = ii % r->npPrimeM; |
---|
249 | } |
---|
250 | while (((*s) >= '0') && ((*s) <= '9')); |
---|
251 | if (ii >= npPrimeM) ii = ii % r->npPrimeM; |
---|
252 | *i=(int)ii; |
---|
253 | } |
---|
254 | else (*i) = 1; |
---|
255 | return s; |
---|
256 | } |
---|
257 | |
---|
258 | const char * npRead (const char *s, number *a, const coeffs r) |
---|
259 | { |
---|
260 | int z; |
---|
261 | int n=1; |
---|
262 | |
---|
263 | s = npEati(s, &z, r); |
---|
264 | if ((*s) == '/') |
---|
265 | { |
---|
266 | s++; |
---|
267 | s = npEati(s, &n, r); |
---|
268 | } |
---|
269 | if (n == 1) |
---|
270 | *a = (number)z; |
---|
271 | else |
---|
272 | { |
---|
273 | if ((z==0)&&(n==0)) WerrorS(nDivBy0); |
---|
274 | else |
---|
275 | { |
---|
276 | #ifdef NV_OPS |
---|
277 | if (r->npPrimeM>NV_MAX_PRIME) |
---|
278 | *a = nvDiv((number)z,(number)n,r); |
---|
279 | else |
---|
280 | #endif |
---|
281 | *a = npDiv((number)z,(number)n,r); |
---|
282 | } |
---|
283 | } |
---|
284 | return s; |
---|
285 | } |
---|
286 | |
---|
287 | /*2 |
---|
288 | * set the charcteristic (allocate and init tables) |
---|
289 | */ |
---|
290 | |
---|
291 | void npSetChar(int c, coeffs r) |
---|
292 | { |
---|
293 | |
---|
294 | // if (c==npPrimeM) return; |
---|
295 | if ((c>1) || (c<(-1))) |
---|
296 | { |
---|
297 | if (c>1) r->npPrimeM = c; |
---|
298 | else r->npPrimeM = -c; |
---|
299 | r->npPminus1M = r->npPrimeM - 1; |
---|
300 | #ifdef NV_OPS |
---|
301 | if (r->npPrimeM >NV_MAX_PRIME) return; |
---|
302 | #endif |
---|
303 | #if !defined(HAVE_DIV_MOD) || !defined(HAVE_MULT_MOD) |
---|
304 | npGen = npExpTable[1]; |
---|
305 | #endif |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | void npKillChar(coeffs r) |
---|
310 | { |
---|
311 | #ifdef HAVE_DIV_MOD |
---|
312 | if (r->npInvTable!=NULL) |
---|
313 | omFreeSize( (void *)r->npInvTable, r->npPrimeM*sizeof(unsigned short) ); |
---|
314 | r->npInvTable=NULL; |
---|
315 | #else |
---|
316 | if (r->npExpTable!=NULL) |
---|
317 | { |
---|
318 | omFreeSize( (void *)r->npExpTable, r->npPrimeM*sizeof(unsigned short) ); |
---|
319 | omFreeSize( (void *)r->npLogTable, r->npPrimeM*sizeof(unsigned short) ); |
---|
320 | r->npExpTable=NULL; r->npLogTable=NULL; |
---|
321 | } |
---|
322 | #endif |
---|
323 | } |
---|
324 | |
---|
325 | void npInitChar(coeffs r, int c) |
---|
326 | { |
---|
327 | int i, w; |
---|
328 | |
---|
329 | if ((c>1) || (c<(-1))) |
---|
330 | { |
---|
331 | if (c>1) r->npPrimeM = c; |
---|
332 | else r->npPrimeM = -c; |
---|
333 | r->npPminus1M = r->npPrimeM - 1; |
---|
334 | #ifdef NV_OPS |
---|
335 | if (r->npPrimeM <=NV_MAX_PRIME) |
---|
336 | #endif |
---|
337 | { |
---|
338 | #if !defined(HAVE_DIV_MOD) || !defined(HAVE_MULT_MOD) |
---|
339 | r->npExpTable=(unsigned short *)omAlloc( r->npPrimeM*sizeof(unsigned short) ); |
---|
340 | r->npLogTable=(unsigned short *)omAlloc( r->npPrimeM*sizeof(unsigned short) ); |
---|
341 | r->npExpTable[0] = 1; |
---|
342 | r->npLogTable[0] = 0; |
---|
343 | if (r->npPrimeM > 2) |
---|
344 | { |
---|
345 | w = 1; |
---|
346 | loop |
---|
347 | { |
---|
348 | r->npLogTable[1] = 0; |
---|
349 | w++; |
---|
350 | i = 0; |
---|
351 | loop |
---|
352 | { |
---|
353 | i++; |
---|
354 | r->npExpTable[i] =(int)(((long)w * (long)r->npExpTable[i-1]) |
---|
355 | % r->npPrimeM); |
---|
356 | r->npLogTable[r->npExpTable[i]] = i; |
---|
357 | if (/*(i == npPrimeM - 1 ) ||*/ (r->npExpTable[i] == 1)) |
---|
358 | break; |
---|
359 | } |
---|
360 | if (i == r->npPrimeM - 1) |
---|
361 | break; |
---|
362 | } |
---|
363 | } |
---|
364 | else |
---|
365 | { |
---|
366 | r->npExpTable[1] = 1; |
---|
367 | r->npLogTable[1] = 0; |
---|
368 | } |
---|
369 | #endif |
---|
370 | #ifdef HAVE_DIV_MOD |
---|
371 | r->npInvTable=(unsigned short*)omAlloc0( r->npPrimeM*sizeof(unsigned short) ); |
---|
372 | #endif |
---|
373 | } |
---|
374 | r->cfKillChar=npKillChar; |
---|
375 | r->cfSetChar=NULL; |
---|
376 | r->cfInit = npInit; |
---|
377 | r->n_Int = npInt; |
---|
378 | r->nAdd = npAdd; |
---|
379 | r->nSub = npSub; |
---|
380 | r->nMult = npMult; |
---|
381 | r->nDiv = npDiv; |
---|
382 | r->nExactDiv= npDiv; |
---|
383 | r->nNeg = npNeg; |
---|
384 | r->nInvers= npInvers; |
---|
385 | r->cfCopy = ndCopy; |
---|
386 | r->nGreater = npGreater; |
---|
387 | r->nEqual = npEqual; |
---|
388 | r->nIsZero = npIsZero; |
---|
389 | r->nIsOne = npIsOne; |
---|
390 | r->nIsMOne = npIsMOne; |
---|
391 | r->nGreaterZero = npGreaterZero; |
---|
392 | r->cfWrite = npWrite; |
---|
393 | r->nRead = npRead; |
---|
394 | r->nPower = npPower; |
---|
395 | r->cfSetMap = npSetMap; |
---|
396 | r->nName= ndName; |
---|
397 | r->nSize = ndSize; |
---|
398 | #ifdef LDEBUG |
---|
399 | r->nDBTest=npDBTest; |
---|
400 | #endif |
---|
401 | #ifdef NV_OPS |
---|
402 | if (c>NV_MAX_PRIME) |
---|
403 | { |
---|
404 | r->nMult = nvMult; |
---|
405 | r->nDiv = nvDiv; |
---|
406 | r->nExactDiv= nvDiv; |
---|
407 | r->nInvers= nvInvers; |
---|
408 | r->nPower= nvPower; |
---|
409 | } |
---|
410 | #endif |
---|
411 | } |
---|
412 | else |
---|
413 | { |
---|
414 | WarnS("nInitChar failed"); |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | #ifdef LDEBUG |
---|
419 | BOOLEAN npDBTest (number a, const coeffs r, const char *f, const int l) |
---|
420 | { |
---|
421 | if (((long)a<0) || ((long)a>r->npPrimeM)) |
---|
422 | { |
---|
423 | Print("wrong mod p number %ld at %s,%d\n",(long)a,f,l); |
---|
424 | return FALSE; |
---|
425 | } |
---|
426 | return TRUE; |
---|
427 | } |
---|
428 | #endif |
---|
429 | |
---|
430 | number npMap0(number from, const coeffs src, const coeffs dst_r) |
---|
431 | { |
---|
432 | return npInit(nlModP(from,dst_r->npPrimeM),dst_r); |
---|
433 | } |
---|
434 | |
---|
435 | number npMapP(number from, const coeffs src, const coeffs dst_r) |
---|
436 | { |
---|
437 | long i = (long)from; |
---|
438 | if (i>src->npPrimeM/2) |
---|
439 | { |
---|
440 | i-=src->npPrimeM; |
---|
441 | while (i < 0) i+=dst_r->npPrimeM; |
---|
442 | } |
---|
443 | i%=dst_r->npPrimeM; |
---|
444 | return (number)i; |
---|
445 | } |
---|
446 | |
---|
447 | static number npMapLongR(number from, const coeffs src, const coeffs dst_r) |
---|
448 | { |
---|
449 | gmp_float *ff=(gmp_float*)from; |
---|
450 | mpf_t *f=ff->_mpfp(); |
---|
451 | number res; |
---|
452 | mpz_ptr dest,ndest; |
---|
453 | int size,i; |
---|
454 | int e,al,bl,in; |
---|
455 | long iz; |
---|
456 | mp_ptr qp,dd,nn; |
---|
457 | |
---|
458 | size = (*f)[0]._mp_size; |
---|
459 | if (size == 0) |
---|
460 | return npInit(0,dst_r); |
---|
461 | if(size<0) |
---|
462 | size = -size; |
---|
463 | |
---|
464 | qp = (*f)[0]._mp_d; |
---|
465 | while(qp[0]==0) |
---|
466 | { |
---|
467 | qp++; |
---|
468 | size--; |
---|
469 | } |
---|
470 | |
---|
471 | if(dst_r->npPrimeM>2) |
---|
472 | e=(*f)[0]._mp_exp-size; |
---|
473 | else |
---|
474 | e=0; |
---|
475 | res = ALLOC_RNUMBER(); |
---|
476 | #if defined(LDEBUG) |
---|
477 | res->debug=123456; |
---|
478 | #endif |
---|
479 | dest = res->z; |
---|
480 | |
---|
481 | if (e<0) |
---|
482 | { |
---|
483 | al = dest->_mp_size = size; |
---|
484 | if (al<2) al = 2; |
---|
485 | dd = (mp_ptr)omAlloc(sizeof(mp_limb_t)*al); |
---|
486 | for (i=0;i<size;i++) dd[i] = qp[i]; |
---|
487 | bl = 1-e; |
---|
488 | nn = (mp_ptr)omAlloc(sizeof(mp_limb_t)*bl); |
---|
489 | nn[bl-1] = 1; |
---|
490 | for (i=bl-2;i>=0;i--) nn[i] = 0; |
---|
491 | ndest = res->n; |
---|
492 | ndest->_mp_d = nn; |
---|
493 | ndest->_mp_alloc = ndest->_mp_size = bl; |
---|
494 | res->s = 0; |
---|
495 | in=mpz_fdiv_ui(ndest,npPrimeM); |
---|
496 | mpz_clear(ndest); |
---|
497 | } |
---|
498 | else |
---|
499 | { |
---|
500 | al = dest->_mp_size = size+e; |
---|
501 | if (al<2) al = 2; |
---|
502 | dd = (mp_ptr)omAlloc(sizeof(mp_limb_t)*al); |
---|
503 | for (i=0;i<size;i++) dd[i+e] = qp[i]; |
---|
504 | for (i=0;i<e;i++) dd[i] = 0; |
---|
505 | res->s = 3; |
---|
506 | } |
---|
507 | |
---|
508 | dest->_mp_d = dd; |
---|
509 | dest->_mp_alloc = al; |
---|
510 | iz=mpz_fdiv_ui(dest,npPrimeM); |
---|
511 | mpz_clear(dest); |
---|
512 | if(res->s==0) |
---|
513 | iz=(long)npDiv((number)iz,(number)in,dst_r); |
---|
514 | omFreeBin((void *)res, rnumber_bin); |
---|
515 | return (number)iz; |
---|
516 | } |
---|
517 | |
---|
518 | #ifdef HAVE_RINGS |
---|
519 | /*2 |
---|
520 | * convert from a GMP integer |
---|
521 | */ |
---|
522 | number npMapGMP(number from) |
---|
523 | { |
---|
524 | int_number erg = (int_number) omAlloc(sizeof(mpz_t)); // evtl. spaeter mit bin |
---|
525 | mpz_init(erg); |
---|
526 | |
---|
527 | mpz_mod_ui(erg, (int_number) from, npPrimeM); |
---|
528 | number r = (number) mpz_get_si(erg); |
---|
529 | |
---|
530 | mpz_clear(erg); |
---|
531 | omFree((void *) erg); |
---|
532 | return (number) r; |
---|
533 | } |
---|
534 | |
---|
535 | /*2 |
---|
536 | * convert from an machine long |
---|
537 | */ |
---|
538 | number npMapMachineInt(number from) |
---|
539 | { |
---|
540 | long i = (long) (((unsigned long) from) % npPrimeM); |
---|
541 | return (number) i; |
---|
542 | } |
---|
543 | #endif |
---|
544 | |
---|
545 | nMapFunc npSetMap(const coeffs src, const coeffs dst) |
---|
546 | { |
---|
547 | #ifdef HAVE_RINGS |
---|
548 | if (nField_is_Ring_2toM(src)) |
---|
549 | { |
---|
550 | return npMapMachineInt; |
---|
551 | } |
---|
552 | if (nField_is_Ring_Z(src) || nField_is_Ring_PtoM(src) || nField_is_Ring_ModN(src)) |
---|
553 | { |
---|
554 | return npMapGMP; |
---|
555 | } |
---|
556 | #endif |
---|
557 | if (nField_is_Q(src)) |
---|
558 | { |
---|
559 | return npMap0; |
---|
560 | } |
---|
561 | if ( nField_is_Zp(src) ) |
---|
562 | { |
---|
563 | if (n_GetChar(src) == n_GetChar(dst)) |
---|
564 | { |
---|
565 | return ndCopyMap; |
---|
566 | } |
---|
567 | else |
---|
568 | { |
---|
569 | return npMapP; |
---|
570 | } |
---|
571 | } |
---|
572 | if (nField_is_long_R(src)) |
---|
573 | { |
---|
574 | return npMapLongR; |
---|
575 | } |
---|
576 | return NULL; /* default */ |
---|
577 | } |
---|
578 | |
---|
579 | // ----------------------------------------------------------- |
---|
580 | // operation for very large primes (32003< p < 2^31-1) |
---|
581 | // ---------------------------------------------------------- |
---|
582 | #ifdef NV_OPS |
---|
583 | |
---|
584 | number nvMult (number a,number b, const coeffs r) |
---|
585 | { |
---|
586 | //if (((long)a == 0) || ((long)b == 0)) |
---|
587 | // return (number)0; |
---|
588 | //else |
---|
589 | return nvMultM(a,b); |
---|
590 | } |
---|
591 | |
---|
592 | void nvInpMult(number &a, number b, const ring r) |
---|
593 | { |
---|
594 | number n=nvMultM(a,b); |
---|
595 | a=n; |
---|
596 | } |
---|
597 | |
---|
598 | |
---|
599 | long nvInvMod(long a) |
---|
600 | { |
---|
601 | long s, t; |
---|
602 | |
---|
603 | long u, v, u0, v0, u1, v1, u2, v2, q, r; |
---|
604 | |
---|
605 | u1=1; v1=0; |
---|
606 | u2=0; v2=1; |
---|
607 | u = a; v = R->npPrimeM; |
---|
608 | |
---|
609 | while (v != 0) |
---|
610 | { |
---|
611 | q = u / v; |
---|
612 | r = u % v; |
---|
613 | u = v; |
---|
614 | v = r; |
---|
615 | u0 = u2; |
---|
616 | v0 = v2; |
---|
617 | u2 = u1 - q*u2; |
---|
618 | v2 = v1- q*v2; |
---|
619 | u1 = u0; |
---|
620 | v1 = v0; |
---|
621 | } |
---|
622 | |
---|
623 | s = u1; |
---|
624 | //t = v1; |
---|
625 | if (s < 0) |
---|
626 | return s + R->npPrimeM; |
---|
627 | else |
---|
628 | return s; |
---|
629 | } |
---|
630 | |
---|
631 | inline number nvInversM (number c, const coeffs r) |
---|
632 | { |
---|
633 | long inv=nvInvMod((long)c,r); |
---|
634 | return (number)inv; |
---|
635 | } |
---|
636 | |
---|
637 | number nvDiv (number a,number b, const coeffs r) |
---|
638 | { |
---|
639 | if ((long)a==0) |
---|
640 | return (number)0; |
---|
641 | else if ((long)b==0) |
---|
642 | { |
---|
643 | WerrorS(nDivBy0); |
---|
644 | return (number)0; |
---|
645 | } |
---|
646 | else |
---|
647 | { |
---|
648 | number inv=nvInversM(b,r); |
---|
649 | return nvMultM(a,inv,r); |
---|
650 | } |
---|
651 | } |
---|
652 | number nvInvers (number c, const coeffs r) |
---|
653 | { |
---|
654 | if ((long)c==0) |
---|
655 | { |
---|
656 | WerrorS(nDivBy0); |
---|
657 | return (number)0; |
---|
658 | } |
---|
659 | return nvInversM(c,r); |
---|
660 | } |
---|
661 | void nvPower (number a, int i, number * result, const coeffs r) |
---|
662 | { |
---|
663 | if (i==0) |
---|
664 | { |
---|
665 | //npInit(1,result); |
---|
666 | *(long *)result = 1; |
---|
667 | } |
---|
668 | else if (i==1) |
---|
669 | { |
---|
670 | *result = a; |
---|
671 | } |
---|
672 | else |
---|
673 | { |
---|
674 | nvPower(a,i-1,result,r); |
---|
675 | *result = nvMultM(a,*result,r); |
---|
676 | } |
---|
677 | } |
---|
678 | #endif |
---|