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