Annotation of OpenXM_contrib2/asir2000/builtin/array.c, Revision 1.44
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.44 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.43 2004/12/18 16:50:10 saito 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.44 ! noro 1181: int *wx,*ptr;
! 1182: int wxsize,nsize;
! 1183: N wn;
! 1184: Q wq;
1.3 noro 1185:
1186: a0 = (Q **)mat->body;
1187: row = mat->row; col = mat->col;
1188: w = (int **)almat(row,col);
1189: for ( ind = 0; ; ind++ ) {
1.12 noro 1190: md = get_lprime(ind);
1.3 noro 1191: STOQ(md,mdq);
1192: for ( i = 0; i < row; i++ )
1193: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1194: if ( q = (Q)ai[j] ) {
1195: t = rem(NM(q),md);
1196: if ( t && SGN(q) < 0 )
1197: t = (md - t) % md;
1198: wi[j] = t;
1199: } else
1200: wi[j] = 0;
1201:
1.27 noro 1202: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1.3 noro 1203: a = (Q **)almat_pointer(rank,rank); /* lhs mat */
1204: MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
1205: for ( j = li = ri = 0; j < col; j++ )
1206: if ( cinfo[j] ) {
1207: /* the column is in lhs */
1208: for ( i = 0; i < rank; i++ ) {
1209: w[i][li] = w[i][j];
1210: a[i][li] = a0[rinfo[i]][j];
1211: }
1212: li++;
1213: } else {
1214: /* the column is in rhs */
1215: for ( i = 0; i < rank; i++ )
1216: b[i][ri] = a0[rinfo[i]][j];
1217: ri++;
1218: }
1219:
1220: /* solve Ax+B=0; A: rank x rank, B: rank x ri */
1221: MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
1222: MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
1223: /* use the right part of w as work area */
1224: /* ri = col - rank */
1225: wc = (int **)almat(rank,ri);
1226: for ( i = 0; i < rank; i++ )
1227: wc[i] = w[i]+rank;
1228: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1229: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
1230:
1231: init_eg(&eg_mul); init_eg(&eg_inv);
1.41 noro 1232: init_eg(&eg_check); init_eg(&eg_intrat);
1.39 noro 1233: period = F4_INTRAT_PERIOD;
1.44 ! noro 1234: nsize = period;
! 1235: wxsize = rank*ri*nsize;
! 1236: wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int));
! 1237: for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
! 1238: for ( q = ONE, count = 0; ; ) {
1.42 noro 1239: if ( DP_Print > 3 )
1.41 noro 1240: fprintf(stderr,"o");
1.3 noro 1241: /* wc = -b mod md */
1.44 ! noro 1242: get_eg(&tmp0);
1.3 noro 1243: for ( i = 0; i < rank; i++ )
1244: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1245: if ( u = (Q)bi[j] ) {
1246: t = rem(NM(u),md);
1247: if ( t && SGN(u) > 0 )
1248: t = (md - t) % md;
1249: wi[j] = t;
1250: } else
1251: wi[j] = 0;
1.44 ! noro 1252: /* wc = A^(-1)wc; wc is not normalized */
! 1253: solve_by_lu_mod(w,rank,md,wc,ri,0);
! 1254: /* wx += q*wc */
! 1255: ptr = wx;
! 1256: for ( i = 0; i < rank; i++ )
! 1257: for ( j = 0, wi = wc[i]; j < ri; j++ ) {
! 1258: if ( wi[j] )
! 1259: muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr);
! 1260: ptr += nsize;
! 1261: }
! 1262: count++;
1.1 noro 1263: get_eg(&tmp1);
1.3 noro 1264: add_eg(&eg_inv,&tmp0,&tmp1);
1265: get_eg(&tmp0);
1266: for ( i = 0; i < rank; i++ )
1267: for ( j = 0; j < ri; j++ ) {
1268: inner_product_mat_int_mod(a,wc,rank,i,j,&u);
1269: addq(b[i][j],u,&s);
1270: if ( s ) {
1271: t = divin(NM(s),md,&tn);
1272: if ( t )
1273: error("generic_gauss_elim_hensel:incosistent");
1274: NTOQ(tn,SGN(s),b[i][j]);
1275: } else
1276: b[i][j] = 0;
1277: }
1278: get_eg(&tmp1);
1279: add_eg(&eg_mul,&tmp0,&tmp1);
1280: /* q = q*md */
1281: mulq(q,mdq,&u); q = u;
1.44 ! noro 1282: if ( count == period ) {
1.41 noro 1283: get_eg(&tmp0);
1.44 ! noro 1284: ptr = wx;
! 1285: for ( i = 0; i < rank; i++ )
! 1286: for ( j = 0, xi = x[i]; j < ri;
! 1287: j++, ptr += nsize ) {
! 1288: for ( k = nsize-1; k >= 0 && !ptr[k]; k-- );
! 1289: if ( k >= 0 ) {
! 1290: wn = NALLOC(k+1);
! 1291: PL(wn) = k+1;
! 1292: for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l];
! 1293: NTOQ(wn,1,wq);
! 1294: subq(xi[j],wq,&u); xi[j] = u;
! 1295: }
! 1296: }
1.41 noro 1297: ret = intmtoratm_q(xmat,NM(q),*nmmat,dn);
1298: get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1);
1299: if ( ret ) {
1.39 noro 1300: for ( j = k = l = 0; j < col; j++ )
1301: if ( cinfo[j] )
1302: rind[k++] = j;
1303: else
1304: cind[l++] = j;
1.41 noro 1305: get_eg(&tmp0);
1306: ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
1307: get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1);
1308: if ( ret ) {
1.42 noro 1309: if ( DP_Print > 3 ) {
1.40 noro 1310: fprintf(stderr,"\n");
1311: print_eg("INV",&eg_inv);
1312: print_eg("MUL",&eg_mul);
1.41 noro 1313: print_eg("INTRAT",&eg_intrat);
1314: print_eg("CHECK",&eg_check);
1.40 noro 1315: fflush(asir_out);
1316: }
1.39 noro 1317: return rank;
1318: }
1.44 ! noro 1319: } else {
! 1320: period = period*3/2;
! 1321: count = 0;
! 1322: nsize += period;
! 1323: wxsize += rank*ri*nsize;
! 1324: wx = (int *)REALLOC(wx,wxsize*sizeof(int));
! 1325: for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
! 1326: }
1.41 noro 1327: }
1.1 noro 1328: }
1329: }
1330: }
1331:
1332: int f4_nocheck;
1333:
1.24 noro 1334: int gensolve_check(MAT mat,MAT nm,Q dn,int *rind,int *cind)
1.1 noro 1335: {
1336: int row,col,rank,clen,i,j,k,l;
1.24 noro 1337: Q s,t;
1.1 noro 1338: Q *w;
1339: Q *mati,*nmk;
1340:
1341: if ( f4_nocheck )
1342: return 1;
1343: row = mat->row; col = mat->col;
1344: rank = nm->row; clen = nm->col;
1345: w = (Q *)MALLOC(clen*sizeof(Q));
1346: for ( i = 0; i < row; i++ ) {
1347: mati = (Q *)mat->body[i];
1348: #if 1
1349: bzero(w,clen*sizeof(Q));
1350: for ( k = 0; k < rank; k++ )
1351: for ( l = 0, nmk = (Q *)nm->body[k]; l < clen; l++ ) {
1352: mulq(mati[rind[k]],nmk[l],&t);
1353: addq(w[l],t,&s); w[l] = s;
1354: }
1355: for ( j = 0; j < clen; j++ ) {
1356: mulq(dn,mati[cind[j]],&t);
1357: if ( cmpq(w[j],t) )
1358: break;
1359: }
1360: #else
1361: for ( j = 0; j < clen; j++ ) {
1362: for ( k = 0, s = 0; k < rank; k++ ) {
1363: mulq(mati[rind[k]],nm->body[k][j],&t);
1364: addq(s,t,&u); s = u;
1365: }
1366: mulq(dn,mati[cind[j]],&t);
1367: if ( cmpq(s,t) )
1368: break;
1369: }
1370: #endif
1371: if ( j != clen )
1372: break;
1373: }
1374: if ( i != row )
1375: return 0;
1376: else
1377: return 1;
1378: }
1379:
1380: /* assuming 0 < c < m */
1381:
1.24 noro 1382: int inttorat(N c,N m,N b,int *sgnp,N *nmp,N *dnp)
1.1 noro 1383: {
1.24 noro 1384: Q qq,t,u1,v1,r1;
1385: N q,u2,v2,r2;
1.1 noro 1386:
1387: u1 = 0; v1 = ONE; u2 = m; v2 = c;
1388: while ( cmpn(v2,b) >= 0 ) {
1389: divn(u2,v2,&q,&r2); u2 = v2; v2 = r2;
1390: NTOQ(q,1,qq); mulq(qq,v1,&t); subq(u1,t,&r1); u1 = v1; v1 = r1;
1391: }
1392: if ( cmpn(NM(v1),b) >= 0 )
1393: return 0;
1394: else {
1395: *nmp = v2;
1396: *dnp = NM(v1);
1397: *sgnp = SGN(v1);
1398: return 1;
1399: }
1400: }
1401:
1402: /* mat->body = N ** */
1403:
1.24 noro 1404: int intmtoratm(MAT mat,N md,MAT nm,Q *dn)
1.1 noro 1405: {
1406: N t,s,b;
1.24 noro 1407: Q dn0,dn1,nm1,q;
1.1 noro 1408: int i,j,k,l,row,col;
1409: Q **rmat;
1410: N **tmat;
1411: N *tmi;
1412: Q *nmk;
1413: N u,unm,udn;
1414: int sgn,ret;
1415:
1.3 noro 1416: if ( UNIN(md) )
1417: return 0;
1.1 noro 1418: row = mat->row; col = mat->col;
1419: bshiftn(md,1,&t);
1420: isqrt(t,&s);
1421: bshiftn(s,64,&b);
1422: if ( !b )
1423: b = ONEN;
1424: dn0 = ONE;
1425: tmat = (N **)mat->body;
1426: rmat = (Q **)nm->body;
1427: for ( i = 0; i < row; i++ )
1428: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1429: if ( tmi[j] ) {
1430: muln(tmi[j],NM(dn0),&s);
1431: remn(s,md,&u);
1432: ret = inttorat(u,md,b,&sgn,&unm,&udn);
1433: if ( !ret )
1434: return 0;
1435: else {
1436: NTOQ(unm,sgn,nm1);
1437: NTOQ(udn,1,dn1);
1438: if ( !UNIQ(dn1) ) {
1439: for ( k = 0; k < i; k++ )
1440: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1441: mulq(nmk[l],dn1,&q); nmk[l] = q;
1442: }
1443: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1444: mulq(nmk[l],dn1,&q); nmk[l] = q;
1445: }
1446: }
1447: rmat[i][j] = nm1;
1448: mulq(dn0,dn1,&q); dn0 = q;
1449: }
1450: }
1451: *dn = dn0;
1452: return 1;
1453: }
1454:
1.3 noro 1455: /* mat->body = Q ** */
1456:
1.24 noro 1457: int intmtoratm_q(MAT mat,N md,MAT nm,Q *dn)
1.3 noro 1458: {
1459: N t,s,b;
1.24 noro 1460: Q dn0,dn1,nm1,q;
1.3 noro 1461: int i,j,k,l,row,col;
1462: Q **rmat;
1463: Q **tmat;
1464: Q *tmi;
1465: Q *nmk;
1466: N u,unm,udn;
1467: int sgn,ret;
1468:
1469: if ( UNIN(md) )
1470: return 0;
1471: row = mat->row; col = mat->col;
1472: bshiftn(md,1,&t);
1473: isqrt(t,&s);
1474: bshiftn(s,64,&b);
1475: if ( !b )
1476: b = ONEN;
1477: dn0 = ONE;
1478: tmat = (Q **)mat->body;
1479: rmat = (Q **)nm->body;
1480: for ( i = 0; i < row; i++ )
1481: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1482: if ( tmi[j] ) {
1483: muln(NM(tmi[j]),NM(dn0),&s);
1484: remn(s,md,&u);
1485: ret = inttorat(u,md,b,&sgn,&unm,&udn);
1486: if ( !ret )
1487: return 0;
1488: else {
1489: if ( SGN(tmi[j])<0 )
1490: sgn = -sgn;
1491: NTOQ(unm,sgn,nm1);
1492: NTOQ(udn,1,dn1);
1493: if ( !UNIQ(dn1) ) {
1494: for ( k = 0; k < i; k++ )
1495: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1496: mulq(nmk[l],dn1,&q); nmk[l] = q;
1497: }
1498: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1499: mulq(nmk[l],dn1,&q); nmk[l] = q;
1500: }
1501: }
1502: rmat[i][j] = nm1;
1503: mulq(dn0,dn1,&q); dn0 = q;
1504: }
1505: }
1506: *dn = dn0;
1507: return 1;
1508: }
1509:
1.4 noro 1510: #define ONE_STEP1 if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1511:
1.24 noro 1512: void reduce_reducers_mod(int **mat,int row,int col,int md)
1.4 noro 1513: {
1514: int i,j,k,l,hc,zzz;
1515: int *t,*s,*tj,*ind;
1516:
1517: /* reduce the reducers */
1518: ind = (int *)ALLOCA(row*sizeof(int));
1519: for ( i = 0; i < row; i++ ) {
1520: t = mat[i];
1521: for ( j = 0; j < col && !t[j]; j++ );
1522: /* register the position of the head term */
1523: ind[i] = j;
1524: for ( l = i-1; l >= 0; l-- ) {
1525: /* reduce mat[i] by mat[l] */
1526: if ( hc = t[ind[l]] ) {
1527: /* mat[i] = mat[i]-hc*mat[l] */
1528: j = ind[l];
1529: s = mat[l]+j;
1530: tj = t+j;
1531: hc = md-hc;
1532: k = col-j;
1533: for ( ; k >= 64; k -= 64 ) {
1534: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1535: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1536: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1537: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1538: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1539: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1540: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1541: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1542: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1543: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1544: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1545: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1546: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1547: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1548: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1549: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1550: }
1.16 noro 1551: for ( ; k > 0; k-- ) {
1.4 noro 1552: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1553: }
1554: }
1555: }
1556: }
1557: }
1558:
1559: /*
1560: mat[i] : reducers (i=0,...,nred-1)
1561: spolys (i=nred,...,row-1)
1562: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1563: 1. reduce the reducers
1564: 2. reduce spolys by the reduced reducers
1565: */
1566:
1.24 noro 1567: void pre_reduce_mod(int **mat,int row,int col,int nred,int md)
1.4 noro 1568: {
1569: int i,j,k,l,hc,inv;
1570: int *t,*s,*tk,*ind;
1571:
1572: #if 1
1573: /* reduce the reducers */
1574: ind = (int *)ALLOCA(row*sizeof(int));
1575: for ( i = 0; i < nred; i++ ) {
1576: /* make mat[i] monic and mat[i] by mat[0],...,mat[i-1] */
1577: t = mat[i];
1578: for ( j = 0; j < col && !t[j]; j++ );
1579: /* register the position of the head term */
1580: ind[i] = j;
1581: inv = invm(t[j],md);
1582: for ( k = j; k < col; k++ )
1583: if ( t[k] )
1584: DMAR(t[k],inv,0,md,t[k])
1585: for ( l = i-1; l >= 0; l-- ) {
1586: /* reduce mat[i] by mat[l] */
1587: if ( hc = t[ind[l]] ) {
1588: /* mat[i] = mat[i]-hc*mat[l] */
1589: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
1590: k < col; k++, tk++, s++ )
1591: if ( *s )
1592: DMAR(*s,hc,*tk,md,*tk)
1593: }
1594: }
1595: }
1596: /* reduce the spolys */
1597: for ( i = nred; i < row; i++ ) {
1598: t = mat[i];
1599: for ( l = nred-1; l >= 0; l-- ) {
1600: /* reduce mat[i] by mat[l] */
1601: if ( hc = t[ind[l]] ) {
1602: /* mat[i] = mat[i]-hc*mat[l] */
1603: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
1604: k < col; k++, tk++, s++ )
1605: if ( *s )
1606: DMAR(*s,hc,*tk,md,*tk)
1607: }
1608: }
1609: }
1610: #endif
1611: }
1612: /*
1613: mat[i] : reducers (i=0,...,nred-1)
1614: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1615: */
1616:
1.24 noro 1617: void reduce_sp_by_red_mod(int *sp,int **redmat,int *ind,int nred,int col,int md)
1.4 noro 1618: {
1619: int i,j,k,hc,zzz;
1.24 noro 1620: int *s,*tj;
1.4 noro 1621:
1622: /* reduce the spolys by redmat */
1623: for ( i = nred-1; i >= 0; i-- ) {
1624: /* reduce sp by redmat[i] */
1625: if ( hc = sp[ind[i]] ) {
1626: /* sp = sp-hc*redmat[i] */
1627: j = ind[i];
1628: hc = md-hc;
1629: s = redmat[i]+j;
1630: tj = sp+j;
1.16 noro 1631: for ( k = col-j; k > 0; k-- ) {
1.4 noro 1632: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1.15 noro 1633: }
1634: }
1.17 noro 1635: }
1636: }
1637:
1638: /*
1.15 noro 1639: mat[i] : compressed reducers (i=0,...,nred-1)
1640: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1641: */
1642:
1.24 noro 1643: void red_by_compress(int m,unsigned int *p,unsigned int *r,
1644: unsigned int *ri,unsigned int hc,int len)
1.18 noro 1645: {
1.19 noro 1646: unsigned int up,lo;
1.18 noro 1647: unsigned int dmy;
1648: unsigned int *pj;
1649:
1.21 noro 1650: p[*ri] = 0; r++; ri++;
1651: for ( len--; len; len--, r++, ri++ ) {
1652: pj = p+ *ri;
1653: DMA(*r,hc,*pj,up,lo);
1.18 noro 1654: if ( up ) {
1655: DSAB(m,up,lo,dmy,*pj);
1656: } else
1657: *pj = lo;
1658: }
1659: }
1660:
1661: /* p -= hc*r */
1662:
1.24 noro 1663: void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1.18 noro 1664: {
1665: register unsigned int up,lo;
1666: unsigned int dmy;
1667:
1668: *p++ = 0; r++; len--;
1669: for ( ; len; len--, r++, p++ )
1670: if ( *r ) {
1.20 noro 1671: DMA(*r,hc,*p,up,lo);
1.18 noro 1672: if ( up ) {
1673: DSAB(m,up,lo,dmy,*p);
1674: } else
1675: *p = lo;
1676: }
1677: }
1678:
1.32 noro 1679: void red_by_vect_sf(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1680: {
1681: *p++ = 0; r++; len--;
1682: for ( ; len; len--, r++, p++ )
1683: if ( *r )
1684: *p = _addsf(_mulsf(*r,hc),*p);
1685: }
1686:
1.21 noro 1687: extern unsigned int **psca;
1688:
1.24 noro 1689: void reduce_sp_by_red_mod_compress (int *sp,CDP *redmat,int *ind,
1690: int nred,int col,int md)
1.15 noro 1691: {
1.24 noro 1692: int i,len;
1.15 noro 1693: CDP ri;
1.24 noro 1694: unsigned int hc;
1.18 noro 1695: unsigned int *usp;
1.15 noro 1696:
1.18 noro 1697: usp = (unsigned int *)sp;
1.15 noro 1698: /* reduce the spolys by redmat */
1699: for ( i = nred-1; i >= 0; i-- ) {
1700: /* reduce sp by redmat[i] */
1.18 noro 1701: usp[ind[i]] %= md;
1702: if ( hc = usp[ind[i]] ) {
1.15 noro 1703: /* sp = sp-hc*redmat[i] */
1704: hc = md-hc;
1705: ri = redmat[i];
1706: len = ri->len;
1.21 noro 1707: red_by_compress(md,usp,psca[ri->psindex],ri->body,hc,len);
1.4 noro 1708: }
1709: }
1.18 noro 1710: for ( i = 0; i < col; i++ )
1.24 noro 1711: if ( usp[i] >= (unsigned int)md )
1.18 noro 1712: usp[i] %= md;
1.4 noro 1713: }
1714:
1715: #define ONE_STEP2 if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
1716:
1.24 noro 1717: int generic_gauss_elim_mod(int **mat0,int row,int col,int md,int *colstat)
1.1 noro 1718: {
1.24 noro 1719: int i,j,k,l,inv,a,rank;
1720: unsigned int *t,*pivot,*pk;
1.18 noro 1721: unsigned int **mat;
1.1 noro 1722:
1.18 noro 1723: mat = (unsigned int **)mat0;
1.1 noro 1724: for ( rank = 0, j = 0; j < col; j++ ) {
1.18 noro 1725: for ( i = rank; i < row; i++ )
1726: mat[i][j] %= md;
1727: for ( i = rank; i < row; i++ )
1728: if ( mat[i][j] )
1729: break;
1.1 noro 1730: if ( i == row ) {
1731: colstat[j] = 0;
1732: continue;
1733: } else
1734: colstat[j] = 1;
1735: if ( i != rank ) {
1736: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
1737: }
1738: pivot = mat[rank];
1739: inv = invm(pivot[j],md);
1.4 noro 1740: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
1741: if ( *pk ) {
1.24 noro 1742: if ( *pk >= (unsigned int)md )
1.18 noro 1743: *pk %= md;
1.4 noro 1744: DMAR(*pk,inv,0,md,*pk)
1.1 noro 1745: }
1746: for ( i = rank+1; i < row; i++ ) {
1747: t = mat[i];
1.18 noro 1748: if ( a = t[j] )
1749: red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1 noro 1750: }
1751: rank++;
1752: }
1753: for ( j = col-1, l = rank-1; j >= 0; j-- )
1754: if ( colstat[j] ) {
1755: pivot = mat[l];
1756: for ( i = 0; i < l; i++ ) {
1757: t = mat[i];
1.18 noro 1758: t[j] %= md;
1759: if ( a = t[j] )
1760: red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1 noro 1761: }
1762: l--;
1.18 noro 1763: }
1764: for ( j = 0, l = 0; l < rank; j++ )
1765: if ( colstat[j] ) {
1766: t = mat[l];
1767: for ( k = j; k < col; k++ )
1.24 noro 1768: if ( t[k] >= (unsigned int)md )
1.18 noro 1769: t[k] %= md;
1770: l++;
1.32 noro 1771: }
1772: return rank;
1773: }
1774:
1775: int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat)
1776: {
1777: int i,j,k,l,inv,a,rank;
1778: unsigned int *t,*pivot,*pk;
1779: unsigned int **mat;
1780:
1781: mat = (unsigned int **)mat0;
1782: for ( rank = 0, j = 0; j < col; j++ ) {
1783: for ( i = rank; i < row; i++ )
1784: if ( mat[i][j] )
1785: break;
1786: if ( i == row ) {
1787: colstat[j] = 0;
1788: continue;
1789: } else
1790: colstat[j] = 1;
1791: if ( i != rank ) {
1792: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
1793: }
1794: pivot = mat[rank];
1795: inv = _invsf(pivot[j]);
1796: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
1797: if ( *pk )
1798: *pk = _mulsf(*pk,inv);
1799: for ( i = rank+1; i < row; i++ ) {
1800: t = mat[i];
1801: if ( a = t[j] )
1802: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
1803: }
1804: rank++;
1805: }
1806: for ( j = col-1, l = rank-1; j >= 0; j-- )
1807: if ( colstat[j] ) {
1808: pivot = mat[l];
1809: for ( i = 0; i < l; i++ ) {
1810: t = mat[i];
1811: if ( a = t[j] )
1812: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
1813: }
1814: l--;
1.1 noro 1815: }
1816: return rank;
1817: }
1818:
1819: /* LU decomposition; a[i][i] = 1/U[i][i] */
1820:
1.24 noro 1821: int lu_gfmmat(GFMMAT mat,unsigned int md,int *perm)
1.1 noro 1822: {
1823: int row,col;
1.24 noro 1824: int i,j,k;
1.1 noro 1825: unsigned int *t,*pivot;
1826: unsigned int **a;
1827: unsigned int inv,m;
1828:
1829: row = mat->row; col = mat->col;
1830: a = mat->body;
1831: bzero(perm,row*sizeof(int));
1832:
1833: for ( i = 0; i < row; i++ )
1834: perm[i] = i;
1835: for ( k = 0; k < col; k++ ) {
1836: for ( i = k; i < row && !a[i][k]; i++ );
1837: if ( i == row )
1838: return 0;
1839: if ( i != k ) {
1840: j = perm[i]; perm[i] = perm[k]; perm[k] = j;
1841: t = a[i]; a[i] = a[k]; a[k] = t;
1842: }
1843: pivot = a[k];
1844: pivot[k] = inv = invm(pivot[k],md);
1845: for ( i = k+1; i < row; i++ ) {
1846: t = a[i];
1847: if ( m = t[k] ) {
1848: DMAR(inv,m,0,md,t[k])
1849: for ( j = k+1, m = md - t[k]; j < col; j++ )
1850: if ( pivot[j] ) {
1.8 noro 1851: unsigned int tj;
1852:
1853: DMAR(m,pivot[j],t[j],md,tj)
1854: t[j] = tj;
1.1 noro 1855: }
1856: }
1857: }
1858: }
1859: return 1;
1860: }
1861:
1.3 noro 1862: /*
1863: Input
1864: a: a row x col matrix
1865: md : a modulus
1866:
1867: Output:
1868: return : d = the rank of mat
1869: a[0..(d-1)][0..(d-1)] : LU decomposition (a[i][i] = 1/U[i][i])
1870: rinfo: array of length row
1871: cinfo: array of length col
1872: i-th row in new a <-> rinfo[i]-th row in old a
1873: cinfo[j]=1 <=> j-th column is contained in the LU decomp.
1874: */
1875:
1.24 noro 1876: int find_lhs_and_lu_mod(unsigned int **a,int row,int col,
1877: unsigned int md,int **rinfo,int **cinfo)
1.3 noro 1878: {
1.24 noro 1879: int i,j,k,d;
1.3 noro 1880: int *rp,*cp;
1881: unsigned int *t,*pivot;
1882: unsigned int inv,m;
1883:
1884: *rinfo = rp = (int *)MALLOC_ATOMIC(row*sizeof(int));
1885: *cinfo = cp = (int *)MALLOC_ATOMIC(col*sizeof(int));
1886: for ( i = 0; i < row; i++ )
1887: rp[i] = i;
1888: for ( k = 0, d = 0; k < col; k++ ) {
1889: for ( i = d; i < row && !a[i][k]; i++ );
1890: if ( i == row ) {
1891: cp[k] = 0;
1892: continue;
1893: } else
1894: cp[k] = 1;
1895: if ( i != d ) {
1896: j = rp[i]; rp[i] = rp[d]; rp[d] = j;
1897: t = a[i]; a[i] = a[d]; a[d] = t;
1898: }
1899: pivot = a[d];
1900: pivot[k] = inv = invm(pivot[k],md);
1901: for ( i = d+1; i < row; i++ ) {
1902: t = a[i];
1903: if ( m = t[k] ) {
1904: DMAR(inv,m,0,md,t[k])
1905: for ( j = k+1, m = md - t[k]; j < col; j++ )
1906: if ( pivot[j] ) {
1.8 noro 1907: unsigned int tj;
1908: DMAR(m,pivot[j],t[j],md,tj)
1909: t[j] = tj;
1.3 noro 1910: }
1911: }
1912: }
1913: d++;
1914: }
1915: return d;
1916: }
1917:
1918: /*
1919: Input
1920: a : n x n matrix; a result of LU-decomposition
1921: md : modulus
1922: b : n x l matrix
1923: Output
1924: b = a^(-1)b
1925: */
1926:
1.44 ! noro 1927: void solve_by_lu_mod(int **a,int n,int md,int **b,int l,int normalize)
1.3 noro 1928: {
1929: unsigned int *y,*c;
1930: int i,j,k;
1931: unsigned int t,m,m2;
1932:
1933: y = (int *)MALLOC_ATOMIC(n*sizeof(int));
1934: c = (int *)MALLOC_ATOMIC(n*sizeof(int));
1935: m2 = md>>1;
1936: for ( k = 0; k < l; k++ ) {
1937: /* copy b[.][k] to c */
1938: for ( i = 0; i < n; i++ )
1939: c[i] = (unsigned int)b[i][k];
1940: /* solve Ly=c */
1941: for ( i = 0; i < n; i++ ) {
1942: for ( t = c[i], j = 0; j < i; j++ )
1943: if ( a[i][j] ) {
1944: m = md - a[i][j];
1945: DMAR(m,y[j],t,md,t)
1946: }
1947: y[i] = t;
1948: }
1949: /* solve Uc=y */
1950: for ( i = n-1; i >= 0; i-- ) {
1951: for ( t = y[i], j =i+1; j < n; j++ )
1952: if ( a[i][j] ) {
1953: m = md - a[i][j];
1954: DMAR(m,c[j],t,md,t)
1955: }
1956: /* a[i][i] = 1/U[i][i] */
1957: DMAR(t,a[i][i],0,md,c[i])
1958: }
1959: /* copy c to b[.][k] with normalization */
1.44 ! noro 1960: if ( normalize )
! 1961: for ( i = 0; i < n; i++ )
! 1962: b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]);
! 1963: else
! 1964: for ( i = 0; i < n; i++ )
! 1965: b[i][k] = c[i];
1.3 noro 1966: }
1967: }
1968:
1.24 noro 1969: void Pleqm1(NODE arg,VECT *rp)
1.1 noro 1970: {
1971: MAT m;
1972: VECT vect;
1973: pointer **mat;
1974: Q *v;
1975: Q q;
1976: int **wmat;
1977: int md,i,j,row,col,t,n,status;
1978:
1979: asir_assert(ARG0(arg),O_MAT,"leqm1");
1980: asir_assert(ARG1(arg),O_N,"leqm1");
1981: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
1982: row = m->row; col = m->col; mat = m->body;
1983: wmat = (int **)almat(row,col);
1984: for ( i = 0; i < row; i++ )
1985: for ( j = 0; j < col; j++ )
1986: if ( q = (Q)mat[i][j] ) {
1987: t = rem(NM(q),md);
1988: if ( SGN(q) < 0 )
1989: t = (md - t) % md;
1990: wmat[i][j] = t;
1991: } else
1992: wmat[i][j] = 0;
1993: status = gauss_elim_mod1(wmat,row,col,md);
1994: if ( status < 0 )
1995: *rp = 0;
1996: else if ( status > 0 )
1997: *rp = (VECT)ONE;
1998: else {
1999: n = col - 1;
2000: MKVECT(vect,n);
2001: for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
2002: t = (md-wmat[i][n])%md; STOQ(t,v[i]);
2003: }
2004: *rp = vect;
2005: }
2006: }
2007:
1.24 noro 2008: int gauss_elim_mod1(int **mat,int row,int col,int md)
1.1 noro 2009: {
2010: int i,j,k,inv,a,n;
2011: int *t,*pivot;
2012:
2013: n = col - 1;
2014: for ( j = 0; j < n; j++ ) {
2015: for ( i = j; i < row && !mat[i][j]; i++ );
2016: if ( i == row )
2017: return 1;
2018: if ( i != j ) {
2019: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2020: }
2021: pivot = mat[j];
2022: inv = invm(pivot[j],md);
2023: for ( k = j; k <= n; k++ )
2024: pivot[k] = dmar(pivot[k],inv,0,md);
2025: for ( i = j+1; i < row; i++ ) {
2026: t = mat[i];
2027: if ( i != j && (a = t[j]) )
2028: for ( k = j, a = md - a; k <= n; k++ )
2029: t[k] = dmar(pivot[k],a,t[k],md);
2030: }
2031: }
2032: for ( i = n; i < row && !mat[i][n]; i++ );
2033: if ( i == row ) {
2034: for ( j = n-1; j >= 0; j-- ) {
2035: for ( i = j-1, a = (md-mat[j][n])%md; i >= 0; i-- ) {
2036: mat[i][n] = dmar(mat[i][j],a,mat[i][n],md);
2037: mat[i][j] = 0;
2038: }
2039: }
2040: return 0;
2041: } else
2042: return -1;
2043: }
2044:
1.24 noro 2045: void Pgeninvm(NODE arg,LIST *rp)
1.1 noro 2046: {
2047: MAT m;
2048: pointer **mat;
2049: Q **tmat;
2050: Q q;
2051: unsigned int **wmat;
2052: int md,i,j,row,col,t,status;
2053: MAT mat1,mat2;
2054: NODE node1,node2;
2055:
2056: asir_assert(ARG0(arg),O_MAT,"leqm1");
2057: asir_assert(ARG1(arg),O_N,"leqm1");
2058: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2059: row = m->row; col = m->col; mat = m->body;
2060: wmat = (unsigned int **)almat(row,col+row);
2061: for ( i = 0; i < row; i++ ) {
2062: bzero((char *)wmat[i],(col+row)*sizeof(int));
2063: for ( j = 0; j < col; j++ )
2064: if ( q = (Q)mat[i][j] ) {
2065: t = rem(NM(q),md);
2066: if ( SGN(q) < 0 )
2067: t = (md - t) % md;
2068: wmat[i][j] = t;
2069: }
2070: wmat[i][col+i] = 1;
2071: }
2072: status = gauss_elim_geninv_mod(wmat,row,col,md);
2073: if ( status > 0 )
2074: *rp = 0;
2075: else {
2076: MKMAT(mat1,col,row); MKMAT(mat2,row-col,row);
2077: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
2078: for ( j = 0; j < row; j++ )
1.24 noro 2079: UTOQ(wmat[i][j+col],tmat[i][j]);
1.1 noro 2080: for ( tmat = (Q **)mat2->body; i < row; i++ )
2081: for ( j = 0; j < row; j++ )
1.24 noro 2082: UTOQ(wmat[i][j+col],tmat[i-col][j]);
1.1 noro 2083: MKNODE(node2,mat2,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2084: }
2085: }
2086:
1.24 noro 2087: int gauss_elim_geninv_mod(unsigned int **mat,int row,int col,int md)
1.1 noro 2088: {
2089: int i,j,k,inv,a,n,m;
2090: unsigned int *t,*pivot;
2091:
2092: n = col; m = row+col;
2093: for ( j = 0; j < n; j++ ) {
2094: for ( i = j; i < row && !mat[i][j]; i++ );
2095: if ( i == row )
2096: return 1;
2097: if ( i != j ) {
2098: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2099: }
2100: pivot = mat[j];
2101: inv = invm(pivot[j],md);
2102: for ( k = j; k < m; k++ )
2103: pivot[k] = dmar(pivot[k],inv,0,md);
2104: for ( i = j+1; i < row; i++ ) {
2105: t = mat[i];
2106: if ( a = t[j] )
2107: for ( k = j, a = md - a; k < m; k++ )
2108: t[k] = dmar(pivot[k],a,t[k],md);
2109: }
2110: }
2111: for ( j = n-1; j >= 0; j-- ) {
2112: pivot = mat[j];
2113: for ( i = j-1; i >= 0; i-- ) {
2114: t = mat[i];
2115: if ( a = t[j] )
2116: for ( k = j, a = md - a; k < m; k++ )
2117: t[k] = dmar(pivot[k],a,t[k],md);
2118: }
2119: }
2120: return 0;
2121: }
2122:
1.24 noro 2123: void Psolve_by_lu_gfmmat(NODE arg,VECT *rp)
1.1 noro 2124: {
2125: GFMMAT lu;
2126: Q *perm,*rhs,*v;
2127: int n,i;
2128: unsigned int md;
2129: unsigned int *b,*sol;
2130: VECT r;
2131:
2132: lu = (GFMMAT)ARG0(arg);
2133: perm = (Q *)BDY((VECT)ARG1(arg));
2134: rhs = (Q *)BDY((VECT)ARG2(arg));
2135: md = (unsigned int)QTOS((Q)ARG3(arg));
2136: n = lu->col;
2137: b = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2138: sol = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2139: for ( i = 0; i < n; i++ )
2140: b[i] = QTOS(rhs[QTOS(perm[i])]);
2141: solve_by_lu_gfmmat(lu,md,b,sol);
2142: MKVECT(r,n);
2143: for ( i = 0, v = (Q *)r->body; i < n; i++ )
1.24 noro 2144: UTOQ(sol[i],v[i]);
1.1 noro 2145: *rp = r;
2146: }
2147:
1.24 noro 2148: void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md,
2149: unsigned int *b,unsigned int *x)
1.1 noro 2150: {
2151: int n;
2152: unsigned int **a;
2153: unsigned int *y;
2154: int i,j;
2155: unsigned int t,m;
2156:
2157: n = lu->col;
2158: a = lu->body;
2159: y = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2160: /* solve Ly=b */
2161: for ( i = 0; i < n; i++ ) {
2162: for ( t = b[i], j = 0; j < i; j++ )
2163: if ( a[i][j] ) {
2164: m = md - a[i][j];
2165: DMAR(m,y[j],t,md,t)
2166: }
2167: y[i] = t;
2168: }
2169: /* solve Ux=y */
2170: for ( i = n-1; i >= 0; i-- ) {
2171: for ( t = y[i], j =i+1; j < n; j++ )
2172: if ( a[i][j] ) {
2173: m = md - a[i][j];
2174: DMAR(m,x[j],t,md,t)
2175: }
2176: /* a[i][i] = 1/U[i][i] */
2177: DMAR(t,a[i][i],0,md,x[i])
2178: }
2179: }
2180:
1.24 noro 2181: void Plu_gfmmat(NODE arg,LIST *rp)
1.1 noro 2182: {
2183: MAT m;
2184: GFMMAT mm;
2185: unsigned int md;
2186: int i,row,col,status;
2187: int *iperm;
2188: Q *v;
2189: VECT perm;
2190: NODE n0;
2191:
2192: asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
2193: asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
2194: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
2195: mat_to_gfmmat(m,md,&mm);
2196: row = m->row;
2197: col = m->col;
2198: iperm = (int *)MALLOC_ATOMIC(row*sizeof(int));
2199: status = lu_gfmmat(mm,md,iperm);
2200: if ( !status )
2201: n0 = 0;
2202: else {
2203: MKVECT(perm,row);
2204: for ( i = 0, v = (Q *)perm->body; i < row; i++ )
2205: STOQ(iperm[i],v[i]);
2206: n0 = mknode(2,mm,perm);
2207: }
2208: MKLIST(*rp,n0);
2209: }
2210:
1.24 noro 2211: void Pmat_to_gfmmat(NODE arg,GFMMAT *rp)
1.1 noro 2212: {
2213: MAT m;
2214: unsigned int md;
2215:
2216: asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
2217: asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
2218: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
2219: mat_to_gfmmat(m,md,rp);
2220: }
2221:
1.24 noro 2222: void mat_to_gfmmat(MAT m,unsigned int md,GFMMAT *rp)
1.1 noro 2223: {
2224: unsigned int **wmat;
2225: unsigned int t;
2226: Q **mat;
2227: Q q;
2228: int i,j,row,col;
2229:
2230: row = m->row; col = m->col; mat = (Q **)m->body;
2231: wmat = (unsigned int **)almat(row,col);
2232: for ( i = 0; i < row; i++ ) {
2233: bzero((char *)wmat[i],col*sizeof(unsigned int));
2234: for ( j = 0; j < col; j++ )
2235: if ( q = mat[i][j] ) {
2236: t = (unsigned int)rem(NM(q),md);
2237: if ( SGN(q) < 0 )
2238: t = (md - t) % md;
2239: wmat[i][j] = t;
2240: }
2241: }
2242: TOGFMMAT(row,col,wmat,*rp);
2243: }
2244:
1.27 noro 2245: void Pgeninvm_swap(arg,rp)
2246: NODE arg;
2247: LIST *rp;
1.1 noro 2248: {
2249: MAT m;
2250: pointer **mat;
2251: Q **tmat;
2252: Q *tvect;
2253: Q q;
2254: unsigned int **wmat,**invmat;
2255: int *index;
2256: unsigned int t,md;
2257: int i,j,row,col,status;
2258: MAT mat1;
2259: VECT vect1;
2260: NODE node1,node2;
2261:
2262: asir_assert(ARG0(arg),O_MAT,"geninvm_swap");
2263: asir_assert(ARG1(arg),O_N,"geninvm_swap");
2264: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2265: row = m->row; col = m->col; mat = m->body;
2266: wmat = (unsigned int **)almat(row,col+row);
2267: for ( i = 0; i < row; i++ ) {
2268: bzero((char *)wmat[i],(col+row)*sizeof(int));
2269: for ( j = 0; j < col; j++ )
2270: if ( q = (Q)mat[i][j] ) {
2271: t = (unsigned int)rem(NM(q),md);
2272: if ( SGN(q) < 0 )
2273: t = (md - t) % md;
2274: wmat[i][j] = t;
2275: }
2276: wmat[i][col+i] = 1;
2277: }
2278: status = gauss_elim_geninv_mod_swap(wmat,row,col,md,&invmat,&index);
2279: if ( status > 0 )
2280: *rp = 0;
2281: else {
2282: MKMAT(mat1,col,col);
2283: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
2284: for ( j = 0; j < col; j++ )
2285: UTOQ(invmat[i][j],tmat[i][j]);
2286: MKVECT(vect1,row);
2287: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
2288: STOQ(index[i],tvect[i]);
2289: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2290: }
2291: }
2292:
1.27 noro 2293: gauss_elim_geninv_mod_swap(mat,row,col,md,invmatp,indexp)
2294: unsigned int **mat;
2295: int row,col;
2296: unsigned int md;
2297: unsigned int ***invmatp;
2298: int **indexp;
1.1 noro 2299: {
2300: int i,j,k,inv,a,n,m;
2301: unsigned int *t,*pivot,*s;
2302: int *index;
2303: unsigned int **invmat;
2304:
2305: n = col; m = row+col;
2306: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
2307: for ( i = 0; i < row; i++ )
2308: index[i] = i;
2309: for ( j = 0; j < n; j++ ) {
2310: for ( i = j; i < row && !mat[i][j]; i++ );
2311: if ( i == row ) {
2312: *indexp = 0; *invmatp = 0; return 1;
2313: }
2314: if ( i != j ) {
2315: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2316: k = index[i]; index[i] = index[j]; index[j] = k;
2317: }
2318: pivot = mat[j];
2319: inv = (unsigned int)invm(pivot[j],md);
2320: for ( k = j; k < m; k++ )
2321: if ( pivot[k] )
2322: pivot[k] = (unsigned int)dmar(pivot[k],inv,0,md);
2323: for ( i = j+1; i < row; i++ ) {
2324: t = mat[i];
2325: if ( a = t[j] )
2326: for ( k = j, a = md - a; k < m; k++ )
2327: if ( pivot[k] )
2328: t[k] = dmar(pivot[k],a,t[k],md);
2329: }
2330: }
2331: for ( j = n-1; j >= 0; j-- ) {
2332: pivot = mat[j];
2333: for ( i = j-1; i >= 0; i-- ) {
2334: t = mat[i];
2335: if ( a = t[j] )
2336: for ( k = j, a = md - a; k < m; k++ )
2337: if ( pivot[k] )
2338: t[k] = dmar(pivot[k],a,t[k],md);
2339: }
2340: }
2341: *invmatp = invmat = (unsigned int **)almat(col,col);
1.27 noro 2342: for ( i = 0; i < col; i++ )
2343: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
2344: s[j] = t[col+index[j]];
2345: return 0;
2346: }
2347:
2348: void Pgeninv_sf_swap(NODE arg,LIST *rp)
2349: {
2350: MAT m;
2351: GFS **mat,**tmat;
2352: Q *tvect;
2353: GFS q;
2354: int **wmat,**invmat;
2355: int *index;
2356: unsigned int t;
2357: int i,j,row,col,status;
2358: MAT mat1;
2359: VECT vect1;
2360: NODE node1,node2;
2361:
2362: asir_assert(ARG0(arg),O_MAT,"geninv_sf_swap");
2363: m = (MAT)ARG0(arg);
2364: row = m->row; col = m->col; mat = (GFS **)m->body;
2365: wmat = (int **)almat(row,col+row);
2366: for ( i = 0; i < row; i++ ) {
2367: bzero((char *)wmat[i],(col+row)*sizeof(int));
2368: for ( j = 0; j < col; j++ )
2369: if ( q = (GFS)mat[i][j] )
2370: wmat[i][j] = FTOIF(CONT(q));
2371: wmat[i][col+i] = _onesf();
2372: }
2373: status = gauss_elim_geninv_sf_swap(wmat,row,col,&invmat,&index);
2374: if ( status > 0 )
2375: *rp = 0;
2376: else {
2377: MKMAT(mat1,col,col);
2378: for ( i = 0, tmat = (GFS **)mat1->body; i < col; i++ )
2379: for ( j = 0; j < col; j++ )
2380: if ( t = invmat[i][j] ) {
2381: MKGFS(IFTOF(t),tmat[i][j]);
2382: }
2383: MKVECT(vect1,row);
2384: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
2385: STOQ(index[i],tvect[i]);
2386: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2387: }
2388: }
2389:
2390: int gauss_elim_geninv_sf_swap(int **mat,int row,int col,
2391: int ***invmatp,int **indexp)
2392: {
2393: int i,j,k,inv,a,n,m,u;
2394: int *t,*pivot,*s;
2395: int *index;
2396: int **invmat;
2397:
2398: n = col; m = row+col;
2399: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
2400: for ( i = 0; i < row; i++ )
2401: index[i] = i;
2402: for ( j = 0; j < n; j++ ) {
2403: for ( i = j; i < row && !mat[i][j]; i++ );
2404: if ( i == row ) {
2405: *indexp = 0; *invmatp = 0; return 1;
2406: }
2407: if ( i != j ) {
2408: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2409: k = index[i]; index[i] = index[j]; index[j] = k;
2410: }
2411: pivot = mat[j];
2412: inv = _invsf(pivot[j]);
2413: for ( k = j; k < m; k++ )
2414: if ( pivot[k] )
2415: pivot[k] = _mulsf(pivot[k],inv);
2416: for ( i = j+1; i < row; i++ ) {
2417: t = mat[i];
2418: if ( a = t[j] )
2419: for ( k = j, a = _chsgnsf(a); k < m; k++ )
2420: if ( pivot[k] ) {
2421: u = _mulsf(pivot[k],a);
2422: t[k] = _addsf(u,t[k]);
2423: }
2424: }
2425: }
2426: for ( j = n-1; j >= 0; j-- ) {
2427: pivot = mat[j];
2428: for ( i = j-1; i >= 0; i-- ) {
2429: t = mat[i];
2430: if ( a = t[j] )
2431: for ( k = j, a = _chsgnsf(a); k < m; k++ )
2432: if ( pivot[k] ) {
2433: u = _mulsf(pivot[k],a);
2434: t[k] = _addsf(u,t[k]);
2435: }
2436: }
2437: }
2438: *invmatp = invmat = (int **)almat(col,col);
1.1 noro 2439: for ( i = 0; i < col; i++ )
2440: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
2441: s[j] = t[col+index[j]];
2442: return 0;
2443: }
2444:
2445: void _addn(N,N,N);
2446: int _subn(N,N,N);
2447: void _muln(N,N,N);
2448:
1.24 noro 2449: void inner_product_int(Q *a,Q *b,int n,Q *r)
1.1 noro 2450: {
2451: int la,lb,i;
2452: int sgn,sgn1;
2453: N wm,wma,sum,t;
2454:
2455: for ( la = lb = 0, i = 0; i < n; i++ ) {
2456: if ( a[i] )
2457: if ( DN(a[i]) )
2458: error("inner_product_int : invalid argument");
2459: else
2460: la = MAX(PL(NM(a[i])),la);
2461: if ( b[i] )
2462: if ( DN(b[i]) )
2463: error("inner_product_int : invalid argument");
2464: else
2465: lb = MAX(PL(NM(b[i])),lb);
2466: }
2467: sgn = 0;
2468: sum= NALLOC(la+lb+2);
2469: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
2470: wm = NALLOC(la+lb+2);
2471: wma = NALLOC(la+lb+2);
2472: for ( i = 0; i < n; i++ ) {
2473: if ( !a[i] || !b[i] )
2474: continue;
2475: _muln(NM(a[i]),NM(b[i]),wm);
2476: sgn1 = SGN(a[i])*SGN(b[i]);
2477: if ( !sgn ) {
2478: sgn = sgn1;
2479: t = wm; wm = sum; sum = t;
2480: } else if ( sgn == sgn1 ) {
2481: _addn(sum,wm,wma);
2482: if ( !PL(wma) )
2483: sgn = 0;
2484: t = wma; wma = sum; sum = t;
2485: } else {
2486: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
2487: sgn *= _subn(sum,wm,wma);
2488: t = wma; wma = sum; sum = t;
2489: }
2490: }
2491: GC_free(wm);
2492: GC_free(wma);
2493: if ( !sgn ) {
2494: GC_free(sum);
2495: *r = 0;
2496: } else
2497: NTOQ(sum,sgn,*r);
2498: }
2499:
1.3 noro 2500: /* (k,l) element of a*b where a: .x n matrix, b: n x . integer matrix */
2501:
1.24 noro 2502: void inner_product_mat_int_mod(Q **a,int **b,int n,int k,int l,Q *r)
1.3 noro 2503: {
2504: int la,lb,i;
2505: int sgn,sgn1;
2506: N wm,wma,sum,t;
2507: Q aki;
2508: int bil,bilsgn;
2509: struct oN tn;
2510:
2511: for ( la = 0, i = 0; i < n; i++ ) {
2512: if ( aki = a[k][i] )
2513: if ( DN(aki) )
2514: error("inner_product_int : invalid argument");
2515: else
2516: la = MAX(PL(NM(aki)),la);
2517: }
2518: lb = 1;
2519: sgn = 0;
2520: sum= NALLOC(la+lb+2);
2521: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
2522: wm = NALLOC(la+lb+2);
2523: wma = NALLOC(la+lb+2);
2524: for ( i = 0; i < n; i++ ) {
2525: if ( !(aki = a[k][i]) || !(bil = b[i][l]) )
2526: continue;
2527: tn.p = 1;
2528: if ( bil > 0 ) {
2529: tn.b[0] = bil; bilsgn = 1;
2530: } else {
2531: tn.b[0] = -bil; bilsgn = -1;
2532: }
2533: _muln(NM(aki),&tn,wm);
2534: sgn1 = SGN(aki)*bilsgn;
2535: if ( !sgn ) {
2536: sgn = sgn1;
2537: t = wm; wm = sum; sum = t;
2538: } else if ( sgn == sgn1 ) {
2539: _addn(sum,wm,wma);
2540: if ( !PL(wma) )
2541: sgn = 0;
2542: t = wma; wma = sum; sum = t;
2543: } else {
2544: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
2545: sgn *= _subn(sum,wm,wma);
2546: t = wma; wma = sum; sum = t;
2547: }
2548: }
2549: GC_free(wm);
2550: GC_free(wma);
2551: if ( !sgn ) {
2552: GC_free(sum);
2553: *r = 0;
2554: } else
2555: NTOQ(sum,sgn,*r);
2556: }
2557:
1.24 noro 2558: void Pmul_mat_vect_int(NODE arg,VECT *rp)
1.1 noro 2559: {
2560: MAT mat;
2561: VECT vect,r;
2562: int row,col,i;
2563:
2564: mat = (MAT)ARG0(arg);
2565: vect = (VECT)ARG1(arg);
2566: row = mat->row;
2567: col = mat->col;
2568: MKVECT(r,row);
1.24 noro 2569: for ( i = 0; i < row; i++ ) {
2570: inner_product_int((Q *)mat->body[i],(Q *)vect->body,col,(Q *)&r->body[i]);
2571: }
1.1 noro 2572: *rp = r;
2573: }
2574:
1.24 noro 2575: void Pnbpoly_up2(NODE arg,GF2N *rp)
1.1 noro 2576: {
2577: int m,type,ret;
2578: UP2 r;
2579:
2580: m = QTOS((Q)ARG0(arg));
2581: type = QTOS((Q)ARG1(arg));
2582: ret = generate_ONB_polynomial(&r,m,type);
2583: if ( ret == 0 )
2584: MKGF2N(r,*rp);
2585: else
2586: *rp = 0;
2587: }
2588:
1.24 noro 2589: void Px962_irredpoly_up2(NODE arg,GF2N *rp)
1.1 noro 2590: {
1.24 noro 2591: int m,ret,w;
1.1 noro 2592: GF2N prev;
2593: UP2 r;
2594:
2595: m = QTOS((Q)ARG0(arg));
2596: prev = (GF2N)ARG1(arg);
2597: if ( !prev ) {
2598: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2599: bzero((char *)r->b,w*sizeof(unsigned int));
2600: } else {
2601: r = prev->body;
2602: if ( degup2(r) != m ) {
2603: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2604: bzero((char *)r->b,w*sizeof(unsigned int));
2605: }
2606: }
1.24 noro 2607: ret = _generate_irreducible_polynomial(r,m);
1.1 noro 2608: if ( ret == 0 )
2609: MKGF2N(r,*rp);
2610: else
2611: *rp = 0;
2612: }
2613:
1.24 noro 2614: void Pirredpoly_up2(NODE arg,GF2N *rp)
1.1 noro 2615: {
1.24 noro 2616: int m,ret,w;
1.1 noro 2617: GF2N prev;
2618: UP2 r;
2619:
2620: m = QTOS((Q)ARG0(arg));
2621: prev = (GF2N)ARG1(arg);
2622: if ( !prev ) {
2623: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2624: bzero((char *)r->b,w*sizeof(unsigned int));
2625: } else {
2626: r = prev->body;
2627: if ( degup2(r) != m ) {
2628: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2629: bzero((char *)r->b,w*sizeof(unsigned int));
2630: }
2631: }
1.24 noro 2632: ret = _generate_good_irreducible_polynomial(r,m);
1.1 noro 2633: if ( ret == 0 )
2634: MKGF2N(r,*rp);
2635: else
2636: *rp = 0;
2637: }
2638:
1.26 noro 2639: void Pmat_swap_row_destructive(NODE arg, MAT *m)
2640: {
2641: int i1,i2;
2642: pointer *t;
2643: MAT mat;
2644:
2645: asir_assert(ARG0(arg),O_MAT,"mat_swap_row_destructive");
2646: asir_assert(ARG1(arg),O_N,"mat_swap_row_destructive");
2647: asir_assert(ARG2(arg),O_N,"mat_swap_row_destructive");
2648: mat = (MAT)ARG0(arg);
2649: i1 = QTOS((Q)ARG1(arg));
2650: i2 = QTOS((Q)ARG2(arg));
2651: if ( i1 < 0 || i2 < 0 || i1 >= mat->row || i2 >= mat->row )
2652: error("mat_swap_row_destructive : Out of range");
2653: t = mat->body[i1];
2654: mat->body[i1] = mat->body[i2];
2655: mat->body[i2] = t;
2656: *m = mat;
2657: }
2658:
2659: void Pmat_swap_col_destructive(NODE arg, MAT *m)
2660: {
2661: int j1,j2,i,n;
2662: pointer *mi;
2663: pointer t;
2664: MAT mat;
2665:
2666: asir_assert(ARG0(arg),O_MAT,"mat_swap_col_destructive");
2667: asir_assert(ARG1(arg),O_N,"mat_swap_col_destructive");
2668: asir_assert(ARG2(arg),O_N,"mat_swap_col_destructive");
2669: mat = (MAT)ARG0(arg);
2670: j1 = QTOS((Q)ARG1(arg));
2671: j2 = QTOS((Q)ARG2(arg));
2672: if ( j1 < 0 || j2 < 0 || j1 >= mat->col || j2 >= mat->col )
2673: error("mat_swap_col_destructive : Out of range");
2674: n = mat->row;
2675: for ( i = 0; i < n; i++ ) {
2676: mi = mat->body[i];
2677: t = mi[j1]; mi[j1] = mi[j2]; mi[j2] = t;
2678: }
2679: *m = mat;
2680: }
1.1 noro 2681: /*
2682: * f = type 'type' normal polynomial of degree m if exists
2683: * IEEE P1363 A.7.2
2684: *
2685: * return value : 0 --- exists
2686: * 1 --- does not exist
2687: * -1 --- failure (memory allocation error)
2688: */
2689:
2690: int generate_ONB_polynomial(UP2 *rp,int m,int type)
2691: {
2692: int i,r;
2693: int w;
2694: UP2 f,f0,f1,f2,t;
2695:
2696: w = (m>>5)+1;
2697: switch ( type ) {
2698: case 1:
2699: if ( !TypeT_NB_check(m,1) ) return 1;
2700: NEWUP2(f,w); *rp = f; f->w = w;
2701: /* set all the bits */
2702: for ( i = 0; i < w; i++ )
2703: f->b[i] = 0xffffffff;
2704: /* mask the top word if necessary */
2705: if ( r = (m+1)&31 )
2706: f->b[w-1] &= (1<<r)-1;
2707: return 0;
2708: break;
2709: case 2:
2710: if ( !TypeT_NB_check(m,2) ) return 1;
2711: NEWUP2(f,w); *rp = f;
2712: W_NEWUP2(f0,w);
2713: W_NEWUP2(f1,w);
2714: W_NEWUP2(f2,w);
2715:
2716: /* recursion for genrating Type II normal polynomial */
2717:
2718: /* f0 = 1, f1 = t+1 */
2719: f0->w = 1; f0->b[0] = 1;
2720: f1->w = 1; f1->b[0] = 3;
2721: for ( i = 2; i <= m; i++ ) {
2722: /* f2 = t*f1+f0 */
2723: _bshiftup2(f1,-1,f2);
2724: _addup2_destructive(f2,f0);
2725: /* cyclic change of the variables */
2726: t = f0; f0 = f1; f1 = f2; f2 = t;
2727: }
2728: _copyup2(f1,f);
2729: return 0;
2730: break;
2731: default:
2732: return -1;
2733: break;
2734: }
2735: }
2736:
2737: /*
2738: * f = an irreducible trinomial or pentanomial of degree d 'after' f
2739: * return value : 0 --- exists
2740: * 1 --- does not exist (exhaustion)
2741: */
2742:
2743: int _generate_irreducible_polynomial(UP2 f,int d)
2744: {
2745: int ret,i,j,k,nz,i0,j0,k0;
2746: int w;
2747: unsigned int *fd;
2748:
2749: /*
2750: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
2751: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
2752: * otherwise i0,j0,k0 is set to 0.
2753: */
2754:
2755: fd = f->b;
2756: w = (d>>5)+1;
2757: if ( f->w && (d==degup2(f)) ) {
2758: for ( nz = 0, i = d; i >= 0; i-- )
2759: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
2760: switch ( nz ) {
2761: case 3:
2762: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2763: /* reset i0-th bit */
2764: fd[i0>>5] &= ~(1<<(i0&31));
2765: j0 = k0 = 0;
2766: break;
2767: case 5:
2768: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2769: /* reset i0-th bit */
2770: fd[i0>>5] &= ~(1<<(i0&31));
2771: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
2772: /* reset j0-th bit */
2773: fd[j0>>5] &= ~(1<<(j0&31));
2774: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
2775: /* reset k0-th bit */
2776: fd[k0>>5] &= ~(1<<(k0&31));
2777: break;
2778: default:
2779: f->w = 0; break;
2780: }
2781: } else
2782: f->w = 0;
2783:
2784: if ( !f->w ) {
2785: fd = f->b;
2786: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
2787: i0 = j0 = k0 = 0;
2788: }
2789: /* if j0 > 0 then f is already a pentanomial */
2790: if ( j0 > 0 ) goto PENTA;
2791:
2792: /* searching for an irreducible trinomial */
2793:
2794: for ( i = 1; 2*i <= d; i++ ) {
2795: /* skip the polynomials 'before' f */
2796: if ( i < i0 ) continue;
2797: if ( i == i0 ) { i0 = 0; continue; }
2798: /* set i-th bit */
2799: fd[i>>5] |= (1<<(i&31));
2800: ret = irredcheck_dddup2(f);
2801: if ( ret == 1 ) return 0;
2802: /* reset i-th bit */
2803: fd[i>>5] &= ~(1<<(i&31));
2804: }
2805:
2806: /* searching for an irreducible pentanomial */
2807: PENTA:
2808: for ( i = 1; i < d; i++ ) {
2809: /* skip the polynomials 'before' f */
2810: if ( i < i0 ) continue;
2811: if ( i == i0 ) i0 = 0;
2812: /* set i-th bit */
2813: fd[i>>5] |= (1<<(i&31));
2814: for ( j = i+1; j < d; j++ ) {
2815: /* skip the polynomials 'before' f */
2816: if ( j < j0 ) continue;
2817: if ( j == j0 ) j0 = 0;
2818: /* set j-th bit */
2819: fd[j>>5] |= (1<<(j&31));
2820: for ( k = j+1; k < d; k++ ) {
2821: /* skip the polynomials 'before' f */
2822: if ( k < k0 ) continue;
2823: else if ( k == k0 ) { k0 = 0; continue; }
2824: /* set k-th bit */
2825: fd[k>>5] |= (1<<(k&31));
2826: ret = irredcheck_dddup2(f);
2827: if ( ret == 1 ) return 0;
2828: /* reset k-th bit */
2829: fd[k>>5] &= ~(1<<(k&31));
2830: }
2831: /* reset j-th bit */
2832: fd[j>>5] &= ~(1<<(j&31));
2833: }
2834: /* reset i-th bit */
2835: fd[i>>5] &= ~(1<<(i&31));
2836: }
2837: /* exhausted */
2838: return 1;
2839: }
2840:
2841: /*
2842: * f = an irreducible trinomial or pentanomial of degree d 'after' f
2843: *
2844: * searching strategy:
2845: * trinomial x^d+x^i+1:
2846: * i is as small as possible.
2847: * trinomial x^d+x^i+x^j+x^k+1:
2848: * i is as small as possible.
2849: * For such i, j is as small as possible.
2850: * For such i and j, 'k' is as small as possible.
2851: *
2852: * return value : 0 --- exists
2853: * 1 --- does not exist (exhaustion)
2854: */
2855:
2856: int _generate_good_irreducible_polynomial(UP2 f,int d)
2857: {
2858: int ret,i,j,k,nz,i0,j0,k0;
2859: int w;
2860: unsigned int *fd;
2861:
2862: /*
2863: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
2864: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
2865: * otherwise i0,j0,k0 is set to 0.
2866: */
2867:
2868: fd = f->b;
2869: w = (d>>5)+1;
2870: if ( f->w && (d==degup2(f)) ) {
2871: for ( nz = 0, i = d; i >= 0; i-- )
2872: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
2873: switch ( nz ) {
2874: case 3:
2875: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2876: /* reset i0-th bit */
2877: fd[i0>>5] &= ~(1<<(i0&31));
2878: j0 = k0 = 0;
2879: break;
2880: case 5:
2881: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2882: /* reset i0-th bit */
2883: fd[i0>>5] &= ~(1<<(i0&31));
2884: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
2885: /* reset j0-th bit */
2886: fd[j0>>5] &= ~(1<<(j0&31));
2887: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
2888: /* reset k0-th bit */
2889: fd[k0>>5] &= ~(1<<(k0&31));
2890: break;
2891: default:
2892: f->w = 0; break;
2893: }
2894: } else
2895: f->w = 0;
2896:
2897: if ( !f->w ) {
2898: fd = f->b;
2899: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
2900: i0 = j0 = k0 = 0;
2901: }
2902: /* if j0 > 0 then f is already a pentanomial */
2903: if ( j0 > 0 ) goto PENTA;
2904:
2905: /* searching for an irreducible trinomial */
2906:
2907: for ( i = 1; 2*i <= d; i++ ) {
2908: /* skip the polynomials 'before' f */
2909: if ( i < i0 ) continue;
2910: if ( i == i0 ) { i0 = 0; continue; }
2911: /* set i-th bit */
2912: fd[i>>5] |= (1<<(i&31));
2913: ret = irredcheck_dddup2(f);
2914: if ( ret == 1 ) return 0;
2915: /* reset i-th bit */
2916: fd[i>>5] &= ~(1<<(i&31));
2917: }
2918:
2919: /* searching for an irreducible pentanomial */
2920: PENTA:
2921: for ( i = 3; i < d; i++ ) {
2922: /* skip the polynomials 'before' f */
2923: if ( i < i0 ) continue;
2924: if ( i == i0 ) i0 = 0;
2925: /* set i-th bit */
2926: fd[i>>5] |= (1<<(i&31));
2927: for ( j = 2; j < i; j++ ) {
2928: /* skip the polynomials 'before' f */
2929: if ( j < j0 ) continue;
2930: if ( j == j0 ) j0 = 0;
2931: /* set j-th bit */
2932: fd[j>>5] |= (1<<(j&31));
2933: for ( k = 1; k < j; k++ ) {
2934: /* skip the polynomials 'before' f */
2935: if ( k < k0 ) continue;
2936: else if ( k == k0 ) { k0 = 0; continue; }
2937: /* set k-th bit */
2938: fd[k>>5] |= (1<<(k&31));
2939: ret = irredcheck_dddup2(f);
2940: if ( ret == 1 ) return 0;
2941: /* reset k-th bit */
2942: fd[k>>5] &= ~(1<<(k&31));
2943: }
2944: /* reset j-th bit */
2945: fd[j>>5] &= ~(1<<(j&31));
2946: }
2947: /* reset i-th bit */
2948: fd[i>>5] &= ~(1<<(i&31));
2949: }
2950: /* exhausted */
2951: return 1;
1.3 noro 2952: }
2953:
1.24 noro 2954: void printqmat(Q **mat,int row,int col)
1.3 noro 2955: {
2956: int i,j;
2957:
2958: for ( i = 0; i < row; i++ ) {
2959: for ( j = 0; j < col; j++ ) {
1.8 noro 2960: printnum((Num)mat[i][j]); printf(" ");
1.3 noro 2961: }
2962: printf("\n");
2963: }
2964: }
2965:
1.24 noro 2966: void printimat(int **mat,int row,int col)
1.3 noro 2967: {
2968: int i,j;
2969:
2970: for ( i = 0; i < row; i++ ) {
2971: for ( j = 0; j < col; j++ ) {
2972: printf("%d ",mat[i][j]);
2973: }
2974: printf("\n");
2975: }
1.36 noro 2976: }
2977:
2978: void Pnd_det(NODE arg,P *rp)
2979: {
1.37 noro 2980: if ( argc(arg) == 1 )
2981: nd_det(0,ARG0(arg),rp);
2982: else
2983: nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp);
1.1 noro 2984: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>