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