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