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