Annotation of OpenXM_contrib2/asir2000/builtin/array.c, Revision 1.12
1.6 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.7 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.6 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.12 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.11 2000/12/05 06:59:15 noro Exp $
1.6 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "base.h"
52: #include "parse.h"
53: #include "inline.h"
1.4 noro 54:
55: #if 0
1.1 noro 56: #undef DMAR
57: #define DMAR(a1,a2,a3,d,r) (r)=dmar(a1,a2,a3,d);
1.4 noro 58: #endif
1.1 noro 59:
1.11 noro 60: extern int DP_Print; /* XXX */
1.1 noro 61:
1.3 noro 62: void inner_product_mat_int_mod(Q **,int **,int,int,int,Q *);
63: void solve_by_lu_mod(int **,int,int,int **,int);
1.1 noro 64: void solve_by_lu_gfmmat(GFMMAT,unsigned int,unsigned int *,unsigned int *);
65: int lu_gfmmat(GFMMAT,unsigned int,int *);
66: void mat_to_gfmmat(MAT,unsigned int,GFMMAT *);
67:
68: int generic_gauss_elim_mod(int **,int,int,int,int *);
69: int generic_gauss_elim(MAT ,MAT *,Q *,int **,int **);
70:
71: int gauss_elim_mod(int **,int,int,int);
72: int gauss_elim_mod1(int **,int,int,int);
73: int gauss_elim_geninv_mod(unsigned int **,int,int,int);
74: int gauss_elim_geninv_mod_swap(unsigned int **,int,int,unsigned int,unsigned int ***,int **);
75: void Pnewvect(), Pnewmat(), Psepvect(), Psize(), Pdet(), Pleqm(), Pleqm1(), Pgeninvm();
1.9 noro 76: void Pnewbytearray();
1.1 noro 77:
78: void Pgeneric_gauss_elim_mod();
79:
80: void Pmat_to_gfmmat(),Plu_gfmmat(),Psolve_by_lu_gfmmat();
81: void Pgeninvm_swap(), Premainder(), Psremainder(), Pvtol();
82: void sepvect();
83: void Pmulmat_gf2n();
84: void Pbconvmat_gf2n();
85: void Pmul_vect_mat_gf2n();
86: void PNBmul_gf2n();
87: void Pmul_mat_vect_int();
88: void Psepmat_destructive();
89: void Px962_irredpoly_up2();
90: void Pirredpoly_up2();
91: void Pnbpoly_up2();
92: void Pqsort();
93:
94: struct ftab array_tab[] = {
95: {"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4},
96: {"lu_gfmmat",Plu_gfmmat,2},
97: {"mat_to_gfmmat",Pmat_to_gfmmat,2},
98: {"generic_gauss_elim_mod",Pgeneric_gauss_elim_mod,2},
99: {"newvect",Pnewvect,-2},
100: {"newmat",Pnewmat,-3},
1.9 noro 101: {"newbytearray",Pnewbytearray,-2},
1.1 noro 102: {"sepmat_destructive",Psepmat_destructive,2},
103: {"sepvect",Psepvect,2},
104: {"qsort",Pqsort,-2},
105: {"vtol",Pvtol,1},
106: {"size",Psize,1},
107: {"det",Pdet,-2},
108: {"leqm",Pleqm,2},
109: {"leqm1",Pleqm1,2},
110: {"geninvm",Pgeninvm,2},
111: {"geninvm_swap",Pgeninvm_swap,2},
112: {"remainder",Premainder,2},
113: {"sremainder",Psremainder,2},
114: {"mulmat_gf2n",Pmulmat_gf2n,1},
115: {"bconvmat_gf2n",Pbconvmat_gf2n,-4},
116: {"mul_vect_mat_gf2n",Pmul_vect_mat_gf2n,2},
117: {"mul_mat_vect_int",Pmul_mat_vect_int,2},
118: {"nbmul_gf2n",PNBmul_gf2n,3},
119: {"x962_irredpoly_up2",Px962_irredpoly_up2,2},
120: {"irredpoly_up2",Pirredpoly_up2,2},
121: {"nbpoly_up2",Pnbpoly_up2,2},
122: {0,0,0},
123: };
124:
125: int comp_obj(a,b)
126: Obj *a,*b;
127: {
128: return arf_comp(CO,*a,*b);
129: }
130:
131: static FUNC generic_comp_obj_func;
132: static NODE generic_comp_obj_arg;
133:
134: int generic_comp_obj(a,b)
135: Obj *a,*b;
136: {
137: Q r;
138:
139: BDY(generic_comp_obj_arg)=(pointer)(*a);
140: BDY(NEXT(generic_comp_obj_arg))=(pointer)(*b);
141: r = (Q)bevalf(generic_comp_obj_func,generic_comp_obj_arg);
142: if ( !r )
143: return 0;
144: else
145: return SGN(r)>0?1:-1;
146: }
147:
148:
149: void Pqsort(arg,rp)
150: NODE arg;
151: VECT *rp;
152: {
153: VECT vect;
154: char buf[BUFSIZ];
155: char *fname;
156: NODE n;
157: P p;
158: V v;
159:
160: asir_assert(ARG0(arg),O_VECT,"qsort");
161: vect = (VECT)ARG0(arg);
162: if ( argc(arg) == 1 )
163: qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))comp_obj);
164: else {
165: p = (P)ARG1(arg);
166: if ( !p || OID(p)!=2 )
167: error("qsort : invalid argument");
168: v = VR(p);
169: if ( (int)v->attr != V_SR )
170: error("qsort : no such function");
171: generic_comp_obj_func = (FUNC)v->priv;
172: MKNODE(n,0,0); MKNODE(generic_comp_obj_arg,0,n);
173: qsort(BDY(vect),vect->len,sizeof(Obj),(int (*)(const void *,const void *))generic_comp_obj);
174: }
175: *rp = vect;
176: }
177:
178: void PNBmul_gf2n(arg,rp)
179: NODE arg;
180: GF2N *rp;
181: {
182: GF2N a,b;
183: GF2MAT mat;
184: int n,w;
185: unsigned int *ab,*bb;
186: UP2 r;
187:
188: a = (GF2N)ARG0(arg);
189: b = (GF2N)ARG1(arg);
190: mat = (GF2MAT)ARG2(arg);
191: if ( !a || !b )
192: *rp = 0;
193: else {
194: n = mat->row;
195: w = (n+BSH-1)/BSH;
196:
197: ab = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
198: bzero((char *)ab,w*sizeof(unsigned int));
199: bcopy(a->body->b,ab,(a->body->w)*sizeof(unsigned int));
200:
201: bb = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
202: bzero((char *)bb,w*sizeof(unsigned int));
203: bcopy(b->body->b,bb,(b->body->w)*sizeof(unsigned int));
204:
205: NEWUP2(r,w);
206: bzero((char *)r->b,w*sizeof(unsigned int));
207: mul_nb(mat,ab,bb,r->b);
208: r->w = w;
209: _adjup2(r);
210: if ( !r->w )
211: *rp = 0;
212: else
213: MKGF2N(r,*rp);
214: }
215: }
216:
217: void Pmul_vect_mat_gf2n(arg,rp)
218: NODE arg;
219: GF2N *rp;
220: {
221: GF2N a;
222: GF2MAT mat;
223: int n,w;
224: unsigned int *b;
225: UP2 r;
226:
227: a = (GF2N)ARG0(arg);
228: mat = (GF2MAT)ARG1(arg);
229: if ( !a )
230: *rp = 0;
231: else {
232: n = mat->row;
233: w = (n+BSH-1)/BSH;
234: b = (unsigned int *)ALLOCA(w*sizeof(unsigned int));
235: bzero((char *)b,w*sizeof(unsigned int));
236: bcopy(a->body->b,b,(a->body->w)*sizeof(unsigned int));
237: NEWUP2(r,w);
238: bzero((char *)r->b,w*sizeof(unsigned int));
239: mulgf2vectmat(mat->row,b,mat->body,r->b);
240: r->w = w;
241: _adjup2(r);
242: if ( !r->w )
243: *rp = 0;
244: else {
245: MKGF2N(r,*rp);
246: }
247: }
248: }
249:
250: void Pbconvmat_gf2n(arg,rp)
251: NODE arg;
252: LIST *rp;
253: {
254: P p0,p1;
255: int to;
256: GF2MAT p01,p10;
257: GF2N root;
258: NODE n0,n1;
259:
260: p0 = (P)ARG0(arg);
261: p1 = (P)ARG1(arg);
262: to = ARG2(arg)?1:0;
263: if ( argc(arg) == 4 ) {
264: root = (GF2N)ARG3(arg);
265: compute_change_of_basis_matrix_with_root(p0,p1,to,root,&p01,&p10);
266: } else
267: compute_change_of_basis_matrix(p0,p1,to,&p01,&p10);
268: MKNODE(n1,p10,0); MKNODE(n0,p01,n1);
269: MKLIST(*rp,n0);
270: }
271:
272: void Pmulmat_gf2n(arg,rp)
273: NODE arg;
274: GF2MAT *rp;
275: {
276: GF2MAT m;
277:
278: if ( !compute_multiplication_matrix((P)ARG0(arg),&m) )
279: error("mulmat_gf2n : input is not a normal polynomial");
280: *rp = m;
281: }
282:
283: void Psepmat_destructive(arg,rp)
284: NODE arg;
285: LIST *rp;
286: {
287: MAT mat,mat1;
288: int i,j,row,col;
289: Q **a,**a1;
290: Q ent;
291: N nm,mod,rem,quo;
292: int sgn;
293: NODE n0,n1;
294:
295: mat = (MAT)ARG0(arg); mod = NM((Q)ARG1(arg));
296: row = mat->row; col = mat->col;
297: MKMAT(mat1,row,col);
298: a = (Q **)mat->body; a1 = (Q **)mat1->body;
299: for ( i = 0; i < row; i++ )
300: for ( j = 0; j < col; j++ ) {
301: ent = a[i][j];
302: if ( !ent )
303: continue;
304: nm = NM(ent);
305: sgn = SGN(ent);
306: divn(nm,mod,&quo,&rem);
307: /* if ( quo != nm && rem != nm ) */
308: /* GC_free(nm); */
309: /* GC_free(ent); */
310: NTOQ(rem,sgn,a[i][j]); NTOQ(quo,sgn,a1[i][j]);
311: }
312: MKNODE(n1,mat1,0); MKNODE(n0,mat,n1);
313: MKLIST(*rp,n0);
314: }
315:
316: void Psepvect(arg,rp)
317: NODE arg;
318: VECT *rp;
319: {
320: sepvect((VECT)ARG0(arg),QTOS((Q)ARG1(arg)),rp);
321: }
322:
323: void sepvect(v,d,rp)
324: VECT v;
325: int d;
326: VECT *rp;
327: {
328: int i,j,k,n,q,q1,r;
329: pointer *pv,*pw,*pu;
330: VECT w,u;
331:
332: n = v->len;
333: if ( d > n )
334: d = n;
335: q = n/d; r = n%d; q1 = q+1;
336: MKVECT(w,d); *rp = w;
337: pv = BDY(v); pw = BDY(w); k = 0;
338: for ( i = 0; i < r; i++ ) {
339: MKVECT(u,q1); pw[i] = (pointer)u;
340: for ( pu = BDY(u), j = 0; j < q1; j++, k++ )
341: pu[j] = pv[k];
342: }
343: for ( ; i < d; i++ ) {
344: MKVECT(u,q); pw[i] = (pointer)u;
345: for ( pu = BDY(u), j = 0; j < q; j++, k++ )
346: pu[j] = pv[k];
347: }
348: }
349:
350: void Pnewvect(arg,rp)
351: NODE arg;
352: VECT *rp;
353: {
354: int len,i,r;
355: VECT vect;
356: pointer *vb;
357: LIST list;
358: NODE tn;
359:
360: asir_assert(ARG0(arg),O_N,"newvect");
361: len = QTOS((Q)ARG0(arg));
1.5 noro 362: if ( len < 0 )
1.1 noro 363: error("newvect : invalid size");
364: MKVECT(vect,len);
365: if ( argc(arg) == 2 ) {
366: list = (LIST)ARG1(arg);
367: asir_assert(list,O_LIST,"newvect");
368: for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) );
369: if ( r > len ) {
370: *rp = vect;
371: return;
372: }
373: for ( i = 0, tn = BDY(list), vb = BDY(vect); tn; i++, tn = NEXT(tn) )
374: vb[i] = (pointer)BDY(tn);
375: }
376: *rp = vect;
1.9 noro 377: }
378:
379: void Pnewbytearray(arg,rp)
380: NODE arg;
381: BYTEARRAY *rp;
382: {
383: int len,i,r;
384: BYTEARRAY array;
385: unsigned char *vb;
1.10 noro 386: char *str;
1.9 noro 387: LIST list;
388: NODE tn;
389:
390: asir_assert(ARG0(arg),O_N,"newbytearray");
391: len = QTOS((Q)ARG0(arg));
392: if ( len < 0 )
393: error("newbytearray : invalid size");
394: MKBYTEARRAY(array,len);
395: if ( argc(arg) == 2 ) {
1.10 noro 396: if ( !ARG1(arg) )
397: error("newbytearray : invalid initialization");
398: switch ( OID((Obj)ARG1(arg)) ) {
399: case O_LIST:
400: list = (LIST)ARG1(arg);
401: asir_assert(list,O_LIST,"newbytearray");
402: for ( r = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) );
403: if ( r <= len ) {
404: for ( i = 0, tn = BDY(list), vb = BDY(array); tn;
405: i++, tn = NEXT(tn) )
406: vb[i] = (unsigned char)QTOS((Q)BDY(tn));
407: }
408: break;
409: case O_STR:
410: str = BDY((STRING)ARG1(arg));
411: r = strlen(str);
412: if ( r <= len )
413: bcopy(str,BDY(array),r);
414: break;
415: default:
416: if ( !ARG1(arg) )
417: error("newbytearray : invalid initialization");
1.9 noro 418: }
419: }
420: *rp = array;
1.1 noro 421: }
422:
423: void Pnewmat(arg,rp)
424: NODE arg;
425: MAT *rp;
426: {
427: int row,col;
428: int i,j,r,c;
429: NODE tn,sn;
430: MAT m;
431: pointer **mb;
432: LIST list;
433:
434: asir_assert(ARG0(arg),O_N,"newmat");
435: asir_assert(ARG1(arg),O_N,"newmat");
436: row = QTOS((Q)ARG0(arg)); col = QTOS((Q)ARG1(arg));
1.5 noro 437: if ( row < 0 || col < 0 )
1.1 noro 438: error("newmat : invalid size");
439: MKMAT(m,row,col);
440: if ( argc(arg) == 3 ) {
441: list = (LIST)ARG2(arg);
442: asir_assert(list,O_LIST,"newmat");
443: for ( r = 0, c = 0, tn = BDY(list); tn; r++, tn = NEXT(tn) ) {
444: for ( j = 0, sn = BDY((LIST)BDY(tn)); sn; j++, sn = NEXT(sn) );
445: c = MAX(c,j);
446: }
447: if ( (r > row) || (c > col) ) {
448: *rp = m;
449: return;
450: }
451: for ( i = 0, tn = BDY(list), mb = BDY(m); tn; i++, tn = NEXT(tn) ) {
452: asir_assert(BDY(tn),O_LIST,"newmat");
453: for ( j = 0, sn = BDY((LIST)BDY(tn)); sn; j++, sn = NEXT(sn) )
454: mb[i][j] = (pointer)BDY(sn);
455: }
456: }
457: *rp = m;
458: }
459:
460: void Pvtol(arg,rp)
461: NODE arg;
462: LIST *rp;
463: {
464: NODE n,n1;
465: VECT v;
466: pointer *a;
467: int len,i;
468:
469: asir_assert(ARG0(arg),O_VECT,"vtol");
470: v = (VECT)ARG0(arg); len = v->len; a = BDY(v);
471: for ( i = len - 1, n = 0; i >= 0; i-- ) {
472: MKNODE(n1,a[i],n); n = n1;
473: }
474: MKLIST(*rp,n);
475: }
476:
477: void Premainder(arg,rp)
478: NODE arg;
479: Obj *rp;
480: {
481: Obj a;
482: VECT v,w;
483: MAT m,l;
484: pointer *vb,*wb;
485: pointer **mb,**lb;
486: int id,i,j,n,row,col,t,smd,sgn;
487: Q md,q;
488:
489: a = (Obj)ARG0(arg); md = (Q)ARG1(arg);
490: if ( !a )
491: *rp = 0;
492: else {
493: id = OID(a);
494: switch ( id ) {
495: case O_N:
496: case O_P:
497: cmp(md,(P)a,(P *)rp); break;
498: case O_VECT:
499: smd = QTOS(md);
500: v = (VECT)a; n = v->len; vb = v->body;
501: MKVECT(w,n); wb = w->body;
502: for ( i = 0; i < n; i++ ) {
503: if ( q = (Q)vb[i] ) {
504: sgn = SGN(q); t = rem(NM(q),smd);
505: STOQ(t,q);
506: if ( q )
507: SGN(q) = sgn;
508: }
509: wb[i] = (pointer)q;
510: }
511: *rp = (Obj)w;
512: break;
513: case O_MAT:
514: m = (MAT)a; row = m->row; col = m->col; mb = m->body;
515: MKMAT(l,row,col); lb = l->body;
516: for ( i = 0; i < row; i++ )
517: for ( j = 0, vb = mb[i], wb = lb[i]; j < col; j++ )
518: cmp(md,(P)vb[j],(P *)&wb[j]);
519: *rp = (Obj)l;
520: break;
521: default:
522: error("remainder : invalid argument");
523: }
524: }
525: }
526:
527: void Psremainder(arg,rp)
528: NODE arg;
529: Obj *rp;
530: {
531: Obj a;
532: VECT v,w;
533: MAT m,l;
534: pointer *vb,*wb;
535: pointer **mb,**lb;
536: unsigned int t,smd;
537: int id,i,j,n,row,col;
538: Q md,q;
539:
540: a = (Obj)ARG0(arg); md = (Q)ARG1(arg);
541: if ( !a )
542: *rp = 0;
543: else {
544: id = OID(a);
545: switch ( id ) {
546: case O_N:
547: case O_P:
548: cmp(md,(P)a,(P *)rp); break;
549: case O_VECT:
550: smd = QTOS(md);
551: v = (VECT)a; n = v->len; vb = v->body;
552: MKVECT(w,n); wb = w->body;
553: for ( i = 0; i < n; i++ ) {
554: if ( q = (Q)vb[i] ) {
555: t = (unsigned int)rem(NM(q),smd);
556: if ( SGN(q) < 0 )
557: t = (smd - t) % smd;
558: UTOQ(t,q);
559: }
560: wb[i] = (pointer)q;
561: }
562: *rp = (Obj)w;
563: break;
564: case O_MAT:
565: m = (MAT)a; row = m->row; col = m->col; mb = m->body;
566: MKMAT(l,row,col); lb = l->body;
567: for ( i = 0; i < row; i++ )
568: for ( j = 0, vb = mb[i], wb = lb[i]; j < col; j++ )
569: cmp(md,(P)vb[j],(P *)&wb[j]);
570: *rp = (Obj)l;
571: break;
572: default:
573: error("remainder : invalid argument");
574: }
575: }
576: }
577:
578: void Psize(arg,rp)
579: NODE arg;
580: LIST *rp;
581: {
582:
583: int n,m;
584: Q q;
585: NODE t,s;
586:
587: if ( !ARG0(arg) )
588: t = 0;
589: else {
590: switch (OID(ARG0(arg))) {
591: case O_VECT:
592: n = ((VECT)ARG0(arg))->len;
593: STOQ(n,q); MKNODE(t,q,0);
594: break;
595: case O_MAT:
596: n = ((MAT)ARG0(arg))->row; m = ((MAT)ARG0(arg))->col;
597: STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s);
598: break;
599: default:
600: error("size : invalid argument"); break;
601: }
602: }
603: MKLIST(*rp,t);
604: }
605:
606: void Pdet(arg,rp)
607: NODE arg;
608: P *rp;
609: {
610: MAT m;
611: int n,i,j,mod;
612: P d;
613: P **mat,**w;
614:
615: m = (MAT)ARG0(arg);
616: asir_assert(m,O_MAT,"det");
617: if ( m->row != m->col )
618: error("det : non-square matrix");
619: else if ( argc(arg) == 1 )
620: detp(CO,(P **)BDY(m),m->row,rp);
621: else {
622: n = m->row; mod = QTOS((Q)ARG1(arg)); mat = (P **)BDY(m);
623: w = (P **)almat_pointer(n,n);
624: for ( i = 0; i < n; i++ )
625: for ( j = 0; j < n; j++ )
626: ptomp(mod,mat[i][j],&w[i][j]);
627: detmp(CO,mod,w,n,&d);
628: mptop(d,rp);
629: }
630: }
631:
632: /*
633: input : a row x col matrix A
634: A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
635:
636: output : [B,R,C]
637: B : a rank(A) x col-rank(A) matrix
638: R : a vector of length rank(A)
639: C : a vector of length col-rank(A)
640: B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
641: */
642:
643: void Pgeneric_gauss_elim_mod(arg,rp)
644: NODE arg;
645: LIST *rp;
646: {
647: NODE n0;
648: MAT m,mat;
649: VECT rind,cind;
650: Q **tmat;
651: int **wmat;
652: Q *rib,*cib;
653: int *colstat;
654: Q q;
655: int md,i,j,k,l,row,col,t,n,rank;
656:
657: asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim_mod");
658: asir_assert(ARG1(arg),O_N,"generic_gauss_elim_mod");
659: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
660: row = m->row; col = m->col; tmat = (Q **)m->body;
661: wmat = (int **)almat(row,col);
662: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
663: for ( i = 0; i < row; i++ )
664: for ( j = 0; j < col; j++ )
665: if ( q = (Q)tmat[i][j] ) {
666: t = rem(NM(q),md);
667: if ( t && SGN(q) < 0 )
668: t = (md - t) % md;
669: wmat[i][j] = t;
670: } else
671: wmat[i][j] = 0;
672: rank = generic_gauss_elim_mod(wmat,row,col,md,colstat);
673:
674: MKMAT(mat,rank,col-rank);
675: tmat = (Q **)mat->body;
676: for ( i = 0; i < rank; i++ )
677: for ( j = k = 0; j < col; j++ )
678: if ( !colstat[j] ) {
679: UTOQ(wmat[i][j],tmat[i][k]); k++;
680: }
681:
682: MKVECT(rind,rank);
683: MKVECT(cind,col-rank);
684: rib = (Q *)rind->body; cib = (Q *)cind->body;
685: for ( j = k = l = 0; j < col; j++ )
686: if ( colstat[j] ) {
687: STOQ(j,rib[k]); k++;
688: } else {
689: STOQ(j,cib[l]); l++;
690: }
691: n0 = mknode(3,mat,rind,cind);
692: MKLIST(*rp,n0);
693: }
694:
695: void Pleqm(arg,rp)
696: NODE arg;
697: VECT *rp;
698: {
699: MAT m;
700: VECT vect;
701: pointer **mat;
702: Q *v;
703: Q q;
704: int **wmat;
705: int md,i,j,row,col,t,n,status;
706:
707: asir_assert(ARG0(arg),O_MAT,"leqm");
708: asir_assert(ARG1(arg),O_N,"leqm");
709: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
710: row = m->row; col = m->col; mat = m->body;
711: wmat = (int **)almat(row,col);
712: for ( i = 0; i < row; i++ )
713: for ( j = 0; j < col; j++ )
714: if ( q = (Q)mat[i][j] ) {
715: t = rem(NM(q),md);
716: if ( SGN(q) < 0 )
717: t = (md - t) % md;
718: wmat[i][j] = t;
719: } else
720: wmat[i][j] = 0;
721: status = gauss_elim_mod(wmat,row,col,md);
722: if ( status < 0 )
723: *rp = 0;
724: else if ( status > 0 )
725: *rp = (VECT)ONE;
726: else {
727: n = col - 1;
728: MKVECT(vect,n);
729: for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
730: t = (md-wmat[i][n])%md; STOQ(t,v[i]);
731: }
732: *rp = vect;
733: }
734: }
735:
736: int gauss_elim_mod(mat,row,col,md)
737: int **mat;
738: int row,col,md;
739: {
740: int i,j,k,inv,a,n;
741: int *t,*pivot;
742:
743: n = col - 1;
744: for ( j = 0; j < n; j++ ) {
745: for ( i = j; i < row && !mat[i][j]; i++ );
746: if ( i == row )
747: return 1;
748: if ( i != j ) {
749: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
750: }
751: pivot = mat[j];
752: inv = invm(pivot[j],md);
753: for ( k = j; k <= n; k++ ) {
754: /* pivot[k] = dmar(pivot[k],inv,0,md); */
755: DMAR(pivot[k],inv,0,md,pivot[k])
756: }
757: for ( i = 0; i < row; i++ ) {
758: t = mat[i];
759: if ( i != j && (a = t[j]) )
760: for ( k = j, a = md - a; k <= n; k++ ) {
1.8 noro 761: unsigned int tk;
1.1 noro 762: /* t[k] = dmar(pivot[k],a,t[k],md); */
1.8 noro 763: DMAR(pivot[k],a,t[k],md,tk)
764: t[k] = tk;
1.1 noro 765: }
766: }
767: }
768: for ( i = n; i < row && !mat[i][n]; i++ );
769: if ( i == row )
770: return 0;
771: else
772: return -1;
773: }
774:
1.4 noro 775: struct oEGT eg_mod,eg_elim,eg_elim1,eg_elim2,eg_chrem,eg_gschk,eg_intrat,eg_symb;
1.1 noro 776:
777: int generic_gauss_elim(mat,nm,dn,rindp,cindp)
778: MAT mat;
779: MAT *nm;
780: Q *dn;
781: int **rindp,**cindp;
782: {
783: int **wmat;
784: Q **bmat;
785: N **tmat;
786: Q *bmi;
787: N *tmi;
788: Q q;
789: int *wmi;
790: int *colstat,*wcolstat,*rind,*cind;
791: int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv;
792: N m1,m2,m3,s,u;
793: MAT r,crmat;
794: struct oEGT tmp0,tmp1;
795: struct oEGT eg_mod_split,eg_elim_split,eg_chrem_split;
796: struct oEGT eg_intrat_split,eg_gschk_split;
797: int ret;
798:
799: init_eg(&eg_mod_split); init_eg(&eg_chrem_split);
800: init_eg(&eg_elim_split); init_eg(&eg_intrat_split);
801: init_eg(&eg_gschk_split);
802: bmat = (Q **)mat->body;
803: row = mat->row; col = mat->col;
804: wmat = (int **)almat(row,col);
805: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
806: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
807: for ( ind = 0; ; ind++ ) {
1.11 noro 808: if ( DP_Print ) {
1.2 noro 809: fprintf(asir_out,"."); fflush(asir_out);
810: }
1.12 ! noro 811: md = get_lprime(ind);
1.1 noro 812: get_eg(&tmp0);
813: for ( i = 0; i < row; i++ )
814: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
815: if ( q = (Q)bmi[j] ) {
816: t = rem(NM(q),md);
817: if ( t && SGN(q) < 0 )
818: t = (md - t) % md;
819: wmi[j] = t;
820: } else
821: wmi[j] = 0;
822: get_eg(&tmp1);
823: add_eg(&eg_mod,&tmp0,&tmp1);
824: add_eg(&eg_mod_split,&tmp0,&tmp1);
825: get_eg(&tmp0);
826: rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
827: get_eg(&tmp1);
828: add_eg(&eg_elim,&tmp0,&tmp1);
829: add_eg(&eg_elim_split,&tmp0,&tmp1);
830: if ( !ind ) {
831: RESET:
832: UTON(md,m1);
833: rank0 = rank;
834: bcopy(wcolstat,colstat,col*sizeof(int));
835: MKMAT(crmat,rank,col-rank);
836: MKMAT(r,rank,col-rank); *nm = r;
837: tmat = (N **)crmat->body;
838: for ( i = 0; i < rank; i++ )
839: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
840: if ( !colstat[j] ) {
841: UTON(wmi[j],tmi[k]); k++;
842: }
843: } else {
844: if ( rank < rank0 ) {
1.11 noro 845: if ( DP_Print ) {
1.1 noro 846: fprintf(asir_out,"lower rank matrix; continuing...\n");
1.2 noro 847: fflush(asir_out);
848: }
1.1 noro 849: continue;
850: } else if ( rank > rank0 ) {
1.11 noro 851: if ( DP_Print ) {
1.1 noro 852: fprintf(asir_out,"higher rank matrix; resetting...\n");
1.2 noro 853: fflush(asir_out);
854: }
1.1 noro 855: goto RESET;
856: } else {
857: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
858: if ( j < col ) {
1.11 noro 859: if ( DP_Print ) {
1.1 noro 860: fprintf(asir_out,"inconsitent colstat; resetting...\n");
1.2 noro 861: fflush(asir_out);
862: }
1.1 noro 863: goto RESET;
864: }
865: }
866:
867: get_eg(&tmp0);
868: inv = invm(rem(m1,md),md);
869: UTON(md,m2); muln(m1,m2,&m3);
870: for ( i = 0; i < rank; i++ )
871: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
872: if ( !colstat[j] ) {
873: if ( tmi[k] ) {
874: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
875: t = rem(tmi[k],md);
876: if ( wmi[j] >= t )
877: t = wmi[j]-t;
878: else
879: t = md-(t-wmi[j]);
880: DMAR(t,inv,0,md,t1)
881: UTON(t1,u);
882: muln(m1,u,&s);
883: addn(tmi[k],s,&u); tmi[k] = u;
884: } else if ( wmi[j] ) {
885: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
886: DMAR(wmi[j],inv,0,md,t)
887: UTON(t,u);
888: muln(m1,u,&s); tmi[k] = s;
889: }
890: k++;
891: }
892: m1 = m3;
893: get_eg(&tmp1);
894: add_eg(&eg_chrem,&tmp0,&tmp1);
895: add_eg(&eg_chrem_split,&tmp0,&tmp1);
896:
897: get_eg(&tmp0);
898: ret = intmtoratm(crmat,m1,*nm,dn);
899: get_eg(&tmp1);
900: add_eg(&eg_intrat,&tmp0,&tmp1);
901: add_eg(&eg_intrat_split,&tmp0,&tmp1);
902: if ( ret ) {
903: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
904: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
905: for ( j = k = l = 0; j < col; j++ )
906: if ( colstat[j] )
907: rind[k++] = j;
908: else
909: cind[l++] = j;
910: get_eg(&tmp0);
1.3 noro 911: if ( gensolve_check(mat,*nm,*dn,rind,cind) ) {
912: get_eg(&tmp1);
913: add_eg(&eg_gschk,&tmp0,&tmp1);
914: add_eg(&eg_gschk_split,&tmp0,&tmp1);
1.11 noro 915: if ( DP_Print ) {
1.3 noro 916: print_eg("Mod",&eg_mod_split);
917: print_eg("Elim",&eg_elim_split);
918: print_eg("ChRem",&eg_chrem_split);
919: print_eg("IntRat",&eg_intrat_split);
920: print_eg("Check",&eg_gschk_split);
921: fflush(asir_out);
922: }
923: return rank;
924: }
925: }
926: }
927: }
928: }
929:
930: int generic_gauss_elim_hensel(mat,nmmat,dn,rindp,cindp)
931: MAT mat;
932: MAT *nmmat;
933: Q *dn;
934: int **rindp,**cindp;
935: {
936: MAT bmat,xmat;
937: Q **a0,**a,**b,**x,**nm;
938: Q *ai,*bi,*xi;
939: int row,col;
940: int **w;
941: int *wi;
942: int **wc;
943: Q mdq,q,s,u;
944: N tn;
945: int ind,md,i,j,k,l,li,ri,rank;
946: unsigned int t;
947: int *cinfo,*rinfo;
948: int *rind,*cind;
949: int count;
950: struct oEGT eg_mul,eg_inv,tmp0,tmp1;
951:
952: a0 = (Q **)mat->body;
953: row = mat->row; col = mat->col;
954: w = (int **)almat(row,col);
955: for ( ind = 0; ; ind++ ) {
1.12 ! noro 956: md = get_lprime(ind);
1.3 noro 957: STOQ(md,mdq);
958: for ( i = 0; i < row; i++ )
959: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
960: if ( q = (Q)ai[j] ) {
961: t = rem(NM(q),md);
962: if ( t && SGN(q) < 0 )
963: t = (md - t) % md;
964: wi[j] = t;
965: } else
966: wi[j] = 0;
967:
968: rank = find_lhs_and_lu_mod(w,row,col,md,&rinfo,&cinfo);
969: a = (Q **)almat_pointer(rank,rank); /* lhs mat */
970: MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
971: for ( j = li = ri = 0; j < col; j++ )
972: if ( cinfo[j] ) {
973: /* the column is in lhs */
974: for ( i = 0; i < rank; i++ ) {
975: w[i][li] = w[i][j];
976: a[i][li] = a0[rinfo[i]][j];
977: }
978: li++;
979: } else {
980: /* the column is in rhs */
981: for ( i = 0; i < rank; i++ )
982: b[i][ri] = a0[rinfo[i]][j];
983: ri++;
984: }
985:
986: /* solve Ax+B=0; A: rank x rank, B: rank x ri */
987: MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
988: MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
989: /* use the right part of w as work area */
990: /* ri = col - rank */
991: wc = (int **)almat(rank,ri);
992: for ( i = 0; i < rank; i++ )
993: wc[i] = w[i]+rank;
994: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
995: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
996:
997: init_eg(&eg_mul); init_eg(&eg_inv);
998: for ( q = ONE, count = 0; ; count++ ) {
999: fprintf(stderr,".");
1000: /* wc = -b mod md */
1001: for ( i = 0; i < rank; i++ )
1002: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1003: if ( u = (Q)bi[j] ) {
1004: t = rem(NM(u),md);
1005: if ( t && SGN(u) > 0 )
1006: t = (md - t) % md;
1007: wi[j] = t;
1008: } else
1009: wi[j] = 0;
1010: /* wc = A^(-1)wc; wc is normalized */
1011: get_eg(&tmp0);
1012: solve_by_lu_mod(w,rank,md,wc,ri);
1.1 noro 1013: get_eg(&tmp1);
1.3 noro 1014: add_eg(&eg_inv,&tmp0,&tmp1);
1015: /* x = x-q*wc */
1016: for ( i = 0; i < rank; i++ )
1017: for ( j = 0, xi = x[i], wi = wc[i]; j < ri; j++ ) {
1018: STOQ(wi[j],u); mulq(q,u,&s);
1019: subq(xi[j],s,&u); xi[j] = u;
1020: }
1021: get_eg(&tmp0);
1022: for ( i = 0; i < rank; i++ )
1023: for ( j = 0; j < ri; j++ ) {
1024: inner_product_mat_int_mod(a,wc,rank,i,j,&u);
1025: addq(b[i][j],u,&s);
1026: if ( s ) {
1027: t = divin(NM(s),md,&tn);
1028: if ( t )
1029: error("generic_gauss_elim_hensel:incosistent");
1030: NTOQ(tn,SGN(s),b[i][j]);
1031: } else
1032: b[i][j] = 0;
1033: }
1034: get_eg(&tmp1);
1035: add_eg(&eg_mul,&tmp0,&tmp1);
1036: /* q = q*md */
1037: mulq(q,mdq,&u); q = u;
1038: if ( !(count % 2) && intmtoratm_q(xmat,NM(q),*nmmat,dn) ) {
1039: for ( j = k = l = 0; j < col; j++ )
1040: if ( cinfo[j] )
1041: rind[k++] = j;
1042: else
1043: cind[l++] = j;
1044: if ( gensolve_check(mat,*nmmat,*dn,rind,cind) ) {
1045: fprintf(stderr,"\n");
1046: print_eg("INV",&eg_inv);
1047: print_eg("MUL",&eg_mul);
1048: fflush(asir_out);
1049: return rank;
1050: }
1.1 noro 1051: }
1052: }
1053: }
1054: }
1055:
1056: int f4_nocheck;
1057:
1058: int gensolve_check(mat,nm,dn,rind,cind)
1059: MAT mat,nm;
1060: Q dn;
1061: int *rind,*cind;
1062: {
1063: int row,col,rank,clen,i,j,k,l;
1064: Q s,t,u;
1065: Q *w;
1066: Q *mati,*nmk;
1067:
1068: if ( f4_nocheck )
1069: return 1;
1070: row = mat->row; col = mat->col;
1071: rank = nm->row; clen = nm->col;
1072: w = (Q *)MALLOC(clen*sizeof(Q));
1073: for ( i = 0; i < row; i++ ) {
1074: mati = (Q *)mat->body[i];
1075: #if 1
1076: bzero(w,clen*sizeof(Q));
1077: for ( k = 0; k < rank; k++ )
1078: for ( l = 0, nmk = (Q *)nm->body[k]; l < clen; l++ ) {
1079: mulq(mati[rind[k]],nmk[l],&t);
1080: addq(w[l],t,&s); w[l] = s;
1081: }
1082: for ( j = 0; j < clen; j++ ) {
1083: mulq(dn,mati[cind[j]],&t);
1084: if ( cmpq(w[j],t) )
1085: break;
1086: }
1087: #else
1088: for ( j = 0; j < clen; j++ ) {
1089: for ( k = 0, s = 0; k < rank; k++ ) {
1090: mulq(mati[rind[k]],nm->body[k][j],&t);
1091: addq(s,t,&u); s = u;
1092: }
1093: mulq(dn,mati[cind[j]],&t);
1094: if ( cmpq(s,t) )
1095: break;
1096: }
1097: #endif
1098: if ( j != clen )
1099: break;
1100: }
1101: if ( i != row )
1102: return 0;
1103: else
1104: return 1;
1105: }
1106:
1107: /* assuming 0 < c < m */
1108:
1109: int inttorat(c,m,b,sgnp,nmp,dnp)
1110: N c,m,b;
1111: int *sgnp;
1112: N *nmp,*dnp;
1113: {
1114: Q qq,t,u1,v1,r1,nm;
1115: N q,r,u2,v2,r2;
1116:
1117: u1 = 0; v1 = ONE; u2 = m; v2 = c;
1118: while ( cmpn(v2,b) >= 0 ) {
1119: divn(u2,v2,&q,&r2); u2 = v2; v2 = r2;
1120: NTOQ(q,1,qq); mulq(qq,v1,&t); subq(u1,t,&r1); u1 = v1; v1 = r1;
1121: }
1122: if ( cmpn(NM(v1),b) >= 0 )
1123: return 0;
1124: else {
1125: *nmp = v2;
1126: *dnp = NM(v1);
1127: *sgnp = SGN(v1);
1128: return 1;
1129: }
1130: }
1131:
1132: /* mat->body = N ** */
1133:
1134: int intmtoratm(mat,md,nm,dn)
1135: MAT mat;
1136: N md;
1137: MAT nm;
1138: Q *dn;
1139: {
1140: N t,s,b;
1141: Q bound,dn0,dn1,nm1,q,tq;
1142: int i,j,k,l,row,col;
1143: Q **rmat;
1144: N **tmat;
1145: N *tmi;
1146: Q *nmk;
1147: N u,unm,udn;
1148: int sgn,ret;
1149:
1.3 noro 1150: if ( UNIN(md) )
1151: return 0;
1.1 noro 1152: row = mat->row; col = mat->col;
1153: bshiftn(md,1,&t);
1154: isqrt(t,&s);
1155: bshiftn(s,64,&b);
1156: if ( !b )
1157: b = ONEN;
1158: dn0 = ONE;
1159: tmat = (N **)mat->body;
1160: rmat = (Q **)nm->body;
1161: for ( i = 0; i < row; i++ )
1162: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1163: if ( tmi[j] ) {
1164: muln(tmi[j],NM(dn0),&s);
1165: remn(s,md,&u);
1166: ret = inttorat(u,md,b,&sgn,&unm,&udn);
1167: if ( !ret )
1168: return 0;
1169: else {
1170: NTOQ(unm,sgn,nm1);
1171: NTOQ(udn,1,dn1);
1172: if ( !UNIQ(dn1) ) {
1173: for ( k = 0; k < i; k++ )
1174: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1175: mulq(nmk[l],dn1,&q); nmk[l] = q;
1176: }
1177: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1178: mulq(nmk[l],dn1,&q); nmk[l] = q;
1179: }
1180: }
1181: rmat[i][j] = nm1;
1182: mulq(dn0,dn1,&q); dn0 = q;
1183: }
1184: }
1185: *dn = dn0;
1186: return 1;
1187: }
1188:
1.3 noro 1189: /* mat->body = Q ** */
1190:
1191: int intmtoratm_q(mat,md,nm,dn)
1192: MAT mat;
1193: N md;
1194: MAT nm;
1195: Q *dn;
1196: {
1197: N t,s,b;
1198: Q bound,dn0,dn1,nm1,q,tq;
1199: int i,j,k,l,row,col;
1200: Q **rmat;
1201: Q **tmat;
1202: Q *tmi;
1203: Q *nmk;
1204: N u,unm,udn;
1205: int sgn,ret;
1206:
1207: if ( UNIN(md) )
1208: return 0;
1209: row = mat->row; col = mat->col;
1210: bshiftn(md,1,&t);
1211: isqrt(t,&s);
1212: bshiftn(s,64,&b);
1213: if ( !b )
1214: b = ONEN;
1215: dn0 = ONE;
1216: tmat = (Q **)mat->body;
1217: rmat = (Q **)nm->body;
1218: for ( i = 0; i < row; i++ )
1219: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1220: if ( tmi[j] ) {
1221: muln(NM(tmi[j]),NM(dn0),&s);
1222: remn(s,md,&u);
1223: ret = inttorat(u,md,b,&sgn,&unm,&udn);
1224: if ( !ret )
1225: return 0;
1226: else {
1227: if ( SGN(tmi[j])<0 )
1228: sgn = -sgn;
1229: NTOQ(unm,sgn,nm1);
1230: NTOQ(udn,1,dn1);
1231: if ( !UNIQ(dn1) ) {
1232: for ( k = 0; k < i; k++ )
1233: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1234: mulq(nmk[l],dn1,&q); nmk[l] = q;
1235: }
1236: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1237: mulq(nmk[l],dn1,&q); nmk[l] = q;
1238: }
1239: }
1240: rmat[i][j] = nm1;
1241: mulq(dn0,dn1,&q); dn0 = q;
1242: }
1243: }
1244: *dn = dn0;
1245: return 1;
1246: }
1247:
1.4 noro 1248: #define ONE_STEP1 if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1249:
1250: void reduce_reducers_mod(mat,row,col,md)
1251: int **mat;
1252: int row,col;
1253: int md;
1254: {
1255: int i,j,k,l,hc,zzz;
1256: int *t,*s,*tj,*ind;
1257:
1258: /* reduce the reducers */
1259: ind = (int *)ALLOCA(row*sizeof(int));
1260: for ( i = 0; i < row; i++ ) {
1261: t = mat[i];
1262: for ( j = 0; j < col && !t[j]; j++ );
1263: /* register the position of the head term */
1264: ind[i] = j;
1265: for ( l = i-1; l >= 0; l-- ) {
1266: /* reduce mat[i] by mat[l] */
1267: if ( hc = t[ind[l]] ) {
1268: /* mat[i] = mat[i]-hc*mat[l] */
1269: j = ind[l];
1270: s = mat[l]+j;
1271: tj = t+j;
1272: hc = md-hc;
1273: k = col-j;
1274: for ( ; k >= 64; k -= 64 ) {
1275: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1276: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1277: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1278: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1279: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1280: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1281: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1282: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1283: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1284: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1285: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1286: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1287: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1288: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1289: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1290: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1291: }
1292: for ( ; k >= 0; k-- ) {
1293: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1294: }
1295: }
1296: }
1297: }
1298: }
1299:
1300: /*
1301: mat[i] : reducers (i=0,...,nred-1)
1302: spolys (i=nred,...,row-1)
1303: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1304: 1. reduce the reducers
1305: 2. reduce spolys by the reduced reducers
1306: */
1307:
1308: void pre_reduce_mod(mat,row,col,nred,md)
1309: int **mat;
1310: int row,col,nred;
1311: int md;
1312: {
1313: int i,j,k,l,hc,inv;
1314: int *t,*s,*tk,*ind;
1315:
1316: #if 1
1317: /* reduce the reducers */
1318: ind = (int *)ALLOCA(row*sizeof(int));
1319: for ( i = 0; i < nred; i++ ) {
1320: /* make mat[i] monic and mat[i] by mat[0],...,mat[i-1] */
1321: t = mat[i];
1322: for ( j = 0; j < col && !t[j]; j++ );
1323: /* register the position of the head term */
1324: ind[i] = j;
1325: inv = invm(t[j],md);
1326: for ( k = j; k < col; k++ )
1327: if ( t[k] )
1328: DMAR(t[k],inv,0,md,t[k])
1329: for ( l = i-1; l >= 0; l-- ) {
1330: /* reduce mat[i] by mat[l] */
1331: if ( hc = t[ind[l]] ) {
1332: /* mat[i] = mat[i]-hc*mat[l] */
1333: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
1334: k < col; k++, tk++, s++ )
1335: if ( *s )
1336: DMAR(*s,hc,*tk,md,*tk)
1337: }
1338: }
1339: }
1340: /* reduce the spolys */
1341: for ( i = nred; i < row; i++ ) {
1342: t = mat[i];
1343: for ( l = nred-1; l >= 0; l-- ) {
1344: /* reduce mat[i] by mat[l] */
1345: if ( hc = t[ind[l]] ) {
1346: /* mat[i] = mat[i]-hc*mat[l] */
1347: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
1348: k < col; k++, tk++, s++ )
1349: if ( *s )
1350: DMAR(*s,hc,*tk,md,*tk)
1351: }
1352: }
1353: }
1354: #endif
1355: }
1356: /*
1357: mat[i] : reducers (i=0,...,nred-1)
1358: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1359: */
1360:
1361: void reduce_sp_by_red_mod(sp,redmat,ind,nred,col,md)
1362: int *sp,**redmat;
1363: int *ind;
1364: int nred,col;
1365: int md;
1366: {
1367: int i,j,k,hc,zzz;
1368: int *t,*s,*tj;
1369:
1370: /* reduce the spolys by redmat */
1371: for ( i = nred-1; i >= 0; i-- ) {
1372: /* reduce sp by redmat[i] */
1373: if ( hc = sp[ind[i]] ) {
1374: /* sp = sp-hc*redmat[i] */
1375: j = ind[i];
1376: hc = md-hc;
1377: s = redmat[i]+j;
1378: tj = sp+j;
1379: for ( k = col-j; k >= 0; k-- ) {
1380: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1381: }
1382: }
1383: }
1384: }
1385:
1386: #define ONE_STEP2 if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
1387:
1.1 noro 1388: int generic_gauss_elim_mod(mat,row,col,md,colstat)
1389: int **mat;
1390: int row,col,md;
1391: int *colstat;
1392: {
1.4 noro 1393: int i,j,k,l,inv,a,rank,zzz;
1394: int *t,*pivot,*pk,*tk;
1.1 noro 1395:
1396: for ( rank = 0, j = 0; j < col; j++ ) {
1397: for ( i = rank; i < row && !mat[i][j]; i++ );
1398: if ( i == row ) {
1399: colstat[j] = 0;
1400: continue;
1401: } else
1402: colstat[j] = 1;
1403: if ( i != rank ) {
1404: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
1405: }
1406: pivot = mat[rank];
1407: inv = invm(pivot[j],md);
1.4 noro 1408: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
1409: if ( *pk ) {
1410: DMAR(*pk,inv,0,md,*pk)
1.1 noro 1411: }
1412: for ( i = rank+1; i < row; i++ ) {
1413: t = mat[i];
1.4 noro 1414: if ( a = t[j] ) {
1415: a = md - a; pk = pivot+j; tk = t+j;
1416: k = col-j;
1417: for ( ; k >= 64; k -= 64 ) {
1418: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1419: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1420: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1421: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1422: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1423: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1424: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1425: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1426: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1427: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1428: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1429: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1430: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1431: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1432: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1433: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1434: }
1435: for ( ; k >= 0; k -- ) {
1436: if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
1437: }
1438: }
1.1 noro 1439: }
1440: rank++;
1441: }
1442: for ( j = col-1, l = rank-1; j >= 0; j-- )
1443: if ( colstat[j] ) {
1444: pivot = mat[l];
1445: for ( i = 0; i < l; i++ ) {
1446: t = mat[i];
1.4 noro 1447: if ( a = t[j] ) {
1448: a = md-a; pk = pivot+j; tk = t+j;
1449: k = col-j;
1450: for ( ; k >= 64; k -= 64 ) {
1451: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1452: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1453: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1454: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1455: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1456: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1457: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1458: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1459: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1460: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1461: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1462: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1463: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1464: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1465: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1466: ONE_STEP2 ONE_STEP2 ONE_STEP2 ONE_STEP2
1467: }
1468: for ( ; k >= 0; k -- ) {
1469: if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
1470: }
1471: }
1.1 noro 1472: }
1473: l--;
1474: }
1475: return rank;
1476: }
1477:
1478: /* LU decomposition; a[i][i] = 1/U[i][i] */
1479:
1480: int lu_gfmmat(mat,md,perm)
1481: GFMMAT mat;
1482: unsigned int md;
1483: int *perm;
1484: {
1485: int row,col;
1486: int i,j,k,l;
1487: unsigned int *t,*pivot;
1488: unsigned int **a;
1489: unsigned int inv,m;
1490:
1491: row = mat->row; col = mat->col;
1492: a = mat->body;
1493: bzero(perm,row*sizeof(int));
1494:
1495: for ( i = 0; i < row; i++ )
1496: perm[i] = i;
1497: for ( k = 0; k < col; k++ ) {
1498: for ( i = k; i < row && !a[i][k]; i++ );
1499: if ( i == row )
1500: return 0;
1501: if ( i != k ) {
1502: j = perm[i]; perm[i] = perm[k]; perm[k] = j;
1503: t = a[i]; a[i] = a[k]; a[k] = t;
1504: }
1505: pivot = a[k];
1506: pivot[k] = inv = invm(pivot[k],md);
1507: for ( i = k+1; i < row; i++ ) {
1508: t = a[i];
1509: if ( m = t[k] ) {
1510: DMAR(inv,m,0,md,t[k])
1511: for ( j = k+1, m = md - t[k]; j < col; j++ )
1512: if ( pivot[j] ) {
1.8 noro 1513: unsigned int tj;
1514:
1515: DMAR(m,pivot[j],t[j],md,tj)
1516: t[j] = tj;
1.1 noro 1517: }
1518: }
1519: }
1520: }
1521: return 1;
1522: }
1523:
1.3 noro 1524: /*
1525: Input
1526: a: a row x col matrix
1527: md : a modulus
1528:
1529: Output:
1530: return : d = the rank of mat
1531: a[0..(d-1)][0..(d-1)] : LU decomposition (a[i][i] = 1/U[i][i])
1532: rinfo: array of length row
1533: cinfo: array of length col
1534: i-th row in new a <-> rinfo[i]-th row in old a
1535: cinfo[j]=1 <=> j-th column is contained in the LU decomp.
1536: */
1537:
1538: int find_lhs_and_lu_mod(a,row,col,md,rinfo,cinfo)
1539: unsigned int **a;
1540: unsigned int md;
1541: int **rinfo,**cinfo;
1542: {
1543: int i,j,k,l,d;
1544: int *rp,*cp;
1545: unsigned int *t,*pivot;
1546: unsigned int inv,m;
1547:
1548: *rinfo = rp = (int *)MALLOC_ATOMIC(row*sizeof(int));
1549: *cinfo = cp = (int *)MALLOC_ATOMIC(col*sizeof(int));
1550: for ( i = 0; i < row; i++ )
1551: rp[i] = i;
1552: for ( k = 0, d = 0; k < col; k++ ) {
1553: for ( i = d; i < row && !a[i][k]; i++ );
1554: if ( i == row ) {
1555: cp[k] = 0;
1556: continue;
1557: } else
1558: cp[k] = 1;
1559: if ( i != d ) {
1560: j = rp[i]; rp[i] = rp[d]; rp[d] = j;
1561: t = a[i]; a[i] = a[d]; a[d] = t;
1562: }
1563: pivot = a[d];
1564: pivot[k] = inv = invm(pivot[k],md);
1565: for ( i = d+1; i < row; i++ ) {
1566: t = a[i];
1567: if ( m = t[k] ) {
1568: DMAR(inv,m,0,md,t[k])
1569: for ( j = k+1, m = md - t[k]; j < col; j++ )
1570: if ( pivot[j] ) {
1.8 noro 1571: unsigned int tj;
1572: DMAR(m,pivot[j],t[j],md,tj)
1573: t[j] = tj;
1.3 noro 1574: }
1575: }
1576: }
1577: d++;
1578: }
1579: return d;
1580: }
1581:
1582: /*
1583: Input
1584: a : n x n matrix; a result of LU-decomposition
1585: md : modulus
1586: b : n x l matrix
1587: Output
1588: b = a^(-1)b
1589: */
1590:
1591: void solve_by_lu_mod(a,n,md,b,l)
1592: int **a;
1593: int n;
1594: int md;
1595: int **b;
1596: int l;
1597: {
1598: unsigned int *y,*c;
1599: int i,j,k;
1600: unsigned int t,m,m2;
1601:
1602: y = (int *)MALLOC_ATOMIC(n*sizeof(int));
1603: c = (int *)MALLOC_ATOMIC(n*sizeof(int));
1604: m2 = md>>1;
1605: for ( k = 0; k < l; k++ ) {
1606: /* copy b[.][k] to c */
1607: for ( i = 0; i < n; i++ )
1608: c[i] = (unsigned int)b[i][k];
1609: /* solve Ly=c */
1610: for ( i = 0; i < n; i++ ) {
1611: for ( t = c[i], j = 0; j < i; j++ )
1612: if ( a[i][j] ) {
1613: m = md - a[i][j];
1614: DMAR(m,y[j],t,md,t)
1615: }
1616: y[i] = t;
1617: }
1618: /* solve Uc=y */
1619: for ( i = n-1; i >= 0; i-- ) {
1620: for ( t = y[i], j =i+1; j < n; j++ )
1621: if ( a[i][j] ) {
1622: m = md - a[i][j];
1623: DMAR(m,c[j],t,md,t)
1624: }
1625: /* a[i][i] = 1/U[i][i] */
1626: DMAR(t,a[i][i],0,md,c[i])
1627: }
1628: /* copy c to b[.][k] with normalization */
1629: for ( i = 0; i < n; i++ )
1630: b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]);
1631: }
1632: }
1633:
1.1 noro 1634: void Pleqm1(arg,rp)
1635: NODE arg;
1636: VECT *rp;
1637: {
1638: MAT m;
1639: VECT vect;
1640: pointer **mat;
1641: Q *v;
1642: Q q;
1643: int **wmat;
1644: int md,i,j,row,col,t,n,status;
1645:
1646: asir_assert(ARG0(arg),O_MAT,"leqm1");
1647: asir_assert(ARG1(arg),O_N,"leqm1");
1648: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
1649: row = m->row; col = m->col; mat = m->body;
1650: wmat = (int **)almat(row,col);
1651: for ( i = 0; i < row; i++ )
1652: for ( j = 0; j < col; j++ )
1653: if ( q = (Q)mat[i][j] ) {
1654: t = rem(NM(q),md);
1655: if ( SGN(q) < 0 )
1656: t = (md - t) % md;
1657: wmat[i][j] = t;
1658: } else
1659: wmat[i][j] = 0;
1660: status = gauss_elim_mod1(wmat,row,col,md);
1661: if ( status < 0 )
1662: *rp = 0;
1663: else if ( status > 0 )
1664: *rp = (VECT)ONE;
1665: else {
1666: n = col - 1;
1667: MKVECT(vect,n);
1668: for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
1669: t = (md-wmat[i][n])%md; STOQ(t,v[i]);
1670: }
1671: *rp = vect;
1672: }
1673: }
1674:
1675: gauss_elim_mod1(mat,row,col,md)
1676: int **mat;
1677: int row,col,md;
1678: {
1679: int i,j,k,inv,a,n;
1680: int *t,*pivot;
1681:
1682: n = col - 1;
1683: for ( j = 0; j < n; j++ ) {
1684: for ( i = j; i < row && !mat[i][j]; i++ );
1685: if ( i == row )
1686: return 1;
1687: if ( i != j ) {
1688: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
1689: }
1690: pivot = mat[j];
1691: inv = invm(pivot[j],md);
1692: for ( k = j; k <= n; k++ )
1693: pivot[k] = dmar(pivot[k],inv,0,md);
1694: for ( i = j+1; i < row; i++ ) {
1695: t = mat[i];
1696: if ( i != j && (a = t[j]) )
1697: for ( k = j, a = md - a; k <= n; k++ )
1698: t[k] = dmar(pivot[k],a,t[k],md);
1699: }
1700: }
1701: for ( i = n; i < row && !mat[i][n]; i++ );
1702: if ( i == row ) {
1703: for ( j = n-1; j >= 0; j-- ) {
1704: for ( i = j-1, a = (md-mat[j][n])%md; i >= 0; i-- ) {
1705: mat[i][n] = dmar(mat[i][j],a,mat[i][n],md);
1706: mat[i][j] = 0;
1707: }
1708: }
1709: return 0;
1710: } else
1711: return -1;
1712: }
1713:
1714: void Pgeninvm(arg,rp)
1715: NODE arg;
1716: LIST *rp;
1717: {
1718: MAT m;
1719: pointer **mat;
1720: Q **tmat;
1721: Q q;
1722: unsigned int **wmat;
1723: int md,i,j,row,col,t,status;
1724: MAT mat1,mat2;
1725: NODE node1,node2;
1726:
1727: asir_assert(ARG0(arg),O_MAT,"leqm1");
1728: asir_assert(ARG1(arg),O_N,"leqm1");
1729: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
1730: row = m->row; col = m->col; mat = m->body;
1731: wmat = (unsigned int **)almat(row,col+row);
1732: for ( i = 0; i < row; i++ ) {
1733: bzero((char *)wmat[i],(col+row)*sizeof(int));
1734: for ( j = 0; j < col; j++ )
1735: if ( q = (Q)mat[i][j] ) {
1736: t = rem(NM(q),md);
1737: if ( SGN(q) < 0 )
1738: t = (md - t) % md;
1739: wmat[i][j] = t;
1740: }
1741: wmat[i][col+i] = 1;
1742: }
1743: status = gauss_elim_geninv_mod(wmat,row,col,md);
1744: if ( status > 0 )
1745: *rp = 0;
1746: else {
1747: MKMAT(mat1,col,row); MKMAT(mat2,row-col,row);
1748: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
1749: for ( j = 0; j < row; j++ )
1750: STOQ(wmat[i][j+col],tmat[i][j]);
1751: for ( tmat = (Q **)mat2->body; i < row; i++ )
1752: for ( j = 0; j < row; j++ )
1753: STOQ(wmat[i][j+col],tmat[i-col][j]);
1754: MKNODE(node2,mat2,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
1755: }
1756: }
1757:
1758: int gauss_elim_geninv_mod(mat,row,col,md)
1759: unsigned int **mat;
1760: int row,col,md;
1761: {
1762: int i,j,k,inv,a,n,m;
1763: unsigned int *t,*pivot;
1764:
1765: n = col; m = row+col;
1766: for ( j = 0; j < n; j++ ) {
1767: for ( i = j; i < row && !mat[i][j]; i++ );
1768: if ( i == row )
1769: return 1;
1770: if ( i != j ) {
1771: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
1772: }
1773: pivot = mat[j];
1774: inv = invm(pivot[j],md);
1775: for ( k = j; k < m; k++ )
1776: pivot[k] = dmar(pivot[k],inv,0,md);
1777: for ( i = j+1; i < row; i++ ) {
1778: t = mat[i];
1779: if ( a = t[j] )
1780: for ( k = j, a = md - a; k < m; k++ )
1781: t[k] = dmar(pivot[k],a,t[k],md);
1782: }
1783: }
1784: for ( j = n-1; j >= 0; j-- ) {
1785: pivot = mat[j];
1786: for ( i = j-1; i >= 0; i-- ) {
1787: t = mat[i];
1788: if ( a = t[j] )
1789: for ( k = j, a = md - a; k < m; k++ )
1790: t[k] = dmar(pivot[k],a,t[k],md);
1791: }
1792: }
1793: return 0;
1794: }
1795:
1796: void Psolve_by_lu_gfmmat(arg,rp)
1797: NODE arg;
1798: VECT *rp;
1799: {
1800: GFMMAT lu;
1801: Q *perm,*rhs,*v;
1802: int n,i;
1803: unsigned int md;
1804: unsigned int *b,*sol;
1805: VECT r;
1806:
1807: lu = (GFMMAT)ARG0(arg);
1808: perm = (Q *)BDY((VECT)ARG1(arg));
1809: rhs = (Q *)BDY((VECT)ARG2(arg));
1810: md = (unsigned int)QTOS((Q)ARG3(arg));
1811: n = lu->col;
1812: b = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
1813: sol = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
1814: for ( i = 0; i < n; i++ )
1815: b[i] = QTOS(rhs[QTOS(perm[i])]);
1816: solve_by_lu_gfmmat(lu,md,b,sol);
1817: MKVECT(r,n);
1818: for ( i = 0, v = (Q *)r->body; i < n; i++ )
1819: STOQ(sol[i],v[i]);
1820: *rp = r;
1821: }
1822:
1823: void solve_by_lu_gfmmat(lu,md,b,x)
1824: GFMMAT lu;
1825: unsigned int md;
1826: unsigned int *b;
1827: unsigned int *x;
1828: {
1829: int n;
1830: unsigned int **a;
1831: unsigned int *y;
1832: int i,j;
1833: unsigned int t,m;
1834:
1835: n = lu->col;
1836: a = lu->body;
1837: y = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
1838: /* solve Ly=b */
1839: for ( i = 0; i < n; i++ ) {
1840: for ( t = b[i], j = 0; j < i; j++ )
1841: if ( a[i][j] ) {
1842: m = md - a[i][j];
1843: DMAR(m,y[j],t,md,t)
1844: }
1845: y[i] = t;
1846: }
1847: /* solve Ux=y */
1848: for ( i = n-1; i >= 0; i-- ) {
1849: for ( t = y[i], j =i+1; j < n; j++ )
1850: if ( a[i][j] ) {
1851: m = md - a[i][j];
1852: DMAR(m,x[j],t,md,t)
1853: }
1854: /* a[i][i] = 1/U[i][i] */
1855: DMAR(t,a[i][i],0,md,x[i])
1856: }
1857: }
1858:
1859: void Plu_gfmmat(arg,rp)
1860: NODE arg;
1861: LIST *rp;
1862: {
1863: MAT m;
1864: GFMMAT mm;
1865: unsigned int md;
1866: int i,row,col,status;
1867: int *iperm;
1868: Q *v;
1869: VECT perm;
1870: NODE n0;
1871:
1872: asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
1873: asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
1874: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
1875: mat_to_gfmmat(m,md,&mm);
1876: row = m->row;
1877: col = m->col;
1878: iperm = (int *)MALLOC_ATOMIC(row*sizeof(int));
1879: status = lu_gfmmat(mm,md,iperm);
1880: if ( !status )
1881: n0 = 0;
1882: else {
1883: MKVECT(perm,row);
1884: for ( i = 0, v = (Q *)perm->body; i < row; i++ )
1885: STOQ(iperm[i],v[i]);
1886: n0 = mknode(2,mm,perm);
1887: }
1888: MKLIST(*rp,n0);
1889: }
1890:
1891: void Pmat_to_gfmmat(arg,rp)
1892: NODE arg;
1893: GFMMAT *rp;
1894: {
1895: MAT m;
1896: unsigned int md;
1897:
1898: asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
1899: asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
1900: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
1901: mat_to_gfmmat(m,md,rp);
1902: }
1903:
1904: void mat_to_gfmmat(m,md,rp)
1905: MAT m;
1906: unsigned int md;
1907: GFMMAT *rp;
1908: {
1909: unsigned int **wmat;
1910: unsigned int t;
1911: Q **mat;
1912: Q q;
1913: int i,j,row,col;
1914:
1915: row = m->row; col = m->col; mat = (Q **)m->body;
1916: wmat = (unsigned int **)almat(row,col);
1917: for ( i = 0; i < row; i++ ) {
1918: bzero((char *)wmat[i],col*sizeof(unsigned int));
1919: for ( j = 0; j < col; j++ )
1920: if ( q = mat[i][j] ) {
1921: t = (unsigned int)rem(NM(q),md);
1922: if ( SGN(q) < 0 )
1923: t = (md - t) % md;
1924: wmat[i][j] = t;
1925: }
1926: }
1927: TOGFMMAT(row,col,wmat,*rp);
1928: }
1929:
1930: void Pgeninvm_swap(arg,rp)
1931: NODE arg;
1932: LIST *rp;
1933: {
1934: MAT m;
1935: pointer **mat;
1936: Q **tmat;
1937: Q *tvect;
1938: Q q;
1939: unsigned int **wmat,**invmat;
1940: int *index;
1941: unsigned int t,md;
1942: int i,j,row,col,status;
1943: MAT mat1;
1944: VECT vect1;
1945: NODE node1,node2;
1946:
1947: asir_assert(ARG0(arg),O_MAT,"geninvm_swap");
1948: asir_assert(ARG1(arg),O_N,"geninvm_swap");
1949: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
1950: row = m->row; col = m->col; mat = m->body;
1951: wmat = (unsigned int **)almat(row,col+row);
1952: for ( i = 0; i < row; i++ ) {
1953: bzero((char *)wmat[i],(col+row)*sizeof(int));
1954: for ( j = 0; j < col; j++ )
1955: if ( q = (Q)mat[i][j] ) {
1956: t = (unsigned int)rem(NM(q),md);
1957: if ( SGN(q) < 0 )
1958: t = (md - t) % md;
1959: wmat[i][j] = t;
1960: }
1961: wmat[i][col+i] = 1;
1962: }
1963: status = gauss_elim_geninv_mod_swap(wmat,row,col,md,&invmat,&index);
1964: if ( status > 0 )
1965: *rp = 0;
1966: else {
1967: MKMAT(mat1,col,col);
1968: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
1969: for ( j = 0; j < col; j++ )
1970: UTOQ(invmat[i][j],tmat[i][j]);
1971: MKVECT(vect1,row);
1972: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
1973: STOQ(index[i],tvect[i]);
1974: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
1975: }
1976: }
1977:
1978: gauss_elim_geninv_mod_swap(mat,row,col,md,invmatp,indexp)
1979: unsigned int **mat;
1980: int row,col;
1981: unsigned int md;
1982: unsigned int ***invmatp;
1983: int **indexp;
1984: {
1985: int i,j,k,inv,a,n,m;
1986: unsigned int *t,*pivot,*s;
1987: int *index;
1988: unsigned int **invmat;
1989:
1990: n = col; m = row+col;
1991: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
1992: for ( i = 0; i < row; i++ )
1993: index[i] = i;
1994: for ( j = 0; j < n; j++ ) {
1995: for ( i = j; i < row && !mat[i][j]; i++ );
1996: if ( i == row ) {
1997: *indexp = 0; *invmatp = 0; return 1;
1998: }
1999: if ( i != j ) {
2000: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2001: k = index[i]; index[i] = index[j]; index[j] = k;
2002: }
2003: pivot = mat[j];
2004: inv = (unsigned int)invm(pivot[j],md);
2005: for ( k = j; k < m; k++ )
2006: if ( pivot[k] )
2007: pivot[k] = (unsigned int)dmar(pivot[k],inv,0,md);
2008: for ( i = j+1; i < row; i++ ) {
2009: t = mat[i];
2010: if ( a = t[j] )
2011: for ( k = j, a = md - a; k < m; k++ )
2012: if ( pivot[k] )
2013: t[k] = dmar(pivot[k],a,t[k],md);
2014: }
2015: }
2016: for ( j = n-1; j >= 0; j-- ) {
2017: pivot = mat[j];
2018: for ( i = j-1; i >= 0; i-- ) {
2019: t = mat[i];
2020: if ( a = t[j] )
2021: for ( k = j, a = md - a; k < m; k++ )
2022: if ( pivot[k] )
2023: t[k] = dmar(pivot[k],a,t[k],md);
2024: }
2025: }
2026: *invmatp = invmat = (unsigned int **)almat(col,col);
2027: for ( i = 0; i < col; i++ )
2028: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
2029: s[j] = t[col+index[j]];
2030: return 0;
2031: }
2032:
2033: void _addn(N,N,N);
2034: int _subn(N,N,N);
2035: void _muln(N,N,N);
2036:
2037: void inner_product_int(a,b,n,r)
2038: Q *a,*b;
2039: int n;
2040: Q *r;
2041: {
2042: int la,lb,i;
2043: int sgn,sgn1;
2044: N wm,wma,sum,t;
2045:
2046: for ( la = lb = 0, i = 0; i < n; i++ ) {
2047: if ( a[i] )
2048: if ( DN(a[i]) )
2049: error("inner_product_int : invalid argument");
2050: else
2051: la = MAX(PL(NM(a[i])),la);
2052: if ( b[i] )
2053: if ( DN(b[i]) )
2054: error("inner_product_int : invalid argument");
2055: else
2056: lb = MAX(PL(NM(b[i])),lb);
2057: }
2058: sgn = 0;
2059: sum= NALLOC(la+lb+2);
2060: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
2061: wm = NALLOC(la+lb+2);
2062: wma = NALLOC(la+lb+2);
2063: for ( i = 0; i < n; i++ ) {
2064: if ( !a[i] || !b[i] )
2065: continue;
2066: _muln(NM(a[i]),NM(b[i]),wm);
2067: sgn1 = SGN(a[i])*SGN(b[i]);
2068: if ( !sgn ) {
2069: sgn = sgn1;
2070: t = wm; wm = sum; sum = t;
2071: } else if ( sgn == sgn1 ) {
2072: _addn(sum,wm,wma);
2073: if ( !PL(wma) )
2074: sgn = 0;
2075: t = wma; wma = sum; sum = t;
2076: } else {
2077: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
2078: sgn *= _subn(sum,wm,wma);
2079: t = wma; wma = sum; sum = t;
2080: }
2081: }
2082: GC_free(wm);
2083: GC_free(wma);
2084: if ( !sgn ) {
2085: GC_free(sum);
2086: *r = 0;
2087: } else
2088: NTOQ(sum,sgn,*r);
2089: }
2090:
1.3 noro 2091: /* (k,l) element of a*b where a: .x n matrix, b: n x . integer matrix */
2092:
2093: void inner_product_mat_int_mod(a,b,n,k,l,r)
2094: Q **a;
2095: int **b;
2096: int n,k,l;
2097: Q *r;
2098: {
2099: int la,lb,i;
2100: int sgn,sgn1;
2101: N wm,wma,sum,t;
2102: Q aki;
2103: int bil,bilsgn;
2104: struct oN tn;
2105:
2106: for ( la = 0, i = 0; i < n; i++ ) {
2107: if ( aki = a[k][i] )
2108: if ( DN(aki) )
2109: error("inner_product_int : invalid argument");
2110: else
2111: la = MAX(PL(NM(aki)),la);
2112: }
2113: lb = 1;
2114: sgn = 0;
2115: sum= NALLOC(la+lb+2);
2116: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
2117: wm = NALLOC(la+lb+2);
2118: wma = NALLOC(la+lb+2);
2119: for ( i = 0; i < n; i++ ) {
2120: if ( !(aki = a[k][i]) || !(bil = b[i][l]) )
2121: continue;
2122: tn.p = 1;
2123: if ( bil > 0 ) {
2124: tn.b[0] = bil; bilsgn = 1;
2125: } else {
2126: tn.b[0] = -bil; bilsgn = -1;
2127: }
2128: _muln(NM(aki),&tn,wm);
2129: sgn1 = SGN(aki)*bilsgn;
2130: if ( !sgn ) {
2131: sgn = sgn1;
2132: t = wm; wm = sum; sum = t;
2133: } else if ( sgn == sgn1 ) {
2134: _addn(sum,wm,wma);
2135: if ( !PL(wma) )
2136: sgn = 0;
2137: t = wma; wma = sum; sum = t;
2138: } else {
2139: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
2140: sgn *= _subn(sum,wm,wma);
2141: t = wma; wma = sum; sum = t;
2142: }
2143: }
2144: GC_free(wm);
2145: GC_free(wma);
2146: if ( !sgn ) {
2147: GC_free(sum);
2148: *r = 0;
2149: } else
2150: NTOQ(sum,sgn,*r);
2151: }
2152:
1.1 noro 2153: void Pmul_mat_vect_int(arg,rp)
2154: NODE arg;
2155: VECT *rp;
2156: {
2157: MAT mat;
2158: VECT vect,r;
2159: int row,col,i;
2160:
2161: mat = (MAT)ARG0(arg);
2162: vect = (VECT)ARG1(arg);
2163: row = mat->row;
2164: col = mat->col;
2165: MKVECT(r,row);
2166: for ( i = 0; i < row; i++ )
2167: inner_product_int(mat->body[i],vect->body,col,&r->body[i]);
2168: *rp = r;
2169: }
2170:
2171: void Pnbpoly_up2(arg,rp)
2172: NODE arg;
2173: GF2N *rp;
2174: {
2175: int m,type,ret;
2176: UP2 r;
2177:
2178: m = QTOS((Q)ARG0(arg));
2179: type = QTOS((Q)ARG1(arg));
2180: ret = generate_ONB_polynomial(&r,m,type);
2181: if ( ret == 0 )
2182: MKGF2N(r,*rp);
2183: else
2184: *rp = 0;
2185: }
2186:
2187: void Px962_irredpoly_up2(arg,rp)
2188: NODE arg;
2189: GF2N *rp;
2190: {
2191: int m,type,ret,w;
2192: GF2N prev;
2193: UP2 r;
2194:
2195: m = QTOS((Q)ARG0(arg));
2196: prev = (GF2N)ARG1(arg);
2197: if ( !prev ) {
2198: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2199: bzero((char *)r->b,w*sizeof(unsigned int));
2200: } else {
2201: r = prev->body;
2202: if ( degup2(r) != m ) {
2203: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2204: bzero((char *)r->b,w*sizeof(unsigned int));
2205: }
2206: }
2207: ret = _generate_irreducible_polynomial(r,m,type);
2208: if ( ret == 0 )
2209: MKGF2N(r,*rp);
2210: else
2211: *rp = 0;
2212: }
2213:
2214: void Pirredpoly_up2(arg,rp)
2215: NODE arg;
2216: GF2N *rp;
2217: {
2218: int m,type,ret,w;
2219: GF2N prev;
2220: UP2 r;
2221:
2222: m = QTOS((Q)ARG0(arg));
2223: prev = (GF2N)ARG1(arg);
2224: if ( !prev ) {
2225: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2226: bzero((char *)r->b,w*sizeof(unsigned int));
2227: } else {
2228: r = prev->body;
2229: if ( degup2(r) != m ) {
2230: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2231: bzero((char *)r->b,w*sizeof(unsigned int));
2232: }
2233: }
2234: ret = _generate_good_irreducible_polynomial(r,m,type);
2235: if ( ret == 0 )
2236: MKGF2N(r,*rp);
2237: else
2238: *rp = 0;
2239: }
2240:
2241: /*
2242: * f = type 'type' normal polynomial of degree m if exists
2243: * IEEE P1363 A.7.2
2244: *
2245: * return value : 0 --- exists
2246: * 1 --- does not exist
2247: * -1 --- failure (memory allocation error)
2248: */
2249:
2250: int generate_ONB_polynomial(UP2 *rp,int m,int type)
2251: {
2252: int i,r;
2253: int w;
2254: UP2 f,f0,f1,f2,t;
2255:
2256: w = (m>>5)+1;
2257: switch ( type ) {
2258: case 1:
2259: if ( !TypeT_NB_check(m,1) ) return 1;
2260: NEWUP2(f,w); *rp = f; f->w = w;
2261: /* set all the bits */
2262: for ( i = 0; i < w; i++ )
2263: f->b[i] = 0xffffffff;
2264: /* mask the top word if necessary */
2265: if ( r = (m+1)&31 )
2266: f->b[w-1] &= (1<<r)-1;
2267: return 0;
2268: break;
2269: case 2:
2270: if ( !TypeT_NB_check(m,2) ) return 1;
2271: NEWUP2(f,w); *rp = f;
2272: W_NEWUP2(f0,w);
2273: W_NEWUP2(f1,w);
2274: W_NEWUP2(f2,w);
2275:
2276: /* recursion for genrating Type II normal polynomial */
2277:
2278: /* f0 = 1, f1 = t+1 */
2279: f0->w = 1; f0->b[0] = 1;
2280: f1->w = 1; f1->b[0] = 3;
2281: for ( i = 2; i <= m; i++ ) {
2282: /* f2 = t*f1+f0 */
2283: _bshiftup2(f1,-1,f2);
2284: _addup2_destructive(f2,f0);
2285: /* cyclic change of the variables */
2286: t = f0; f0 = f1; f1 = f2; f2 = t;
2287: }
2288: _copyup2(f1,f);
2289: return 0;
2290: break;
2291: default:
2292: return -1;
2293: break;
2294: }
2295: }
2296:
2297: /*
2298: * f = an irreducible trinomial or pentanomial of degree d 'after' f
2299: * return value : 0 --- exists
2300: * 1 --- does not exist (exhaustion)
2301: */
2302:
2303: int _generate_irreducible_polynomial(UP2 f,int d)
2304: {
2305: int ret,i,j,k,nz,i0,j0,k0;
2306: int w;
2307: unsigned int *fd;
2308:
2309: /*
2310: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
2311: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
2312: * otherwise i0,j0,k0 is set to 0.
2313: */
2314:
2315: fd = f->b;
2316: w = (d>>5)+1;
2317: if ( f->w && (d==degup2(f)) ) {
2318: for ( nz = 0, i = d; i >= 0; i-- )
2319: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
2320: switch ( nz ) {
2321: case 3:
2322: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2323: /* reset i0-th bit */
2324: fd[i0>>5] &= ~(1<<(i0&31));
2325: j0 = k0 = 0;
2326: break;
2327: case 5:
2328: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2329: /* reset i0-th bit */
2330: fd[i0>>5] &= ~(1<<(i0&31));
2331: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
2332: /* reset j0-th bit */
2333: fd[j0>>5] &= ~(1<<(j0&31));
2334: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
2335: /* reset k0-th bit */
2336: fd[k0>>5] &= ~(1<<(k0&31));
2337: break;
2338: default:
2339: f->w = 0; break;
2340: }
2341: } else
2342: f->w = 0;
2343:
2344: if ( !f->w ) {
2345: fd = f->b;
2346: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
2347: i0 = j0 = k0 = 0;
2348: }
2349: /* if j0 > 0 then f is already a pentanomial */
2350: if ( j0 > 0 ) goto PENTA;
2351:
2352: /* searching for an irreducible trinomial */
2353:
2354: for ( i = 1; 2*i <= d; i++ ) {
2355: /* skip the polynomials 'before' f */
2356: if ( i < i0 ) continue;
2357: if ( i == i0 ) { i0 = 0; continue; }
2358: /* set i-th bit */
2359: fd[i>>5] |= (1<<(i&31));
2360: ret = irredcheck_dddup2(f);
2361: if ( ret == 1 ) return 0;
2362: /* reset i-th bit */
2363: fd[i>>5] &= ~(1<<(i&31));
2364: }
2365:
2366: /* searching for an irreducible pentanomial */
2367: PENTA:
2368: for ( i = 1; i < d; i++ ) {
2369: /* skip the polynomials 'before' f */
2370: if ( i < i0 ) continue;
2371: if ( i == i0 ) i0 = 0;
2372: /* set i-th bit */
2373: fd[i>>5] |= (1<<(i&31));
2374: for ( j = i+1; j < d; j++ ) {
2375: /* skip the polynomials 'before' f */
2376: if ( j < j0 ) continue;
2377: if ( j == j0 ) j0 = 0;
2378: /* set j-th bit */
2379: fd[j>>5] |= (1<<(j&31));
2380: for ( k = j+1; k < d; k++ ) {
2381: /* skip the polynomials 'before' f */
2382: if ( k < k0 ) continue;
2383: else if ( k == k0 ) { k0 = 0; continue; }
2384: /* set k-th bit */
2385: fd[k>>5] |= (1<<(k&31));
2386: ret = irredcheck_dddup2(f);
2387: if ( ret == 1 ) return 0;
2388: /* reset k-th bit */
2389: fd[k>>5] &= ~(1<<(k&31));
2390: }
2391: /* reset j-th bit */
2392: fd[j>>5] &= ~(1<<(j&31));
2393: }
2394: /* reset i-th bit */
2395: fd[i>>5] &= ~(1<<(i&31));
2396: }
2397: /* exhausted */
2398: return 1;
2399: }
2400:
2401: /*
2402: * f = an irreducible trinomial or pentanomial of degree d 'after' f
2403: *
2404: * searching strategy:
2405: * trinomial x^d+x^i+1:
2406: * i is as small as possible.
2407: * trinomial x^d+x^i+x^j+x^k+1:
2408: * i is as small as possible.
2409: * For such i, j is as small as possible.
2410: * For such i and j, 'k' is as small as possible.
2411: *
2412: * return value : 0 --- exists
2413: * 1 --- does not exist (exhaustion)
2414: */
2415:
2416: int _generate_good_irreducible_polynomial(UP2 f,int d)
2417: {
2418: int ret,i,j,k,nz,i0,j0,k0;
2419: int w;
2420: unsigned int *fd;
2421:
2422: /*
2423: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
2424: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
2425: * otherwise i0,j0,k0 is set to 0.
2426: */
2427:
2428: fd = f->b;
2429: w = (d>>5)+1;
2430: if ( f->w && (d==degup2(f)) ) {
2431: for ( nz = 0, i = d; i >= 0; i-- )
2432: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
2433: switch ( nz ) {
2434: case 3:
2435: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2436: /* reset i0-th bit */
2437: fd[i0>>5] &= ~(1<<(i0&31));
2438: j0 = k0 = 0;
2439: break;
2440: case 5:
2441: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2442: /* reset i0-th bit */
2443: fd[i0>>5] &= ~(1<<(i0&31));
2444: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
2445: /* reset j0-th bit */
2446: fd[j0>>5] &= ~(1<<(j0&31));
2447: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
2448: /* reset k0-th bit */
2449: fd[k0>>5] &= ~(1<<(k0&31));
2450: break;
2451: default:
2452: f->w = 0; break;
2453: }
2454: } else
2455: f->w = 0;
2456:
2457: if ( !f->w ) {
2458: fd = f->b;
2459: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
2460: i0 = j0 = k0 = 0;
2461: }
2462: /* if j0 > 0 then f is already a pentanomial */
2463: if ( j0 > 0 ) goto PENTA;
2464:
2465: /* searching for an irreducible trinomial */
2466:
2467: for ( i = 1; 2*i <= d; i++ ) {
2468: /* skip the polynomials 'before' f */
2469: if ( i < i0 ) continue;
2470: if ( i == i0 ) { i0 = 0; continue; }
2471: /* set i-th bit */
2472: fd[i>>5] |= (1<<(i&31));
2473: ret = irredcheck_dddup2(f);
2474: if ( ret == 1 ) return 0;
2475: /* reset i-th bit */
2476: fd[i>>5] &= ~(1<<(i&31));
2477: }
2478:
2479: /* searching for an irreducible pentanomial */
2480: PENTA:
2481: for ( i = 3; i < d; i++ ) {
2482: /* skip the polynomials 'before' f */
2483: if ( i < i0 ) continue;
2484: if ( i == i0 ) i0 = 0;
2485: /* set i-th bit */
2486: fd[i>>5] |= (1<<(i&31));
2487: for ( j = 2; j < i; j++ ) {
2488: /* skip the polynomials 'before' f */
2489: if ( j < j0 ) continue;
2490: if ( j == j0 ) j0 = 0;
2491: /* set j-th bit */
2492: fd[j>>5] |= (1<<(j&31));
2493: for ( k = 1; k < j; k++ ) {
2494: /* skip the polynomials 'before' f */
2495: if ( k < k0 ) continue;
2496: else if ( k == k0 ) { k0 = 0; continue; }
2497: /* set k-th bit */
2498: fd[k>>5] |= (1<<(k&31));
2499: ret = irredcheck_dddup2(f);
2500: if ( ret == 1 ) return 0;
2501: /* reset k-th bit */
2502: fd[k>>5] &= ~(1<<(k&31));
2503: }
2504: /* reset j-th bit */
2505: fd[j>>5] &= ~(1<<(j&31));
2506: }
2507: /* reset i-th bit */
2508: fd[i>>5] &= ~(1<<(i&31));
2509: }
2510: /* exhausted */
2511: return 1;
1.3 noro 2512: }
2513:
2514: printqmat(mat,row,col)
2515: Q **mat;
2516: int row,col;
2517: {
2518: int i,j;
2519:
2520: for ( i = 0; i < row; i++ ) {
2521: for ( j = 0; j < col; j++ ) {
1.8 noro 2522: printnum((Num)mat[i][j]); printf(" ");
1.3 noro 2523: }
2524: printf("\n");
2525: }
2526: }
2527:
2528: printimat(mat,row,col)
2529: int **mat;
2530: int row,col;
2531: {
2532: int i,j;
2533:
2534: for ( i = 0; i < row; i++ ) {
2535: for ( j = 0; j < col; j++ ) {
2536: printf("%d ",mat[i][j]);
2537: }
2538: printf("\n");
2539: }
1.1 noro 2540: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>