Annotation of OpenXM_contrib2/asir2000/builtin/array.c, Revision 1.54
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.54 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.53 2006/06/12 11:52:10 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:
1545: int generic_gauss_elim_hensel_dalg(MAT mat,MAT *nmmat,Q *dn,int **rindp,int **cindp)
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 *mb;
1570: DP m;
1571: int col1;
1572:
1573: nf = get_numberfield();
1574: mb = nf->mb;
1575: a0 = (Q **)mat->body;
1576: row = mat->row; col = mat->col;
1577: w = (int **)almat(row,col);
1578: for ( ind = 0; ; ind++ ) {
1579: md = get_lprime(ind);
1580: STOQ(md,mdq);
1581: for ( i = 0; i < row; i++ )
1582: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1583: if ( q = (Q)ai[j] ) {
1584: t = rem(NM(q),md);
1585: if ( t && SGN(q) < 0 )
1586: t = (md - t) % md;
1587: wi[j] = t;
1588: } else
1589: wi[j] = 0;
1590:
1591: if ( DP_Print ) {
1592: fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
1593: }
1594: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1595: if ( DP_Print ) {
1596: fprintf(asir_out,"done.\n"); fflush(asir_out);
1597: }
1598: for ( i = 0; i < col-1; i++ ) {
1599: if ( !cinfo[i] ) {
1600: m = mb[i];
1601: for ( j = i+1; j < col-1; j++ )
1602: if ( dp_redble(mb[j],m) )
1603: cinfo[j] = -1;
1604: }
1605: }
1606: a = (Q **)almat_pointer(rank,rank); /* lhs mat */
1607: MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
1608: for ( j = li = ri = 0; j < col; j++ )
1609: if ( cinfo[j] > 0 ) {
1610: /* the column is in lhs */
1611: for ( i = 0; i < rank; i++ ) {
1612: w[i][li] = w[i][j];
1613: a[i][li] = a0[rinfo[i]][j];
1614: }
1615: li++;
1616: } else if ( !cinfo[j] ) {
1617: /* the column is in rhs */
1618: for ( i = 0; i < rank; i++ )
1619: b[i][ri] = a0[rinfo[i]][j];
1620: ri++;
1621: }
1622:
1623: /* solve Ax+B=0; A: rank x rank, B: rank x ri */
1624: MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
1625: MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
1626: /* use the right part of w as work area */
1627: wc = (int **)almat(rank,ri);
1628: for ( i = 0; i < rank; i++ )
1629: wc[i] = w[i]+rank;
1630: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1631: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
1632: init_eg(&eg_mul); init_eg(&eg_inv);
1633: init_eg(&eg_check); init_eg(&eg_intrat);
1634: period = F4_INTRAT_PERIOD;
1635: nsize = period;
1636: wxsize = rank*ri*nsize;
1637: wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int));
1638: for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
1639: for ( q = ONE, count = 0; ; ) {
1640: if ( DP_Print )
1641: fprintf(stderr,"o");
1642: /* wc = -b mod md */
1643: get_eg(&tmp0);
1644: for ( i = 0; i < rank; i++ )
1645: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1646: if ( u = (Q)bi[j] ) {
1647: t = rem(NM(u),md);
1648: if ( t && SGN(u) > 0 )
1649: t = (md - t) % md;
1650: wi[j] = t;
1651: } else
1652: wi[j] = 0;
1653: /* wc = A^(-1)wc; wc is not normalized */
1654: solve_by_lu_mod(w,rank,md,wc,ri,0);
1655: /* wx += q*wc */
1656: ptr = wx;
1657: for ( i = 0; i < rank; i++ )
1658: for ( j = 0, wi = wc[i]; j < ri; j++ ) {
1659: if ( wi[j] )
1660: muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr);
1661: ptr += nsize;
1662: }
1663: count++;
1664: get_eg(&tmp1);
1665: add_eg(&eg_inv,&tmp0,&tmp1);
1666: get_eg(&tmp0);
1667: for ( i = 0; i < rank; i++ )
1668: for ( j = 0; j < ri; j++ ) {
1669: inner_product_mat_int_mod(a,wc,rank,i,j,&u);
1670: addq(b[i][j],u,&s);
1671: if ( s ) {
1672: t = divin(NM(s),md,&tn);
1673: if ( t )
1674: error("generic_gauss_elim_hensel:incosistent");
1675: NTOQ(tn,SGN(s),b[i][j]);
1676: } else
1677: b[i][j] = 0;
1678: }
1679: get_eg(&tmp1);
1680: add_eg(&eg_mul,&tmp0,&tmp1);
1681: /* q = q*md */
1682: mulq(q,mdq,&u); q = u;
1683: if ( count == period ) {
1684: get_eg(&tmp0);
1685: ptr = wx;
1686: for ( i = 0; i < rank; i++ )
1687: for ( j = 0, xi = x[i]; j < ri;
1688: j++, ptr += nsize ) {
1689: for ( k = nsize-1; k >= 0 && !ptr[k]; k-- );
1690: if ( k >= 0 ) {
1691: wn = NALLOC(k+1);
1692: PL(wn) = k+1;
1693: for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l];
1694: NTOQ(wn,1,wq);
1695: subq(xi[j],wq,&u); xi[j] = u;
1696: }
1697: }
1698: ret = intmtoratm_q(xmat,NM(q),*nmmat,dn);
1699: get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1);
1700: if ( ret ) {
1701: for ( j = k = l = 0; j < col; j++ )
1702: if ( cinfo[j] > 0 )
1703: rind[k++] = j;
1704: else if ( !cinfo[j] )
1.39 noro 1705: cind[l++] = j;
1.41 noro 1706: get_eg(&tmp0);
1707: ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
1708: get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1);
1709: if ( ret ) {
1.42 noro 1710: if ( DP_Print > 3 ) {
1.40 noro 1711: fprintf(stderr,"\n");
1712: print_eg("INV",&eg_inv);
1713: print_eg("MUL",&eg_mul);
1.41 noro 1714: print_eg("INTRAT",&eg_intrat);
1715: print_eg("CHECK",&eg_check);
1.40 noro 1716: fflush(asir_out);
1717: }
1.39 noro 1718: return rank;
1719: }
1.44 noro 1720: } else {
1721: period = period*3/2;
1722: count = 0;
1723: nsize += period;
1724: wxsize += rank*ri*nsize;
1725: wx = (int *)REALLOC(wx,wxsize*sizeof(int));
1726: for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
1727: }
1.41 noro 1728: }
1.1 noro 1729: }
1730: }
1731: }
1732:
1733: int f4_nocheck;
1734:
1.24 noro 1735: int gensolve_check(MAT mat,MAT nm,Q dn,int *rind,int *cind)
1.1 noro 1736: {
1737: int row,col,rank,clen,i,j,k,l;
1.24 noro 1738: Q s,t;
1.1 noro 1739: Q *w;
1740: Q *mati,*nmk;
1741:
1742: if ( f4_nocheck )
1743: return 1;
1744: row = mat->row; col = mat->col;
1745: rank = nm->row; clen = nm->col;
1746: w = (Q *)MALLOC(clen*sizeof(Q));
1747: for ( i = 0; i < row; i++ ) {
1748: mati = (Q *)mat->body[i];
1749: #if 1
1750: bzero(w,clen*sizeof(Q));
1751: for ( k = 0; k < rank; k++ )
1752: for ( l = 0, nmk = (Q *)nm->body[k]; l < clen; l++ ) {
1753: mulq(mati[rind[k]],nmk[l],&t);
1754: addq(w[l],t,&s); w[l] = s;
1755: }
1756: for ( j = 0; j < clen; j++ ) {
1757: mulq(dn,mati[cind[j]],&t);
1758: if ( cmpq(w[j],t) )
1759: break;
1760: }
1761: #else
1762: for ( j = 0; j < clen; j++ ) {
1763: for ( k = 0, s = 0; k < rank; k++ ) {
1764: mulq(mati[rind[k]],nm->body[k][j],&t);
1765: addq(s,t,&u); s = u;
1766: }
1767: mulq(dn,mati[cind[j]],&t);
1768: if ( cmpq(s,t) )
1769: break;
1770: }
1771: #endif
1772: if ( j != clen )
1773: break;
1774: }
1775: if ( i != row )
1776: return 0;
1777: else
1778: return 1;
1779: }
1780:
1781: /* assuming 0 < c < m */
1782:
1.24 noro 1783: int inttorat(N c,N m,N b,int *sgnp,N *nmp,N *dnp)
1.1 noro 1784: {
1.24 noro 1785: Q qq,t,u1,v1,r1;
1786: N q,u2,v2,r2;
1.1 noro 1787:
1788: u1 = 0; v1 = ONE; u2 = m; v2 = c;
1789: while ( cmpn(v2,b) >= 0 ) {
1790: divn(u2,v2,&q,&r2); u2 = v2; v2 = r2;
1791: NTOQ(q,1,qq); mulq(qq,v1,&t); subq(u1,t,&r1); u1 = v1; v1 = r1;
1792: }
1793: if ( cmpn(NM(v1),b) >= 0 )
1794: return 0;
1795: else {
1796: *nmp = v2;
1797: *dnp = NM(v1);
1798: *sgnp = SGN(v1);
1799: return 1;
1800: }
1801: }
1802:
1803: /* mat->body = N ** */
1804:
1.24 noro 1805: int intmtoratm(MAT mat,N md,MAT nm,Q *dn)
1.1 noro 1806: {
1807: N t,s,b;
1.24 noro 1808: Q dn0,dn1,nm1,q;
1.1 noro 1809: int i,j,k,l,row,col;
1810: Q **rmat;
1811: N **tmat;
1812: N *tmi;
1813: Q *nmk;
1814: N u,unm,udn;
1815: int sgn,ret;
1816:
1.3 noro 1817: if ( UNIN(md) )
1818: return 0;
1.1 noro 1819: row = mat->row; col = mat->col;
1820: bshiftn(md,1,&t);
1821: isqrt(t,&s);
1822: bshiftn(s,64,&b);
1823: if ( !b )
1824: b = ONEN;
1825: dn0 = ONE;
1826: tmat = (N **)mat->body;
1827: rmat = (Q **)nm->body;
1828: for ( i = 0; i < row; i++ )
1829: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1830: if ( tmi[j] ) {
1831: muln(tmi[j],NM(dn0),&s);
1832: remn(s,md,&u);
1833: ret = inttorat(u,md,b,&sgn,&unm,&udn);
1834: if ( !ret )
1835: return 0;
1836: else {
1837: NTOQ(unm,sgn,nm1);
1838: NTOQ(udn,1,dn1);
1839: if ( !UNIQ(dn1) ) {
1840: for ( k = 0; k < i; k++ )
1841: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1842: mulq(nmk[l],dn1,&q); nmk[l] = q;
1843: }
1844: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1845: mulq(nmk[l],dn1,&q); nmk[l] = q;
1846: }
1847: }
1848: rmat[i][j] = nm1;
1849: mulq(dn0,dn1,&q); dn0 = q;
1850: }
1851: }
1852: *dn = dn0;
1853: return 1;
1854: }
1855:
1.3 noro 1856: /* mat->body = Q ** */
1857:
1.24 noro 1858: int intmtoratm_q(MAT mat,N md,MAT nm,Q *dn)
1.3 noro 1859: {
1860: N t,s,b;
1.24 noro 1861: Q dn0,dn1,nm1,q;
1.3 noro 1862: int i,j,k,l,row,col;
1863: Q **rmat;
1864: Q **tmat;
1865: Q *tmi;
1866: Q *nmk;
1867: N u,unm,udn;
1868: int sgn,ret;
1869:
1870: if ( UNIN(md) )
1871: return 0;
1872: row = mat->row; col = mat->col;
1873: bshiftn(md,1,&t);
1874: isqrt(t,&s);
1875: bshiftn(s,64,&b);
1876: if ( !b )
1877: b = ONEN;
1878: dn0 = ONE;
1879: tmat = (Q **)mat->body;
1880: rmat = (Q **)nm->body;
1881: for ( i = 0; i < row; i++ )
1882: for ( j = 0, tmi = tmat[i]; j < col; j++ )
1883: if ( tmi[j] ) {
1884: muln(NM(tmi[j]),NM(dn0),&s);
1885: remn(s,md,&u);
1886: ret = inttorat(u,md,b,&sgn,&unm,&udn);
1887: if ( !ret )
1888: return 0;
1889: else {
1890: if ( SGN(tmi[j])<0 )
1891: sgn = -sgn;
1892: NTOQ(unm,sgn,nm1);
1893: NTOQ(udn,1,dn1);
1894: if ( !UNIQ(dn1) ) {
1895: for ( k = 0; k < i; k++ )
1896: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
1897: mulq(nmk[l],dn1,&q); nmk[l] = q;
1898: }
1899: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
1900: mulq(nmk[l],dn1,&q); nmk[l] = q;
1901: }
1902: }
1903: rmat[i][j] = nm1;
1904: mulq(dn0,dn1,&q); dn0 = q;
1905: }
1906: }
1907: *dn = dn0;
1908: return 1;
1909: }
1910:
1.4 noro 1911: #define ONE_STEP1 if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1912:
1.24 noro 1913: void reduce_reducers_mod(int **mat,int row,int col,int md)
1.4 noro 1914: {
1915: int i,j,k,l,hc,zzz;
1916: int *t,*s,*tj,*ind;
1917:
1918: /* reduce the reducers */
1919: ind = (int *)ALLOCA(row*sizeof(int));
1920: for ( i = 0; i < row; i++ ) {
1921: t = mat[i];
1922: for ( j = 0; j < col && !t[j]; j++ );
1923: /* register the position of the head term */
1924: ind[i] = j;
1925: for ( l = i-1; l >= 0; l-- ) {
1926: /* reduce mat[i] by mat[l] */
1927: if ( hc = t[ind[l]] ) {
1928: /* mat[i] = mat[i]-hc*mat[l] */
1929: j = ind[l];
1930: s = mat[l]+j;
1931: tj = t+j;
1932: hc = md-hc;
1933: k = col-j;
1934: for ( ; k >= 64; k -= 64 ) {
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: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1949: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1950: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
1951: }
1.16 noro 1952: for ( ; k > 0; k-- ) {
1.4 noro 1953: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1954: }
1955: }
1956: }
1957: }
1958: }
1959:
1960: /*
1961: mat[i] : reducers (i=0,...,nred-1)
1962: spolys (i=nred,...,row-1)
1963: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1964: 1. reduce the reducers
1965: 2. reduce spolys by the reduced reducers
1966: */
1967:
1.24 noro 1968: void pre_reduce_mod(int **mat,int row,int col,int nred,int md)
1.4 noro 1969: {
1970: int i,j,k,l,hc,inv;
1971: int *t,*s,*tk,*ind;
1972:
1973: #if 1
1974: /* reduce the reducers */
1975: ind = (int *)ALLOCA(row*sizeof(int));
1976: for ( i = 0; i < nred; i++ ) {
1977: /* make mat[i] monic and mat[i] by mat[0],...,mat[i-1] */
1978: t = mat[i];
1979: for ( j = 0; j < col && !t[j]; j++ );
1980: /* register the position of the head term */
1981: ind[i] = j;
1982: inv = invm(t[j],md);
1983: for ( k = j; k < col; k++ )
1984: if ( t[k] )
1985: DMAR(t[k],inv,0,md,t[k])
1986: for ( l = i-1; l >= 0; l-- ) {
1987: /* reduce mat[i] by mat[l] */
1988: if ( hc = t[ind[l]] ) {
1989: /* mat[i] = mat[i]-hc*mat[l] */
1990: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
1991: k < col; k++, tk++, s++ )
1992: if ( *s )
1993: DMAR(*s,hc,*tk,md,*tk)
1994: }
1995: }
1996: }
1997: /* reduce the spolys */
1998: for ( i = nred; i < row; i++ ) {
1999: t = mat[i];
2000: for ( l = nred-1; l >= 0; l-- ) {
2001: /* reduce mat[i] by mat[l] */
2002: if ( hc = t[ind[l]] ) {
2003: /* mat[i] = mat[i]-hc*mat[l] */
2004: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
2005: k < col; k++, tk++, s++ )
2006: if ( *s )
2007: DMAR(*s,hc,*tk,md,*tk)
2008: }
2009: }
2010: }
2011: #endif
2012: }
2013: /*
2014: mat[i] : reducers (i=0,...,nred-1)
2015: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
2016: */
2017:
1.24 noro 2018: void reduce_sp_by_red_mod(int *sp,int **redmat,int *ind,int nred,int col,int md)
1.4 noro 2019: {
2020: int i,j,k,hc,zzz;
1.24 noro 2021: int *s,*tj;
1.4 noro 2022:
2023: /* reduce the spolys by redmat */
2024: for ( i = nred-1; i >= 0; i-- ) {
2025: /* reduce sp by redmat[i] */
2026: if ( hc = sp[ind[i]] ) {
2027: /* sp = sp-hc*redmat[i] */
2028: j = ind[i];
2029: hc = md-hc;
2030: s = redmat[i]+j;
2031: tj = sp+j;
1.16 noro 2032: for ( k = col-j; k > 0; k-- ) {
1.4 noro 2033: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
1.15 noro 2034: }
2035: }
1.17 noro 2036: }
2037: }
2038:
2039: /*
1.15 noro 2040: mat[i] : compressed reducers (i=0,...,nred-1)
2041: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
2042: */
2043:
1.24 noro 2044: void red_by_compress(int m,unsigned int *p,unsigned int *r,
2045: unsigned int *ri,unsigned int hc,int len)
1.18 noro 2046: {
1.19 noro 2047: unsigned int up,lo;
1.18 noro 2048: unsigned int dmy;
2049: unsigned int *pj;
2050:
1.21 noro 2051: p[*ri] = 0; r++; ri++;
2052: for ( len--; len; len--, r++, ri++ ) {
2053: pj = p+ *ri;
2054: DMA(*r,hc,*pj,up,lo);
1.18 noro 2055: if ( up ) {
2056: DSAB(m,up,lo,dmy,*pj);
2057: } else
2058: *pj = lo;
2059: }
2060: }
2061:
2062: /* p -= hc*r */
2063:
1.24 noro 2064: void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1.18 noro 2065: {
2066: register unsigned int up,lo;
2067: unsigned int dmy;
2068:
2069: *p++ = 0; r++; len--;
2070: for ( ; len; len--, r++, p++ )
2071: if ( *r ) {
1.20 noro 2072: DMA(*r,hc,*p,up,lo);
1.18 noro 2073: if ( up ) {
2074: DSAB(m,up,lo,dmy,*p);
2075: } else
2076: *p = lo;
2077: }
2078: }
2079:
1.32 noro 2080: void red_by_vect_sf(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
2081: {
2082: *p++ = 0; r++; len--;
2083: for ( ; len; len--, r++, p++ )
2084: if ( *r )
2085: *p = _addsf(_mulsf(*r,hc),*p);
2086: }
2087:
1.21 noro 2088: extern unsigned int **psca;
2089:
1.24 noro 2090: void reduce_sp_by_red_mod_compress (int *sp,CDP *redmat,int *ind,
2091: int nred,int col,int md)
1.15 noro 2092: {
1.24 noro 2093: int i,len;
1.15 noro 2094: CDP ri;
1.24 noro 2095: unsigned int hc;
1.18 noro 2096: unsigned int *usp;
1.15 noro 2097:
1.18 noro 2098: usp = (unsigned int *)sp;
1.15 noro 2099: /* reduce the spolys by redmat */
2100: for ( i = nred-1; i >= 0; i-- ) {
2101: /* reduce sp by redmat[i] */
1.18 noro 2102: usp[ind[i]] %= md;
2103: if ( hc = usp[ind[i]] ) {
1.15 noro 2104: /* sp = sp-hc*redmat[i] */
2105: hc = md-hc;
2106: ri = redmat[i];
2107: len = ri->len;
1.21 noro 2108: red_by_compress(md,usp,psca[ri->psindex],ri->body,hc,len);
1.4 noro 2109: }
2110: }
1.18 noro 2111: for ( i = 0; i < col; i++ )
1.24 noro 2112: if ( usp[i] >= (unsigned int)md )
1.18 noro 2113: usp[i] %= md;
1.4 noro 2114: }
2115:
2116: #define ONE_STEP2 if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
2117:
1.24 noro 2118: int generic_gauss_elim_mod(int **mat0,int row,int col,int md,int *colstat)
1.1 noro 2119: {
1.24 noro 2120: int i,j,k,l,inv,a,rank;
2121: unsigned int *t,*pivot,*pk;
1.18 noro 2122: unsigned int **mat;
1.1 noro 2123:
1.18 noro 2124: mat = (unsigned int **)mat0;
1.1 noro 2125: for ( rank = 0, j = 0; j < col; j++ ) {
1.18 noro 2126: for ( i = rank; i < row; i++ )
2127: mat[i][j] %= md;
2128: for ( i = rank; i < row; i++ )
2129: if ( mat[i][j] )
2130: break;
1.1 noro 2131: if ( i == row ) {
2132: colstat[j] = 0;
2133: continue;
2134: } else
2135: colstat[j] = 1;
2136: if ( i != rank ) {
2137: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
2138: }
2139: pivot = mat[rank];
2140: inv = invm(pivot[j],md);
1.4 noro 2141: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
2142: if ( *pk ) {
1.24 noro 2143: if ( *pk >= (unsigned int)md )
1.18 noro 2144: *pk %= md;
1.4 noro 2145: DMAR(*pk,inv,0,md,*pk)
1.1 noro 2146: }
2147: for ( i = rank+1; i < row; i++ ) {
2148: t = mat[i];
1.18 noro 2149: if ( a = t[j] )
2150: red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1 noro 2151: }
2152: rank++;
2153: }
2154: for ( j = col-1, l = rank-1; j >= 0; j-- )
2155: if ( colstat[j] ) {
2156: pivot = mat[l];
2157: for ( i = 0; i < l; i++ ) {
2158: t = mat[i];
1.18 noro 2159: t[j] %= md;
2160: if ( a = t[j] )
2161: red_by_vect(md,t+j,pivot+j,md-a,col-j);
1.1 noro 2162: }
2163: l--;
1.18 noro 2164: }
2165: for ( j = 0, l = 0; l < rank; j++ )
2166: if ( colstat[j] ) {
2167: t = mat[l];
2168: for ( k = j; k < col; k++ )
1.24 noro 2169: if ( t[k] >= (unsigned int)md )
1.18 noro 2170: t[k] %= md;
2171: l++;
1.32 noro 2172: }
2173: return rank;
2174: }
2175:
2176: int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat)
2177: {
2178: int i,j,k,l,inv,a,rank;
2179: unsigned int *t,*pivot,*pk;
2180: unsigned int **mat;
2181:
2182: mat = (unsigned int **)mat0;
2183: for ( rank = 0, j = 0; j < col; j++ ) {
2184: for ( i = rank; i < row; i++ )
2185: if ( mat[i][j] )
2186: break;
2187: if ( i == row ) {
2188: colstat[j] = 0;
2189: continue;
2190: } else
2191: colstat[j] = 1;
2192: if ( i != rank ) {
2193: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
2194: }
2195: pivot = mat[rank];
2196: inv = _invsf(pivot[j]);
2197: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
2198: if ( *pk )
2199: *pk = _mulsf(*pk,inv);
2200: for ( i = rank+1; i < row; i++ ) {
2201: t = mat[i];
2202: if ( a = t[j] )
2203: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
2204: }
2205: rank++;
2206: }
2207: for ( j = col-1, l = rank-1; j >= 0; j-- )
2208: if ( colstat[j] ) {
2209: pivot = mat[l];
2210: for ( i = 0; i < l; i++ ) {
2211: t = mat[i];
2212: if ( a = t[j] )
2213: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
2214: }
2215: l--;
1.1 noro 2216: }
2217: return rank;
2218: }
2219:
2220: /* LU decomposition; a[i][i] = 1/U[i][i] */
2221:
1.24 noro 2222: int lu_gfmmat(GFMMAT mat,unsigned int md,int *perm)
1.1 noro 2223: {
2224: int row,col;
1.24 noro 2225: int i,j,k;
1.1 noro 2226: unsigned int *t,*pivot;
2227: unsigned int **a;
2228: unsigned int inv,m;
2229:
2230: row = mat->row; col = mat->col;
2231: a = mat->body;
2232: bzero(perm,row*sizeof(int));
2233:
2234: for ( i = 0; i < row; i++ )
2235: perm[i] = i;
2236: for ( k = 0; k < col; k++ ) {
2237: for ( i = k; i < row && !a[i][k]; i++ );
2238: if ( i == row )
2239: return 0;
2240: if ( i != k ) {
2241: j = perm[i]; perm[i] = perm[k]; perm[k] = j;
2242: t = a[i]; a[i] = a[k]; a[k] = t;
2243: }
2244: pivot = a[k];
2245: pivot[k] = inv = invm(pivot[k],md);
2246: for ( i = k+1; i < row; i++ ) {
2247: t = a[i];
2248: if ( m = t[k] ) {
2249: DMAR(inv,m,0,md,t[k])
2250: for ( j = k+1, m = md - t[k]; j < col; j++ )
2251: if ( pivot[j] ) {
1.8 noro 2252: unsigned int tj;
2253:
2254: DMAR(m,pivot[j],t[j],md,tj)
2255: t[j] = tj;
1.1 noro 2256: }
2257: }
2258: }
2259: }
2260: return 1;
2261: }
2262:
1.3 noro 2263: /*
2264: Input
2265: a: a row x col matrix
2266: md : a modulus
2267:
2268: Output:
2269: return : d = the rank of mat
2270: a[0..(d-1)][0..(d-1)] : LU decomposition (a[i][i] = 1/U[i][i])
2271: rinfo: array of length row
2272: cinfo: array of length col
2273: i-th row in new a <-> rinfo[i]-th row in old a
2274: cinfo[j]=1 <=> j-th column is contained in the LU decomp.
2275: */
2276:
1.24 noro 2277: int find_lhs_and_lu_mod(unsigned int **a,int row,int col,
2278: unsigned int md,int **rinfo,int **cinfo)
1.3 noro 2279: {
1.24 noro 2280: int i,j,k,d;
1.3 noro 2281: int *rp,*cp;
2282: unsigned int *t,*pivot;
2283: unsigned int inv,m;
2284:
2285: *rinfo = rp = (int *)MALLOC_ATOMIC(row*sizeof(int));
2286: *cinfo = cp = (int *)MALLOC_ATOMIC(col*sizeof(int));
2287: for ( i = 0; i < row; i++ )
2288: rp[i] = i;
2289: for ( k = 0, d = 0; k < col; k++ ) {
2290: for ( i = d; i < row && !a[i][k]; i++ );
2291: if ( i == row ) {
2292: cp[k] = 0;
2293: continue;
2294: } else
2295: cp[k] = 1;
2296: if ( i != d ) {
2297: j = rp[i]; rp[i] = rp[d]; rp[d] = j;
2298: t = a[i]; a[i] = a[d]; a[d] = t;
2299: }
2300: pivot = a[d];
2301: pivot[k] = inv = invm(pivot[k],md);
2302: for ( i = d+1; i < row; i++ ) {
2303: t = a[i];
2304: if ( m = t[k] ) {
2305: DMAR(inv,m,0,md,t[k])
2306: for ( j = k+1, m = md - t[k]; j < col; j++ )
2307: if ( pivot[j] ) {
1.8 noro 2308: unsigned int tj;
2309: DMAR(m,pivot[j],t[j],md,tj)
2310: t[j] = tj;
1.3 noro 2311: }
2312: }
2313: }
2314: d++;
2315: }
2316: return d;
2317: }
2318:
1.53 noro 2319: int lu_mod(unsigned int **a,int n,unsigned int md,int **rinfo)
2320: {
2321: int i,j,k;
2322: int *rp;
2323: unsigned int *t,*pivot;
2324: unsigned int inv,m;
2325:
2326: *rinfo = rp = (int *)MALLOC_ATOMIC(n*sizeof(int));
2327: for ( i = 0; i < n; i++ ) rp[i] = i;
2328: for ( k = 0; k < n; k++ ) {
2329: for ( i = k; i < n && !a[i][k]; i++ );
2330: if ( i == n ) return 0;
2331: if ( i != k ) {
2332: j = rp[i]; rp[i] = rp[k]; rp[k] = j;
2333: t = a[i]; a[i] = a[k]; a[k] = t;
2334: }
2335: pivot = a[k];
2336: inv = invm(pivot[k],md);
2337: for ( i = k+1; i < n; i++ ) {
2338: t = a[i];
2339: if ( m = t[k] ) {
2340: DMAR(inv,m,0,md,t[k])
2341: for ( j = k+1, m = md - t[k]; j < n; j++ )
2342: if ( pivot[j] ) {
2343: unsigned int tj;
2344: DMAR(m,pivot[j],t[j],md,tj)
2345: t[j] = tj;
2346: }
2347: }
2348: }
2349: }
2350: return 1;
2351: }
2352:
1.3 noro 2353: /*
2354: Input
2355: a : n x n matrix; a result of LU-decomposition
2356: md : modulus
2357: b : n x l matrix
2358: Output
2359: b = a^(-1)b
2360: */
2361:
1.44 noro 2362: void solve_by_lu_mod(int **a,int n,int md,int **b,int l,int normalize)
1.3 noro 2363: {
2364: unsigned int *y,*c;
2365: int i,j,k;
2366: unsigned int t,m,m2;
2367:
2368: y = (int *)MALLOC_ATOMIC(n*sizeof(int));
2369: c = (int *)MALLOC_ATOMIC(n*sizeof(int));
2370: m2 = md>>1;
2371: for ( k = 0; k < l; k++ ) {
2372: /* copy b[.][k] to c */
2373: for ( i = 0; i < n; i++ )
2374: c[i] = (unsigned int)b[i][k];
2375: /* solve Ly=c */
2376: for ( i = 0; i < n; i++ ) {
2377: for ( t = c[i], j = 0; j < i; j++ )
2378: if ( a[i][j] ) {
2379: m = md - a[i][j];
2380: DMAR(m,y[j],t,md,t)
2381: }
2382: y[i] = t;
2383: }
2384: /* solve Uc=y */
2385: for ( i = n-1; i >= 0; i-- ) {
2386: for ( t = y[i], j =i+1; j < n; j++ )
2387: if ( a[i][j] ) {
2388: m = md - a[i][j];
2389: DMAR(m,c[j],t,md,t)
2390: }
2391: /* a[i][i] = 1/U[i][i] */
2392: DMAR(t,a[i][i],0,md,c[i])
2393: }
2394: /* copy c to b[.][k] with normalization */
1.44 noro 2395: if ( normalize )
2396: for ( i = 0; i < n; i++ )
2397: b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]);
2398: else
2399: for ( i = 0; i < n; i++ )
2400: b[i][k] = c[i];
1.3 noro 2401: }
2402: }
2403:
1.24 noro 2404: void Pleqm1(NODE arg,VECT *rp)
1.1 noro 2405: {
2406: MAT m;
2407: VECT vect;
2408: pointer **mat;
2409: Q *v;
2410: Q q;
2411: int **wmat;
2412: int md,i,j,row,col,t,n,status;
2413:
2414: asir_assert(ARG0(arg),O_MAT,"leqm1");
2415: asir_assert(ARG1(arg),O_N,"leqm1");
2416: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2417: row = m->row; col = m->col; mat = m->body;
2418: wmat = (int **)almat(row,col);
2419: for ( i = 0; i < row; i++ )
2420: for ( j = 0; j < col; j++ )
2421: if ( q = (Q)mat[i][j] ) {
2422: t = rem(NM(q),md);
2423: if ( SGN(q) < 0 )
2424: t = (md - t) % md;
2425: wmat[i][j] = t;
2426: } else
2427: wmat[i][j] = 0;
2428: status = gauss_elim_mod1(wmat,row,col,md);
2429: if ( status < 0 )
2430: *rp = 0;
2431: else if ( status > 0 )
2432: *rp = (VECT)ONE;
2433: else {
2434: n = col - 1;
2435: MKVECT(vect,n);
2436: for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
2437: t = (md-wmat[i][n])%md; STOQ(t,v[i]);
2438: }
2439: *rp = vect;
2440: }
2441: }
2442:
1.24 noro 2443: int gauss_elim_mod1(int **mat,int row,int col,int md)
1.1 noro 2444: {
2445: int i,j,k,inv,a,n;
2446: int *t,*pivot;
2447:
2448: n = col - 1;
2449: for ( j = 0; j < n; j++ ) {
2450: for ( i = j; i < row && !mat[i][j]; i++ );
2451: if ( i == row )
2452: return 1;
2453: if ( i != j ) {
2454: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2455: }
2456: pivot = mat[j];
2457: inv = invm(pivot[j],md);
2458: for ( k = j; k <= n; k++ )
2459: pivot[k] = dmar(pivot[k],inv,0,md);
2460: for ( i = j+1; i < row; i++ ) {
2461: t = mat[i];
2462: if ( i != j && (a = t[j]) )
2463: for ( k = j, a = md - a; k <= n; k++ )
2464: t[k] = dmar(pivot[k],a,t[k],md);
2465: }
2466: }
2467: for ( i = n; i < row && !mat[i][n]; i++ );
2468: if ( i == row ) {
2469: for ( j = n-1; j >= 0; j-- ) {
2470: for ( i = j-1, a = (md-mat[j][n])%md; i >= 0; i-- ) {
2471: mat[i][n] = dmar(mat[i][j],a,mat[i][n],md);
2472: mat[i][j] = 0;
2473: }
2474: }
2475: return 0;
2476: } else
2477: return -1;
2478: }
2479:
1.24 noro 2480: void Pgeninvm(NODE arg,LIST *rp)
1.1 noro 2481: {
2482: MAT m;
2483: pointer **mat;
2484: Q **tmat;
2485: Q q;
2486: unsigned int **wmat;
2487: int md,i,j,row,col,t,status;
2488: MAT mat1,mat2;
2489: NODE node1,node2;
2490:
2491: asir_assert(ARG0(arg),O_MAT,"leqm1");
2492: asir_assert(ARG1(arg),O_N,"leqm1");
2493: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2494: row = m->row; col = m->col; mat = m->body;
2495: wmat = (unsigned int **)almat(row,col+row);
2496: for ( i = 0; i < row; i++ ) {
2497: bzero((char *)wmat[i],(col+row)*sizeof(int));
2498: for ( j = 0; j < col; j++ )
2499: if ( q = (Q)mat[i][j] ) {
2500: t = rem(NM(q),md);
2501: if ( SGN(q) < 0 )
2502: t = (md - t) % md;
2503: wmat[i][j] = t;
2504: }
2505: wmat[i][col+i] = 1;
2506: }
2507: status = gauss_elim_geninv_mod(wmat,row,col,md);
2508: if ( status > 0 )
2509: *rp = 0;
2510: else {
2511: MKMAT(mat1,col,row); MKMAT(mat2,row-col,row);
2512: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
2513: for ( j = 0; j < row; j++ )
1.24 noro 2514: UTOQ(wmat[i][j+col],tmat[i][j]);
1.1 noro 2515: for ( tmat = (Q **)mat2->body; i < row; i++ )
2516: for ( j = 0; j < row; j++ )
1.24 noro 2517: UTOQ(wmat[i][j+col],tmat[i-col][j]);
1.1 noro 2518: MKNODE(node2,mat2,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2519: }
2520: }
2521:
1.24 noro 2522: int gauss_elim_geninv_mod(unsigned int **mat,int row,int col,int md)
1.1 noro 2523: {
2524: int i,j,k,inv,a,n,m;
2525: unsigned int *t,*pivot;
2526:
2527: n = col; m = row+col;
2528: for ( j = 0; j < n; j++ ) {
2529: for ( i = j; i < row && !mat[i][j]; i++ );
2530: if ( i == row )
2531: return 1;
2532: if ( i != j ) {
2533: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2534: }
2535: pivot = mat[j];
2536: inv = invm(pivot[j],md);
2537: for ( k = j; k < m; k++ )
2538: pivot[k] = dmar(pivot[k],inv,0,md);
2539: for ( i = j+1; i < row; i++ ) {
2540: t = mat[i];
2541: if ( a = t[j] )
2542: for ( k = j, a = md - a; k < m; k++ )
2543: t[k] = dmar(pivot[k],a,t[k],md);
2544: }
2545: }
2546: for ( j = n-1; j >= 0; j-- ) {
2547: pivot = mat[j];
2548: for ( i = j-1; i >= 0; i-- ) {
2549: t = mat[i];
2550: if ( a = t[j] )
2551: for ( k = j, a = md - a; k < m; k++ )
2552: t[k] = dmar(pivot[k],a,t[k],md);
2553: }
2554: }
2555: return 0;
2556: }
2557:
1.24 noro 2558: void Psolve_by_lu_gfmmat(NODE arg,VECT *rp)
1.1 noro 2559: {
2560: GFMMAT lu;
2561: Q *perm,*rhs,*v;
2562: int n,i;
2563: unsigned int md;
2564: unsigned int *b,*sol;
2565: VECT r;
2566:
2567: lu = (GFMMAT)ARG0(arg);
2568: perm = (Q *)BDY((VECT)ARG1(arg));
2569: rhs = (Q *)BDY((VECT)ARG2(arg));
2570: md = (unsigned int)QTOS((Q)ARG3(arg));
2571: n = lu->col;
2572: b = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2573: sol = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2574: for ( i = 0; i < n; i++ )
2575: b[i] = QTOS(rhs[QTOS(perm[i])]);
2576: solve_by_lu_gfmmat(lu,md,b,sol);
2577: MKVECT(r,n);
2578: for ( i = 0, v = (Q *)r->body; i < n; i++ )
1.24 noro 2579: UTOQ(sol[i],v[i]);
1.1 noro 2580: *rp = r;
2581: }
2582:
1.24 noro 2583: void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md,
2584: unsigned int *b,unsigned int *x)
1.1 noro 2585: {
2586: int n;
2587: unsigned int **a;
2588: unsigned int *y;
2589: int i,j;
2590: unsigned int t,m;
2591:
2592: n = lu->col;
2593: a = lu->body;
2594: y = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
2595: /* solve Ly=b */
2596: for ( i = 0; i < n; i++ ) {
2597: for ( t = b[i], j = 0; j < i; j++ )
2598: if ( a[i][j] ) {
2599: m = md - a[i][j];
2600: DMAR(m,y[j],t,md,t)
2601: }
2602: y[i] = t;
2603: }
2604: /* solve Ux=y */
2605: for ( i = n-1; i >= 0; i-- ) {
2606: for ( t = y[i], j =i+1; j < n; j++ )
2607: if ( a[i][j] ) {
2608: m = md - a[i][j];
2609: DMAR(m,x[j],t,md,t)
2610: }
2611: /* a[i][i] = 1/U[i][i] */
2612: DMAR(t,a[i][i],0,md,x[i])
2613: }
2614: }
2615:
1.53 noro 2616: void Plu_mat(NODE arg,LIST *rp)
2617: {
2618: MAT m,lu;
2619: Q dn;
2620: Q *v;
2621: int n,i;
2622: int *iperm;
2623: VECT perm;
2624: NODE n0;
2625:
2626: asir_assert(ARG0(arg),O_MAT,"lu_mat");
2627: m = (MAT)ARG0(arg);
2628: n = m->row;
2629: MKMAT(lu,n,n);
2630: lu_dec_cr(m,lu,&dn,&iperm);
2631: MKVECT(perm,n);
2632: for ( i = 0, v = (Q *)perm->body; i < n; i++ )
2633: STOQ(iperm[i],v[i]);
2634: n0 = mknode(3,lu,dn,perm);
2635: MKLIST(*rp,n0);
2636: }
2637:
1.24 noro 2638: void Plu_gfmmat(NODE arg,LIST *rp)
1.1 noro 2639: {
2640: MAT m;
2641: GFMMAT mm;
2642: unsigned int md;
2643: int i,row,col,status;
2644: int *iperm;
2645: Q *v;
2646: VECT perm;
2647: NODE n0;
2648:
1.53 noro 2649: asir_assert(ARG0(arg),O_MAT,"lu_gfmmat");
2650: asir_assert(ARG1(arg),O_N,"lu_gfmmat");
1.1 noro 2651: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
2652: mat_to_gfmmat(m,md,&mm);
2653: row = m->row;
2654: col = m->col;
2655: iperm = (int *)MALLOC_ATOMIC(row*sizeof(int));
2656: status = lu_gfmmat(mm,md,iperm);
2657: if ( !status )
2658: n0 = 0;
2659: else {
2660: MKVECT(perm,row);
2661: for ( i = 0, v = (Q *)perm->body; i < row; i++ )
2662: STOQ(iperm[i],v[i]);
2663: n0 = mknode(2,mm,perm);
2664: }
2665: MKLIST(*rp,n0);
2666: }
2667:
1.24 noro 2668: void Pmat_to_gfmmat(NODE arg,GFMMAT *rp)
1.1 noro 2669: {
2670: MAT m;
2671: unsigned int md;
2672:
2673: asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
2674: asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
2675: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
2676: mat_to_gfmmat(m,md,rp);
2677: }
2678:
1.24 noro 2679: void mat_to_gfmmat(MAT m,unsigned int md,GFMMAT *rp)
1.1 noro 2680: {
2681: unsigned int **wmat;
2682: unsigned int t;
2683: Q **mat;
2684: Q q;
2685: int i,j,row,col;
2686:
2687: row = m->row; col = m->col; mat = (Q **)m->body;
2688: wmat = (unsigned int **)almat(row,col);
2689: for ( i = 0; i < row; i++ ) {
2690: bzero((char *)wmat[i],col*sizeof(unsigned int));
2691: for ( j = 0; j < col; j++ )
2692: if ( q = mat[i][j] ) {
2693: t = (unsigned int)rem(NM(q),md);
2694: if ( SGN(q) < 0 )
2695: t = (md - t) % md;
2696: wmat[i][j] = t;
2697: }
2698: }
2699: TOGFMMAT(row,col,wmat,*rp);
2700: }
2701:
1.27 noro 2702: void Pgeninvm_swap(arg,rp)
2703: NODE arg;
2704: LIST *rp;
1.1 noro 2705: {
2706: MAT m;
2707: pointer **mat;
2708: Q **tmat;
2709: Q *tvect;
2710: Q q;
2711: unsigned int **wmat,**invmat;
2712: int *index;
2713: unsigned int t,md;
2714: int i,j,row,col,status;
2715: MAT mat1;
2716: VECT vect1;
2717: NODE node1,node2;
2718:
2719: asir_assert(ARG0(arg),O_MAT,"geninvm_swap");
2720: asir_assert(ARG1(arg),O_N,"geninvm_swap");
2721: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2722: row = m->row; col = m->col; mat = m->body;
2723: wmat = (unsigned int **)almat(row,col+row);
2724: for ( i = 0; i < row; i++ ) {
2725: bzero((char *)wmat[i],(col+row)*sizeof(int));
2726: for ( j = 0; j < col; j++ )
2727: if ( q = (Q)mat[i][j] ) {
2728: t = (unsigned int)rem(NM(q),md);
2729: if ( SGN(q) < 0 )
2730: t = (md - t) % md;
2731: wmat[i][j] = t;
2732: }
2733: wmat[i][col+i] = 1;
2734: }
2735: status = gauss_elim_geninv_mod_swap(wmat,row,col,md,&invmat,&index);
2736: if ( status > 0 )
2737: *rp = 0;
2738: else {
2739: MKMAT(mat1,col,col);
2740: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
2741: for ( j = 0; j < col; j++ )
2742: UTOQ(invmat[i][j],tmat[i][j]);
2743: MKVECT(vect1,row);
2744: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
2745: STOQ(index[i],tvect[i]);
2746: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2747: }
2748: }
2749:
1.27 noro 2750: gauss_elim_geninv_mod_swap(mat,row,col,md,invmatp,indexp)
2751: unsigned int **mat;
2752: int row,col;
2753: unsigned int md;
2754: unsigned int ***invmatp;
2755: int **indexp;
1.1 noro 2756: {
2757: int i,j,k,inv,a,n,m;
2758: unsigned int *t,*pivot,*s;
2759: int *index;
2760: unsigned int **invmat;
2761:
2762: n = col; m = row+col;
2763: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
2764: for ( i = 0; i < row; i++ )
2765: index[i] = i;
2766: for ( j = 0; j < n; j++ ) {
2767: for ( i = j; i < row && !mat[i][j]; i++ );
2768: if ( i == row ) {
2769: *indexp = 0; *invmatp = 0; return 1;
2770: }
2771: if ( i != j ) {
2772: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2773: k = index[i]; index[i] = index[j]; index[j] = k;
2774: }
2775: pivot = mat[j];
2776: inv = (unsigned int)invm(pivot[j],md);
2777: for ( k = j; k < m; k++ )
2778: if ( pivot[k] )
2779: pivot[k] = (unsigned int)dmar(pivot[k],inv,0,md);
2780: for ( i = j+1; i < row; i++ ) {
2781: t = mat[i];
2782: if ( a = t[j] )
2783: for ( k = j, a = md - a; k < m; k++ )
2784: if ( pivot[k] )
2785: t[k] = dmar(pivot[k],a,t[k],md);
2786: }
2787: }
2788: for ( j = n-1; j >= 0; j-- ) {
2789: pivot = mat[j];
2790: for ( i = j-1; i >= 0; i-- ) {
2791: t = mat[i];
2792: if ( a = t[j] )
2793: for ( k = j, a = md - a; k < m; k++ )
2794: if ( pivot[k] )
2795: t[k] = dmar(pivot[k],a,t[k],md);
2796: }
2797: }
2798: *invmatp = invmat = (unsigned int **)almat(col,col);
1.27 noro 2799: for ( i = 0; i < col; i++ )
2800: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
2801: s[j] = t[col+index[j]];
2802: return 0;
2803: }
2804:
2805: void Pgeninv_sf_swap(NODE arg,LIST *rp)
2806: {
2807: MAT m;
2808: GFS **mat,**tmat;
2809: Q *tvect;
2810: GFS q;
2811: int **wmat,**invmat;
2812: int *index;
2813: unsigned int t;
2814: int i,j,row,col,status;
2815: MAT mat1;
2816: VECT vect1;
2817: NODE node1,node2;
2818:
2819: asir_assert(ARG0(arg),O_MAT,"geninv_sf_swap");
2820: m = (MAT)ARG0(arg);
2821: row = m->row; col = m->col; mat = (GFS **)m->body;
2822: wmat = (int **)almat(row,col+row);
2823: for ( i = 0; i < row; i++ ) {
2824: bzero((char *)wmat[i],(col+row)*sizeof(int));
2825: for ( j = 0; j < col; j++ )
2826: if ( q = (GFS)mat[i][j] )
2827: wmat[i][j] = FTOIF(CONT(q));
2828: wmat[i][col+i] = _onesf();
2829: }
2830: status = gauss_elim_geninv_sf_swap(wmat,row,col,&invmat,&index);
2831: if ( status > 0 )
2832: *rp = 0;
2833: else {
2834: MKMAT(mat1,col,col);
2835: for ( i = 0, tmat = (GFS **)mat1->body; i < col; i++ )
2836: for ( j = 0; j < col; j++ )
2837: if ( t = invmat[i][j] ) {
2838: MKGFS(IFTOF(t),tmat[i][j]);
2839: }
2840: MKVECT(vect1,row);
2841: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
2842: STOQ(index[i],tvect[i]);
2843: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2844: }
2845: }
2846:
2847: int gauss_elim_geninv_sf_swap(int **mat,int row,int col,
2848: int ***invmatp,int **indexp)
2849: {
2850: int i,j,k,inv,a,n,m,u;
2851: int *t,*pivot,*s;
2852: int *index;
2853: int **invmat;
2854:
2855: n = col; m = row+col;
2856: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
2857: for ( i = 0; i < row; i++ )
2858: index[i] = i;
2859: for ( j = 0; j < n; j++ ) {
2860: for ( i = j; i < row && !mat[i][j]; i++ );
2861: if ( i == row ) {
2862: *indexp = 0; *invmatp = 0; return 1;
2863: }
2864: if ( i != j ) {
2865: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2866: k = index[i]; index[i] = index[j]; index[j] = k;
2867: }
2868: pivot = mat[j];
2869: inv = _invsf(pivot[j]);
2870: for ( k = j; k < m; k++ )
2871: if ( pivot[k] )
2872: pivot[k] = _mulsf(pivot[k],inv);
2873: for ( i = j+1; i < row; i++ ) {
2874: t = mat[i];
2875: if ( a = t[j] )
2876: for ( k = j, a = _chsgnsf(a); k < m; k++ )
2877: if ( pivot[k] ) {
2878: u = _mulsf(pivot[k],a);
2879: t[k] = _addsf(u,t[k]);
2880: }
2881: }
2882: }
2883: for ( j = n-1; j >= 0; j-- ) {
2884: pivot = mat[j];
2885: for ( i = j-1; i >= 0; i-- ) {
2886: t = mat[i];
2887: if ( a = t[j] )
2888: for ( k = j, a = _chsgnsf(a); k < m; k++ )
2889: if ( pivot[k] ) {
2890: u = _mulsf(pivot[k],a);
2891: t[k] = _addsf(u,t[k]);
2892: }
2893: }
2894: }
2895: *invmatp = invmat = (int **)almat(col,col);
1.1 noro 2896: for ( i = 0; i < col; i++ )
2897: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
2898: s[j] = t[col+index[j]];
2899: return 0;
2900: }
2901:
2902: void _addn(N,N,N);
2903: int _subn(N,N,N);
2904: void _muln(N,N,N);
2905:
1.24 noro 2906: void inner_product_int(Q *a,Q *b,int n,Q *r)
1.1 noro 2907: {
2908: int la,lb,i;
2909: int sgn,sgn1;
2910: N wm,wma,sum,t;
2911:
2912: for ( la = lb = 0, i = 0; i < n; i++ ) {
2913: if ( a[i] )
2914: if ( DN(a[i]) )
2915: error("inner_product_int : invalid argument");
2916: else
2917: la = MAX(PL(NM(a[i])),la);
2918: if ( b[i] )
2919: if ( DN(b[i]) )
2920: error("inner_product_int : invalid argument");
2921: else
2922: lb = MAX(PL(NM(b[i])),lb);
2923: }
2924: sgn = 0;
2925: sum= NALLOC(la+lb+2);
2926: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
2927: wm = NALLOC(la+lb+2);
2928: wma = NALLOC(la+lb+2);
2929: for ( i = 0; i < n; i++ ) {
2930: if ( !a[i] || !b[i] )
2931: continue;
2932: _muln(NM(a[i]),NM(b[i]),wm);
2933: sgn1 = SGN(a[i])*SGN(b[i]);
2934: if ( !sgn ) {
2935: sgn = sgn1;
2936: t = wm; wm = sum; sum = t;
2937: } else if ( sgn == sgn1 ) {
2938: _addn(sum,wm,wma);
2939: if ( !PL(wma) )
2940: sgn = 0;
2941: t = wma; wma = sum; sum = t;
2942: } else {
2943: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
2944: sgn *= _subn(sum,wm,wma);
2945: t = wma; wma = sum; sum = t;
2946: }
2947: }
2948: GC_free(wm);
2949: GC_free(wma);
2950: if ( !sgn ) {
2951: GC_free(sum);
2952: *r = 0;
2953: } else
2954: NTOQ(sum,sgn,*r);
2955: }
2956:
1.3 noro 2957: /* (k,l) element of a*b where a: .x n matrix, b: n x . integer matrix */
2958:
1.24 noro 2959: void inner_product_mat_int_mod(Q **a,int **b,int n,int k,int l,Q *r)
1.3 noro 2960: {
2961: int la,lb,i;
2962: int sgn,sgn1;
2963: N wm,wma,sum,t;
2964: Q aki;
2965: int bil,bilsgn;
2966: struct oN tn;
2967:
2968: for ( la = 0, i = 0; i < n; i++ ) {
2969: if ( aki = a[k][i] )
2970: if ( DN(aki) )
2971: error("inner_product_int : invalid argument");
2972: else
2973: la = MAX(PL(NM(aki)),la);
2974: }
2975: lb = 1;
2976: sgn = 0;
2977: sum= NALLOC(la+lb+2);
2978: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
2979: wm = NALLOC(la+lb+2);
2980: wma = NALLOC(la+lb+2);
2981: for ( i = 0; i < n; i++ ) {
2982: if ( !(aki = a[k][i]) || !(bil = b[i][l]) )
2983: continue;
2984: tn.p = 1;
2985: if ( bil > 0 ) {
2986: tn.b[0] = bil; bilsgn = 1;
2987: } else {
2988: tn.b[0] = -bil; bilsgn = -1;
2989: }
2990: _muln(NM(aki),&tn,wm);
2991: sgn1 = SGN(aki)*bilsgn;
2992: if ( !sgn ) {
2993: sgn = sgn1;
2994: t = wm; wm = sum; sum = t;
2995: } else if ( sgn == sgn1 ) {
2996: _addn(sum,wm,wma);
2997: if ( !PL(wma) )
2998: sgn = 0;
2999: t = wma; wma = sum; sum = t;
3000: } else {
3001: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
3002: sgn *= _subn(sum,wm,wma);
3003: t = wma; wma = sum; sum = t;
3004: }
3005: }
3006: GC_free(wm);
3007: GC_free(wma);
3008: if ( !sgn ) {
3009: GC_free(sum);
3010: *r = 0;
3011: } else
3012: NTOQ(sum,sgn,*r);
3013: }
3014:
1.24 noro 3015: void Pmul_mat_vect_int(NODE arg,VECT *rp)
1.1 noro 3016: {
3017: MAT mat;
3018: VECT vect,r;
3019: int row,col,i;
3020:
3021: mat = (MAT)ARG0(arg);
3022: vect = (VECT)ARG1(arg);
3023: row = mat->row;
3024: col = mat->col;
3025: MKVECT(r,row);
1.24 noro 3026: for ( i = 0; i < row; i++ ) {
3027: inner_product_int((Q *)mat->body[i],(Q *)vect->body,col,(Q *)&r->body[i]);
3028: }
1.1 noro 3029: *rp = r;
3030: }
3031:
1.24 noro 3032: void Pnbpoly_up2(NODE arg,GF2N *rp)
1.1 noro 3033: {
3034: int m,type,ret;
3035: UP2 r;
3036:
3037: m = QTOS((Q)ARG0(arg));
3038: type = QTOS((Q)ARG1(arg));
3039: ret = generate_ONB_polynomial(&r,m,type);
3040: if ( ret == 0 )
3041: MKGF2N(r,*rp);
3042: else
3043: *rp = 0;
3044: }
3045:
1.24 noro 3046: void Px962_irredpoly_up2(NODE arg,GF2N *rp)
1.1 noro 3047: {
1.24 noro 3048: int m,ret,w;
1.1 noro 3049: GF2N prev;
3050: UP2 r;
3051:
3052: m = QTOS((Q)ARG0(arg));
3053: prev = (GF2N)ARG1(arg);
3054: if ( !prev ) {
3055: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
3056: bzero((char *)r->b,w*sizeof(unsigned int));
3057: } else {
3058: r = prev->body;
3059: if ( degup2(r) != m ) {
3060: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
3061: bzero((char *)r->b,w*sizeof(unsigned int));
3062: }
3063: }
1.24 noro 3064: ret = _generate_irreducible_polynomial(r,m);
1.1 noro 3065: if ( ret == 0 )
3066: MKGF2N(r,*rp);
3067: else
3068: *rp = 0;
3069: }
3070:
1.24 noro 3071: void Pirredpoly_up2(NODE arg,GF2N *rp)
1.1 noro 3072: {
1.24 noro 3073: int m,ret,w;
1.1 noro 3074: GF2N prev;
3075: UP2 r;
3076:
3077: m = QTOS((Q)ARG0(arg));
3078: prev = (GF2N)ARG1(arg);
3079: if ( !prev ) {
3080: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
3081: bzero((char *)r->b,w*sizeof(unsigned int));
3082: } else {
3083: r = prev->body;
3084: if ( degup2(r) != m ) {
3085: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
3086: bzero((char *)r->b,w*sizeof(unsigned int));
3087: }
3088: }
1.24 noro 3089: ret = _generate_good_irreducible_polynomial(r,m);
1.1 noro 3090: if ( ret == 0 )
3091: MKGF2N(r,*rp);
3092: else
3093: *rp = 0;
3094: }
3095:
1.26 noro 3096: void Pmat_swap_row_destructive(NODE arg, MAT *m)
3097: {
3098: int i1,i2;
3099: pointer *t;
3100: MAT mat;
3101:
3102: asir_assert(ARG0(arg),O_MAT,"mat_swap_row_destructive");
3103: asir_assert(ARG1(arg),O_N,"mat_swap_row_destructive");
3104: asir_assert(ARG2(arg),O_N,"mat_swap_row_destructive");
3105: mat = (MAT)ARG0(arg);
3106: i1 = QTOS((Q)ARG1(arg));
3107: i2 = QTOS((Q)ARG2(arg));
3108: if ( i1 < 0 || i2 < 0 || i1 >= mat->row || i2 >= mat->row )
3109: error("mat_swap_row_destructive : Out of range");
3110: t = mat->body[i1];
3111: mat->body[i1] = mat->body[i2];
3112: mat->body[i2] = t;
3113: *m = mat;
3114: }
3115:
3116: void Pmat_swap_col_destructive(NODE arg, MAT *m)
3117: {
3118: int j1,j2,i,n;
3119: pointer *mi;
3120: pointer t;
3121: MAT mat;
3122:
3123: asir_assert(ARG0(arg),O_MAT,"mat_swap_col_destructive");
3124: asir_assert(ARG1(arg),O_N,"mat_swap_col_destructive");
3125: asir_assert(ARG2(arg),O_N,"mat_swap_col_destructive");
3126: mat = (MAT)ARG0(arg);
3127: j1 = QTOS((Q)ARG1(arg));
3128: j2 = QTOS((Q)ARG2(arg));
3129: if ( j1 < 0 || j2 < 0 || j1 >= mat->col || j2 >= mat->col )
3130: error("mat_swap_col_destructive : Out of range");
3131: n = mat->row;
3132: for ( i = 0; i < n; i++ ) {
3133: mi = mat->body[i];
3134: t = mi[j1]; mi[j1] = mi[j2]; mi[j2] = t;
3135: }
3136: *m = mat;
3137: }
1.1 noro 3138: /*
3139: * f = type 'type' normal polynomial of degree m if exists
3140: * IEEE P1363 A.7.2
3141: *
3142: * return value : 0 --- exists
3143: * 1 --- does not exist
3144: * -1 --- failure (memory allocation error)
3145: */
3146:
3147: int generate_ONB_polynomial(UP2 *rp,int m,int type)
3148: {
3149: int i,r;
3150: int w;
3151: UP2 f,f0,f1,f2,t;
3152:
3153: w = (m>>5)+1;
3154: switch ( type ) {
3155: case 1:
3156: if ( !TypeT_NB_check(m,1) ) return 1;
3157: NEWUP2(f,w); *rp = f; f->w = w;
3158: /* set all the bits */
3159: for ( i = 0; i < w; i++ )
3160: f->b[i] = 0xffffffff;
3161: /* mask the top word if necessary */
3162: if ( r = (m+1)&31 )
3163: f->b[w-1] &= (1<<r)-1;
3164: return 0;
3165: break;
3166: case 2:
3167: if ( !TypeT_NB_check(m,2) ) return 1;
3168: NEWUP2(f,w); *rp = f;
3169: W_NEWUP2(f0,w);
3170: W_NEWUP2(f1,w);
3171: W_NEWUP2(f2,w);
3172:
3173: /* recursion for genrating Type II normal polynomial */
3174:
3175: /* f0 = 1, f1 = t+1 */
3176: f0->w = 1; f0->b[0] = 1;
3177: f1->w = 1; f1->b[0] = 3;
3178: for ( i = 2; i <= m; i++ ) {
3179: /* f2 = t*f1+f0 */
3180: _bshiftup2(f1,-1,f2);
3181: _addup2_destructive(f2,f0);
3182: /* cyclic change of the variables */
3183: t = f0; f0 = f1; f1 = f2; f2 = t;
3184: }
3185: _copyup2(f1,f);
3186: return 0;
3187: break;
3188: default:
3189: return -1;
3190: break;
3191: }
3192: }
3193:
3194: /*
3195: * f = an irreducible trinomial or pentanomial of degree d 'after' f
3196: * return value : 0 --- exists
3197: * 1 --- does not exist (exhaustion)
3198: */
3199:
3200: int _generate_irreducible_polynomial(UP2 f,int d)
3201: {
3202: int ret,i,j,k,nz,i0,j0,k0;
3203: int w;
3204: unsigned int *fd;
3205:
3206: /*
3207: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
3208: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
3209: * otherwise i0,j0,k0 is set to 0.
3210: */
3211:
3212: fd = f->b;
3213: w = (d>>5)+1;
3214: if ( f->w && (d==degup2(f)) ) {
3215: for ( nz = 0, i = d; i >= 0; i-- )
3216: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
3217: switch ( nz ) {
3218: case 3:
3219: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
3220: /* reset i0-th bit */
3221: fd[i0>>5] &= ~(1<<(i0&31));
3222: j0 = k0 = 0;
3223: break;
3224: case 5:
3225: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
3226: /* reset i0-th bit */
3227: fd[i0>>5] &= ~(1<<(i0&31));
3228: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
3229: /* reset j0-th bit */
3230: fd[j0>>5] &= ~(1<<(j0&31));
3231: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
3232: /* reset k0-th bit */
3233: fd[k0>>5] &= ~(1<<(k0&31));
3234: break;
3235: default:
3236: f->w = 0; break;
3237: }
3238: } else
3239: f->w = 0;
3240:
3241: if ( !f->w ) {
3242: fd = f->b;
3243: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
3244: i0 = j0 = k0 = 0;
3245: }
3246: /* if j0 > 0 then f is already a pentanomial */
3247: if ( j0 > 0 ) goto PENTA;
3248:
3249: /* searching for an irreducible trinomial */
3250:
3251: for ( i = 1; 2*i <= d; i++ ) {
3252: /* skip the polynomials 'before' f */
3253: if ( i < i0 ) continue;
3254: if ( i == i0 ) { i0 = 0; continue; }
3255: /* set i-th bit */
3256: fd[i>>5] |= (1<<(i&31));
3257: ret = irredcheck_dddup2(f);
3258: if ( ret == 1 ) return 0;
3259: /* reset i-th bit */
3260: fd[i>>5] &= ~(1<<(i&31));
3261: }
3262:
3263: /* searching for an irreducible pentanomial */
3264: PENTA:
3265: for ( i = 1; i < d; i++ ) {
3266: /* skip the polynomials 'before' f */
3267: if ( i < i0 ) continue;
3268: if ( i == i0 ) i0 = 0;
3269: /* set i-th bit */
3270: fd[i>>5] |= (1<<(i&31));
3271: for ( j = i+1; j < d; j++ ) {
3272: /* skip the polynomials 'before' f */
3273: if ( j < j0 ) continue;
3274: if ( j == j0 ) j0 = 0;
3275: /* set j-th bit */
3276: fd[j>>5] |= (1<<(j&31));
3277: for ( k = j+1; k < d; k++ ) {
3278: /* skip the polynomials 'before' f */
3279: if ( k < k0 ) continue;
3280: else if ( k == k0 ) { k0 = 0; continue; }
3281: /* set k-th bit */
3282: fd[k>>5] |= (1<<(k&31));
3283: ret = irredcheck_dddup2(f);
3284: if ( ret == 1 ) return 0;
3285: /* reset k-th bit */
3286: fd[k>>5] &= ~(1<<(k&31));
3287: }
3288: /* reset j-th bit */
3289: fd[j>>5] &= ~(1<<(j&31));
3290: }
3291: /* reset i-th bit */
3292: fd[i>>5] &= ~(1<<(i&31));
3293: }
3294: /* exhausted */
3295: return 1;
3296: }
3297:
3298: /*
3299: * f = an irreducible trinomial or pentanomial of degree d 'after' f
3300: *
3301: * searching strategy:
3302: * trinomial x^d+x^i+1:
3303: * i is as small as possible.
3304: * trinomial x^d+x^i+x^j+x^k+1:
3305: * i is as small as possible.
3306: * For such i, j is as small as possible.
3307: * For such i and j, 'k' is as small as possible.
3308: *
3309: * return value : 0 --- exists
3310: * 1 --- does not exist (exhaustion)
3311: */
3312:
3313: int _generate_good_irreducible_polynomial(UP2 f,int d)
3314: {
3315: int ret,i,j,k,nz,i0,j0,k0;
3316: int w;
3317: unsigned int *fd;
3318:
3319: /*
3320: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
3321: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
3322: * otherwise i0,j0,k0 is set to 0.
3323: */
3324:
3325: fd = f->b;
3326: w = (d>>5)+1;
3327: if ( f->w && (d==degup2(f)) ) {
3328: for ( nz = 0, i = d; i >= 0; i-- )
3329: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
3330: switch ( nz ) {
3331: case 3:
3332: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
3333: /* reset i0-th bit */
3334: fd[i0>>5] &= ~(1<<(i0&31));
3335: j0 = k0 = 0;
3336: break;
3337: case 5:
3338: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
3339: /* reset i0-th bit */
3340: fd[i0>>5] &= ~(1<<(i0&31));
3341: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
3342: /* reset j0-th bit */
3343: fd[j0>>5] &= ~(1<<(j0&31));
3344: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
3345: /* reset k0-th bit */
3346: fd[k0>>5] &= ~(1<<(k0&31));
3347: break;
3348: default:
3349: f->w = 0; break;
3350: }
3351: } else
3352: f->w = 0;
3353:
3354: if ( !f->w ) {
3355: fd = f->b;
3356: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
3357: i0 = j0 = k0 = 0;
3358: }
3359: /* if j0 > 0 then f is already a pentanomial */
3360: if ( j0 > 0 ) goto PENTA;
3361:
3362: /* searching for an irreducible trinomial */
3363:
3364: for ( i = 1; 2*i <= d; i++ ) {
3365: /* skip the polynomials 'before' f */
3366: if ( i < i0 ) continue;
3367: if ( i == i0 ) { i0 = 0; continue; }
3368: /* set i-th bit */
3369: fd[i>>5] |= (1<<(i&31));
3370: ret = irredcheck_dddup2(f);
3371: if ( ret == 1 ) return 0;
3372: /* reset i-th bit */
3373: fd[i>>5] &= ~(1<<(i&31));
3374: }
3375:
3376: /* searching for an irreducible pentanomial */
3377: PENTA:
3378: for ( i = 3; i < d; i++ ) {
3379: /* skip the polynomials 'before' f */
3380: if ( i < i0 ) continue;
3381: if ( i == i0 ) i0 = 0;
3382: /* set i-th bit */
3383: fd[i>>5] |= (1<<(i&31));
3384: for ( j = 2; j < i; j++ ) {
3385: /* skip the polynomials 'before' f */
3386: if ( j < j0 ) continue;
3387: if ( j == j0 ) j0 = 0;
3388: /* set j-th bit */
3389: fd[j>>5] |= (1<<(j&31));
3390: for ( k = 1; k < j; k++ ) {
3391: /* skip the polynomials 'before' f */
3392: if ( k < k0 ) continue;
3393: else if ( k == k0 ) { k0 = 0; continue; }
3394: /* set k-th bit */
3395: fd[k>>5] |= (1<<(k&31));
3396: ret = irredcheck_dddup2(f);
3397: if ( ret == 1 ) return 0;
3398: /* reset k-th bit */
3399: fd[k>>5] &= ~(1<<(k&31));
3400: }
3401: /* reset j-th bit */
3402: fd[j>>5] &= ~(1<<(j&31));
3403: }
3404: /* reset i-th bit */
3405: fd[i>>5] &= ~(1<<(i&31));
3406: }
3407: /* exhausted */
3408: return 1;
1.3 noro 3409: }
3410:
1.24 noro 3411: void printqmat(Q **mat,int row,int col)
1.3 noro 3412: {
3413: int i,j;
3414:
3415: for ( i = 0; i < row; i++ ) {
3416: for ( j = 0; j < col; j++ ) {
1.8 noro 3417: printnum((Num)mat[i][j]); printf(" ");
1.3 noro 3418: }
3419: printf("\n");
3420: }
3421: }
3422:
1.24 noro 3423: void printimat(int **mat,int row,int col)
1.3 noro 3424: {
3425: int i,j;
3426:
3427: for ( i = 0; i < row; i++ ) {
3428: for ( j = 0; j < col; j++ ) {
3429: printf("%d ",mat[i][j]);
3430: }
3431: printf("\n");
3432: }
1.36 noro 3433: }
3434:
3435: void Pnd_det(NODE arg,P *rp)
3436: {
1.37 noro 3437: if ( argc(arg) == 1 )
3438: nd_det(0,ARG0(arg),rp);
3439: else
3440: nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp);
1.1 noro 3441: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>