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