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