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