Annotation of OpenXM_contrib2/asir2000/builtin/array.c, Revision 1.39
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.39 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.38 2004/09/21 05:23:13 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;
761: STOQ(m,q); MKNODE(s,q,0); STOQ(n,q); MKNODE(t,q,s);
762: break;
763: default:
764: error("size : invalid argument"); break;
765: }
766: }
767: MKLIST(*rp,t);
768: }
769:
1.24 noro 770: void Pdet(NODE arg,P *rp)
1.1 noro 771: {
772: MAT m;
773: int n,i,j,mod;
774: P d;
775: P **mat,**w;
776:
777: m = (MAT)ARG0(arg);
778: asir_assert(m,O_MAT,"det");
779: if ( m->row != m->col )
780: error("det : non-square matrix");
781: else if ( argc(arg) == 1 )
782: detp(CO,(P **)BDY(m),m->row,rp);
783: else {
784: n = m->row; mod = QTOS((Q)ARG1(arg)); mat = (P **)BDY(m);
785: w = (P **)almat_pointer(n,n);
786: for ( i = 0; i < n; i++ )
787: for ( j = 0; j < n; j++ )
788: ptomp(mod,mat[i][j],&w[i][j]);
789: detmp(CO,mod,w,n,&d);
790: mptop(d,rp);
1.23 noro 791: }
792: }
793:
1.24 noro 794: void Pinvmat(NODE arg,LIST *rp)
1.23 noro 795: {
796: MAT m,r;
797: int n,i,j,mod;
798: P dn;
799: P **mat,**imat,**w;
800: NODE nd;
801:
802: m = (MAT)ARG0(arg);
803: asir_assert(m,O_MAT,"invmat");
804: if ( m->row != m->col )
805: error("invmat : non-square matrix");
806: else if ( argc(arg) == 1 ) {
807: n = m->row;
808: invmatp(CO,(P **)BDY(m),n,&imat,&dn);
809: NEWMAT(r); r->row = n; r->col = n; r->body = (pointer **)imat;
810: nd = mknode(2,r,dn);
811: MKLIST(*rp,nd);
812: } else {
813: n = m->row; mod = QTOS((Q)ARG1(arg)); mat = (P **)BDY(m);
814: w = (P **)almat_pointer(n,n);
815: for ( i = 0; i < n; i++ )
816: for ( j = 0; j < n; j++ )
817: ptomp(mod,mat[i][j],&w[i][j]);
818: #if 0
819: detmp(CO,mod,w,n,&d);
820: mptop(d,rp);
821: #else
822: error("not implemented yet");
823: #endif
1.1 noro 824: }
1.25 noro 825: }
826:
827: /*
828: input : a row x col matrix A
829: A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
830:
831: output : [B,R,C]
832: B : a rank(A) x col-rank(A) matrix
833: R : a vector of length rank(A)
834: C : a vector of length col-rank(A)
835: B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
836: */
837:
838: void Pgeneric_gauss_elim(NODE arg,LIST *rp)
839: {
840: NODE n0;
841: MAT m,nm;
842: int *ri,*ci;
843: VECT rind,cind;
844: Q dn,q;
845: int i,j,k,l,row,col,t,rank;
846:
847: asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim");
848: m = (MAT)ARG0(arg);
849: row = m->row; col = m->col;
850: rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
851: t = col-rank;
852: MKVECT(rind,rank);
853: MKVECT(cind,t);
854: for ( i = 0; i < rank; i++ ) {
855: STOQ(ri[i],q);
856: BDY(rind)[i] = (pointer)q;
857: }
858: for ( i = 0; i < t; i++ ) {
859: STOQ(ci[i],q);
860: BDY(cind)[i] = (pointer)q;
861: }
862: n0 = mknode(4,nm,dn,rind,cind);
863: MKLIST(*rp,n0);
1.1 noro 864: }
865:
866: /*
867: input : a row x col matrix A
868: A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
869:
870: output : [B,R,C]
871: B : a rank(A) x col-rank(A) matrix
872: R : a vector of length rank(A)
873: C : a vector of length col-rank(A)
874: B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
875: */
876:
1.24 noro 877: void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp)
1.1 noro 878: {
879: NODE n0;
880: MAT m,mat;
881: VECT rind,cind;
882: Q **tmat;
883: int **wmat;
884: Q *rib,*cib;
885: int *colstat;
886: Q q;
1.24 noro 887: int md,i,j,k,l,row,col,t,rank;
1.1 noro 888:
889: asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim_mod");
890: asir_assert(ARG1(arg),O_N,"generic_gauss_elim_mod");
891: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
892: row = m->row; col = m->col; tmat = (Q **)m->body;
893: wmat = (int **)almat(row,col);
894: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
895: for ( i = 0; i < row; i++ )
896: for ( j = 0; j < col; j++ )
897: if ( q = (Q)tmat[i][j] ) {
898: t = rem(NM(q),md);
899: if ( t && SGN(q) < 0 )
900: t = (md - t) % md;
901: wmat[i][j] = t;
902: } else
903: wmat[i][j] = 0;
904: rank = generic_gauss_elim_mod(wmat,row,col,md,colstat);
905:
906: MKMAT(mat,rank,col-rank);
907: tmat = (Q **)mat->body;
908: for ( i = 0; i < rank; i++ )
909: for ( j = k = 0; j < col; j++ )
910: if ( !colstat[j] ) {
911: UTOQ(wmat[i][j],tmat[i][k]); k++;
912: }
913:
914: MKVECT(rind,rank);
915: MKVECT(cind,col-rank);
916: rib = (Q *)rind->body; cib = (Q *)cind->body;
917: for ( j = k = l = 0; j < col; j++ )
918: if ( colstat[j] ) {
919: STOQ(j,rib[k]); k++;
920: } else {
921: STOQ(j,cib[l]); l++;
922: }
923: n0 = mknode(3,mat,rind,cind);
924: MKLIST(*rp,n0);
925: }
926:
1.24 noro 927: void Pleqm(NODE arg,VECT *rp)
1.1 noro 928: {
929: MAT m;
930: VECT vect;
931: pointer **mat;
932: Q *v;
933: Q q;
934: int **wmat;
935: int md,i,j,row,col,t,n,status;
936:
937: asir_assert(ARG0(arg),O_MAT,"leqm");
938: asir_assert(ARG1(arg),O_N,"leqm");
939: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
940: row = m->row; col = m->col; mat = m->body;
941: wmat = (int **)almat(row,col);
942: for ( i = 0; i < row; i++ )
943: for ( j = 0; j < col; j++ )
944: if ( q = (Q)mat[i][j] ) {
945: t = rem(NM(q),md);
946: if ( SGN(q) < 0 )
947: t = (md - t) % md;
948: wmat[i][j] = t;
949: } else
950: wmat[i][j] = 0;
951: status = gauss_elim_mod(wmat,row,col,md);
952: if ( status < 0 )
953: *rp = 0;
954: else if ( status > 0 )
955: *rp = (VECT)ONE;
956: else {
957: n = col - 1;
958: MKVECT(vect,n);
959: for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
960: t = (md-wmat[i][n])%md; STOQ(t,v[i]);
961: }
962: *rp = vect;
963: }
964: }
965:
1.24 noro 966: int gauss_elim_mod(int **mat,int row,int col,int md)
1.1 noro 967: {
968: int i,j,k,inv,a,n;
969: int *t,*pivot;
970:
971: n = col - 1;
972: for ( j = 0; j < n; j++ ) {
973: for ( i = j; i < row && !mat[i][j]; i++ );
974: if ( i == row )
975: return 1;
976: if ( i != j ) {
977: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
978: }
979: pivot = mat[j];
980: inv = invm(pivot[j],md);
981: for ( k = j; k <= n; k++ ) {
982: /* pivot[k] = dmar(pivot[k],inv,0,md); */
983: DMAR(pivot[k],inv,0,md,pivot[k])
984: }
985: for ( i = 0; i < row; i++ ) {
986: t = mat[i];
987: if ( i != j && (a = t[j]) )
988: for ( k = j, a = md - a; k <= n; k++ ) {
1.8 noro 989: unsigned int tk;
1.1 noro 990: /* t[k] = dmar(pivot[k],a,t[k],md); */
1.8 noro 991: DMAR(pivot[k],a,t[k],md,tk)
992: t[k] = tk;
1.1 noro 993: }
994: }
995: }
996: for ( i = n; i < row && !mat[i][n]; i++ );
997: if ( i == row )
998: return 0;
999: else
1000: return -1;
1001: }
1002:
1.4 noro 1003: struct oEGT eg_mod,eg_elim,eg_elim1,eg_elim2,eg_chrem,eg_gschk,eg_intrat,eg_symb;
1.31 noro 1004: struct oEGT eg_conv;
1.1 noro 1005:
1.24 noro 1006: int generic_gauss_elim(MAT mat,MAT *nm,Q *dn,int **rindp,int **cindp)
1.1 noro 1007: {
1008: int **wmat;
1009: Q **bmat;
1010: N **tmat;
1011: Q *bmi;
1012: N *tmi;
1013: Q q;
1014: int *wmi;
1015: int *colstat,*wcolstat,*rind,*cind;
1016: int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv;
1017: N m1,m2,m3,s,u;
1018: MAT r,crmat;
1019: struct oEGT tmp0,tmp1;
1020: struct oEGT eg_mod_split,eg_elim_split,eg_chrem_split;
1021: struct oEGT eg_intrat_split,eg_gschk_split;
1022: int ret;
1023:
1024: init_eg(&eg_mod_split); init_eg(&eg_chrem_split);
1025: init_eg(&eg_elim_split); init_eg(&eg_intrat_split);
1026: init_eg(&eg_gschk_split);
1027: bmat = (Q **)mat->body;
1028: row = mat->row; col = mat->col;
1029: wmat = (int **)almat(row,col);
1030: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1031: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1032: for ( ind = 0; ; ind++ ) {
1.11 noro 1033: if ( DP_Print ) {
1.2 noro 1034: fprintf(asir_out,"."); fflush(asir_out);
1035: }
1.12 noro 1036: md = get_lprime(ind);
1.1 noro 1037: get_eg(&tmp0);
1038: for ( i = 0; i < row; i++ )
1039: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
1040: if ( q = (Q)bmi[j] ) {
1041: t = rem(NM(q),md);
1042: if ( t && SGN(q) < 0 )
1043: t = (md - t) % md;
1044: wmi[j] = t;
1045: } else
1046: wmi[j] = 0;
1047: get_eg(&tmp1);
1048: add_eg(&eg_mod,&tmp0,&tmp1);
1049: add_eg(&eg_mod_split,&tmp0,&tmp1);
1050: get_eg(&tmp0);
1051: rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
1052: get_eg(&tmp1);
1053: add_eg(&eg_elim,&tmp0,&tmp1);
1054: add_eg(&eg_elim_split,&tmp0,&tmp1);
1055: if ( !ind ) {
1056: RESET:
1057: UTON(md,m1);
1058: rank0 = rank;
1059: bcopy(wcolstat,colstat,col*sizeof(int));
1060: MKMAT(crmat,rank,col-rank);
1061: MKMAT(r,rank,col-rank); *nm = r;
1062: tmat = (N **)crmat->body;
1063: for ( i = 0; i < rank; i++ )
1064: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
1065: if ( !colstat[j] ) {
1066: UTON(wmi[j],tmi[k]); k++;
1067: }
1068: } else {
1069: if ( rank < rank0 ) {
1.11 noro 1070: if ( DP_Print ) {
1.1 noro 1071: fprintf(asir_out,"lower rank matrix; continuing...\n");
1.2 noro 1072: fflush(asir_out);
1073: }
1.1 noro 1074: continue;
1075: } else if ( rank > rank0 ) {
1.11 noro 1076: if ( DP_Print ) {
1.1 noro 1077: fprintf(asir_out,"higher rank matrix; resetting...\n");
1.2 noro 1078: fflush(asir_out);
1079: }
1.1 noro 1080: goto RESET;
1081: } else {
1082: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
1083: if ( j < col ) {
1.11 noro 1084: if ( DP_Print ) {
1.1 noro 1085: fprintf(asir_out,"inconsitent colstat; resetting...\n");
1.2 noro 1086: fflush(asir_out);
1087: }
1.1 noro 1088: goto RESET;
1089: }
1090: }
1091:
1092: get_eg(&tmp0);
1093: inv = invm(rem(m1,md),md);
1094: UTON(md,m2); muln(m1,m2,&m3);
1095: for ( i = 0; i < rank; i++ )
1096: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
1097: if ( !colstat[j] ) {
1098: if ( tmi[k] ) {
1099: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
1100: t = rem(tmi[k],md);
1101: if ( wmi[j] >= t )
1102: t = wmi[j]-t;
1103: else
1104: t = md-(t-wmi[j]);
1105: DMAR(t,inv,0,md,t1)
1106: UTON(t1,u);
1107: muln(m1,u,&s);
1108: addn(tmi[k],s,&u); tmi[k] = u;
1109: } else if ( wmi[j] ) {
1110: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
1111: DMAR(wmi[j],inv,0,md,t)
1112: UTON(t,u);
1113: muln(m1,u,&s); tmi[k] = s;
1114: }
1115: k++;
1116: }
1117: m1 = m3;
1118: get_eg(&tmp1);
1119: add_eg(&eg_chrem,&tmp0,&tmp1);
1120: add_eg(&eg_chrem_split,&tmp0,&tmp1);
1121:
1122: get_eg(&tmp0);
1.38 noro 1123: if ( ind % F4_INTRAT_PERIOD )
1.13 noro 1124: ret = 0;
1125: else
1126: ret = intmtoratm(crmat,m1,*nm,dn);
1.1 noro 1127: get_eg(&tmp1);
1128: add_eg(&eg_intrat,&tmp0,&tmp1);
1129: add_eg(&eg_intrat_split,&tmp0,&tmp1);
1130: if ( ret ) {
1131: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1132: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
1133: for ( j = k = l = 0; j < col; j++ )
1134: if ( colstat[j] )
1135: rind[k++] = j;
1136: else
1137: cind[l++] = j;
1138: get_eg(&tmp0);
1.3 noro 1139: if ( gensolve_check(mat,*nm,*dn,rind,cind) ) {
1140: get_eg(&tmp1);
1141: add_eg(&eg_gschk,&tmp0,&tmp1);
1142: add_eg(&eg_gschk_split,&tmp0,&tmp1);
1.11 noro 1143: if ( DP_Print ) {
1.3 noro 1144: print_eg("Mod",&eg_mod_split);
1145: print_eg("Elim",&eg_elim_split);
1146: print_eg("ChRem",&eg_chrem_split);
1147: print_eg("IntRat",&eg_intrat_split);
1148: print_eg("Check",&eg_gschk_split);
1149: fflush(asir_out);
1150: }
1151: return rank;
1152: }
1153: }
1154: }
1155: }
1156: }
1157:
1.24 noro 1158: int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn,int **rindp,int **cindp)
1.3 noro 1159: {
1160: MAT bmat,xmat;
1161: Q **a0,**a,**b,**x,**nm;
1162: Q *ai,*bi,*xi;
1163: int row,col;
1164: int **w;
1165: int *wi;
1166: int **wc;
1167: Q mdq,q,s,u;
1168: N tn;
1169: int ind,md,i,j,k,l,li,ri,rank;
1170: unsigned int t;
1171: int *cinfo,*rinfo;
1172: int *rind,*cind;
1173: int count;
1174: struct oEGT eg_mul,eg_inv,tmp0,tmp1;
1.39 ! noro 1175: int period;
1.3 noro 1176:
1177: a0 = (Q **)mat->body;
1178: row = mat->row; col = mat->col;
1179: w = (int **)almat(row,col);
1180: for ( ind = 0; ; ind++ ) {
1.12 noro 1181: md = get_lprime(ind);
1.3 noro 1182: STOQ(md,mdq);
1183: for ( i = 0; i < row; i++ )
1184: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1185: if ( q = (Q)ai[j] ) {
1186: t = rem(NM(q),md);
1187: if ( t && SGN(q) < 0 )
1188: t = (md - t) % md;
1189: wi[j] = t;
1190: } else
1191: wi[j] = 0;
1192:
1.27 noro 1193: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1.3 noro 1194: a = (Q **)almat_pointer(rank,rank); /* lhs mat */
1195: MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
1196: for ( j = li = ri = 0; j < col; j++ )
1197: if ( cinfo[j] ) {
1198: /* the column is in lhs */
1199: for ( i = 0; i < rank; i++ ) {
1200: w[i][li] = w[i][j];
1201: a[i][li] = a0[rinfo[i]][j];
1202: }
1203: li++;
1204: } else {
1205: /* the column is in rhs */
1206: for ( i = 0; i < rank; i++ )
1207: b[i][ri] = a0[rinfo[i]][j];
1208: ri++;
1209: }
1210:
1211: /* solve Ax+B=0; A: rank x rank, B: rank x ri */
1212: MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
1213: MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
1214: /* use the right part of w as work area */
1215: /* ri = col - rank */
1216: wc = (int **)almat(rank,ri);
1217: for ( i = 0; i < rank; i++ )
1218: wc[i] = w[i]+rank;
1219: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1220: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
1221:
1222: init_eg(&eg_mul); init_eg(&eg_inv);
1.39 ! noro 1223: period = F4_INTRAT_PERIOD;
1.3 noro 1224: for ( q = ONE, count = 0; ; count++ ) {
1225: fprintf(stderr,".");
1226: /* wc = -b mod md */
1227: for ( i = 0; i < rank; i++ )
1228: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1229: if ( u = (Q)bi[j] ) {
1230: t = rem(NM(u),md);
1231: if ( t && SGN(u) > 0 )
1232: t = (md - t) % md;
1233: wi[j] = t;
1234: } else
1235: wi[j] = 0;
1236: /* wc = A^(-1)wc; wc is normalized */
1237: get_eg(&tmp0);
1238: solve_by_lu_mod(w,rank,md,wc,ri);
1.1 noro 1239: get_eg(&tmp1);
1.3 noro 1240: add_eg(&eg_inv,&tmp0,&tmp1);
1241: /* x = x-q*wc */
1242: for ( i = 0; i < rank; i++ )
1243: for ( j = 0, xi = x[i], wi = wc[i]; j < ri; j++ ) {
1244: STOQ(wi[j],u); mulq(q,u,&s);
1245: subq(xi[j],s,&u); xi[j] = u;
1246: }
1247: get_eg(&tmp0);
1248: for ( i = 0; i < rank; i++ )
1249: for ( j = 0; j < ri; j++ ) {
1250: inner_product_mat_int_mod(a,wc,rank,i,j,&u);
1251: addq(b[i][j],u,&s);
1252: if ( s ) {
1253: t = divin(NM(s),md,&tn);
1254: if ( t )
1255: error("generic_gauss_elim_hensel:incosistent");
1256: NTOQ(tn,SGN(s),b[i][j]);
1257: } else
1258: b[i][j] = 0;
1259: }
1260: get_eg(&tmp1);
1261: add_eg(&eg_mul,&tmp0,&tmp1);
1262: /* q = q*md */
1263: mulq(q,mdq,&u); q = u;
1.39 ! noro 1264: if ( !(count % period) )
! 1265: if ( intmtoratm_q(xmat,NM(q),*nmmat,dn) ) {
! 1266: for ( j = k = l = 0; j < col; j++ )
! 1267: if ( cinfo[j] )
! 1268: rind[k++] = j;
! 1269: else
! 1270: cind[l++] = j;
! 1271: if ( gensolve_check(mat,*nmmat,*dn,rind,cind) ) {
! 1272: fprintf(stderr,"\n");
! 1273: print_eg("INV",&eg_inv);
! 1274: print_eg("MUL",&eg_mul);
! 1275: fflush(asir_out);
! 1276: return rank;
! 1277: }
! 1278: } else
! 1279: period *=2;
1.1 noro 1280: }
1281: }
1282: }
1283:
1284: int f4_nocheck;
1285:
1.24 noro 1286: int gensolve_check(MAT mat,MAT nm,Q dn,int *rind,int *cind)
1.1 noro 1287: {
1288: int row,col,rank,clen,i,j,k,l;
1.24 noro 1289: Q s,t;
1.1 noro 1290: Q *w;
1291: Q *mati,*nmk;
1292:
1293: if ( f4_nocheck )
1294: return 1;
1295: row = mat->row; col = mat->col;
1296: rank = nm->row; clen = nm->col;
1297: w = (Q *)MALLOC(clen*sizeof(Q));
1298: for ( i = 0; i < row; i++ ) {
1299: mati = (Q *)mat->body[i];
1300: #if 1
1301: bzero(w,clen*sizeof(Q));
1302: for ( k = 0; k < rank; k++ )
1303: for ( l = 0, nmk = (Q *)nm->body[k]; l < clen; l++ ) {
1304: mulq(mati[rind[k]],nmk[l],&t);
1305: addq(w[l],t,&s); w[l] = s;
1306: }
1307: for ( j = 0; j < clen; j++ ) {
1308: mulq(dn,mati[cind[j]],&t);
1309: if ( cmpq(w[j],t) )
1310: break;
1311: }
1312: #else
1313: for ( j = 0; j < clen; j++ ) {
1314: for ( k = 0, s = 0; k < rank; k++ ) {
1315: mulq(mati[rind[k]],nm->body[k][j],&t);
1316: addq(s,t,&u); s = u;
1317: }
1318: mulq(dn,mati[cind[j]],&t);
1319: if ( cmpq(s,t) )
1320: break;
1321: }
1322: #endif
1323: if ( j != clen )
1324: break;
1325: }
1326: if ( i != row )
1327: return 0;
1328: else
1329: return 1;
1330: }
1331:
1332: /* assuming 0 < c < m */
1333:
1.24 noro 1334: int inttorat(N c,N m,N b,int *sgnp,N *nmp,N *dnp)
1.1 noro 1335: {
1.24 noro 1336: Q qq,t,u1,v1,r1;
1337: N q,u2,v2,r2;
1.1 noro 1338:
1339: u1 = 0; v1 = ONE; u2 = m; v2 = c;
1340: while ( cmpn(v2,b) >= 0 ) {
1341: divn(u2,v2,&q,&r2); u2 = v2; v2 = r2;
1342: NTOQ(q,1,qq); mulq(qq,v1,&t); subq(u1,t,&r1); u1 = v1; v1 = r1;
1343: }
1344: if ( cmpn(NM(v1),b) >= 0 )
1345: return 0;
1346: else {
1347: *nmp = v2;
1348: *dnp = NM(v1);
1349: *sgnp = SGN(v1);
1350: return 1;
1351: }
1352: }
1353:
1354: /* mat->body = N ** */
1355:
1.24 noro 1356: int intmtoratm(MAT mat,N md,MAT nm,Q *dn)
1.1 noro 1357: {
1358: N t,s,b;
1.24 noro 1359: Q dn0,dn1,nm1,q;
1.1 noro 1360: int i,j,k,l,row,col;
1361: Q **rmat;
1362: N **tmat;
1363: N *tmi;
1364: Q *nmk;
1365: N u,unm,udn;
1366: int sgn,ret;
1367:
1.3 noro 1368: if ( UNIN(md) )
1369: return 0;
1.1 noro 1370: row = mat->row; col = mat->col;
1371: bshiftn(md,1,&t);
1372: isqrt(t,&s);
1373: bshiftn(s,64,&b);
1374: if ( !b )
1375: b = ONEN;
1376: dn0 = ONE;
1377: tmat = (N **)mat->body;
1378: rmat = (Q **)nm->body;
1379: for ( i = 0; i < row; i++ )
1380: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1381: if ( tmi[j] ) {
1382: muln(tmi[j],NM(dn0),&s);
1383: remn(s,md,&u);
1384: ret = inttorat(u,md,b,&sgn,&unm,&udn);
1385: if ( !ret )
1386: return 0;
1387: else {
1388: NTOQ(unm,sgn,nm1);
1389: NTOQ(udn,1,dn1);
1390: if ( !UNIQ(dn1) ) {
1391: for ( k = 0; k < i; k++ )
1392: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1393: mulq(nmk[l],dn1,&q); nmk[l] = q;
1394: }
1395: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1396: mulq(nmk[l],dn1,&q); nmk[l] = q;
1397: }
1398: }
1399: rmat[i][j] = nm1;
1400: mulq(dn0,dn1,&q); dn0 = q;
1401: }
1402: }
1403: *dn = dn0;
1404: return 1;
1405: }
1406:
1.3 noro 1407: /* mat->body = Q ** */
1408:
1.24 noro 1409: int intmtoratm_q(MAT mat,N md,MAT nm,Q *dn)
1.3 noro 1410: {
1411: N t,s,b;
1.24 noro 1412: Q dn0,dn1,nm1,q;
1.3 noro 1413: int i,j,k,l,row,col;
1414: Q **rmat;
1415: Q **tmat;
1416: Q *tmi;
1417: Q *nmk;
1418: N u,unm,udn;
1419: int sgn,ret;
1420:
1421: if ( UNIN(md) )
1422: return 0;
1423: row = mat->row; col = mat->col;
1424: bshiftn(md,1,&t);
1425: isqrt(t,&s);
1426: bshiftn(s,64,&b);
1427: if ( !b )
1428: b = ONEN;
1429: dn0 = ONE;
1430: tmat = (Q **)mat->body;
1431: rmat = (Q **)nm->body;
1432: for ( i = 0; i < row; i++ )
1433: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1434: if ( tmi[j] ) {
1435: muln(NM(tmi[j]),NM(dn0),&s);
1436: remn(s,md,&u);
1437: ret = inttorat(u,md,b,&sgn,&unm,&udn);
1438: if ( !ret )
1439: return 0;
1440: else {
1441: if ( SGN(tmi[j])<0 )
1442: sgn = -sgn;
1443: NTOQ(unm,sgn,nm1);
1444: NTOQ(udn,1,dn1);
1445: if ( !UNIQ(dn1) ) {
1446: for ( k = 0; k < i; k++ )
1447: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1448: mulq(nmk[l],dn1,&q); nmk[l] = q;
1449: }
1450: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1451: mulq(nmk[l],dn1,&q); nmk[l] = q;
1452: }
1453: }
1454: rmat[i][j] = nm1;
1455: mulq(dn0,dn1,&q); dn0 = q;
1456: }
1457: }
1458: *dn = dn0;
1459: return 1;
1460: }
1461:
1.4 noro 1462: #define ONE_STEP1 if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1463:
1.24 noro 1464: void reduce_reducers_mod(int **mat,int row,int col,int md)
1.4 noro 1465: {
1466: int i,j,k,l,hc,zzz;
1467: int *t,*s,*tj,*ind;
1468:
1469: /* reduce the reducers */
1470: ind = (int *)ALLOCA(row*sizeof(int));
1471: for ( i = 0; i < row; i++ ) {
1472: t = mat[i];
1473: for ( j = 0; j < col && !t[j]; j++ );
1474: /* register the position of the head term */
1475: ind[i] = j;
1476: for ( l = i-1; l >= 0; l-- ) {
1477: /* reduce mat[i] by mat[l] */
1478: if ( hc = t[ind[l]] ) {
1479: /* mat[i] = mat[i]-hc*mat[l] */
1480: j = ind[l];
1481: s = mat[l]+j;
1482: tj = t+j;
1483: hc = md-hc;
1484: k = col-j;
1485: for ( ; k >= 64; k -= 64 ) {
1486: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1487: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1488: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1489: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1490: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1491: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1492: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1493: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1494: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1495: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1496: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1497: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1498: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1499: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1500: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1501: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1502: }
1.16 noro 1503: for ( ; k > 0; k-- ) {
1.4 noro 1504: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1505: }
1506: }
1507: }
1508: }
1509: }
1510:
1511: /*
1512: mat[i] : reducers (i=0,...,nred-1)
1513: spolys (i=nred,...,row-1)
1514: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1515: 1. reduce the reducers
1516: 2. reduce spolys by the reduced reducers
1517: */
1518:
1.24 noro 1519: void pre_reduce_mod(int **mat,int row,int col,int nred,int md)
1.4 noro 1520: {
1521: int i,j,k,l,hc,inv;
1522: int *t,*s,*tk,*ind;
1523:
1524: #if 1
1525: /* reduce the reducers */
1526: ind = (int *)ALLOCA(row*sizeof(int));
1527: for ( i = 0; i < nred; i++ ) {
1528: /* make mat[i] monic and mat[i] by mat[0],...,mat[i-1] */
1529: t = mat[i];
1530: for ( j = 0; j < col && !t[j]; j++ );
1531: /* register the position of the head term */
1532: ind[i] = j;
1533: inv = invm(t[j],md);
1534: for ( k = j; k < col; k++ )
1535: if ( t[k] )
1536: DMAR(t[k],inv,0,md,t[k])
1537: for ( l = i-1; l >= 0; l-- ) {
1538: /* reduce mat[i] by mat[l] */
1539: if ( hc = t[ind[l]] ) {
1540: /* mat[i] = mat[i]-hc*mat[l] */
1541: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
1542: k < col; k++, tk++, s++ )
1543: if ( *s )
1544: DMAR(*s,hc,*tk,md,*tk)
1545: }
1546: }
1547: }
1548: /* reduce the spolys */
1549: for ( i = nred; i < row; i++ ) {
1550: t = mat[i];
1551: for ( l = nred-1; l >= 0; l-- ) {
1552: /* reduce mat[i] by mat[l] */
1553: if ( hc = t[ind[l]] ) {
1554: /* mat[i] = mat[i]-hc*mat[l] */
1555: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
1556: k < col; k++, tk++, s++ )
1557: if ( *s )
1558: DMAR(*s,hc,*tk,md,*tk)
1559: }
1560: }
1561: }
1562: #endif
1563: }
1564: /*
1565: mat[i] : reducers (i=0,...,nred-1)
1566: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1567: */
1568:
1.24 noro 1569: void reduce_sp_by_red_mod(int *sp,int **redmat,int *ind,int nred,int col,int md)
1.4 noro 1570: {
1571: int i,j,k,hc,zzz;
1.24 noro 1572: int *s,*tj;
1.4 noro 1573:
1574: /* reduce the spolys by redmat */
1575: for ( i = nred-1; i >= 0; i-- ) {
1576: /* reduce sp by redmat[i] */
1577: if ( hc = sp[ind[i]] ) {
1578: /* sp = sp-hc*redmat[i] */
1579: j = ind[i];
1580: hc = md-hc;
1581: s = redmat[i]+j;
1582: tj = sp+j;
1.16 noro 1583: for ( k = col-j; k > 0; k-- ) {
1.4 noro 1584: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1.15 noro 1585: }
1586: }
1.17 noro 1587: }
1588: }
1589:
1590: /*
1.15 noro 1591: mat[i] : compressed reducers (i=0,...,nred-1)
1592: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1593: */
1594:
1.24 noro 1595: void red_by_compress(int m,unsigned int *p,unsigned int *r,
1596: unsigned int *ri,unsigned int hc,int len)
1.18 noro 1597: {
1.19 noro 1598: unsigned int up,lo;
1.18 noro 1599: unsigned int dmy;
1600: unsigned int *pj;
1601:
1.21 noro 1602: p[*ri] = 0; r++; ri++;
1603: for ( len--; len; len--, r++, ri++ ) {
1604: pj = p+ *ri;
1605: DMA(*r,hc,*pj,up,lo);
1.18 noro 1606: if ( up ) {
1607: DSAB(m,up,lo,dmy,*pj);
1608: } else
1609: *pj = lo;
1610: }
1611: }
1612:
1613: /* p -= hc*r */
1614:
1.24 noro 1615: void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1.18 noro 1616: {
1617: register unsigned int up,lo;
1618: unsigned int dmy;
1619:
1620: *p++ = 0; r++; len--;
1621: for ( ; len; len--, r++, p++ )
1622: if ( *r ) {
1.20 noro 1623: DMA(*r,hc,*p,up,lo);
1.18 noro 1624: if ( up ) {
1625: DSAB(m,up,lo,dmy,*p);
1626: } else
1627: *p = lo;
1628: }
1629: }
1630:
1.32 noro 1631: void red_by_vect_sf(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1632: {
1633: *p++ = 0; r++; len--;
1634: for ( ; len; len--, r++, p++ )
1635: if ( *r )
1636: *p = _addsf(_mulsf(*r,hc),*p);
1637: }
1638:
1.21 noro 1639: extern unsigned int **psca;
1640:
1.24 noro 1641: void reduce_sp_by_red_mod_compress (int *sp,CDP *redmat,int *ind,
1642: int nred,int col,int md)
1.15 noro 1643: {
1.24 noro 1644: int i,len;
1.15 noro 1645: CDP ri;
1.24 noro 1646: unsigned int hc;
1.18 noro 1647: unsigned int *usp;
1.15 noro 1648:
1.18 noro 1649: usp = (unsigned int *)sp;
1.15 noro 1650: /* reduce the spolys by redmat */
1651: for ( i = nred-1; i >= 0; i-- ) {
1652: /* reduce sp by redmat[i] */
1.18 noro 1653: usp[ind[i]] %= md;
1654: if ( hc = usp[ind[i]] ) {
1.15 noro 1655: /* sp = sp-hc*redmat[i] */
1656: hc = md-hc;
1657: ri = redmat[i];
1658: len = ri->len;
1.21 noro 1659: red_by_compress(md,usp,psca[ri->psindex],ri->body,hc,len);
1.4 noro 1660: }
1661: }
1.18 noro 1662: for ( i = 0; i < col; i++ )
1.24 noro 1663: if ( usp[i] >= (unsigned int)md )
1.18 noro 1664: usp[i] %= md;
1.4 noro 1665: }
1666:
1667: #define ONE_STEP2 if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
1668:
1.24 noro 1669: int generic_gauss_elim_mod(int **mat0,int row,int col,int md,int *colstat)
1.1 noro 1670: {
1.24 noro 1671: int i,j,k,l,inv,a,rank;
1672: unsigned int *t,*pivot,*pk;
1.18 noro 1673: unsigned int **mat;
1.1 noro 1674:
1.18 noro 1675: mat = (unsigned int **)mat0;
1.1 noro 1676: for ( rank = 0, j = 0; j < col; j++ ) {
1.18 noro 1677: for ( i = rank; i < row; i++ )
1678: mat[i][j] %= md;
1679: for ( i = rank; i < row; i++ )
1680: if ( mat[i][j] )
1681: break;
1.1 noro 1682: if ( i == row ) {
1683: colstat[j] = 0;
1684: continue;
1685: } else
1686: colstat[j] = 1;
1687: if ( i != rank ) {
1688: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
1689: }
1690: pivot = mat[rank];
1691: inv = invm(pivot[j],md);
1.4 noro 1692: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
1693: if ( *pk ) {
1.24 noro 1694: if ( *pk >= (unsigned int)md )
1.18 noro 1695: *pk %= md;
1.4 noro 1696: DMAR(*pk,inv,0,md,*pk)
1.1 noro 1697: }
1698: for ( i = rank+1; i < row; i++ ) {
1699: t = mat[i];
1.18 noro 1700: if ( a = t[j] )
1701: red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1 noro 1702: }
1703: rank++;
1704: }
1705: for ( j = col-1, l = rank-1; j >= 0; j-- )
1706: if ( colstat[j] ) {
1707: pivot = mat[l];
1708: for ( i = 0; i < l; i++ ) {
1709: t = mat[i];
1.18 noro 1710: t[j] %= md;
1711: if ( a = t[j] )
1712: red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1 noro 1713: }
1714: l--;
1.18 noro 1715: }
1716: for ( j = 0, l = 0; l < rank; j++ )
1717: if ( colstat[j] ) {
1718: t = mat[l];
1719: for ( k = j; k < col; k++ )
1.24 noro 1720: if ( t[k] >= (unsigned int)md )
1.18 noro 1721: t[k] %= md;
1722: l++;
1.32 noro 1723: }
1724: return rank;
1725: }
1726:
1727: int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat)
1728: {
1729: int i,j,k,l,inv,a,rank;
1730: unsigned int *t,*pivot,*pk;
1731: unsigned int **mat;
1732:
1733: mat = (unsigned int **)mat0;
1734: for ( rank = 0, j = 0; j < col; j++ ) {
1735: for ( i = rank; i < row; i++ )
1736: if ( mat[i][j] )
1737: break;
1738: if ( i == row ) {
1739: colstat[j] = 0;
1740: continue;
1741: } else
1742: colstat[j] = 1;
1743: if ( i != rank ) {
1744: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
1745: }
1746: pivot = mat[rank];
1747: inv = _invsf(pivot[j]);
1748: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
1749: if ( *pk )
1750: *pk = _mulsf(*pk,inv);
1751: for ( i = rank+1; i < row; i++ ) {
1752: t = mat[i];
1753: if ( a = t[j] )
1754: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
1755: }
1756: rank++;
1757: }
1758: for ( j = col-1, l = rank-1; j >= 0; j-- )
1759: if ( colstat[j] ) {
1760: pivot = mat[l];
1761: for ( i = 0; i < l; i++ ) {
1762: t = mat[i];
1763: if ( a = t[j] )
1764: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
1765: }
1766: l--;
1.1 noro 1767: }
1768: return rank;
1769: }
1770:
1771: /* LU decomposition; a[i][i] = 1/U[i][i] */
1772:
1.24 noro 1773: int lu_gfmmat(GFMMAT mat,unsigned int md,int *perm)
1.1 noro 1774: {
1775: int row,col;
1.24 noro 1776: int i,j,k;
1.1 noro 1777: unsigned int *t,*pivot;
1778: unsigned int **a;
1779: unsigned int inv,m;
1780:
1781: row = mat->row; col = mat->col;
1782: a = mat->body;
1783: bzero(perm,row*sizeof(int));
1784:
1785: for ( i = 0; i < row; i++ )
1786: perm[i] = i;
1787: for ( k = 0; k < col; k++ ) {
1788: for ( i = k; i < row && !a[i][k]; i++ );
1789: if ( i == row )
1790: return 0;
1791: if ( i != k ) {
1792: j = perm[i]; perm[i] = perm[k]; perm[k] = j;
1793: t = a[i]; a[i] = a[k]; a[k] = t;
1794: }
1795: pivot = a[k];
1796: pivot[k] = inv = invm(pivot[k],md);
1797: for ( i = k+1; i < row; i++ ) {
1798: t = a[i];
1799: if ( m = t[k] ) {
1800: DMAR(inv,m,0,md,t[k])
1801: for ( j = k+1, m = md - t[k]; j < col; j++ )
1802: if ( pivot[j] ) {
1.8 noro 1803: unsigned int tj;
1804:
1805: DMAR(m,pivot[j],t[j],md,tj)
1806: t[j] = tj;
1.1 noro 1807: }
1808: }
1809: }
1810: }
1811: return 1;
1812: }
1813:
1.3 noro 1814: /*
1815: Input
1816: a: a row x col matrix
1817: md : a modulus
1818:
1819: Output:
1820: return : d = the rank of mat
1821: a[0..(d-1)][0..(d-1)] : LU decomposition (a[i][i] = 1/U[i][i])
1822: rinfo: array of length row
1823: cinfo: array of length col
1824: i-th row in new a <-> rinfo[i]-th row in old a
1825: cinfo[j]=1 <=> j-th column is contained in the LU decomp.
1826: */
1827:
1.24 noro 1828: int find_lhs_and_lu_mod(unsigned int **a,int row,int col,
1829: unsigned int md,int **rinfo,int **cinfo)
1.3 noro 1830: {
1.24 noro 1831: int i,j,k,d;
1.3 noro 1832: int *rp,*cp;
1833: unsigned int *t,*pivot;
1834: unsigned int inv,m;
1835:
1836: *rinfo = rp = (int *)MALLOC_ATOMIC(row*sizeof(int));
1837: *cinfo = cp = (int *)MALLOC_ATOMIC(col*sizeof(int));
1838: for ( i = 0; i < row; i++ )
1839: rp[i] = i;
1840: for ( k = 0, d = 0; k < col; k++ ) {
1841: for ( i = d; i < row && !a[i][k]; i++ );
1842: if ( i == row ) {
1843: cp[k] = 0;
1844: continue;
1845: } else
1846: cp[k] = 1;
1847: if ( i != d ) {
1848: j = rp[i]; rp[i] = rp[d]; rp[d] = j;
1849: t = a[i]; a[i] = a[d]; a[d] = t;
1850: }
1851: pivot = a[d];
1852: pivot[k] = inv = invm(pivot[k],md);
1853: for ( i = d+1; i < row; i++ ) {
1854: t = a[i];
1855: if ( m = t[k] ) {
1856: DMAR(inv,m,0,md,t[k])
1857: for ( j = k+1, m = md - t[k]; j < col; j++ )
1858: if ( pivot[j] ) {
1.8 noro 1859: unsigned int tj;
1860: DMAR(m,pivot[j],t[j],md,tj)
1861: t[j] = tj;
1.3 noro 1862: }
1863: }
1864: }
1865: d++;
1866: }
1867: return d;
1868: }
1869:
1870: /*
1871: Input
1872: a : n x n matrix; a result of LU-decomposition
1873: md : modulus
1874: b : n x l matrix
1875: Output
1876: b = a^(-1)b
1877: */
1878:
1.24 noro 1879: void solve_by_lu_mod(int **a,int n,int md,int **b,int l)
1.3 noro 1880: {
1881: unsigned int *y,*c;
1882: int i,j,k;
1883: unsigned int t,m,m2;
1884:
1885: y = (int *)MALLOC_ATOMIC(n*sizeof(int));
1886: c = (int *)MALLOC_ATOMIC(n*sizeof(int));
1887: m2 = md>>1;
1888: for ( k = 0; k < l; k++ ) {
1889: /* copy b[.][k] to c */
1890: for ( i = 0; i < n; i++ )
1891: c[i] = (unsigned int)b[i][k];
1892: /* solve Ly=c */
1893: for ( i = 0; i < n; i++ ) {
1894: for ( t = c[i], j = 0; j < i; j++ )
1895: if ( a[i][j] ) {
1896: m = md - a[i][j];
1897: DMAR(m,y[j],t,md,t)
1898: }
1899: y[i] = t;
1900: }
1901: /* solve Uc=y */
1902: for ( i = n-1; i >= 0; i-- ) {
1903: for ( t = y[i], j =i+1; j < n; j++ )
1904: if ( a[i][j] ) {
1905: m = md - a[i][j];
1906: DMAR(m,c[j],t,md,t)
1907: }
1908: /* a[i][i] = 1/U[i][i] */
1909: DMAR(t,a[i][i],0,md,c[i])
1910: }
1911: /* copy c to b[.][k] with normalization */
1912: for ( i = 0; i < n; i++ )
1913: b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]);
1914: }
1915: }
1916:
1.24 noro 1917: void Pleqm1(NODE arg,VECT *rp)
1.1 noro 1918: {
1919: MAT m;
1920: VECT vect;
1921: pointer **mat;
1922: Q *v;
1923: Q q;
1924: int **wmat;
1925: int md,i,j,row,col,t,n,status;
1926:
1927: asir_assert(ARG0(arg),O_MAT,"leqm1");
1928: asir_assert(ARG1(arg),O_N,"leqm1");
1929: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
1930: row = m->row; col = m->col; mat = m->body;
1931: wmat = (int **)almat(row,col);
1932: for ( i = 0; i < row; i++ )
1933: for ( j = 0; j < col; j++ )
1934: if ( q = (Q)mat[i][j] ) {
1935: t = rem(NM(q),md);
1936: if ( SGN(q) < 0 )
1937: t = (md - t) % md;
1938: wmat[i][j] = t;
1939: } else
1940: wmat[i][j] = 0;
1941: status = gauss_elim_mod1(wmat,row,col,md);
1942: if ( status < 0 )
1943: *rp = 0;
1944: else if ( status > 0 )
1945: *rp = (VECT)ONE;
1946: else {
1947: n = col - 1;
1948: MKVECT(vect,n);
1949: for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
1950: t = (md-wmat[i][n])%md; STOQ(t,v[i]);
1951: }
1952: *rp = vect;
1953: }
1954: }
1955:
1.24 noro 1956: int gauss_elim_mod1(int **mat,int row,int col,int md)
1.1 noro 1957: {
1958: int i,j,k,inv,a,n;
1959: int *t,*pivot;
1960:
1961: n = col - 1;
1962: for ( j = 0; j < n; j++ ) {
1963: for ( i = j; i < row && !mat[i][j]; i++ );
1964: if ( i == row )
1965: return 1;
1966: if ( i != j ) {
1967: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
1968: }
1969: pivot = mat[j];
1970: inv = invm(pivot[j],md);
1971: for ( k = j; k <= n; k++ )
1972: pivot[k] = dmar(pivot[k],inv,0,md);
1973: for ( i = j+1; i < row; i++ ) {
1974: t = mat[i];
1975: if ( i != j && (a = t[j]) )
1976: for ( k = j, a = md - a; k <= n; k++ )
1977: t[k] = dmar(pivot[k],a,t[k],md);
1978: }
1979: }
1980: for ( i = n; i < row && !mat[i][n]; i++ );
1981: if ( i == row ) {
1982: for ( j = n-1; j >= 0; j-- ) {
1983: for ( i = j-1, a = (md-mat[j][n])%md; i >= 0; i-- ) {
1984: mat[i][n] = dmar(mat[i][j],a,mat[i][n],md);
1985: mat[i][j] = 0;
1986: }
1987: }
1988: return 0;
1989: } else
1990: return -1;
1991: }
1992:
1.24 noro 1993: void Pgeninvm(NODE arg,LIST *rp)
1.1 noro 1994: {
1995: MAT m;
1996: pointer **mat;
1997: Q **tmat;
1998: Q q;
1999: unsigned int **wmat;
2000: int md,i,j,row,col,t,status;
2001: MAT mat1,mat2;
2002: NODE node1,node2;
2003:
2004: asir_assert(ARG0(arg),O_MAT,"leqm1");
2005: asir_assert(ARG1(arg),O_N,"leqm1");
2006: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2007: row = m->row; col = m->col; mat = m->body;
2008: wmat = (unsigned int **)almat(row,col+row);
2009: for ( i = 0; i < row; i++ ) {
2010: bzero((char *)wmat[i],(col+row)*sizeof(int));
2011: for ( j = 0; j < col; j++ )
2012: if ( q = (Q)mat[i][j] ) {
2013: t = rem(NM(q),md);
2014: if ( SGN(q) < 0 )
2015: t = (md - t) % md;
2016: wmat[i][j] = t;
2017: }
2018: wmat[i][col+i] = 1;
2019: }
2020: status = gauss_elim_geninv_mod(wmat,row,col,md);
2021: if ( status > 0 )
2022: *rp = 0;
2023: else {
2024: MKMAT(mat1,col,row); MKMAT(mat2,row-col,row);
2025: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
2026: for ( j = 0; j < row; j++ )
1.24 noro 2027: UTOQ(wmat[i][j+col],tmat[i][j]);
1.1 noro 2028: for ( tmat = (Q **)mat2->body; i < row; i++ )
2029: for ( j = 0; j < row; j++ )
1.24 noro 2030: UTOQ(wmat[i][j+col],tmat[i-col][j]);
1.1 noro 2031: MKNODE(node2,mat2,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2032: }
2033: }
2034:
1.24 noro 2035: int gauss_elim_geninv_mod(unsigned int **mat,int row,int col,int md)
1.1 noro 2036: {
2037: int i,j,k,inv,a,n,m;
2038: unsigned int *t,*pivot;
2039:
2040: n = col; m = row+col;
2041: for ( j = 0; j < n; j++ ) {
2042: for ( i = j; i < row && !mat[i][j]; i++ );
2043: if ( i == row )
2044: return 1;
2045: if ( i != j ) {
2046: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2047: }
2048: pivot = mat[j];
2049: inv = invm(pivot[j],md);
2050: for ( k = j; k < m; k++ )
2051: pivot[k] = dmar(pivot[k],inv,0,md);
2052: for ( i = j+1; i < row; i++ ) {
2053: t = mat[i];
2054: if ( a = t[j] )
2055: for ( k = j, a = md - a; k < m; k++ )
2056: t[k] = dmar(pivot[k],a,t[k],md);
2057: }
2058: }
2059: for ( j = n-1; j >= 0; j-- ) {
2060: pivot = mat[j];
2061: for ( i = j-1; i >= 0; i-- ) {
2062: t = mat[i];
2063: if ( a = t[j] )
2064: for ( k = j, a = md - a; k < m; k++ )
2065: t[k] = dmar(pivot[k],a,t[k],md);
2066: }
2067: }
2068: return 0;
2069: }
2070:
1.24 noro 2071: void Psolve_by_lu_gfmmat(NODE arg,VECT *rp)
1.1 noro 2072: {
2073: GFMMAT lu;
2074: Q *perm,*rhs,*v;
2075: int n,i;
2076: unsigned int md;
2077: unsigned int *b,*sol;
2078: VECT r;
2079:
2080: lu = (GFMMAT)ARG0(arg);
2081: perm = (Q *)BDY((VECT)ARG1(arg));
2082: rhs = (Q *)BDY((VECT)ARG2(arg));
2083: md = (unsigned int)QTOS((Q)ARG3(arg));
2084: n = lu->col;
2085: b = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2086: sol = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2087: for ( i = 0; i < n; i++ )
2088: b[i] = QTOS(rhs[QTOS(perm[i])]);
2089: solve_by_lu_gfmmat(lu,md,b,sol);
2090: MKVECT(r,n);
2091: for ( i = 0, v = (Q *)r->body; i < n; i++ )
1.24 noro 2092: UTOQ(sol[i],v[i]);
1.1 noro 2093: *rp = r;
2094: }
2095:
1.24 noro 2096: void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md,
2097: unsigned int *b,unsigned int *x)
1.1 noro 2098: {
2099: int n;
2100: unsigned int **a;
2101: unsigned int *y;
2102: int i,j;
2103: unsigned int t,m;
2104:
2105: n = lu->col;
2106: a = lu->body;
2107: y = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2108: /* solve Ly=b */
2109: for ( i = 0; i < n; i++ ) {
2110: for ( t = b[i], j = 0; j < i; j++ )
2111: if ( a[i][j] ) {
2112: m = md - a[i][j];
2113: DMAR(m,y[j],t,md,t)
2114: }
2115: y[i] = t;
2116: }
2117: /* solve Ux=y */
2118: for ( i = n-1; i >= 0; i-- ) {
2119: for ( t = y[i], j =i+1; j < n; j++ )
2120: if ( a[i][j] ) {
2121: m = md - a[i][j];
2122: DMAR(m,x[j],t,md,t)
2123: }
2124: /* a[i][i] = 1/U[i][i] */
2125: DMAR(t,a[i][i],0,md,x[i])
2126: }
2127: }
2128:
1.24 noro 2129: void Plu_gfmmat(NODE arg,LIST *rp)
1.1 noro 2130: {
2131: MAT m;
2132: GFMMAT mm;
2133: unsigned int md;
2134: int i,row,col,status;
2135: int *iperm;
2136: Q *v;
2137: VECT perm;
2138: NODE n0;
2139:
2140: asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
2141: asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
2142: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
2143: mat_to_gfmmat(m,md,&mm);
2144: row = m->row;
2145: col = m->col;
2146: iperm = (int *)MALLOC_ATOMIC(row*sizeof(int));
2147: status = lu_gfmmat(mm,md,iperm);
2148: if ( !status )
2149: n0 = 0;
2150: else {
2151: MKVECT(perm,row);
2152: for ( i = 0, v = (Q *)perm->body; i < row; i++ )
2153: STOQ(iperm[i],v[i]);
2154: n0 = mknode(2,mm,perm);
2155: }
2156: MKLIST(*rp,n0);
2157: }
2158:
1.24 noro 2159: void Pmat_to_gfmmat(NODE arg,GFMMAT *rp)
1.1 noro 2160: {
2161: MAT m;
2162: unsigned int md;
2163:
2164: asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
2165: asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
2166: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
2167: mat_to_gfmmat(m,md,rp);
2168: }
2169:
1.24 noro 2170: void mat_to_gfmmat(MAT m,unsigned int md,GFMMAT *rp)
1.1 noro 2171: {
2172: unsigned int **wmat;
2173: unsigned int t;
2174: Q **mat;
2175: Q q;
2176: int i,j,row,col;
2177:
2178: row = m->row; col = m->col; mat = (Q **)m->body;
2179: wmat = (unsigned int **)almat(row,col);
2180: for ( i = 0; i < row; i++ ) {
2181: bzero((char *)wmat[i],col*sizeof(unsigned int));
2182: for ( j = 0; j < col; j++ )
2183: if ( q = mat[i][j] ) {
2184: t = (unsigned int)rem(NM(q),md);
2185: if ( SGN(q) < 0 )
2186: t = (md - t) % md;
2187: wmat[i][j] = t;
2188: }
2189: }
2190: TOGFMMAT(row,col,wmat,*rp);
2191: }
2192:
1.27 noro 2193: void Pgeninvm_swap(arg,rp)
2194: NODE arg;
2195: LIST *rp;
1.1 noro 2196: {
2197: MAT m;
2198: pointer **mat;
2199: Q **tmat;
2200: Q *tvect;
2201: Q q;
2202: unsigned int **wmat,**invmat;
2203: int *index;
2204: unsigned int t,md;
2205: int i,j,row,col,status;
2206: MAT mat1;
2207: VECT vect1;
2208: NODE node1,node2;
2209:
2210: asir_assert(ARG0(arg),O_MAT,"geninvm_swap");
2211: asir_assert(ARG1(arg),O_N,"geninvm_swap");
2212: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2213: row = m->row; col = m->col; mat = m->body;
2214: wmat = (unsigned int **)almat(row,col+row);
2215: for ( i = 0; i < row; i++ ) {
2216: bzero((char *)wmat[i],(col+row)*sizeof(int));
2217: for ( j = 0; j < col; j++ )
2218: if ( q = (Q)mat[i][j] ) {
2219: t = (unsigned int)rem(NM(q),md);
2220: if ( SGN(q) < 0 )
2221: t = (md - t) % md;
2222: wmat[i][j] = t;
2223: }
2224: wmat[i][col+i] = 1;
2225: }
2226: status = gauss_elim_geninv_mod_swap(wmat,row,col,md,&invmat,&index);
2227: if ( status > 0 )
2228: *rp = 0;
2229: else {
2230: MKMAT(mat1,col,col);
2231: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
2232: for ( j = 0; j < col; j++ )
2233: UTOQ(invmat[i][j],tmat[i][j]);
2234: MKVECT(vect1,row);
2235: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
2236: STOQ(index[i],tvect[i]);
2237: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2238: }
2239: }
2240:
1.27 noro 2241: gauss_elim_geninv_mod_swap(mat,row,col,md,invmatp,indexp)
2242: unsigned int **mat;
2243: int row,col;
2244: unsigned int md;
2245: unsigned int ***invmatp;
2246: int **indexp;
1.1 noro 2247: {
2248: int i,j,k,inv,a,n,m;
2249: unsigned int *t,*pivot,*s;
2250: int *index;
2251: unsigned int **invmat;
2252:
2253: n = col; m = row+col;
2254: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
2255: for ( i = 0; i < row; i++ )
2256: index[i] = i;
2257: for ( j = 0; j < n; j++ ) {
2258: for ( i = j; i < row && !mat[i][j]; i++ );
2259: if ( i == row ) {
2260: *indexp = 0; *invmatp = 0; return 1;
2261: }
2262: if ( i != j ) {
2263: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2264: k = index[i]; index[i] = index[j]; index[j] = k;
2265: }
2266: pivot = mat[j];
2267: inv = (unsigned int)invm(pivot[j],md);
2268: for ( k = j; k < m; k++ )
2269: if ( pivot[k] )
2270: pivot[k] = (unsigned int)dmar(pivot[k],inv,0,md);
2271: for ( i = j+1; i < row; i++ ) {
2272: t = mat[i];
2273: if ( a = t[j] )
2274: for ( k = j, a = md - a; k < m; k++ )
2275: if ( pivot[k] )
2276: t[k] = dmar(pivot[k],a,t[k],md);
2277: }
2278: }
2279: for ( j = n-1; j >= 0; j-- ) {
2280: pivot = mat[j];
2281: for ( i = j-1; i >= 0; i-- ) {
2282: t = mat[i];
2283: if ( a = t[j] )
2284: for ( k = j, a = md - a; k < m; k++ )
2285: if ( pivot[k] )
2286: t[k] = dmar(pivot[k],a,t[k],md);
2287: }
2288: }
2289: *invmatp = invmat = (unsigned int **)almat(col,col);
1.27 noro 2290: for ( i = 0; i < col; i++ )
2291: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
2292: s[j] = t[col+index[j]];
2293: return 0;
2294: }
2295:
2296: void Pgeninv_sf_swap(NODE arg,LIST *rp)
2297: {
2298: MAT m;
2299: GFS **mat,**tmat;
2300: Q *tvect;
2301: GFS q;
2302: int **wmat,**invmat;
2303: int *index;
2304: unsigned int t;
2305: int i,j,row,col,status;
2306: MAT mat1;
2307: VECT vect1;
2308: NODE node1,node2;
2309:
2310: asir_assert(ARG0(arg),O_MAT,"geninv_sf_swap");
2311: m = (MAT)ARG0(arg);
2312: row = m->row; col = m->col; mat = (GFS **)m->body;
2313: wmat = (int **)almat(row,col+row);
2314: for ( i = 0; i < row; i++ ) {
2315: bzero((char *)wmat[i],(col+row)*sizeof(int));
2316: for ( j = 0; j < col; j++ )
2317: if ( q = (GFS)mat[i][j] )
2318: wmat[i][j] = FTOIF(CONT(q));
2319: wmat[i][col+i] = _onesf();
2320: }
2321: status = gauss_elim_geninv_sf_swap(wmat,row,col,&invmat,&index);
2322: if ( status > 0 )
2323: *rp = 0;
2324: else {
2325: MKMAT(mat1,col,col);
2326: for ( i = 0, tmat = (GFS **)mat1->body; i < col; i++ )
2327: for ( j = 0; j < col; j++ )
2328: if ( t = invmat[i][j] ) {
2329: MKGFS(IFTOF(t),tmat[i][j]);
2330: }
2331: MKVECT(vect1,row);
2332: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
2333: STOQ(index[i],tvect[i]);
2334: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2335: }
2336: }
2337:
2338: int gauss_elim_geninv_sf_swap(int **mat,int row,int col,
2339: int ***invmatp,int **indexp)
2340: {
2341: int i,j,k,inv,a,n,m,u;
2342: int *t,*pivot,*s;
2343: int *index;
2344: int **invmat;
2345:
2346: n = col; m = row+col;
2347: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
2348: for ( i = 0; i < row; i++ )
2349: index[i] = i;
2350: for ( j = 0; j < n; j++ ) {
2351: for ( i = j; i < row && !mat[i][j]; i++ );
2352: if ( i == row ) {
2353: *indexp = 0; *invmatp = 0; return 1;
2354: }
2355: if ( i != j ) {
2356: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2357: k = index[i]; index[i] = index[j]; index[j] = k;
2358: }
2359: pivot = mat[j];
2360: inv = _invsf(pivot[j]);
2361: for ( k = j; k < m; k++ )
2362: if ( pivot[k] )
2363: pivot[k] = _mulsf(pivot[k],inv);
2364: for ( i = j+1; i < row; i++ ) {
2365: t = mat[i];
2366: if ( a = t[j] )
2367: for ( k = j, a = _chsgnsf(a); k < m; k++ )
2368: if ( pivot[k] ) {
2369: u = _mulsf(pivot[k],a);
2370: t[k] = _addsf(u,t[k]);
2371: }
2372: }
2373: }
2374: for ( j = n-1; j >= 0; j-- ) {
2375: pivot = mat[j];
2376: for ( i = j-1; i >= 0; i-- ) {
2377: t = mat[i];
2378: if ( a = t[j] )
2379: for ( k = j, a = _chsgnsf(a); k < m; k++ )
2380: if ( pivot[k] ) {
2381: u = _mulsf(pivot[k],a);
2382: t[k] = _addsf(u,t[k]);
2383: }
2384: }
2385: }
2386: *invmatp = invmat = (int **)almat(col,col);
1.1 noro 2387: for ( i = 0; i < col; i++ )
2388: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
2389: s[j] = t[col+index[j]];
2390: return 0;
2391: }
2392:
2393: void _addn(N,N,N);
2394: int _subn(N,N,N);
2395: void _muln(N,N,N);
2396:
1.24 noro 2397: void inner_product_int(Q *a,Q *b,int n,Q *r)
1.1 noro 2398: {
2399: int la,lb,i;
2400: int sgn,sgn1;
2401: N wm,wma,sum,t;
2402:
2403: for ( la = lb = 0, i = 0; i < n; i++ ) {
2404: if ( a[i] )
2405: if ( DN(a[i]) )
2406: error("inner_product_int : invalid argument");
2407: else
2408: la = MAX(PL(NM(a[i])),la);
2409: if ( b[i] )
2410: if ( DN(b[i]) )
2411: error("inner_product_int : invalid argument");
2412: else
2413: lb = MAX(PL(NM(b[i])),lb);
2414: }
2415: sgn = 0;
2416: sum= NALLOC(la+lb+2);
2417: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
2418: wm = NALLOC(la+lb+2);
2419: wma = NALLOC(la+lb+2);
2420: for ( i = 0; i < n; i++ ) {
2421: if ( !a[i] || !b[i] )
2422: continue;
2423: _muln(NM(a[i]),NM(b[i]),wm);
2424: sgn1 = SGN(a[i])*SGN(b[i]);
2425: if ( !sgn ) {
2426: sgn = sgn1;
2427: t = wm; wm = sum; sum = t;
2428: } else if ( sgn == sgn1 ) {
2429: _addn(sum,wm,wma);
2430: if ( !PL(wma) )
2431: sgn = 0;
2432: t = wma; wma = sum; sum = t;
2433: } else {
2434: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
2435: sgn *= _subn(sum,wm,wma);
2436: t = wma; wma = sum; sum = t;
2437: }
2438: }
2439: GC_free(wm);
2440: GC_free(wma);
2441: if ( !sgn ) {
2442: GC_free(sum);
2443: *r = 0;
2444: } else
2445: NTOQ(sum,sgn,*r);
2446: }
2447:
1.3 noro 2448: /* (k,l) element of a*b where a: .x n matrix, b: n x . integer matrix */
2449:
1.24 noro 2450: void inner_product_mat_int_mod(Q **a,int **b,int n,int k,int l,Q *r)
1.3 noro 2451: {
2452: int la,lb,i;
2453: int sgn,sgn1;
2454: N wm,wma,sum,t;
2455: Q aki;
2456: int bil,bilsgn;
2457: struct oN tn;
2458:
2459: for ( la = 0, i = 0; i < n; i++ ) {
2460: if ( aki = a[k][i] )
2461: if ( DN(aki) )
2462: error("inner_product_int : invalid argument");
2463: else
2464: la = MAX(PL(NM(aki)),la);
2465: }
2466: lb = 1;
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 ( !(aki = a[k][i]) || !(bil = b[i][l]) )
2474: continue;
2475: tn.p = 1;
2476: if ( bil > 0 ) {
2477: tn.b[0] = bil; bilsgn = 1;
2478: } else {
2479: tn.b[0] = -bil; bilsgn = -1;
2480: }
2481: _muln(NM(aki),&tn,wm);
2482: sgn1 = SGN(aki)*bilsgn;
2483: if ( !sgn ) {
2484: sgn = sgn1;
2485: t = wm; wm = sum; sum = t;
2486: } else if ( sgn == sgn1 ) {
2487: _addn(sum,wm,wma);
2488: if ( !PL(wma) )
2489: sgn = 0;
2490: t = wma; wma = sum; sum = t;
2491: } else {
2492: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
2493: sgn *= _subn(sum,wm,wma);
2494: t = wma; wma = sum; sum = t;
2495: }
2496: }
2497: GC_free(wm);
2498: GC_free(wma);
2499: if ( !sgn ) {
2500: GC_free(sum);
2501: *r = 0;
2502: } else
2503: NTOQ(sum,sgn,*r);
2504: }
2505:
1.24 noro 2506: void Pmul_mat_vect_int(NODE arg,VECT *rp)
1.1 noro 2507: {
2508: MAT mat;
2509: VECT vect,r;
2510: int row,col,i;
2511:
2512: mat = (MAT)ARG0(arg);
2513: vect = (VECT)ARG1(arg);
2514: row = mat->row;
2515: col = mat->col;
2516: MKVECT(r,row);
1.24 noro 2517: for ( i = 0; i < row; i++ ) {
2518: inner_product_int((Q *)mat->body[i],(Q *)vect->body,col,(Q *)&r->body[i]);
2519: }
1.1 noro 2520: *rp = r;
2521: }
2522:
1.24 noro 2523: void Pnbpoly_up2(NODE arg,GF2N *rp)
1.1 noro 2524: {
2525: int m,type,ret;
2526: UP2 r;
2527:
2528: m = QTOS((Q)ARG0(arg));
2529: type = QTOS((Q)ARG1(arg));
2530: ret = generate_ONB_polynomial(&r,m,type);
2531: if ( ret == 0 )
2532: MKGF2N(r,*rp);
2533: else
2534: *rp = 0;
2535: }
2536:
1.24 noro 2537: void Px962_irredpoly_up2(NODE arg,GF2N *rp)
1.1 noro 2538: {
1.24 noro 2539: int m,ret,w;
1.1 noro 2540: GF2N prev;
2541: UP2 r;
2542:
2543: m = QTOS((Q)ARG0(arg));
2544: prev = (GF2N)ARG1(arg);
2545: if ( !prev ) {
2546: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2547: bzero((char *)r->b,w*sizeof(unsigned int));
2548: } else {
2549: r = prev->body;
2550: if ( degup2(r) != m ) {
2551: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2552: bzero((char *)r->b,w*sizeof(unsigned int));
2553: }
2554: }
1.24 noro 2555: ret = _generate_irreducible_polynomial(r,m);
1.1 noro 2556: if ( ret == 0 )
2557: MKGF2N(r,*rp);
2558: else
2559: *rp = 0;
2560: }
2561:
1.24 noro 2562: void Pirredpoly_up2(NODE arg,GF2N *rp)
1.1 noro 2563: {
1.24 noro 2564: int m,ret,w;
1.1 noro 2565: GF2N prev;
2566: UP2 r;
2567:
2568: m = QTOS((Q)ARG0(arg));
2569: prev = (GF2N)ARG1(arg);
2570: if ( !prev ) {
2571: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2572: bzero((char *)r->b,w*sizeof(unsigned int));
2573: } else {
2574: r = prev->body;
2575: if ( degup2(r) != m ) {
2576: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
2577: bzero((char *)r->b,w*sizeof(unsigned int));
2578: }
2579: }
1.24 noro 2580: ret = _generate_good_irreducible_polynomial(r,m);
1.1 noro 2581: if ( ret == 0 )
2582: MKGF2N(r,*rp);
2583: else
2584: *rp = 0;
2585: }
2586:
1.26 noro 2587: void Pmat_swap_row_destructive(NODE arg, MAT *m)
2588: {
2589: int i1,i2;
2590: pointer *t;
2591: MAT mat;
2592:
2593: asir_assert(ARG0(arg),O_MAT,"mat_swap_row_destructive");
2594: asir_assert(ARG1(arg),O_N,"mat_swap_row_destructive");
2595: asir_assert(ARG2(arg),O_N,"mat_swap_row_destructive");
2596: mat = (MAT)ARG0(arg);
2597: i1 = QTOS((Q)ARG1(arg));
2598: i2 = QTOS((Q)ARG2(arg));
2599: if ( i1 < 0 || i2 < 0 || i1 >= mat->row || i2 >= mat->row )
2600: error("mat_swap_row_destructive : Out of range");
2601: t = mat->body[i1];
2602: mat->body[i1] = mat->body[i2];
2603: mat->body[i2] = t;
2604: *m = mat;
2605: }
2606:
2607: void Pmat_swap_col_destructive(NODE arg, MAT *m)
2608: {
2609: int j1,j2,i,n;
2610: pointer *mi;
2611: pointer t;
2612: MAT mat;
2613:
2614: asir_assert(ARG0(arg),O_MAT,"mat_swap_col_destructive");
2615: asir_assert(ARG1(arg),O_N,"mat_swap_col_destructive");
2616: asir_assert(ARG2(arg),O_N,"mat_swap_col_destructive");
2617: mat = (MAT)ARG0(arg);
2618: j1 = QTOS((Q)ARG1(arg));
2619: j2 = QTOS((Q)ARG2(arg));
2620: if ( j1 < 0 || j2 < 0 || j1 >= mat->col || j2 >= mat->col )
2621: error("mat_swap_col_destructive : Out of range");
2622: n = mat->row;
2623: for ( i = 0; i < n; i++ ) {
2624: mi = mat->body[i];
2625: t = mi[j1]; mi[j1] = mi[j2]; mi[j2] = t;
2626: }
2627: *m = mat;
2628: }
1.1 noro 2629: /*
2630: * f = type 'type' normal polynomial of degree m if exists
2631: * IEEE P1363 A.7.2
2632: *
2633: * return value : 0 --- exists
2634: * 1 --- does not exist
2635: * -1 --- failure (memory allocation error)
2636: */
2637:
2638: int generate_ONB_polynomial(UP2 *rp,int m,int type)
2639: {
2640: int i,r;
2641: int w;
2642: UP2 f,f0,f1,f2,t;
2643:
2644: w = (m>>5)+1;
2645: switch ( type ) {
2646: case 1:
2647: if ( !TypeT_NB_check(m,1) ) return 1;
2648: NEWUP2(f,w); *rp = f; f->w = w;
2649: /* set all the bits */
2650: for ( i = 0; i < w; i++ )
2651: f->b[i] = 0xffffffff;
2652: /* mask the top word if necessary */
2653: if ( r = (m+1)&31 )
2654: f->b[w-1] &= (1<<r)-1;
2655: return 0;
2656: break;
2657: case 2:
2658: if ( !TypeT_NB_check(m,2) ) return 1;
2659: NEWUP2(f,w); *rp = f;
2660: W_NEWUP2(f0,w);
2661: W_NEWUP2(f1,w);
2662: W_NEWUP2(f2,w);
2663:
2664: /* recursion for genrating Type II normal polynomial */
2665:
2666: /* f0 = 1, f1 = t+1 */
2667: f0->w = 1; f0->b[0] = 1;
2668: f1->w = 1; f1->b[0] = 3;
2669: for ( i = 2; i <= m; i++ ) {
2670: /* f2 = t*f1+f0 */
2671: _bshiftup2(f1,-1,f2);
2672: _addup2_destructive(f2,f0);
2673: /* cyclic change of the variables */
2674: t = f0; f0 = f1; f1 = f2; f2 = t;
2675: }
2676: _copyup2(f1,f);
2677: return 0;
2678: break;
2679: default:
2680: return -1;
2681: break;
2682: }
2683: }
2684:
2685: /*
2686: * f = an irreducible trinomial or pentanomial of degree d 'after' f
2687: * return value : 0 --- exists
2688: * 1 --- does not exist (exhaustion)
2689: */
2690:
2691: int _generate_irreducible_polynomial(UP2 f,int d)
2692: {
2693: int ret,i,j,k,nz,i0,j0,k0;
2694: int w;
2695: unsigned int *fd;
2696:
2697: /*
2698: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
2699: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
2700: * otherwise i0,j0,k0 is set to 0.
2701: */
2702:
2703: fd = f->b;
2704: w = (d>>5)+1;
2705: if ( f->w && (d==degup2(f)) ) {
2706: for ( nz = 0, i = d; i >= 0; i-- )
2707: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
2708: switch ( nz ) {
2709: case 3:
2710: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2711: /* reset i0-th bit */
2712: fd[i0>>5] &= ~(1<<(i0&31));
2713: j0 = k0 = 0;
2714: break;
2715: case 5:
2716: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2717: /* reset i0-th bit */
2718: fd[i0>>5] &= ~(1<<(i0&31));
2719: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
2720: /* reset j0-th bit */
2721: fd[j0>>5] &= ~(1<<(j0&31));
2722: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
2723: /* reset k0-th bit */
2724: fd[k0>>5] &= ~(1<<(k0&31));
2725: break;
2726: default:
2727: f->w = 0; break;
2728: }
2729: } else
2730: f->w = 0;
2731:
2732: if ( !f->w ) {
2733: fd = f->b;
2734: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
2735: i0 = j0 = k0 = 0;
2736: }
2737: /* if j0 > 0 then f is already a pentanomial */
2738: if ( j0 > 0 ) goto PENTA;
2739:
2740: /* searching for an irreducible trinomial */
2741:
2742: for ( i = 1; 2*i <= d; i++ ) {
2743: /* skip the polynomials 'before' f */
2744: if ( i < i0 ) continue;
2745: if ( i == i0 ) { i0 = 0; continue; }
2746: /* set i-th bit */
2747: fd[i>>5] |= (1<<(i&31));
2748: ret = irredcheck_dddup2(f);
2749: if ( ret == 1 ) return 0;
2750: /* reset i-th bit */
2751: fd[i>>5] &= ~(1<<(i&31));
2752: }
2753:
2754: /* searching for an irreducible pentanomial */
2755: PENTA:
2756: for ( i = 1; i < d; i++ ) {
2757: /* skip the polynomials 'before' f */
2758: if ( i < i0 ) continue;
2759: if ( i == i0 ) i0 = 0;
2760: /* set i-th bit */
2761: fd[i>>5] |= (1<<(i&31));
2762: for ( j = i+1; j < d; j++ ) {
2763: /* skip the polynomials 'before' f */
2764: if ( j < j0 ) continue;
2765: if ( j == j0 ) j0 = 0;
2766: /* set j-th bit */
2767: fd[j>>5] |= (1<<(j&31));
2768: for ( k = j+1; k < d; k++ ) {
2769: /* skip the polynomials 'before' f */
2770: if ( k < k0 ) continue;
2771: else if ( k == k0 ) { k0 = 0; continue; }
2772: /* set k-th bit */
2773: fd[k>>5] |= (1<<(k&31));
2774: ret = irredcheck_dddup2(f);
2775: if ( ret == 1 ) return 0;
2776: /* reset k-th bit */
2777: fd[k>>5] &= ~(1<<(k&31));
2778: }
2779: /* reset j-th bit */
2780: fd[j>>5] &= ~(1<<(j&31));
2781: }
2782: /* reset i-th bit */
2783: fd[i>>5] &= ~(1<<(i&31));
2784: }
2785: /* exhausted */
2786: return 1;
2787: }
2788:
2789: /*
2790: * f = an irreducible trinomial or pentanomial of degree d 'after' f
2791: *
2792: * searching strategy:
2793: * trinomial x^d+x^i+1:
2794: * i is as small as possible.
2795: * trinomial x^d+x^i+x^j+x^k+1:
2796: * i is as small as possible.
2797: * For such i, j is as small as possible.
2798: * For such i and j, 'k' is as small as possible.
2799: *
2800: * return value : 0 --- exists
2801: * 1 --- does not exist (exhaustion)
2802: */
2803:
2804: int _generate_good_irreducible_polynomial(UP2 f,int d)
2805: {
2806: int ret,i,j,k,nz,i0,j0,k0;
2807: int w;
2808: unsigned int *fd;
2809:
2810: /*
2811: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
2812: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
2813: * otherwise i0,j0,k0 is set to 0.
2814: */
2815:
2816: fd = f->b;
2817: w = (d>>5)+1;
2818: if ( f->w && (d==degup2(f)) ) {
2819: for ( nz = 0, i = d; i >= 0; i-- )
2820: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
2821: switch ( nz ) {
2822: case 3:
2823: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2824: /* reset i0-th bit */
2825: fd[i0>>5] &= ~(1<<(i0&31));
2826: j0 = k0 = 0;
2827: break;
2828: case 5:
2829: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
2830: /* reset i0-th bit */
2831: fd[i0>>5] &= ~(1<<(i0&31));
2832: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
2833: /* reset j0-th bit */
2834: fd[j0>>5] &= ~(1<<(j0&31));
2835: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
2836: /* reset k0-th bit */
2837: fd[k0>>5] &= ~(1<<(k0&31));
2838: break;
2839: default:
2840: f->w = 0; break;
2841: }
2842: } else
2843: f->w = 0;
2844:
2845: if ( !f->w ) {
2846: fd = f->b;
2847: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
2848: i0 = j0 = k0 = 0;
2849: }
2850: /* if j0 > 0 then f is already a pentanomial */
2851: if ( j0 > 0 ) goto PENTA;
2852:
2853: /* searching for an irreducible trinomial */
2854:
2855: for ( i = 1; 2*i <= d; i++ ) {
2856: /* skip the polynomials 'before' f */
2857: if ( i < i0 ) continue;
2858: if ( i == i0 ) { i0 = 0; continue; }
2859: /* set i-th bit */
2860: fd[i>>5] |= (1<<(i&31));
2861: ret = irredcheck_dddup2(f);
2862: if ( ret == 1 ) return 0;
2863: /* reset i-th bit */
2864: fd[i>>5] &= ~(1<<(i&31));
2865: }
2866:
2867: /* searching for an irreducible pentanomial */
2868: PENTA:
2869: for ( i = 3; i < d; i++ ) {
2870: /* skip the polynomials 'before' f */
2871: if ( i < i0 ) continue;
2872: if ( i == i0 ) i0 = 0;
2873: /* set i-th bit */
2874: fd[i>>5] |= (1<<(i&31));
2875: for ( j = 2; j < i; j++ ) {
2876: /* skip the polynomials 'before' f */
2877: if ( j < j0 ) continue;
2878: if ( j == j0 ) j0 = 0;
2879: /* set j-th bit */
2880: fd[j>>5] |= (1<<(j&31));
2881: for ( k = 1; k < j; k++ ) {
2882: /* skip the polynomials 'before' f */
2883: if ( k < k0 ) continue;
2884: else if ( k == k0 ) { k0 = 0; continue; }
2885: /* set k-th bit */
2886: fd[k>>5] |= (1<<(k&31));
2887: ret = irredcheck_dddup2(f);
2888: if ( ret == 1 ) return 0;
2889: /* reset k-th bit */
2890: fd[k>>5] &= ~(1<<(k&31));
2891: }
2892: /* reset j-th bit */
2893: fd[j>>5] &= ~(1<<(j&31));
2894: }
2895: /* reset i-th bit */
2896: fd[i>>5] &= ~(1<<(i&31));
2897: }
2898: /* exhausted */
2899: return 1;
1.3 noro 2900: }
2901:
1.24 noro 2902: void printqmat(Q **mat,int row,int col)
1.3 noro 2903: {
2904: int i,j;
2905:
2906: for ( i = 0; i < row; i++ ) {
2907: for ( j = 0; j < col; j++ ) {
1.8 noro 2908: printnum((Num)mat[i][j]); printf(" ");
1.3 noro 2909: }
2910: printf("\n");
2911: }
2912: }
2913:
1.24 noro 2914: void printimat(int **mat,int row,int col)
1.3 noro 2915: {
2916: int i,j;
2917:
2918: for ( i = 0; i < row; i++ ) {
2919: for ( j = 0; j < col; j++ ) {
2920: printf("%d ",mat[i][j]);
2921: }
2922: printf("\n");
2923: }
1.36 noro 2924: }
2925:
2926: void Pnd_det(NODE arg,P *rp)
2927: {
1.37 noro 2928: if ( argc(arg) == 1 )
2929: nd_det(0,ARG0(arg),rp);
2930: else
2931: nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp);
1.1 noro 2932: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>