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