Annotation of OpenXM_contrib2/asir2000/builtin/array.c, Revision 1.78
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.78 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/array.c,v 1.77 2019/03/03 05:21:16 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.76 noro 107: {"lu_mat",Plu_mat,1},
108: {"solve_by_lu_gfmmat",Psolve_by_lu_gfmmat,4},
109: {"lu_gfmmat",Plu_gfmmat,2},
110: {"mat_to_gfmmat",Pmat_to_gfmmat,2},
111: {"generic_gauss_elim",Pgeneric_gauss_elim,1},
112: {"generic_gauss_elim_mod",Pgeneric_gauss_elim_mod,2},
113: {"indep_rows_mod",Pindep_rows_mod,2},
114: {"newvect",Pnewvect,-2},
115: {"vect",Pvect,-99999999},
116: {"vector",Pnewvect,-2},
117: {"exponent_vector",Pexponent_vector,-99999999},
118: {"newmat",Pnewmat,-3},
119: {"matrix",Pnewmat,-3},
120: {"mat",Pmat,-99999999},
121: {"matr",Pmat,-99999999},
122: {"matc",Pmatc,-99999999},
123: {"newbytearray",Pnewbytearray,-2},
124: {"memoryplot_to_coord",Pmemoryplot_to_coord,1},
125: {"sepmat_destructive",Psepmat_destructive,2},
126: {"sepvect",Psepvect,2},
127: {"qsort",Pqsort,-2},
128: {"vtol",Pvtol,1},
129: {"ltov",Pltov,1},
130: {"size",Psize,1},
131: {"det",Pdet,-2},
132: {"nd_det",Pnd_det,-2},
133: {"invmat",Pinvmat,-2},
134: {"leqm",Pleqm,2},
135: {"leqm1",Pleqm1,2},
136: {"geninvm",Pgeninvm,2},
137: {"geninvm_swap",Pgeninvm_swap,2},
138: {"geninv_sf_swap",Pgeninv_sf_swap,1},
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},
149: {"mat_swap_row_destructive",Pmat_swap_row_destructive,3},
150: {"mat_swap_col_destructive",Pmat_swap_col_destructive,3},
151: {"mat_col",Pmat_col,2},
152: {"lusolve_prep",Plusolve_prep,1},
153: {"lusolve_main",Plusolve_main,1},
154: {"triangleq",Ptriangleq,1},
155: {0,0,0},
1.1 noro 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: {
1.76 noro 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;
1.63 noro 195: }
196:
197: void Plusolve_main(NODE arg,VECT *rp)
198: {
1.76 noro 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;
1.63 noro 216: }
217:
218: ent *get_row(FILE *in,int *l)
219: {
1.76 noro 220: int len,i;
221: ent *a;
1.63 noro 222:
1.76 noro 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;
1.63 noro 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: {
1.76 noro 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: }
1.63 noro 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: {
1.76 noro 257: int len;
258: ent *p;
1.63 noro 259:
1.76 noro 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: }
1.63 noro 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: {
1.76 noro 280: ent *ak,*ai,*w;
281: int lk,li,j,m,p,q,r,s,t,j0;
1.63 noro 282:
1.76 noro 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;
1.63 noro 324: }
325:
1.72 ohara 326: void solve_l(int *ll,ent **l,int n,int *rhs,int mod)
1.63 noro 327: {
1.76 noro 328: int j,k,s,len;
329: ent *p;
1.63 noro 330:
1.76 noro 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: }
1.63 noro 338: }
339:
1.72 ohara 340: void solve_u(int *ul,ent **u,int n,int *rhs,int mod)
1.63 noro 341: {
1.76 noro 342: int j,k,s,len,inv;
343: ent *p;
1.63 noro 344:
1.76 noro 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: }
1.63 noro 354: }
355:
1.24 noro 356: int comp_obj(Obj *a,Obj *b)
1.1 noro 357: {
1.76 noro 358: return arf_comp(CO,*a,*b);
1.1 noro 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: {
1.76 noro 367: Q r;
368:
369: BDY(generic_comp_obj_arg)=(pointer)(*a);
370: BDY(NEXT(generic_comp_obj_arg))=(pointer)(*b);
371: r = (Q)bevalf_with_opts(generic_comp_obj_func,generic_comp_obj_arg,generic_comp_obj_option);
372: if ( !r )
373: return 0;
374: else
375: return SGN(r)>0?1:-1;
1.1 noro 376: }
377:
378:
1.46 saito 379: void Pqsort(NODE arg,LIST *rp)
1.1 noro 380: {
1.76 noro 381: VECT vect;
382: NODE n,n1;
383: P p;
384: V v;
385: FUNC func;
386: int len,i;
387: pointer *a;
388: Obj t;
1.35 ohara 389:
1.76 noro 390: t = ARG0(arg);
1.35 ohara 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.76 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);
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;
418: MKNODE(n,0,0); MKNODE(generic_comp_obj_arg,0,n);
419: generic_comp_obj_option = current_option;
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: {
1.76 noro 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: }
1.1 noro 468: }
469:
1.24 noro 470: void Pmul_vect_mat_gf2n(NODE arg,GF2N *rp)
1.1 noro 471: {
1.76 noro 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: }
1.1 noro 499: }
500:
1.24 noro 501: void Pbconvmat_gf2n(NODE arg,LIST *rp)
1.1 noro 502: {
1.76 noro 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);
1.1 noro 519: }
520:
1.24 noro 521: void Pmulmat_gf2n(NODE arg,GF2MAT *rp)
1.1 noro 522: {
1.76 noro 523: GF2MAT m;
1.1 noro 524:
1.76 noro 525: if ( !compute_multiplication_matrix((P)ARG0(arg),&m) )
526: error("mulmat_gf2n : input is not a normal polynomial");
527: *rp = m;
1.1 noro 528: }
529:
1.24 noro 530: void Psepmat_destructive(NODE arg,LIST *rp)
1.1 noro 531: {
1.76 noro 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 ) */
553: /* GCFREE(nm); */
554: /* GCFREE(ent); */
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);
1.1 noro 559: }
560:
1.24 noro 561: void Psepvect(NODE arg,VECT *rp)
1.1 noro 562: {
1.76 noro 563: sepvect((VECT)ARG0(arg),QTOS((Q)ARG1(arg)),rp);
1.1 noro 564: }
565:
1.24 noro 566: void sepvect(VECT v,int d,VECT *rp)
1.1 noro 567: {
1.76 noro 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: }
1.1 noro 588: }
589:
1.24 noro 590: void Pnewvect(NODE arg,VECT *rp)
1.1 noro 591: {
1.76 noro 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));
600: if ( len < 0 )
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.76 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.76 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.76 noro 620: int len,i;
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++);
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: }
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;
1.28 saito 652: }
653:
1.24 noro 654: void Pexponent_vector(NODE arg,DP *rp)
1.14 noro 655: {
1.76 noro 656: nodetod(arg,rp);
1.9 noro 657: }
658:
1.24 noro 659: void Pnewbytearray(NODE arg,BYTEARRAY *rp)
1.9 noro 660: {
1.76 noro 661: int len,i,r;
662: BYTEARRAY array;
663: unsigned char *vb;
664: char *str;
665: LIST list;
666: NODE tn;
667: int ac;
668: struct stat sbuf;
669: char *fname;
670: FILE *fp;
671:
672: ac = argc(arg);
673: if ( ac == 1 ) {
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: }
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);
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");
725: }
726: } else
727: error("newbytearray : invalid argument");
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: {
1.76 noro 735: int len,blen,y,i,j;
736: unsigned char *a;
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: {
1.76 noro 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));
774: if ( row < 0 || col < 0 )
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: }
794: *rp = m;
1.28 saito 795: }
796:
797: void Pmat(NODE arg, MAT *rp)
798: {
1.76 noro 799: int row,col;
800: int i;
801: MAT m;
802: pointer **mb;
803: pointer *ent;
804: NODE tn, sn;
805: VECT v;
806:
807: if ( !arg ) {
808: *rp =0;
809: return;
810: }
811:
812: for (row = 0, tn = arg; tn; tn = NEXT(tn), row++);
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: }
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++);
826: } else {
827: error("mat : invalid argument");
828: }
829:
830: MKMAT(m,row,col);
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;
1.29 saito 846: }
847:
848: void Pmatc(NODE arg, MAT *rp)
849: {
1.76 noro 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++);
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: }
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++);
877: } else {
878: error("matc : invalid argument");
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: }
896: *rp = m;
1.1 noro 897: }
898:
1.24 noro 899: void Pvtol(NODE arg,LIST *rp)
1.1 noro 900: {
1.76 noro 901: NODE n,n1;
902: VECT v;
903: pointer *a;
904: int len,i;
905:
906: if ( OID(ARG0(arg)) == O_LIST ) {
907: *rp = ARG0(arg);
908: return;
909: }
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: {
1.76 noro 920: NODE n;
921: VECT v,v0;
922: int len,i;
923:
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: }
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: {
1.76 noro 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: }
1.1 noro 988: }
989:
1.24 noro 990: void Psremainder(NODE arg,Obj *rp)
1.1 noro 991: {
1.76 noro 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: }
1.1 noro 1037: }
1038:
1.24 noro 1039: void Psize(NODE arg,LIST *rp)
1.1 noro 1040: {
1041:
1.76 noro 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;
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;
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);
1.1 noro 1067: }
1068:
1.24 noro 1069: void Pdet(NODE arg,P *rp)
1.1 noro 1070: {
1.76 noro 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);
1090: }
1.23 noro 1091: }
1092:
1.24 noro 1093: void Pinvmat(NODE arg,LIST *rp)
1.23 noro 1094: {
1.76 noro 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]);
1.23 noro 1117: #if 0
1.76 noro 1118: detmp(CO,mod,w,n,&d);
1119: mptop(d,rp);
1.23 noro 1120: #else
1.76 noro 1121: error("not implemented yet");
1.23 noro 1122: #endif
1.76 noro 1123: }
1.25 noro 1124: }
1125:
1126: /*
1.76 noro 1127: input : a row x col matrix A
1128: A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
1.25 noro 1129:
1.76 noro 1130: output : [B,D,R,C]
1131: B : a rank(A) x col-rank(A) matrix
1132: D : the denominator
1133: R : a vector of length rank(A)
1134: C : a vector of length col-rank(A)
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.76 noro 1140: NODE n0,opt,p;
1141: MAT m,nm;
1142: int *ri,*ci;
1143: VECT rind,cind;
1144: Q dn,q;
1145: int i,row,col,t,rank;
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: }
1161: asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim");
1162: m = (MAT)ARG0(arg);
1163: row = m->row; col = m->col;
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);
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.78 ! noro 1183: int indep_rows_mod(int **mat0,int row,int col,int md,int *rowstat);
! 1184:
1.69 noro 1185: void Pindep_rows_mod(NODE arg,VECT *rp)
1186: {
1.76 noro 1187: MAT m,mat;
1188: VECT rind;
1189: Q **tmat;
1190: int **wmat,**row0;
1191: Q *rib;
1192: int *rowstat,*p;
1193: Q q;
1194: int md,i,j,k,l,row,col,t,rank;
1195:
1196: asir_assert(ARG0(arg),O_MAT,"indep_rows_mod");
1197: asir_assert(ARG1(arg),O_N,"indep_rows_mod");
1198: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
1199: row = m->row; col = m->col; tmat = (Q **)m->body;
1200: wmat = (int **)almat(row,col);
1201:
1202: row0 = (int **)ALLOCA(row*sizeof(int *));
1203: for ( i = 0; i < row; i++ ) row0[i] = wmat[i];
1204:
1205: rowstat = (int *)MALLOC_ATOMIC(row*sizeof(int));
1206: for ( i = 0; i < row; i++ )
1207: for ( j = 0; j < col; j++ )
1208: if ( q = (Q)tmat[i][j] ) {
1209: t = rem(NM(q),md);
1210: if ( t && SGN(q) < 0 )
1211: t = (md - t) % md;
1212: wmat[i][j] = t;
1213: } else
1214: wmat[i][j] = 0;
1215: rank = indep_rows_mod(wmat,row,col,md,rowstat);
1216:
1217: MKVECT(rind,rank);
1218: rib = (Q *)rind->body;
1219: for ( j = 0; j < rank; j++ ) {
1220: STOQ(rowstat[j],rib[j]);
1221: }
1.69 noro 1222: *rp = rind;
1223: }
1224:
1.1 noro 1225: /*
1.76 noro 1226: input : a row x col matrix A
1227: A[I] <-> A[I][0]*x_0+A[I][1]*x_1+...
1.1 noro 1228:
1.76 noro 1229: output : [B,R,C]
1230: B : a rank(A) x col-rank(A) matrix
1231: R : a vector of length rank(A)
1232: C : a vector of length col-rank(A)
1233: RN : a vector of length rank(A) indicating useful rows
1.47 noro 1234:
1.76 noro 1235: B[I] <-> x_{R[I]}+B[I][0]x_{C[0]}+B[I][1]x_{C[1]}+...
1.1 noro 1236: */
1237:
1.24 noro 1238: void Pgeneric_gauss_elim_mod(NODE arg,LIST *rp)
1.1 noro 1239: {
1.76 noro 1240: NODE n0;
1241: MAT m,mat;
1242: VECT rind,cind,rnum;
1243: Q **tmat;
1244: int **wmat,**row0;
1245: Q *rib,*cib,*rnb;
1246: int *colstat,*p;
1247: Q q;
1248: int md,i,j,k,l,row,col,t,rank;
1249:
1250: asir_assert(ARG0(arg),O_MAT,"generic_gauss_elim_mod");
1251: asir_assert(ARG1(arg),O_N,"generic_gauss_elim_mod");
1252: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
1253: row = m->row; col = m->col; tmat = (Q **)m->body;
1254: wmat = (int **)almat(row,col);
1255:
1256: row0 = (int **)ALLOCA(row*sizeof(int *));
1257: for ( i = 0; i < row; i++ ) row0[i] = wmat[i];
1258:
1259: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1260: for ( i = 0; i < row; i++ )
1261: for ( j = 0; j < col; j++ )
1262: if ( q = (Q)tmat[i][j] ) {
1263: t = rem(NM(q),md);
1264: if ( t && SGN(q) < 0 )
1265: t = (md - t) % md;
1266: wmat[i][j] = t;
1267: } else
1268: wmat[i][j] = 0;
1269: rank = generic_gauss_elim_mod(wmat,row,col,md,colstat);
1270:
1271: MKVECT(rnum,rank);
1272: rnb = (Q *)rnum->body;
1273: for ( i = 0; i < rank; i++ )
1274: for ( j = 0, p = wmat[i]; j < row; j++ )
1275: if ( p == row0[j] )
1276: STOQ(j,rnb[i]);
1277:
1278: MKMAT(mat,rank,col-rank);
1279: tmat = (Q **)mat->body;
1280: for ( i = 0; i < rank; i++ )
1281: for ( j = k = 0; j < col; j++ )
1282: if ( !colstat[j] ) {
1283: UTOQ(wmat[i][j],tmat[i][k]); k++;
1284: }
1285:
1286: MKVECT(rind,rank);
1287: MKVECT(cind,col-rank);
1288: rib = (Q *)rind->body; cib = (Q *)cind->body;
1289: for ( j = k = l = 0; j < col; j++ )
1290: if ( colstat[j] ) {
1291: STOQ(j,rib[k]); k++;
1292: } else {
1293: STOQ(j,cib[l]); l++;
1294: }
1295: n0 = mknode(4,mat,rind,cind,rnum);
1296: MKLIST(*rp,n0);
1.1 noro 1297: }
1298:
1.24 noro 1299: void Pleqm(NODE arg,VECT *rp)
1.1 noro 1300: {
1.76 noro 1301: MAT m;
1302: VECT vect;
1303: pointer **mat;
1304: Q *v;
1305: Q q;
1306: int **wmat;
1307: int md,i,j,row,col,t,n,status;
1308:
1309: asir_assert(ARG0(arg),O_MAT,"leqm");
1310: asir_assert(ARG1(arg),O_N,"leqm");
1311: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
1312: row = m->row; col = m->col; mat = m->body;
1313: wmat = (int **)almat(row,col);
1314: for ( i = 0; i < row; i++ )
1315: for ( j = 0; j < col; j++ )
1316: if ( q = (Q)mat[i][j] ) {
1317: t = rem(NM(q),md);
1318: if ( SGN(q) < 0 )
1319: t = (md - t) % md;
1320: wmat[i][j] = t;
1321: } else
1322: wmat[i][j] = 0;
1323: status = gauss_elim_mod(wmat,row,col,md);
1324: if ( status < 0 )
1325: *rp = 0;
1326: else if ( status > 0 )
1327: *rp = (VECT)ONE;
1328: else {
1329: n = col - 1;
1330: MKVECT(vect,n);
1331: for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
1332: t = (md-wmat[i][n])%md; STOQ(t,v[i]);
1333: }
1334: *rp = vect;
1335: }
1.1 noro 1336: }
1337:
1.24 noro 1338: int gauss_elim_mod(int **mat,int row,int col,int md)
1.1 noro 1339: {
1.76 noro 1340: int i,j,k,inv,a,n;
1341: int *t,*pivot;
1.1 noro 1342:
1.76 noro 1343: n = col - 1;
1344: for ( j = 0; j < n; j++ ) {
1345: for ( i = j; i < row && !mat[i][j]; i++ );
1346: if ( i == row )
1347: return 1;
1348: if ( i != j ) {
1349: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
1350: }
1351: pivot = mat[j];
1352: inv = invm(pivot[j],md);
1353: for ( k = j; k <= n; k++ ) {
1354: /* pivot[k] = dmar(pivot[k],inv,0,md); */
1355: DMAR(pivot[k],inv,0,md,pivot[k])
1356: }
1357: for ( i = 0; i < row; i++ ) {
1358: t = mat[i];
1359: if ( i != j && (a = t[j]) )
1360: for ( k = j, a = md - a; k <= n; k++ ) {
1361: unsigned int tk;
1362: /* t[k] = dmar(pivot[k],a,t[k],md); */
1363: DMAR(pivot[k],a,t[k],md,tk)
1364: t[k] = tk;
1365: }
1366: }
1367: }
1368: for ( i = n; i < row && !mat[i][n]; i++ );
1369: if ( i == row )
1370: return 0;
1371: else
1372: return -1;
1.1 noro 1373: }
1374:
1.4 noro 1375: struct oEGT eg_mod,eg_elim,eg_elim1,eg_elim2,eg_chrem,eg_gschk,eg_intrat,eg_symb;
1.31 noro 1376: struct oEGT eg_conv;
1.1 noro 1377:
1.24 noro 1378: int generic_gauss_elim(MAT mat,MAT *nm,Q *dn,int **rindp,int **cindp)
1.1 noro 1379: {
1.76 noro 1380: int **wmat;
1381: Q **bmat;
1382: N **tmat;
1383: Q *bmi;
1384: N *tmi;
1385: Q q;
1386: int *wmi;
1387: int *colstat,*wcolstat,*rind,*cind;
1388: int row,col,ind,md,i,j,k,l,t,t1,rank,rank0,inv;
1389: N m1,m2,m3,s,u;
1390: MAT r,crmat;
1391: struct oEGT tmp0,tmp1;
1392: struct oEGT eg_mod_split,eg_elim_split,eg_chrem_split;
1393: struct oEGT eg_intrat_split,eg_gschk_split;
1394: int ret;
1395:
1396: init_eg(&eg_mod_split); init_eg(&eg_chrem_split);
1397: init_eg(&eg_elim_split); init_eg(&eg_intrat_split);
1398: init_eg(&eg_gschk_split);
1399: bmat = (Q **)mat->body;
1400: row = mat->row; col = mat->col;
1401: wmat = (int **)almat(row,col);
1402: colstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1403: wcolstat = (int *)MALLOC_ATOMIC(col*sizeof(int));
1404: for ( ind = 0; ; ind++ ) {
1405: if ( DP_Print ) {
1406: fprintf(asir_out,"."); fflush(asir_out);
1407: }
1408: md = get_lprime(ind);
1409: get_eg(&tmp0);
1410: for ( i = 0; i < row; i++ )
1411: for ( j = 0, bmi = bmat[i], wmi = wmat[i]; j < col; j++ )
1412: if ( q = (Q)bmi[j] ) {
1413: t = rem(NM(q),md);
1414: if ( t && SGN(q) < 0 )
1415: t = (md - t) % md;
1416: wmi[j] = t;
1417: } else
1418: wmi[j] = 0;
1419: get_eg(&tmp1);
1420: add_eg(&eg_mod,&tmp0,&tmp1);
1421: add_eg(&eg_mod_split,&tmp0,&tmp1);
1422: get_eg(&tmp0);
1423: rank = generic_gauss_elim_mod(wmat,row,col,md,wcolstat);
1424: get_eg(&tmp1);
1425: add_eg(&eg_elim,&tmp0,&tmp1);
1426: add_eg(&eg_elim_split,&tmp0,&tmp1);
1427: if ( !ind ) {
1.1 noro 1428: RESET:
1.76 noro 1429: UTON(md,m1);
1430: rank0 = rank;
1431: bcopy(wcolstat,colstat,col*sizeof(int));
1432: MKMAT(crmat,rank,col-rank);
1433: MKMAT(r,rank,col-rank); *nm = r;
1434: tmat = (N **)crmat->body;
1435: for ( i = 0; i < rank; i++ )
1436: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
1437: if ( !colstat[j] ) {
1438: UTON(wmi[j],tmi[k]); k++;
1439: }
1440: } else {
1441: if ( rank < rank0 ) {
1442: if ( DP_Print ) {
1443: fprintf(asir_out,"lower rank matrix; continuing...\n");
1444: fflush(asir_out);
1445: }
1446: continue;
1447: } else if ( rank > rank0 ) {
1448: if ( DP_Print ) {
1449: fprintf(asir_out,"higher rank matrix; resetting...\n");
1450: fflush(asir_out);
1451: }
1452: goto RESET;
1453: } else {
1454: for ( j = 0; (j<col) && (colstat[j]==wcolstat[j]); j++ );
1455: if ( j < col ) {
1456: if ( DP_Print ) {
1457: fprintf(asir_out,"inconsitent colstat; resetting...\n");
1458: fflush(asir_out);
1459: }
1460: goto RESET;
1461: }
1462: }
1463:
1464: get_eg(&tmp0);
1465: inv = invm(rem(m1,md),md);
1466: UTON(md,m2); muln(m1,m2,&m3);
1467: for ( i = 0; i < rank; i++ )
1468: for ( j = k = 0, tmi = tmat[i], wmi = wmat[i]; j < col; j++ )
1469: if ( !colstat[j] ) {
1470: if ( tmi[k] ) {
1471: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
1472: t = rem(tmi[k],md);
1473: if ( wmi[j] >= t )
1474: t = wmi[j]-t;
1475: else
1476: t = md-(t-wmi[j]);
1477: DMAR(t,inv,0,md,t1)
1478: UTON(t1,u);
1479: muln(m1,u,&s);
1480: addn(tmi[k],s,&u); tmi[k] = u;
1481: } else if ( wmi[j] ) {
1482: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
1483: DMAR(wmi[j],inv,0,md,t)
1484: UTON(t,u);
1485: muln(m1,u,&s); tmi[k] = s;
1486: }
1487: k++;
1488: }
1489: m1 = m3;
1490: get_eg(&tmp1);
1491: add_eg(&eg_chrem,&tmp0,&tmp1);
1492: add_eg(&eg_chrem_split,&tmp0,&tmp1);
1493:
1494: get_eg(&tmp0);
1495: if ( ind % F4_INTRAT_PERIOD )
1496: ret = 0;
1497: else
1498: ret = intmtoratm(crmat,m1,*nm,dn);
1499: get_eg(&tmp1);
1500: add_eg(&eg_intrat,&tmp0,&tmp1);
1501: add_eg(&eg_intrat_split,&tmp0,&tmp1);
1502: if ( ret ) {
1503: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1504: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
1505: for ( j = k = l = 0; j < col; j++ )
1506: if ( colstat[j] )
1507: rind[k++] = j;
1508: else
1509: cind[l++] = j;
1510: get_eg(&tmp0);
1511: if ( gensolve_check(mat,*nm,*dn,rind,cind) ) {
1512: get_eg(&tmp1);
1513: add_eg(&eg_gschk,&tmp0,&tmp1);
1514: add_eg(&eg_gschk_split,&tmp0,&tmp1);
1515: if ( DP_Print ) {
1516: print_eg("Mod",&eg_mod_split);
1517: print_eg("Elim",&eg_elim_split);
1518: print_eg("ChRem",&eg_chrem_split);
1519: print_eg("IntRat",&eg_intrat_split);
1520: print_eg("Check",&eg_gschk_split);
1521: fflush(asir_out);
1522: }
1523: return rank;
1524: }
1525: }
1526: }
1527: }
1.3 noro 1528: }
1529:
1.64 noro 1530: void lu_dec_cr(MAT mat,MAT lu,Q *dn,int **perm);
1531:
1.53 noro 1532: /* XXX broken */
1.64 noro 1533: void lu_dec_cr(MAT mat,MAT lu,Q *dn,int **perm)
1.53 noro 1534: {
1.76 noro 1535: Q **a0,**b;
1536: Q *aiq;
1537: N **a;
1538: N *ai;
1539: Q q,q1,dn2,a1,q0,bik;
1540: MAT m;
1541: unsigned int md;
1542: int n,ind,i,j,rank,t,inv,t1,ret,min,k;
1543: int **w;
1544: int *wi,*rinfo0,*rinfo;
1545: N m1,m2,m3,u,s;
1546:
1547: a0 = (Q **)mat->body;
1548: n = mat->row;
1549: if ( n != mat->col )
1550: error("lu_dec_cr : non-square matrix");
1551: w = (int **)almat(n,n);
1552: MKMAT(m,n,n);
1553: a = (N **)m->body;
1554: UTON(1,m1);
1555: rinfo0 = 0;
1556: ind = 0;
1557: while ( 1 ) {
1558: md = get_lprime(ind);
1559: /* mat mod md */
1560: for ( i = 0; i < n; i++ )
1561: for ( j = 0, aiq = a0[i], wi = w[i]; j < n; j++ )
1562: if ( q = aiq[j] ) {
1563: t = rem(NM(q),md);
1564: if ( t && SGN(q) < 0 )
1565: t = (md - t) % md;
1566: wi[j] = t;
1567: } else
1568: wi[j] = 0;
1569:
1570: if ( !lu_mod((unsigned int **)w,n,md,&rinfo) ) continue;
1571: printf("."); fflush(stdout);
1572: if ( !rinfo0 )
1573: *perm = rinfo0 = rinfo;
1574: else {
1575: for ( i = 0; i < n; i++ )
1576: if ( rinfo[i] != rinfo0[i] ) break;
1577: if ( i < n ) continue;
1578: }
1579: if ( UNIN(m1) ) {
1580: for ( i = 0; i < n; i++ )
1581: for ( j = 0, ai = a[i], wi = w[i]; j < n; j++ ) {
1582: UTON(wi[j],u); ai[j] = u;
1583: }
1584: UTON(md,m1);
1585: } else {
1586: inv = invm(rem(m1,md),md);
1587: UTON(md,m2); muln(m1,m2,&m3);
1588: for ( i = 0; i < n; i++ )
1589: for ( j = 0, ai = a[i], wi = w[i]; j < n; j++ )
1590: if ( ai[i] ) {
1591: /* f3 = f1+m1*(m1 mod m2)^(-1)*(f2 - f1 mod m2) */
1592: t = rem(ai[j],md);
1593: if ( wi[j] >= t )
1594: t = wi[j]-t;
1595: else
1596: t = md-(t-wi[j]);
1597: DMAR(t,inv,0,md,t1)
1598: UTON(t1,u);
1599: muln(m1,u,&s);
1600: addn(ai[j],s,&u); ai[j] = u;
1601: } else if ( wi[j] ) {
1602: /* f3 = m1*(m1 mod m2)^(-1)*f2 */
1603: DMAR(wi[j],inv,0,md,t)
1604: UTON(t,u);
1605: muln(m1,u,&s); ai[j] = s;
1606: }
1607: m1 = m3;
1608: }
1609: if ( (++ind%8) == 0 ) {
1610: ret = intmtoratm(m,m1,lu,dn);
1611: if ( ret ) {
1612: b = (Q **)lu->body;
1613: mulq(*dn,*dn,&dn2);
1614: for ( i = 0; i < n; i++ ) {
1615: for ( j = 0; j < n; j++ ) {
1616: q = 0;
1617: min = MIN(i,j);
1618: for ( k = 0; k <= min; k++ ) {
1619: bik = k==i ? *dn : b[i][k];
1620: mulq(bik,b[k][j],&q0);
1621: addq(q,q0,&q1); q = q1;
1622: }
1623: mulq(a0[rinfo0[i]][j],dn2,&q1);
1624: if ( cmpq(q,q1) ) break;
1625: }
1626: if ( j < n ) break;
1627: }
1628: if ( i == n )
1629: return;
1630: }
1631: }
1632: }
1.53 noro 1633: }
1634:
1.64 noro 1635: void nmat(N **m,int n)
1.53 noro 1636: {
1.76 noro 1637: int i,j;
1.53 noro 1638:
1.76 noro 1639: for ( i = 0; i < n; i++ ) {
1640: for ( j = 0; j < n; j++ ) {
1641: printn(m[i][j]); printf(" ");
1642: }
1643: printf("\n");
1644: }
1.53 noro 1645: }
1646:
1.24 noro 1647: int generic_gauss_elim_hensel(MAT mat,MAT *nmmat,Q *dn,int **rindp,int **cindp)
1.3 noro 1648: {
1.76 noro 1649: MAT bmat,xmat;
1650: Q **a0,**a,**b,**x,**nm;
1651: Q *ai,*bi,*xi;
1652: int row,col;
1653: int **w;
1654: int *wi;
1655: int **wc;
1656: Q mdq,q,s,u;
1657: N tn;
1658: int ind,md,i,j,k,l,li,ri,rank;
1659: unsigned int t;
1660: int *cinfo,*rinfo;
1661: int *rind,*cind;
1662: int count;
1663: int ret;
1664: struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1;
1665: int period;
1666: int *wx,*ptr;
1667: int wxsize,nsize;
1668: N wn;
1669: Q wq;
1670:
1671: a0 = (Q **)mat->body;
1672: row = mat->row; col = mat->col;
1673: w = (int **)almat(row,col);
1674: for ( ind = 0; ; ind++ ) {
1675: md = get_lprime(ind);
1676: STOQ(md,mdq);
1677: for ( i = 0; i < row; i++ )
1678: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1679: if ( q = (Q)ai[j] ) {
1680: t = rem(NM(q),md);
1681: if ( t && SGN(q) < 0 )
1682: t = (md - t) % md;
1683: wi[j] = t;
1684: } else
1685: wi[j] = 0;
1686:
1687: if ( DP_Print > 3 ) {
1688: fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
1689: }
1690: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1691: if ( DP_Print > 3 ) {
1692: fprintf(asir_out,"done.\n"); fflush(asir_out);
1693: }
1694: a = (Q **)almat_pointer(rank,rank); /* lhs mat */
1695: MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
1696: for ( j = li = ri = 0; j < col; j++ )
1697: if ( cinfo[j] ) {
1698: /* the column is in lhs */
1699: for ( i = 0; i < rank; i++ ) {
1700: w[i][li] = w[i][j];
1701: a[i][li] = a0[rinfo[i]][j];
1702: }
1703: li++;
1704: } else {
1705: /* the column is in rhs */
1706: for ( i = 0; i < rank; i++ )
1707: b[i][ri] = a0[rinfo[i]][j];
1708: ri++;
1709: }
1710:
1711: /* solve Ax+B=0; A: rank x rank, B: rank x ri */
1712: MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
1713: MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
1714: /* use the right part of w as work area */
1715: /* ri = col - rank */
1716: wc = (int **)almat(rank,ri);
1717: for ( i = 0; i < rank; i++ )
1718: wc[i] = w[i]+rank;
1719: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1720: *cindp = cind = (int *)MALLOC_ATOMIC((ri)*sizeof(int));
1721:
1722: init_eg(&eg_mul); init_eg(&eg_inv);
1723: init_eg(&eg_check); init_eg(&eg_intrat);
1724: period = F4_INTRAT_PERIOD;
1725: nsize = period;
1726: wxsize = rank*ri*nsize;
1727: wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int));
1728: for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
1729: for ( q = ONE, count = 0; ; ) {
1730: if ( DP_Print > 3 )
1731: fprintf(stderr,"o");
1732: /* wc = -b mod md */
1733: get_eg(&tmp0);
1734: for ( i = 0; i < rank; i++ )
1735: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1736: if ( u = (Q)bi[j] ) {
1737: t = rem(NM(u),md);
1738: if ( t && SGN(u) > 0 )
1739: t = (md - t) % md;
1740: wi[j] = t;
1741: } else
1742: wi[j] = 0;
1743: /* wc = A^(-1)wc; wc is not normalized */
1744: solve_by_lu_mod(w,rank,md,wc,ri,0);
1745: /* wx += q*wc */
1746: ptr = wx;
1747: for ( i = 0; i < rank; i++ )
1748: for ( j = 0, wi = wc[i]; j < ri; j++ ) {
1749: if ( wi[j] )
1750: muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr);
1751: ptr += nsize;
1752: }
1753: count++;
1754: get_eg(&tmp1);
1755: add_eg(&eg_inv,&tmp0,&tmp1);
1756: get_eg(&tmp0);
1757: for ( i = 0; i < rank; i++ )
1758: for ( j = 0; j < ri; j++ ) {
1759: inner_product_mat_int_mod(a,wc,rank,i,j,&u);
1760: addq(b[i][j],u,&s);
1761: if ( s ) {
1762: t = divin(NM(s),md,&tn);
1763: if ( t )
1764: error("generic_gauss_elim_hensel:incosistent");
1765: NTOQ(tn,SGN(s),b[i][j]);
1766: } else
1767: b[i][j] = 0;
1768: }
1769: get_eg(&tmp1);
1770: add_eg(&eg_mul,&tmp0,&tmp1);
1771: /* q = q*md */
1772: mulq(q,mdq,&u); q = u;
1773: if ( count == period ) {
1774: get_eg(&tmp0);
1775: ptr = wx;
1776: for ( i = 0; i < rank; i++ )
1777: for ( j = 0, xi = x[i]; j < ri;
1778: j++, ptr += nsize ) {
1779: for ( k = nsize-1; k >= 0 && !ptr[k]; k-- );
1780: if ( k >= 0 ) {
1781: wn = NALLOC(k+1);
1782: PL(wn) = k+1;
1783: for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l];
1784: NTOQ(wn,1,wq);
1785: subq(xi[j],wq,&u); xi[j] = u;
1786: }
1787: }
1788: ret = intmtoratm_q(xmat,NM(q),*nmmat,dn);
1789: get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1);
1790: if ( ret ) {
1791: rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1792: cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
1793: for ( j = k = l = 0; j < col; j++ )
1794: if ( cinfo[j] )
1795: rind[k++] = j;
1796: else
1797: cind[l++] = j;
1798: get_eg(&tmp0);
1799: ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
1800: get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1);
1801: if ( ret ) {
1802: if ( DP_Print > 3 ) {
1803: fprintf(stderr,"\n");
1804: print_eg("INV",&eg_inv);
1805: print_eg("MUL",&eg_mul);
1806: print_eg("INTRAT",&eg_intrat);
1807: print_eg("CHECK",&eg_check);
1808: fflush(asir_out);
1809: }
1810: *rindp = rind;
1811: *cindp = cind;
1812: for ( j = k = 0; j < col; j++ )
1813: if ( !cinfo[j] )
1814: cind[k++] = j;
1815: return rank;
1816: }
1817: } else {
1818: period = period*3/2;
1819: count = 0;
1820: nsize += period;
1821: wxsize += rank*ri*nsize;
1822: wx = (int *)REALLOC(wx,wxsize*sizeof(int));
1823: for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
1824: }
1825: }
1826: }
1827: }
1.50 noro 1828: }
1829:
1.55 noro 1830: int generic_gauss_elim_hensel_dalg(MAT mat,DP *mb,MAT *nmmat,Q *dn,int **rindp,int **cindp)
1.50 noro 1831: {
1.76 noro 1832: MAT bmat,xmat;
1833: Q **a0,**a,**b,**x,**nm;
1834: Q *ai,*bi,*xi;
1835: int row,col;
1836: int **w;
1837: int *wi;
1838: int **wc;
1839: Q mdq,q,s,u;
1840: N tn;
1841: int ind,md,i,j,k,l,li,ri,rank;
1842: unsigned int t;
1843: int *cinfo,*rinfo;
1844: int *rind,*cind;
1845: int count;
1846: int ret;
1847: struct oEGT eg_mul,eg_inv,eg_intrat,eg_check,tmp0,tmp1;
1848: int period;
1849: int *wx,*ptr;
1850: int wxsize,nsize;
1851: N wn;
1852: Q wq;
1853: NumberField nf;
1854: DP m;
1855: int col1;
1856:
1857: a0 = (Q **)mat->body;
1858: row = mat->row; col = mat->col;
1859: w = (int **)almat(row,col);
1860: for ( ind = 0; ; ind++ ) {
1861: md = get_lprime(ind);
1862: STOQ(md,mdq);
1863: for ( i = 0; i < row; i++ )
1864: for ( j = 0, ai = a0[i], wi = w[i]; j < col; j++ )
1865: if ( q = (Q)ai[j] ) {
1866: t = rem(NM(q),md);
1867: if ( t && SGN(q) < 0 )
1868: t = (md - t) % md;
1869: wi[j] = t;
1870: } else
1871: wi[j] = 0;
1872:
1873: if ( DP_Print ) {
1874: fprintf(asir_out,"LU decomposition.."); fflush(asir_out);
1875: }
1876: rank = find_lhs_and_lu_mod((unsigned int **)w,row,col,md,&rinfo,&cinfo);
1.77 noro 1877: printf("\n");
1878: for ( i = 0; i < row; i++ ) {
1879: for ( j = 0; j < col; j++ )
1880: printf("%d ",w[i][j]);
1881: printf("\n");
1882: }
1.76 noro 1883: if ( DP_Print ) {
1884: fprintf(asir_out,"done.\n"); fflush(asir_out);
1885: }
1886: for ( i = 0; i < col-1; i++ ) {
1887: if ( !cinfo[i] ) {
1888: m = mb[i];
1889: for ( j = i+1; j < col-1; j++ )
1890: if ( dp_redble(mb[j],m) )
1891: cinfo[j] = -1;
1892: }
1893: }
1894: a = (Q **)almat_pointer(rank,rank); /* lhs mat */
1895: MKMAT(bmat,rank,col-rank); b = (Q **)bmat->body; /* lhs mat */
1896: for ( j = li = ri = 0; j < col; j++ )
1897: if ( cinfo[j] > 0 ) {
1898: /* the column is in lhs */
1899: for ( i = 0; i < rank; i++ ) {
1900: w[i][li] = w[i][j];
1901: a[i][li] = a0[rinfo[i]][j];
1902: }
1903: li++;
1904: } else if ( !cinfo[j] ) {
1905: /* the column is in rhs */
1906: for ( i = 0; i < rank; i++ )
1907: b[i][ri] = a0[rinfo[i]][j];
1908: ri++;
1909: }
1910:
1911: /* solve Ax+B=0; A: rank x rank, B: rank x ri */
1912: MKMAT(xmat,rank,ri); x = (Q **)(xmat)->body;
1913: MKMAT(*nmmat,rank,ri); nm = (Q **)(*nmmat)->body;
1914: /* use the right part of w as work area */
1915: wc = (int **)almat(rank,ri);
1916: for ( i = 0; i < rank; i++ )
1917: wc[i] = w[i]+rank;
1918: *rindp = rind = (int *)MALLOC_ATOMIC(rank*sizeof(int));
1919: *cindp = cind = (int *)MALLOC_ATOMIC((col-rank)*sizeof(int));
1920: init_eg(&eg_mul); init_eg(&eg_inv);
1921: init_eg(&eg_check); init_eg(&eg_intrat);
1922: period = F4_INTRAT_PERIOD;
1923: nsize = period;
1924: wxsize = rank*ri*nsize;
1925: wx = (int *)MALLOC_ATOMIC(wxsize*sizeof(int));
1926: for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
1927: for ( q = ONE, count = 0; ; ) {
1928: if ( DP_Print )
1929: fprintf(stderr,"o");
1930: /* wc = -b mod md */
1931: get_eg(&tmp0);
1932: for ( i = 0; i < rank; i++ )
1933: for ( j = 0, bi = b[i], wi = wc[i]; j < ri; j++ )
1934: if ( u = (Q)bi[j] ) {
1935: t = rem(NM(u),md);
1936: if ( t && SGN(u) > 0 )
1937: t = (md - t) % md;
1938: wi[j] = t;
1939: } else
1940: wi[j] = 0;
1941: /* wc = A^(-1)wc; wc is not normalized */
1942: solve_by_lu_mod(w,rank,md,wc,ri,0);
1943: /* wx += q*wc */
1944: ptr = wx;
1945: for ( i = 0; i < rank; i++ )
1946: for ( j = 0, wi = wc[i]; j < ri; j++ ) {
1947: if ( wi[j] )
1948: muln_1(BD(NM(q)),PL(NM(q)),wi[j],ptr);
1949: ptr += nsize;
1950: }
1951: count++;
1952: get_eg(&tmp1);
1953: add_eg(&eg_inv,&tmp0,&tmp1);
1954: get_eg(&tmp0);
1955: for ( i = 0; i < rank; i++ )
1956: for ( j = 0; j < ri; j++ ) {
1957: inner_product_mat_int_mod(a,wc,rank,i,j,&u);
1958: addq(b[i][j],u,&s);
1959: if ( s ) {
1960: t = divin(NM(s),md,&tn);
1961: if ( t )
1962: error("generic_gauss_elim_hensel:incosistent");
1963: NTOQ(tn,SGN(s),b[i][j]);
1964: } else
1965: b[i][j] = 0;
1966: }
1.77 noro 1967: printf("\n");
1968: for ( i = 0; i < rank; i++ ) {
1969: for ( j = 0; j < ri; j++ ) {
1970: printexpr(CO,b[i][j]); printf(" ");
1971: }
1972: printf("\n");
1973: }
1.76 noro 1974: get_eg(&tmp1);
1975: add_eg(&eg_mul,&tmp0,&tmp1);
1976: /* q = q*md */
1977: mulq(q,mdq,&u); q = u;
1978: if ( count == period ) {
1979: get_eg(&tmp0);
1980: ptr = wx;
1981: for ( i = 0; i < rank; i++ )
1982: for ( j = 0, xi = x[i]; j < ri;
1983: j++, ptr += nsize ) {
1984: for ( k = nsize-1; k >= 0 && !ptr[k]; k-- );
1985: if ( k >= 0 ) {
1986: wn = NALLOC(k+1);
1987: PL(wn) = k+1;
1988: for ( l = 0; l <= k; l++ ) BD(wn)[l] = (unsigned int)ptr[l];
1989: NTOQ(wn,1,wq);
1990: subq(xi[j],wq,&u); xi[j] = u;
1991: }
1992: }
1993: ret = intmtoratm_q(xmat,NM(q),*nmmat,dn);
1994: get_eg(&tmp1); add_eg(&eg_intrat,&tmp0,&tmp1);
1995: if ( ret ) {
1996: for ( j = k = l = 0; j < col; j++ )
1997: if ( cinfo[j] > 0 )
1998: rind[k++] = j;
1999: else if ( !cinfo[j] )
2000: cind[l++] = j;
2001: get_eg(&tmp0);
2002: ret = gensolve_check(mat,*nmmat,*dn,rind,cind);
2003: get_eg(&tmp1); add_eg(&eg_check,&tmp0,&tmp1);
2004: if ( ret ) {
2005: if ( DP_Print > 3 ) {
2006: fprintf(stderr,"\n");
2007: print_eg("INV",&eg_inv);
2008: print_eg("MUL",&eg_mul);
2009: print_eg("INTRAT",&eg_intrat);
2010: print_eg("CHECK",&eg_check);
2011: fflush(asir_out);
2012: }
2013: return rank;
1.77 noro 2014: } else
2015: goto reset;
1.76 noro 2016: } else {
1.77 noro 2017: reset:
1.76 noro 2018: period = period*3/2;
2019: count = 0;
2020: nsize += period;
2021: wxsize += rank*ri*nsize;
2022: wx = (int *)REALLOC(wx,wxsize*sizeof(int));
2023: for ( i = 0; i < wxsize; i++ ) wx[i] = 0;
2024: }
2025: }
2026: }
2027: }
1.1 noro 2028: }
2029:
2030: int f4_nocheck;
2031:
1.24 noro 2032: int gensolve_check(MAT mat,MAT nm,Q dn,int *rind,int *cind)
1.1 noro 2033: {
1.76 noro 2034: int row,col,rank,clen,i,j,k,l;
2035: Q s,t;
2036: Q *w;
2037: Q *mati,*nmk;
2038:
2039: if ( f4_nocheck )
2040: return 1;
2041: row = mat->row; col = mat->col;
2042: rank = nm->row; clen = nm->col;
2043: w = (Q *)MALLOC(clen*sizeof(Q));
2044: for ( i = 0; i < row; i++ ) {
2045: mati = (Q *)mat->body[i];
1.1 noro 2046: #if 1
1.76 noro 2047: bzero(w,clen*sizeof(Q));
2048: for ( k = 0; k < rank; k++ )
2049: for ( l = 0, nmk = (Q *)nm->body[k]; l < clen; l++ ) {
2050: mulq(mati[rind[k]],nmk[l],&t);
2051: addq(w[l],t,&s); w[l] = s;
2052: }
2053: for ( j = 0; j < clen; j++ ) {
2054: mulq(dn,mati[cind[j]],&t);
2055: if ( cmpq(w[j],t) )
2056: break;
2057: }
1.1 noro 2058: #else
1.76 noro 2059: for ( j = 0; j < clen; j++ ) {
2060: for ( k = 0, s = 0; k < rank; k++ ) {
2061: mulq(mati[rind[k]],nm->body[k][j],&t);
2062: addq(s,t,&u); s = u;
2063: }
2064: mulq(dn,mati[cind[j]],&t);
2065: if ( cmpq(s,t) )
2066: break;
2067: }
1.1 noro 2068: #endif
1.76 noro 2069: if ( j != clen )
2070: break;
2071: }
2072: if ( i != row )
2073: return 0;
2074: else
2075: return 1;
1.1 noro 2076: }
2077:
2078: /* assuming 0 < c < m */
2079:
1.24 noro 2080: int inttorat(N c,N m,N b,int *sgnp,N *nmp,N *dnp)
1.1 noro 2081: {
1.76 noro 2082: Q qq,t,u1,v1,r1;
2083: N q,u2,v2,r2;
1.1 noro 2084:
1.76 noro 2085: u1 = 0; v1 = ONE; u2 = m; v2 = c;
2086: while ( cmpn(v2,b) >= 0 ) {
2087: divn(u2,v2,&q,&r2); u2 = v2; v2 = r2;
2088: NTOQ(q,1,qq); mulq(qq,v1,&t); subq(u1,t,&r1); u1 = v1; v1 = r1;
2089: }
2090: if ( cmpn(NM(v1),b) >= 0 )
2091: return 0;
2092: else {
2093: *nmp = v2;
2094: *dnp = NM(v1);
2095: *sgnp = SGN(v1);
2096: return 1;
2097: }
1.1 noro 2098: }
2099:
2100: /* mat->body = N ** */
2101:
1.24 noro 2102: int intmtoratm(MAT mat,N md,MAT nm,Q *dn)
1.1 noro 2103: {
1.76 noro 2104: N t,s,b;
2105: Q dn0,dn1,nm1,q;
2106: int i,j,k,l,row,col;
2107: Q **rmat;
2108: N **tmat;
2109: N *tmi;
2110: Q *nmk;
2111: N u,unm,udn;
2112: int sgn,ret;
2113:
2114: if ( UNIN(md) )
2115: return 0;
2116: row = mat->row; col = mat->col;
2117: bshiftn(md,1,&t);
2118: isqrt(t,&s);
2119: bshiftn(s,64,&b);
2120: if ( !b )
2121: b = ONEN;
2122: dn0 = ONE;
2123: tmat = (N **)mat->body;
2124: rmat = (Q **)nm->body;
2125: for ( i = 0; i < row; i++ )
2126: for ( j = 0, tmi = tmat[i]; j < col; j++ )
2127: if ( tmi[j] ) {
2128: muln(tmi[j],NM(dn0),&s);
2129: remn(s,md,&u);
2130: ret = inttorat(u,md,b,&sgn,&unm,&udn);
2131: if ( !ret )
2132: return 0;
2133: else {
2134: NTOQ(unm,sgn,nm1);
2135: NTOQ(udn,1,dn1);
2136: if ( !UNIQ(dn1) ) {
2137: for ( k = 0; k < i; k++ )
2138: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
2139: mulq(nmk[l],dn1,&q); nmk[l] = q;
2140: }
2141: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
2142: mulq(nmk[l],dn1,&q); nmk[l] = q;
2143: }
2144: }
2145: rmat[i][j] = nm1;
2146: mulq(dn0,dn1,&q); dn0 = q;
2147: }
2148: }
2149: *dn = dn0;
2150: return 1;
1.1 noro 2151: }
2152:
1.3 noro 2153: /* mat->body = Q ** */
2154:
1.24 noro 2155: int intmtoratm_q(MAT mat,N md,MAT nm,Q *dn)
1.3 noro 2156: {
1.76 noro 2157: N t,s,b;
2158: Q dn0,dn1,nm1,q;
2159: int i,j,k,l,row,col;
2160: Q **rmat;
2161: Q **tmat;
2162: Q *tmi;
2163: Q *nmk;
2164: N u,unm,udn;
2165: int sgn,ret;
2166:
2167: if ( UNIN(md) )
2168: return 0;
2169: row = mat->row; col = mat->col;
2170: bshiftn(md,1,&t);
2171: isqrt(t,&s);
2172: bshiftn(s,64,&b);
2173: if ( !b )
2174: b = ONEN;
2175: dn0 = ONE;
2176: tmat = (Q **)mat->body;
2177: rmat = (Q **)nm->body;
2178: for ( i = 0; i < row; i++ )
2179: for ( j = 0, tmi = tmat[i]; j < col; j++ )
2180: if ( tmi[j] ) {
2181: muln(NM(tmi[j]),NM(dn0),&s);
2182: remn(s,md,&u);
2183: ret = inttorat(u,md,b,&sgn,&unm,&udn);
2184: if ( !ret )
2185: return 0;
2186: else {
2187: if ( SGN(tmi[j])<0 )
2188: sgn = -sgn;
2189: NTOQ(unm,sgn,nm1);
2190: NTOQ(udn,1,dn1);
2191: if ( !UNIQ(dn1) ) {
2192: for ( k = 0; k < i; k++ )
2193: for ( l = 0, nmk = rmat[k]; l < col; l++ ) {
2194: mulq(nmk[l],dn1,&q); nmk[l] = q;
2195: }
2196: for ( l = 0, nmk = rmat[i]; l < j; l++ ) {
2197: mulq(nmk[l],dn1,&q); nmk[l] = q;
2198: }
2199: }
2200: rmat[i][j] = nm1;
2201: mulq(dn0,dn1,&q); dn0 = q;
2202: }
2203: }
2204: *dn = dn0;
2205: return 1;
1.3 noro 2206: }
2207:
1.4 noro 2208: #define ONE_STEP1 if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
2209:
1.24 noro 2210: void reduce_reducers_mod(int **mat,int row,int col,int md)
1.4 noro 2211: {
1.76 noro 2212: int i,j,k,l,hc,zzz;
2213: int *t,*s,*tj,*ind;
1.4 noro 2214:
1.76 noro 2215: /* reduce the reducers */
2216: ind = (int *)ALLOCA(row*sizeof(int));
2217: for ( i = 0; i < row; i++ ) {
2218: t = mat[i];
2219: for ( j = 0; j < col && !t[j]; j++ );
2220: /* register the position of the head term */
2221: ind[i] = j;
2222: for ( l = i-1; l >= 0; l-- ) {
2223: /* reduce mat[i] by mat[l] */
2224: if ( hc = t[ind[l]] ) {
2225: /* mat[i] = mat[i]-hc*mat[l] */
2226: j = ind[l];
2227: s = mat[l]+j;
2228: tj = t+j;
2229: hc = md-hc;
2230: k = col-j;
2231: for ( ; k >= 64; k -= 64 ) {
2232: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2233: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2234: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2235: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2236: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2237: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2238: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2239: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2240: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2241: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2242: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2243: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2244: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2245: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2246: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2247: ONE_STEP1 ONE_STEP1 ONE_STEP1 ONE_STEP1
2248: }
2249: for ( ; k > 0; k-- ) {
2250: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
2251: }
2252: }
2253: }
2254: }
1.4 noro 2255: }
2256:
2257: /*
1.76 noro 2258: mat[i] : reducers (i=0,...,nred-1)
2259: spolys (i=nred,...,row-1)
2260: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
2261: 1. reduce the reducers
2262: 2. reduce spolys by the reduced reducers
1.4 noro 2263: */
2264:
1.24 noro 2265: void pre_reduce_mod(int **mat,int row,int col,int nred,int md)
1.4 noro 2266: {
1.76 noro 2267: int i,j,k,l,hc,inv;
2268: int *t,*s,*tk,*ind;
1.4 noro 2269:
2270: #if 1
1.76 noro 2271: /* reduce the reducers */
2272: ind = (int *)ALLOCA(row*sizeof(int));
2273: for ( i = 0; i < nred; i++ ) {
2274: /* make mat[i] monic and mat[i] by mat[0],...,mat[i-1] */
2275: t = mat[i];
2276: for ( j = 0; j < col && !t[j]; j++ );
2277: /* register the position of the head term */
2278: ind[i] = j;
2279: inv = invm(t[j],md);
2280: for ( k = j; k < col; k++ )
2281: if ( t[k] )
2282: DMAR(t[k],inv,0,md,t[k])
2283: for ( l = i-1; l >= 0; l-- ) {
2284: /* reduce mat[i] by mat[l] */
2285: if ( hc = t[ind[l]] ) {
2286: /* mat[i] = mat[i]-hc*mat[l] */
2287: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
2288: k < col; k++, tk++, s++ )
2289: if ( *s )
2290: DMAR(*s,hc,*tk,md,*tk)
2291: }
2292: }
2293: }
2294: /* reduce the spolys */
2295: for ( i = nred; i < row; i++ ) {
2296: t = mat[i];
2297: for ( l = nred-1; l >= 0; l-- ) {
2298: /* reduce mat[i] by mat[l] */
2299: if ( hc = t[ind[l]] ) {
2300: /* mat[i] = mat[i]-hc*mat[l] */
2301: for ( k = ind[l], hc = md-hc, s = mat[l]+k, tk = t+k;
2302: k < col; k++, tk++, s++ )
2303: if ( *s )
2304: DMAR(*s,hc,*tk,md,*tk)
2305: }
2306: }
2307: }
1.4 noro 2308: #endif
2309: }
2310: /*
1.76 noro 2311: mat[i] : reducers (i=0,...,nred-1)
2312: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1.4 noro 2313: */
2314:
1.24 noro 2315: void reduce_sp_by_red_mod(int *sp,int **redmat,int *ind,int nred,int col,int md)
1.4 noro 2316: {
1.76 noro 2317: int i,j,k,hc,zzz;
2318: int *s,*tj;
1.4 noro 2319:
1.76 noro 2320: /* reduce the spolys by redmat */
2321: for ( i = nred-1; i >= 0; i-- ) {
2322: /* reduce sp by redmat[i] */
2323: if ( hc = sp[ind[i]] ) {
2324: /* sp = sp-hc*redmat[i] */
2325: j = ind[i];
2326: hc = md-hc;
2327: s = redmat[i]+j;
2328: tj = sp+j;
2329: for ( k = col-j; k > 0; k-- ) {
2330: if ( zzz = *s ) { DMAR(zzz,hc,*tj,md,*tj) } tj++; s++;
2331: }
2332: }
2333: }
1.17 noro 2334: }
2335:
2336: /*
1.76 noro 2337: mat[i] : compressed reducers (i=0,...,nred-1)
2338: mat[0] < mat[1] < ... < mat[nred-1] w.r.t the term order
1.15 noro 2339: */
2340:
1.24 noro 2341: void red_by_compress(int m,unsigned int *p,unsigned int *r,
1.76 noro 2342: unsigned int *ri,unsigned int hc,int len)
1.18 noro 2343: {
1.76 noro 2344: unsigned int up,lo;
2345: unsigned int dmy;
2346: unsigned int *pj;
2347:
2348: p[*ri] = 0; r++; ri++;
2349: for ( len--; len; len--, r++, ri++ ) {
2350: pj = p+ *ri;
2351: DMA(*r,hc,*pj,up,lo);
2352: if ( up ) {
2353: DSAB(m,up,lo,dmy,*pj);
2354: } else
2355: *pj = lo;
2356: }
1.18 noro 2357: }
2358:
2359: /* p -= hc*r */
2360:
1.24 noro 2361: void red_by_vect(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
1.18 noro 2362: {
1.76 noro 2363: unsigned int up,lo,dmy;
1.18 noro 2364:
1.76 noro 2365: *p++ = 0; r++; len--;
2366: for ( ; len; len--, r++, p++ )
2367: if ( *r ) {
2368: DMA(*r,hc,*p,up,lo);
2369: if ( up ) {
2370: DSAB(m,up,lo,dmy,*p);
2371: } else
2372: *p = lo;
2373: }
1.18 noro 2374: }
2375:
1.75 noro 2376: #if defined(__GNUC__) && SIZEOF_LONG==8
1.74 noro 2377: /* 64bit vector += UNIT vector(normalized) */
1.73 noro 2378:
1.74 noro 2379: void red_by_vect64(int m, U64 *p,unsigned int *c,U64 *r,unsigned int hc,int len)
1.73 noro 2380: {
1.74 noro 2381: U64 t;
2382:
2383: /* (p[0],c[0]) is normalized */
2384: *p++ = 0; *c++ = 0; r++; len--;
2385: for ( ; len; len--, r++, p++, c++ )
2386: if ( *r ) {
2387: t = (*p)+(*r)*hc;
2388: if ( t < *p ) (*c)++;
2389: *p = t;
2390: }
1.73 noro 2391: }
2392: #endif
2393:
1.32 noro 2394: void red_by_vect_sf(int m,unsigned int *p,unsigned int *r,unsigned int hc,int len)
2395: {
1.76 noro 2396: *p++ = 0; r++; len--;
2397: for ( ; len; len--, r++, p++ )
2398: if ( *r )
2399: *p = _addsf(_mulsf(*r,hc),*p);
1.32 noro 2400: }
2401:
1.71 noro 2402: extern GZ current_mod_lf;
2403: extern int current_mod_lf_size;
2404:
1.70 noro 2405: void red_by_vect_lf(mpz_t *p,mpz_t *r,mpz_t hc,int len)
2406: {
1.76 noro 2407: mpz_set_ui(*p++,0); r++; len--;
2408: for ( ; len; len--, r++, p++ ) {
1.70 noro 2409: mpz_addmul(*p,*r,hc);
1.71 noro 2410: #if 0
2411: if ( mpz_size(*p) > current_mod_lf_size )
2412: mpz_mod(*p,*p,BDY(current_mod_lf));
2413: #endif
2414: }
1.70 noro 2415: }
2416:
2417:
1.21 noro 2418: extern unsigned int **psca;
2419:
1.24 noro 2420: void reduce_sp_by_red_mod_compress (int *sp,CDP *redmat,int *ind,
1.76 noro 2421: int nred,int col,int md)
1.15 noro 2422: {
1.76 noro 2423: int i,len;
2424: CDP ri;
2425: unsigned int hc;
2426: unsigned int *usp;
2427:
2428: usp = (unsigned int *)sp;
2429: /* reduce the spolys by redmat */
2430: for ( i = nred-1; i >= 0; i-- ) {
2431: /* reduce sp by redmat[i] */
2432: usp[ind[i]] %= md;
2433: if ( hc = usp[ind[i]] ) {
2434: /* sp = sp-hc*redmat[i] */
2435: hc = md-hc;
2436: ri = redmat[i];
2437: len = ri->len;
2438: red_by_compress(md,usp,psca[ri->psindex],ri->body,hc,len);
2439: }
2440: }
2441: for ( i = 0; i < col; i++ )
2442: if ( usp[i] >= (unsigned int)md )
2443: usp[i] %= md;
1.4 noro 2444: }
2445:
2446: #define ONE_STEP2 if ( zzz = *pk ) { DMAR(zzz,a,*tk,md,*tk) } pk++; tk++;
2447:
1.24 noro 2448: int generic_gauss_elim_mod(int **mat0,int row,int col,int md,int *colstat)
1.1 noro 2449: {
1.76 noro 2450: int i,j,k,l,inv,a,rank;
2451: unsigned int *t,*pivot,*pk;
2452: unsigned int **mat;
2453:
2454: mat = (unsigned int **)mat0;
2455: for ( rank = 0, j = 0; j < col; j++ ) {
2456: for ( i = rank; i < row; i++ )
2457: mat[i][j] %= md;
2458: for ( i = rank; i < row; i++ )
2459: if ( mat[i][j] )
2460: break;
2461: if ( i == row ) {
2462: colstat[j] = 0;
2463: continue;
2464: } else
2465: colstat[j] = 1;
2466: if ( i != rank ) {
2467: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
2468: }
2469: pivot = mat[rank];
2470: inv = invm(pivot[j],md);
2471: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
2472: if ( *pk ) {
2473: if ( *pk >= (unsigned int)md )
2474: *pk %= md;
2475: DMAR(*pk,inv,0,md,*pk)
2476: }
2477: for ( i = rank+1; i < row; i++ ) {
2478: t = mat[i];
2479: if ( a = t[j] )
2480: red_by_vect(md,t+j,pivot+j,md-a,col-j);
2481: }
2482: rank++;
2483: }
2484: for ( j = col-1, l = rank-1; j >= 0; j-- )
2485: if ( colstat[j] ) {
2486: pivot = mat[l];
2487: for ( i = 0; i < l; i++ ) {
2488: t = mat[i];
2489: t[j] %= md;
2490: if ( a = t[j] )
2491: red_by_vect(md,t+j,pivot+j,md-a,col-j);
2492: }
2493: l--;
2494: }
2495: for ( j = 0, l = 0; l < rank; j++ )
2496: if ( colstat[j] ) {
2497: t = mat[l];
2498: for ( k = j; k < col; k++ )
2499: if ( t[k] >= (unsigned int)md )
2500: t[k] %= md;
2501: l++;
2502: }
2503: return rank;
1.32 noro 2504: }
2505:
1.65 noro 2506: int generic_gauss_elim_mod2(int **mat0,int row,int col,int md,int *colstat,int *rowstat)
2507: {
1.76 noro 2508: int i,j,k,l,inv,a,rank;
2509: unsigned int *t,*pivot,*pk;
2510: unsigned int **mat;
2511:
2512: for ( i = 0; i < row; i++ ) rowstat[i] = i;
2513: mat = (unsigned int **)mat0;
2514: for ( rank = 0, j = 0; j < col; j++ ) {
2515: for ( i = rank; i < row; i++ )
2516: mat[i][j] %= md;
2517: for ( i = rank; i < row; i++ )
2518: if ( mat[i][j] )
2519: break;
2520: if ( i == row ) {
2521: colstat[j] = 0;
2522: continue;
2523: } else
2524: colstat[j] = 1;
2525: if ( i != rank ) {
2526: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
2527: k = rowstat[i]; rowstat[i] = rowstat[rank]; rowstat[rank] = k;
2528: }
2529: pivot = mat[rank];
2530: inv = invm(pivot[j],md);
2531: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
2532: if ( *pk ) {
2533: if ( *pk >= (unsigned int)md )
2534: *pk %= md;
2535: DMAR(*pk,inv,0,md,*pk)
2536: }
2537: for ( i = rank+1; i < row; i++ ) {
2538: t = mat[i];
2539: if ( a = t[j] )
2540: red_by_vect(md,t+j,pivot+j,md-a,col-j);
2541: }
2542: rank++;
2543: }
2544: for ( j = col-1, l = rank-1; j >= 0; j-- )
2545: if ( colstat[j] ) {
2546: pivot = mat[l];
2547: for ( i = 0; i < l; i++ ) {
2548: t = mat[i];
2549: t[j] %= md;
2550: if ( a = t[j] )
2551: red_by_vect(md,t+j,pivot+j,md-a,col-j);
2552: }
2553: l--;
2554: }
2555: for ( j = 0, l = 0; l < rank; j++ )
2556: if ( colstat[j] ) {
2557: t = mat[l];
2558: for ( k = j; k < col; k++ )
2559: if ( t[k] >= (unsigned int)md )
2560: t[k] %= md;
2561: l++;
2562: }
2563: return rank;
1.65 noro 2564: }
2565:
1.69 noro 2566: int indep_rows_mod(int **mat0,int row,int col,int md,int *rowstat)
2567: {
1.76 noro 2568: int i,j,k,l,inv,a,rank;
2569: unsigned int *t,*pivot,*pk;
2570: unsigned int **mat;
2571:
2572: for ( i = 0; i < row; i++ ) rowstat[i] = i;
2573: mat = (unsigned int **)mat0;
2574: for ( rank = 0, j = 0; j < col; j++ ) {
2575: for ( i = rank; i < row; i++ )
2576: mat[i][j] %= md;
2577: for ( i = rank; i < row; i++ )
2578: if ( mat[i][j] )
2579: break;
2580: if ( i == row ) continue;
2581: if ( i != rank ) {
2582: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
2583: k = rowstat[i]; rowstat[i] = rowstat[rank]; rowstat[rank] = k;
2584: }
2585: pivot = mat[rank];
2586: inv = invm(pivot[j],md);
2587: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
2588: if ( *pk ) {
2589: if ( *pk >= (unsigned int)md )
2590: *pk %= md;
2591: DMAR(*pk,inv,0,md,*pk)
2592: }
2593: for ( i = rank+1; i < row; i++ ) {
2594: t = mat[i];
2595: if ( a = t[j] )
2596: red_by_vect(md,t+j,pivot+j,md-a,col-j);
2597: }
2598: rank++;
2599: }
2600: return rank;
1.69 noro 2601: }
2602:
1.32 noro 2603: int generic_gauss_elim_sf(int **mat0,int row,int col,int md,int *colstat)
2604: {
1.76 noro 2605: int i,j,k,l,inv,a,rank;
2606: unsigned int *t,*pivot,*pk;
2607: unsigned int **mat;
2608:
2609: mat = (unsigned int **)mat0;
2610: for ( rank = 0, j = 0; j < col; j++ ) {
2611: for ( i = rank; i < row; i++ )
2612: if ( mat[i][j] )
2613: break;
2614: if ( i == row ) {
2615: colstat[j] = 0;
2616: continue;
2617: } else
2618: colstat[j] = 1;
2619: if ( i != rank ) {
2620: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
2621: }
2622: pivot = mat[rank];
2623: inv = _invsf(pivot[j]);
2624: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
2625: if ( *pk )
2626: *pk = _mulsf(*pk,inv);
2627: for ( i = rank+1; i < row; i++ ) {
2628: t = mat[i];
2629: if ( a = t[j] )
2630: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
2631: }
2632: rank++;
2633: }
2634: for ( j = col-1, l = rank-1; j >= 0; j-- )
2635: if ( colstat[j] ) {
2636: pivot = mat[l];
2637: for ( i = 0; i < l; i++ ) {
2638: t = mat[i];
2639: if ( a = t[j] )
2640: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
2641: }
2642: l--;
2643: }
2644: return rank;
1.1 noro 2645: }
2646:
2647: /* LU decomposition; a[i][i] = 1/U[i][i] */
2648:
1.24 noro 2649: int lu_gfmmat(GFMMAT mat,unsigned int md,int *perm)
1.1 noro 2650: {
1.76 noro 2651: int row,col;
2652: int i,j,k;
2653: unsigned int *t,*pivot;
2654: unsigned int **a;
2655: unsigned int inv,m;
2656:
2657: row = mat->row; col = mat->col;
2658: a = mat->body;
2659: bzero(perm,row*sizeof(int));
2660:
2661: for ( i = 0; i < row; i++ )
2662: perm[i] = i;
2663: for ( k = 0; k < col; k++ ) {
2664: for ( i = k; i < row && !a[i][k]; i++ );
2665: if ( i == row )
2666: return 0;
2667: if ( i != k ) {
2668: j = perm[i]; perm[i] = perm[k]; perm[k] = j;
2669: t = a[i]; a[i] = a[k]; a[k] = t;
2670: }
2671: pivot = a[k];
2672: pivot[k] = inv = invm(pivot[k],md);
2673: for ( i = k+1; i < row; i++ ) {
2674: t = a[i];
2675: if ( m = t[k] ) {
2676: DMAR(inv,m,0,md,t[k])
2677: for ( j = k+1, m = md - t[k]; j < col; j++ )
2678: if ( pivot[j] ) {
2679: unsigned int tj;
2680:
2681: DMAR(m,pivot[j],t[j],md,tj)
2682: t[j] = tj;
2683: }
2684: }
2685: }
2686: }
2687: return 1;
1.1 noro 2688: }
2689:
1.3 noro 2690: /*
2691: Input
1.76 noro 2692: a: a row x col matrix
2693: md : a modulus
1.3 noro 2694:
2695: Output:
1.76 noro 2696: return : d = the rank of mat
2697: a[0..(d-1)][0..(d-1)] : LU decomposition (a[i][i] = 1/U[i][i])
2698: rinfo: array of length row
2699: cinfo: array of length col
1.3 noro 2700: i-th row in new a <-> rinfo[i]-th row in old a
1.76 noro 2701: cinfo[j]=1 <=> j-th column is contained in the LU decomp.
1.3 noro 2702: */
2703:
1.24 noro 2704: int find_lhs_and_lu_mod(unsigned int **a,int row,int col,
1.76 noro 2705: unsigned int md,int **rinfo,int **cinfo)
1.3 noro 2706: {
1.76 noro 2707: int i,j,k,d;
2708: int *rp,*cp;
2709: unsigned int *t,*pivot;
2710: unsigned int inv,m;
2711:
2712: *rinfo = rp = (int *)MALLOC_ATOMIC(row*sizeof(int));
2713: *cinfo = cp = (int *)MALLOC_ATOMIC(col*sizeof(int));
2714: for ( i = 0; i < row; i++ )
2715: rp[i] = i;
2716: for ( k = 0, d = 0; k < col; k++ ) {
2717: for ( i = d; i < row && !a[i][k]; i++ );
2718: if ( i == row ) {
2719: cp[k] = 0;
2720: continue;
2721: } else
2722: cp[k] = 1;
2723: if ( i != d ) {
2724: j = rp[i]; rp[i] = rp[d]; rp[d] = j;
2725: t = a[i]; a[i] = a[d]; a[d] = t;
2726: }
2727: pivot = a[d];
2728: pivot[k] = inv = invm(pivot[k],md);
2729: for ( i = d+1; i < row; 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 < col; 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: d++;
2742: }
2743: return d;
1.3 noro 2744: }
2745:
1.53 noro 2746: int lu_mod(unsigned int **a,int n,unsigned int md,int **rinfo)
2747: {
1.76 noro 2748: int i,j,k;
2749: int *rp;
2750: unsigned int *t,*pivot;
2751: unsigned int inv,m;
2752:
2753: *rinfo = rp = (int *)MALLOC_ATOMIC(n*sizeof(int));
2754: for ( i = 0; i < n; i++ ) rp[i] = i;
2755: for ( k = 0; k < n; k++ ) {
2756: for ( i = k; i < n && !a[i][k]; i++ );
2757: if ( i == n ) return 0;
2758: if ( i != k ) {
2759: j = rp[i]; rp[i] = rp[k]; rp[k] = j;
2760: t = a[i]; a[i] = a[k]; a[k] = t;
2761: }
2762: pivot = a[k];
2763: inv = invm(pivot[k],md);
2764: for ( i = k+1; i < n; i++ ) {
2765: t = a[i];
2766: if ( m = t[k] ) {
2767: DMAR(inv,m,0,md,t[k])
2768: for ( j = k+1, m = md - t[k]; j < n; j++ )
2769: if ( pivot[j] ) {
2770: unsigned int tj;
2771: DMAR(m,pivot[j],t[j],md,tj)
2772: t[j] = tj;
2773: }
2774: }
2775: }
2776: }
2777: return 1;
1.53 noro 2778: }
2779:
1.3 noro 2780: /*
2781: Input
1.76 noro 2782: a : n x n matrix; a result of LU-decomposition
2783: md : modulus
2784: b : n x l matrix
1.3 noro 2785: Output
1.76 noro 2786: b = a^(-1)b
1.3 noro 2787: */
2788:
1.44 noro 2789: void solve_by_lu_mod(int **a,int n,int md,int **b,int l,int normalize)
1.3 noro 2790: {
1.76 noro 2791: unsigned int *y,*c;
2792: int i,j,k;
2793: unsigned int t,m,m2;
2794:
2795: y = (int *)MALLOC_ATOMIC(n*sizeof(int));
2796: c = (int *)MALLOC_ATOMIC(n*sizeof(int));
2797: m2 = md>>1;
2798: for ( k = 0; k < l; k++ ) {
2799: /* copy b[.][k] to c */
2800: for ( i = 0; i < n; i++ )
2801: c[i] = (unsigned int)b[i][k];
2802: /* solve Ly=c */
2803: for ( i = 0; i < n; i++ ) {
2804: for ( t = c[i], j = 0; j < i; j++ )
2805: if ( a[i][j] ) {
2806: m = md - a[i][j];
2807: DMAR(m,y[j],t,md,t)
2808: }
2809: y[i] = t;
2810: }
2811: /* solve Uc=y */
2812: for ( i = n-1; i >= 0; i-- ) {
2813: for ( t = y[i], j =i+1; j < n; j++ )
2814: if ( a[i][j] ) {
2815: m = md - a[i][j];
2816: DMAR(m,c[j],t,md,t)
2817: }
2818: /* a[i][i] = 1/U[i][i] */
2819: DMAR(t,a[i][i],0,md,c[i])
2820: }
2821: /* copy c to b[.][k] with normalization */
2822: if ( normalize )
2823: for ( i = 0; i < n; i++ )
2824: b[i][k] = (int)(c[i]>m2 ? c[i]-md : c[i]);
2825: else
2826: for ( i = 0; i < n; i++ )
2827: b[i][k] = c[i];
2828: }
1.3 noro 2829: }
2830:
1.24 noro 2831: void Pleqm1(NODE arg,VECT *rp)
1.1 noro 2832: {
1.76 noro 2833: MAT m;
2834: VECT vect;
2835: pointer **mat;
2836: Q *v;
2837: Q q;
2838: int **wmat;
2839: int md,i,j,row,col,t,n,status;
2840:
2841: asir_assert(ARG0(arg),O_MAT,"leqm1");
2842: asir_assert(ARG1(arg),O_N,"leqm1");
2843: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2844: row = m->row; col = m->col; mat = m->body;
2845: wmat = (int **)almat(row,col);
2846: for ( i = 0; i < row; i++ )
2847: for ( j = 0; j < col; j++ )
2848: if ( q = (Q)mat[i][j] ) {
2849: t = rem(NM(q),md);
2850: if ( SGN(q) < 0 )
2851: t = (md - t) % md;
2852: wmat[i][j] = t;
2853: } else
2854: wmat[i][j] = 0;
2855: status = gauss_elim_mod1(wmat,row,col,md);
2856: if ( status < 0 )
2857: *rp = 0;
2858: else if ( status > 0 )
2859: *rp = (VECT)ONE;
2860: else {
2861: n = col - 1;
2862: MKVECT(vect,n);
2863: for ( i = 0, v = (Q *)vect->body; i < n; i++ ) {
2864: t = (md-wmat[i][n])%md; STOQ(t,v[i]);
2865: }
2866: *rp = vect;
2867: }
1.1 noro 2868: }
2869:
1.24 noro 2870: int gauss_elim_mod1(int **mat,int row,int col,int md)
1.1 noro 2871: {
1.76 noro 2872: int i,j,k,inv,a,n;
2873: int *t,*pivot;
1.1 noro 2874:
1.76 noro 2875: n = col - 1;
2876: for ( j = 0; j < n; j++ ) {
2877: for ( i = j; i < row && !mat[i][j]; i++ );
2878: if ( i == row )
2879: return 1;
2880: if ( i != j ) {
2881: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2882: }
2883: pivot = mat[j];
2884: inv = invm(pivot[j],md);
2885: for ( k = j; k <= n; k++ )
2886: pivot[k] = dmar(pivot[k],inv,0,md);
2887: for ( i = j+1; i < row; i++ ) {
2888: t = mat[i];
2889: if ( i != j && (a = t[j]) )
2890: for ( k = j, a = md - a; k <= n; k++ )
2891: t[k] = dmar(pivot[k],a,t[k],md);
2892: }
2893: }
2894: for ( i = n; i < row && !mat[i][n]; i++ );
2895: if ( i == row ) {
2896: for ( j = n-1; j >= 0; j-- ) {
2897: for ( i = j-1, a = (md-mat[j][n])%md; i >= 0; i-- ) {
2898: mat[i][n] = dmar(mat[i][j],a,mat[i][n],md);
2899: mat[i][j] = 0;
2900: }
2901: }
2902: return 0;
2903: } else
2904: return -1;
1.1 noro 2905: }
2906:
1.24 noro 2907: void Pgeninvm(NODE arg,LIST *rp)
1.1 noro 2908: {
1.76 noro 2909: MAT m;
2910: pointer **mat;
2911: Q **tmat;
2912: Q q;
2913: unsigned int **wmat;
2914: int md,i,j,row,col,t,status;
2915: MAT mat1,mat2;
2916: NODE node1,node2;
2917:
2918: asir_assert(ARG0(arg),O_MAT,"leqm1");
2919: asir_assert(ARG1(arg),O_N,"leqm1");
2920: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
2921: row = m->row; col = m->col; mat = m->body;
2922: wmat = (unsigned int **)almat(row,col+row);
2923: for ( i = 0; i < row; i++ ) {
2924: bzero((char *)wmat[i],(col+row)*sizeof(int));
2925: for ( j = 0; j < col; j++ )
2926: if ( q = (Q)mat[i][j] ) {
2927: t = rem(NM(q),md);
2928: if ( SGN(q) < 0 )
2929: t = (md - t) % md;
2930: wmat[i][j] = t;
2931: }
2932: wmat[i][col+i] = 1;
2933: }
2934: status = gauss_elim_geninv_mod(wmat,row,col,md);
2935: if ( status > 0 )
2936: *rp = 0;
2937: else {
2938: MKMAT(mat1,col,row); MKMAT(mat2,row-col,row);
2939: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
2940: for ( j = 0; j < row; j++ )
2941: UTOQ(wmat[i][j+col],tmat[i][j]);
2942: for ( tmat = (Q **)mat2->body; i < row; i++ )
2943: for ( j = 0; j < row; j++ )
2944: UTOQ(wmat[i][j+col],tmat[i-col][j]);
2945: MKNODE(node2,mat2,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
2946: }
1.1 noro 2947: }
2948:
1.24 noro 2949: int gauss_elim_geninv_mod(unsigned int **mat,int row,int col,int md)
1.1 noro 2950: {
1.76 noro 2951: int i,j,k,inv,a,n,m;
2952: unsigned int *t,*pivot;
1.1 noro 2953:
1.76 noro 2954: n = col; m = row+col;
2955: for ( j = 0; j < n; j++ ) {
2956: for ( i = j; i < row && !mat[i][j]; i++ );
2957: if ( i == row )
2958: return 1;
2959: if ( i != j ) {
2960: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
2961: }
2962: pivot = mat[j];
2963: inv = invm(pivot[j],md);
2964: for ( k = j; k < m; k++ )
2965: pivot[k] = dmar(pivot[k],inv,0,md);
2966: for ( i = j+1; i < row; i++ ) {
2967: t = mat[i];
2968: if ( a = t[j] )
2969: for ( k = j, a = md - a; k < m; k++ )
2970: t[k] = dmar(pivot[k],a,t[k],md);
2971: }
2972: }
2973: for ( j = n-1; j >= 0; j-- ) {
2974: pivot = mat[j];
2975: for ( i = j-1; i >= 0; i-- ) {
2976: t = mat[i];
2977: if ( a = t[j] )
2978: for ( k = j, a = md - a; k < m; k++ )
2979: t[k] = dmar(pivot[k],a,t[k],md);
2980: }
2981: }
2982: return 0;
1.1 noro 2983: }
2984:
1.24 noro 2985: void Psolve_by_lu_gfmmat(NODE arg,VECT *rp)
1.1 noro 2986: {
1.76 noro 2987: GFMMAT lu;
2988: Q *perm,*rhs,*v;
2989: int n,i;
2990: unsigned int md;
2991: unsigned int *b,*sol;
2992: VECT r;
2993:
2994: lu = (GFMMAT)ARG0(arg);
2995: perm = (Q *)BDY((VECT)ARG1(arg));
2996: rhs = (Q *)BDY((VECT)ARG2(arg));
2997: md = (unsigned int)QTOS((Q)ARG3(arg));
2998: n = lu->col;
2999: b = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
3000: sol = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
3001: for ( i = 0; i < n; i++ )
3002: b[i] = QTOS(rhs[QTOS(perm[i])]);
3003: solve_by_lu_gfmmat(lu,md,b,sol);
3004: MKVECT(r,n);
3005: for ( i = 0, v = (Q *)r->body; i < n; i++ )
3006: UTOQ(sol[i],v[i]);
3007: *rp = r;
1.1 noro 3008: }
3009:
1.24 noro 3010: void solve_by_lu_gfmmat(GFMMAT lu,unsigned int md,
1.76 noro 3011: unsigned int *b,unsigned int *x)
1.1 noro 3012: {
1.76 noro 3013: int n;
3014: unsigned int **a;
3015: unsigned int *y;
3016: int i,j;
3017: unsigned int t,m;
3018:
3019: n = lu->col;
3020: a = lu->body;
3021: y = (unsigned int *)MALLOC_ATOMIC(n*sizeof(int));
3022: /* solve Ly=b */
3023: for ( i = 0; i < n; i++ ) {
3024: for ( t = b[i], j = 0; j < i; j++ )
3025: if ( a[i][j] ) {
3026: m = md - a[i][j];
3027: DMAR(m,y[j],t,md,t)
3028: }
3029: y[i] = t;
3030: }
3031: /* solve Ux=y */
3032: for ( i = n-1; i >= 0; i-- ) {
3033: for ( t = y[i], j =i+1; j < n; j++ )
3034: if ( a[i][j] ) {
3035: m = md - a[i][j];
3036: DMAR(m,x[j],t,md,t)
3037: }
3038: /* a[i][i] = 1/U[i][i] */
3039: DMAR(t,a[i][i],0,md,x[i])
3040: }
1.1 noro 3041: }
3042:
1.53 noro 3043: void Plu_mat(NODE arg,LIST *rp)
3044: {
1.76 noro 3045: MAT m,lu;
3046: Q dn;
3047: Q *v;
3048: int n,i;
3049: int *iperm;
3050: VECT perm;
3051: NODE n0;
3052:
3053: asir_assert(ARG0(arg),O_MAT,"lu_mat");
3054: m = (MAT)ARG0(arg);
3055: n = m->row;
3056: MKMAT(lu,n,n);
3057: lu_dec_cr(m,lu,&dn,&iperm);
3058: MKVECT(perm,n);
3059: for ( i = 0, v = (Q *)perm->body; i < n; i++ )
3060: STOQ(iperm[i],v[i]);
3061: n0 = mknode(3,lu,dn,perm);
3062: MKLIST(*rp,n0);
1.53 noro 3063: }
3064:
1.24 noro 3065: void Plu_gfmmat(NODE arg,LIST *rp)
1.1 noro 3066: {
1.76 noro 3067: MAT m;
3068: GFMMAT mm;
3069: unsigned int md;
3070: int i,row,col,status;
3071: int *iperm;
3072: Q *v;
3073: VECT perm;
3074: NODE n0;
3075:
3076: asir_assert(ARG0(arg),O_MAT,"lu_gfmmat");
3077: asir_assert(ARG1(arg),O_N,"lu_gfmmat");
3078: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
3079: mat_to_gfmmat(m,md,&mm);
3080: row = m->row;
3081: col = m->col;
3082: iperm = (int *)MALLOC_ATOMIC(row*sizeof(int));
3083: status = lu_gfmmat(mm,md,iperm);
3084: if ( !status )
3085: n0 = 0;
3086: else {
3087: MKVECT(perm,row);
3088: for ( i = 0, v = (Q *)perm->body; i < row; i++ )
3089: STOQ(iperm[i],v[i]);
3090: n0 = mknode(2,mm,perm);
3091: }
3092: MKLIST(*rp,n0);
1.1 noro 3093: }
3094:
1.24 noro 3095: void Pmat_to_gfmmat(NODE arg,GFMMAT *rp)
1.1 noro 3096: {
1.76 noro 3097: MAT m;
3098: unsigned int md;
1.1 noro 3099:
1.76 noro 3100: asir_assert(ARG0(arg),O_MAT,"mat_to_gfmmat");
3101: asir_assert(ARG1(arg),O_N,"mat_to_gfmmat");
3102: m = (MAT)ARG0(arg); md = (unsigned int)QTOS((Q)ARG1(arg));
3103: mat_to_gfmmat(m,md,rp);
1.1 noro 3104: }
3105:
1.24 noro 3106: void mat_to_gfmmat(MAT m,unsigned int md,GFMMAT *rp)
1.1 noro 3107: {
1.76 noro 3108: unsigned int **wmat;
3109: unsigned int t;
3110: Q **mat;
3111: Q q;
3112: int i,j,row,col;
3113:
3114: row = m->row; col = m->col; mat = (Q **)m->body;
3115: wmat = (unsigned int **)almat(row,col);
3116: for ( i = 0; i < row; i++ ) {
3117: bzero((char *)wmat[i],col*sizeof(unsigned int));
3118: for ( j = 0; j < col; j++ )
3119: if ( q = mat[i][j] ) {
3120: t = (unsigned int)rem(NM(q),md);
3121: if ( SGN(q) < 0 )
3122: t = (md - t) % md;
3123: wmat[i][j] = t;
3124: }
3125: }
3126: TOGFMMAT(row,col,wmat,*rp);
1.1 noro 3127: }
3128:
1.72 ohara 3129: void Pgeninvm_swap(NODE arg,LIST *rp)
1.1 noro 3130: {
1.76 noro 3131: MAT m;
3132: pointer **mat;
3133: Q **tmat;
3134: Q *tvect;
3135: Q q;
3136: unsigned int **wmat,**invmat;
3137: int *index;
3138: unsigned int t,md;
3139: int i,j,row,col,status;
3140: MAT mat1;
3141: VECT vect1;
3142: NODE node1,node2;
3143:
3144: asir_assert(ARG0(arg),O_MAT,"geninvm_swap");
3145: asir_assert(ARG1(arg),O_N,"geninvm_swap");
3146: m = (MAT)ARG0(arg); md = QTOS((Q)ARG1(arg));
3147: row = m->row; col = m->col; mat = m->body;
3148: wmat = (unsigned int **)almat(row,col+row);
3149: for ( i = 0; i < row; i++ ) {
3150: bzero((char *)wmat[i],(col+row)*sizeof(int));
3151: for ( j = 0; j < col; j++ )
3152: if ( q = (Q)mat[i][j] ) {
3153: t = (unsigned int)rem(NM(q),md);
3154: if ( SGN(q) < 0 )
3155: t = (md - t) % md;
3156: wmat[i][j] = t;
3157: }
3158: wmat[i][col+i] = 1;
3159: }
3160: status = gauss_elim_geninv_mod_swap(wmat,row,col,md,&invmat,&index);
3161: if ( status > 0 )
3162: *rp = 0;
3163: else {
3164: MKMAT(mat1,col,col);
3165: for ( i = 0, tmat = (Q **)mat1->body; i < col; i++ )
3166: for ( j = 0; j < col; j++ )
3167: UTOQ(invmat[i][j],tmat[i][j]);
3168: MKVECT(vect1,row);
3169: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
3170: STOQ(index[i],tvect[i]);
3171: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
3172: }
1.1 noro 3173: }
3174:
1.72 ohara 3175: int gauss_elim_geninv_mod_swap(unsigned int **mat,int row,int col,unsigned int md,
3176: unsigned int ***invmatp,int **indexp)
1.1 noro 3177: {
1.76 noro 3178: int i,j,k,inv,a,n,m;
3179: unsigned int *t,*pivot,*s;
3180: int *index;
3181: unsigned int **invmat;
3182:
3183: n = col; m = row+col;
3184: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
3185: for ( i = 0; i < row; i++ )
3186: index[i] = i;
3187: for ( j = 0; j < n; j++ ) {
3188: for ( i = j; i < row && !mat[i][j]; i++ );
3189: if ( i == row ) {
3190: *indexp = 0; *invmatp = 0; return 1;
3191: }
3192: if ( i != j ) {
3193: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
3194: k = index[i]; index[i] = index[j]; index[j] = k;
3195: }
3196: pivot = mat[j];
3197: inv = (unsigned int)invm(pivot[j],md);
3198: for ( k = j; k < m; k++ )
3199: if ( pivot[k] )
3200: pivot[k] = (unsigned int)dmar(pivot[k],inv,0,md);
3201: for ( i = j+1; i < row; i++ ) {
3202: t = mat[i];
3203: if ( a = t[j] )
3204: for ( k = j, a = md - a; k < m; k++ )
3205: if ( pivot[k] )
3206: t[k] = dmar(pivot[k],a,t[k],md);
3207: }
3208: }
3209: for ( j = n-1; j >= 0; j-- ) {
3210: pivot = mat[j];
3211: for ( i = j-1; i >= 0; i-- ) {
3212: t = mat[i];
3213: if ( a = t[j] )
3214: for ( k = j, a = md - a; k < m; k++ )
3215: if ( pivot[k] )
3216: t[k] = dmar(pivot[k],a,t[k],md);
3217: }
3218: }
3219: *invmatp = invmat = (unsigned int **)almat(col,col);
3220: for ( i = 0; i < col; i++ )
3221: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
3222: s[j] = t[col+index[j]];
3223: return 0;
1.27 noro 3224: }
3225:
1.78 ! noro 3226: int gauss_elim_geninv_sf_swap(int **mat,int row,int col,int ***invmatp,int **indexp);
! 3227:
1.27 noro 3228: void Pgeninv_sf_swap(NODE arg,LIST *rp)
3229: {
1.76 noro 3230: MAT m;
3231: GFS **mat,**tmat;
3232: Q *tvect;
3233: GFS q;
3234: int **wmat,**invmat;
3235: int *index;
3236: unsigned int t;
3237: int i,j,row,col,status;
3238: MAT mat1;
3239: VECT vect1;
3240: NODE node1,node2;
3241:
3242: asir_assert(ARG0(arg),O_MAT,"geninv_sf_swap");
3243: m = (MAT)ARG0(arg);
3244: row = m->row; col = m->col; mat = (GFS **)m->body;
3245: wmat = (int **)almat(row,col+row);
3246: for ( i = 0; i < row; i++ ) {
3247: bzero((char *)wmat[i],(col+row)*sizeof(int));
3248: for ( j = 0; j < col; j++ )
3249: if ( q = (GFS)mat[i][j] )
3250: wmat[i][j] = FTOIF(CONT(q));
3251: wmat[i][col+i] = _onesf();
3252: }
3253: status = gauss_elim_geninv_sf_swap(wmat,row,col,&invmat,&index);
3254: if ( status > 0 )
3255: *rp = 0;
3256: else {
3257: MKMAT(mat1,col,col);
3258: for ( i = 0, tmat = (GFS **)mat1->body; i < col; i++ )
3259: for ( j = 0; j < col; j++ )
3260: if ( t = invmat[i][j] ) {
3261: MKGFS(IFTOF(t),tmat[i][j]);
3262: }
3263: MKVECT(vect1,row);
3264: for ( i = 0, tvect = (Q *)vect1->body; i < row; i++ )
3265: STOQ(index[i],tvect[i]);
3266: MKNODE(node2,vect1,0); MKNODE(node1,mat1,node2); MKLIST(*rp,node1);
3267: }
1.27 noro 3268: }
3269:
3270: int gauss_elim_geninv_sf_swap(int **mat,int row,int col,
1.76 noro 3271: int ***invmatp,int **indexp)
1.27 noro 3272: {
1.76 noro 3273: int i,j,k,inv,a,n,m,u;
3274: int *t,*pivot,*s;
3275: int *index;
3276: int **invmat;
3277:
3278: n = col; m = row+col;
3279: *indexp = index = (int *)MALLOC_ATOMIC(row*sizeof(int));
3280: for ( i = 0; i < row; i++ )
3281: index[i] = i;
3282: for ( j = 0; j < n; j++ ) {
3283: for ( i = j; i < row && !mat[i][j]; i++ );
3284: if ( i == row ) {
3285: *indexp = 0; *invmatp = 0; return 1;
3286: }
3287: if ( i != j ) {
3288: t = mat[i]; mat[i] = mat[j]; mat[j] = t;
3289: k = index[i]; index[i] = index[j]; index[j] = k;
3290: }
3291: pivot = mat[j];
3292: inv = _invsf(pivot[j]);
3293: for ( k = j; k < m; k++ )
3294: if ( pivot[k] )
3295: pivot[k] = _mulsf(pivot[k],inv);
3296: for ( i = j+1; i < row; i++ ) {
3297: t = mat[i];
3298: if ( a = t[j] )
3299: for ( k = j, a = _chsgnsf(a); k < m; k++ )
3300: if ( pivot[k] ) {
3301: u = _mulsf(pivot[k],a);
3302: t[k] = _addsf(u,t[k]);
3303: }
3304: }
3305: }
3306: for ( j = n-1; j >= 0; j-- ) {
3307: pivot = mat[j];
3308: for ( i = j-1; i >= 0; i-- ) {
3309: t = mat[i];
3310: if ( a = t[j] )
3311: for ( k = j, a = _chsgnsf(a); k < m; k++ )
3312: if ( pivot[k] ) {
3313: u = _mulsf(pivot[k],a);
3314: t[k] = _addsf(u,t[k]);
3315: }
3316: }
3317: }
3318: *invmatp = invmat = (int **)almat(col,col);
3319: for ( i = 0; i < col; i++ )
3320: for ( j = 0, s = invmat[i], t = mat[i]; j < col; j++ )
3321: s[j] = t[col+index[j]];
3322: return 0;
1.1 noro 3323: }
3324:
3325: void _addn(N,N,N);
3326: int _subn(N,N,N);
3327: void _muln(N,N,N);
3328:
1.24 noro 3329: void inner_product_int(Q *a,Q *b,int n,Q *r)
1.1 noro 3330: {
1.76 noro 3331: int la,lb,i;
3332: int sgn,sgn1;
3333: N wm,wma,sum,t;
3334:
3335: for ( la = lb = 0, i = 0; i < n; i++ ) {
3336: if ( a[i] )
3337: if ( DN(a[i]) )
3338: error("inner_product_int : invalid argument");
3339: else
3340: la = MAX(PL(NM(a[i])),la);
3341: if ( b[i] )
3342: if ( DN(b[i]) )
3343: error("inner_product_int : invalid argument");
3344: else
3345: lb = MAX(PL(NM(b[i])),lb);
3346: }
3347: sgn = 0;
3348: sum= NALLOC(la+lb+2);
3349: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
3350: wm = NALLOC(la+lb+2);
3351: wma = NALLOC(la+lb+2);
3352: for ( i = 0; i < n; i++ ) {
3353: if ( !a[i] || !b[i] )
3354: continue;
3355: _muln(NM(a[i]),NM(b[i]),wm);
3356: sgn1 = SGN(a[i])*SGN(b[i]);
3357: if ( !sgn ) {
3358: sgn = sgn1;
3359: t = wm; wm = sum; sum = t;
3360: } else if ( sgn == sgn1 ) {
3361: _addn(sum,wm,wma);
3362: if ( !PL(wma) )
3363: sgn = 0;
3364: t = wma; wma = sum; sum = t;
3365: } else {
3366: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
3367: sgn *= _subn(sum,wm,wma);
3368: t = wma; wma = sum; sum = t;
3369: }
3370: }
3371: GCFREE(wm);
3372: GCFREE(wma);
3373: if ( !sgn ) {
3374: GCFREE(sum);
3375: *r = 0;
3376: } else
3377: NTOQ(sum,sgn,*r);
1.1 noro 3378: }
3379:
1.3 noro 3380: /* (k,l) element of a*b where a: .x n matrix, b: n x . integer matrix */
3381:
1.24 noro 3382: void inner_product_mat_int_mod(Q **a,int **b,int n,int k,int l,Q *r)
1.3 noro 3383: {
1.76 noro 3384: int la,lb,i;
3385: int sgn,sgn1;
3386: N wm,wma,sum,t;
3387: Q aki;
3388: int bil,bilsgn;
3389: struct oN tn;
3390:
3391: for ( la = 0, i = 0; i < n; i++ ) {
3392: if ( aki = a[k][i] )
3393: if ( DN(aki) )
3394: error("inner_product_int : invalid argument");
3395: else
3396: la = MAX(PL(NM(aki)),la);
3397: }
3398: lb = 1;
3399: sgn = 0;
3400: sum= NALLOC(la+lb+2);
3401: bzero((char *)sum,(la+lb+3)*sizeof(unsigned int));
3402: wm = NALLOC(la+lb+2);
3403: wma = NALLOC(la+lb+2);
3404: for ( i = 0; i < n; i++ ) {
3405: if ( !(aki = a[k][i]) || !(bil = b[i][l]) )
3406: continue;
3407: tn.p = 1;
3408: if ( bil > 0 ) {
3409: tn.b[0] = bil; bilsgn = 1;
3410: } else {
3411: tn.b[0] = -bil; bilsgn = -1;
3412: }
3413: _muln(NM(aki),&tn,wm);
3414: sgn1 = SGN(aki)*bilsgn;
3415: if ( !sgn ) {
3416: sgn = sgn1;
3417: t = wm; wm = sum; sum = t;
3418: } else if ( sgn == sgn1 ) {
3419: _addn(sum,wm,wma);
3420: if ( !PL(wma) )
3421: sgn = 0;
3422: t = wma; wma = sum; sum = t;
3423: } else {
3424: /* sgn*sum+sgn1*wm = sgn*(sum-wm) */
3425: sgn *= _subn(sum,wm,wma);
3426: t = wma; wma = sum; sum = t;
3427: }
3428: }
3429: GCFREE(wm);
3430: GCFREE(wma);
3431: if ( !sgn ) {
3432: GCFREE(sum);
3433: *r = 0;
3434: } else
3435: NTOQ(sum,sgn,*r);
1.3 noro 3436: }
3437:
1.24 noro 3438: void Pmul_mat_vect_int(NODE arg,VECT *rp)
1.1 noro 3439: {
1.76 noro 3440: MAT mat;
3441: VECT vect,r;
3442: int row,col,i;
3443:
3444: mat = (MAT)ARG0(arg);
3445: vect = (VECT)ARG1(arg);
3446: row = mat->row;
3447: col = mat->col;
3448: MKVECT(r,row);
3449: for ( i = 0; i < row; i++ ) {
3450: inner_product_int((Q *)mat->body[i],(Q *)vect->body,col,(Q *)&r->body[i]);
3451: }
3452: *rp = r;
1.1 noro 3453: }
3454:
1.24 noro 3455: void Pnbpoly_up2(NODE arg,GF2N *rp)
1.1 noro 3456: {
1.76 noro 3457: int m,type,ret;
3458: UP2 r;
1.1 noro 3459:
1.76 noro 3460: m = QTOS((Q)ARG0(arg));
3461: type = QTOS((Q)ARG1(arg));
3462: ret = generate_ONB_polynomial(&r,m,type);
3463: if ( ret == 0 )
3464: MKGF2N(r,*rp);
3465: else
3466: *rp = 0;
1.1 noro 3467: }
3468:
1.24 noro 3469: void Px962_irredpoly_up2(NODE arg,GF2N *rp)
1.1 noro 3470: {
1.76 noro 3471: int m,ret,w;
3472: GF2N prev;
3473: UP2 r;
3474:
3475: m = QTOS((Q)ARG0(arg));
3476: prev = (GF2N)ARG1(arg);
3477: if ( !prev ) {
3478: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
3479: bzero((char *)r->b,w*sizeof(unsigned int));
3480: } else {
3481: r = prev->body;
3482: if ( degup2(r) != m ) {
3483: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
3484: bzero((char *)r->b,w*sizeof(unsigned int));
3485: }
3486: }
3487: ret = _generate_irreducible_polynomial(r,m);
3488: if ( ret == 0 )
3489: MKGF2N(r,*rp);
3490: else
3491: *rp = 0;
1.1 noro 3492: }
3493:
1.24 noro 3494: void Pirredpoly_up2(NODE arg,GF2N *rp)
1.1 noro 3495: {
1.76 noro 3496: int m,ret,w;
3497: GF2N prev;
3498: UP2 r;
3499:
3500: m = QTOS((Q)ARG0(arg));
3501: prev = (GF2N)ARG1(arg);
3502: if ( !prev ) {
3503: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
3504: bzero((char *)r->b,w*sizeof(unsigned int));
3505: } else {
3506: r = prev->body;
3507: if ( degup2(r) != m ) {
3508: w = (m>>5)+1; NEWUP2(r,w); r->w = 0;
3509: bzero((char *)r->b,w*sizeof(unsigned int));
3510: }
3511: }
3512: ret = _generate_good_irreducible_polynomial(r,m);
3513: if ( ret == 0 )
3514: MKGF2N(r,*rp);
3515: else
3516: *rp = 0;
1.1 noro 3517: }
3518:
1.26 noro 3519: void Pmat_swap_row_destructive(NODE arg, MAT *m)
3520: {
1.76 noro 3521: int i1,i2;
3522: pointer *t;
3523: MAT mat;
3524:
3525: asir_assert(ARG0(arg),O_MAT,"mat_swap_row_destructive");
3526: asir_assert(ARG1(arg),O_N,"mat_swap_row_destructive");
3527: asir_assert(ARG2(arg),O_N,"mat_swap_row_destructive");
3528: mat = (MAT)ARG0(arg);
3529: i1 = QTOS((Q)ARG1(arg));
3530: i2 = QTOS((Q)ARG2(arg));
3531: if ( i1 < 0 || i2 < 0 || i1 >= mat->row || i2 >= mat->row )
3532: error("mat_swap_row_destructive : Out of range");
3533: t = mat->body[i1];
3534: mat->body[i1] = mat->body[i2];
3535: mat->body[i2] = t;
3536: *m = mat;
1.26 noro 3537: }
3538:
3539: void Pmat_swap_col_destructive(NODE arg, MAT *m)
3540: {
1.76 noro 3541: int j1,j2,i,n;
3542: pointer *mi;
3543: pointer t;
3544: MAT mat;
3545:
3546: asir_assert(ARG0(arg),O_MAT,"mat_swap_col_destructive");
3547: asir_assert(ARG1(arg),O_N,"mat_swap_col_destructive");
3548: asir_assert(ARG2(arg),O_N,"mat_swap_col_destructive");
3549: mat = (MAT)ARG0(arg);
3550: j1 = QTOS((Q)ARG1(arg));
3551: j2 = QTOS((Q)ARG2(arg));
3552: if ( j1 < 0 || j2 < 0 || j1 >= mat->col || j2 >= mat->col )
3553: error("mat_swap_col_destructive : Out of range");
3554: n = mat->row;
3555: for ( i = 0; i < n; i++ ) {
3556: mi = mat->body[i];
3557: t = mi[j1]; mi[j1] = mi[j2]; mi[j2] = t;
3558: }
3559: *m = mat;
1.26 noro 3560: }
1.1 noro 3561: /*
3562: * f = type 'type' normal polynomial of degree m if exists
3563: * IEEE P1363 A.7.2
3564: *
3565: * return value : 0 --- exists
3566: * 1 --- does not exist
3567: * -1 --- failure (memory allocation error)
3568: */
3569:
3570: int generate_ONB_polynomial(UP2 *rp,int m,int type)
3571: {
1.76 noro 3572: int i,r;
3573: int w;
3574: UP2 f,f0,f1,f2,t;
3575:
3576: w = (m>>5)+1;
3577: switch ( type ) {
3578: case 1:
3579: if ( !TypeT_NB_check(m,1) ) return 1;
3580: NEWUP2(f,w); *rp = f; f->w = w;
3581: /* set all the bits */
3582: for ( i = 0; i < w; i++ )
3583: f->b[i] = 0xffffffff;
3584: /* mask the top word if necessary */
3585: if ( r = (m+1)&31 )
3586: f->b[w-1] &= (1<<r)-1;
3587: return 0;
3588: break;
3589: case 2:
3590: if ( !TypeT_NB_check(m,2) ) return 1;
3591: NEWUP2(f,w); *rp = f;
3592: W_NEWUP2(f0,w);
3593: W_NEWUP2(f1,w);
3594: W_NEWUP2(f2,w);
3595:
3596: /* recursion for genrating Type II normal polynomial */
3597:
3598: /* f0 = 1, f1 = t+1 */
3599: f0->w = 1; f0->b[0] = 1;
3600: f1->w = 1; f1->b[0] = 3;
3601: for ( i = 2; i <= m; i++ ) {
3602: /* f2 = t*f1+f0 */
3603: _bshiftup2(f1,-1,f2);
3604: _addup2_destructive(f2,f0);
3605: /* cyclic change of the variables */
3606: t = f0; f0 = f1; f1 = f2; f2 = t;
3607: }
3608: _copyup2(f1,f);
3609: return 0;
3610: break;
3611: default:
3612: return -1;
3613: break;
3614: }
1.1 noro 3615: }
3616:
3617: /*
3618: * f = an irreducible trinomial or pentanomial of degree d 'after' f
3619: * return value : 0 --- exists
3620: * 1 --- does not exist (exhaustion)
3621: */
3622:
3623: int _generate_irreducible_polynomial(UP2 f,int d)
3624: {
1.76 noro 3625: int ret,i,j,k,nz,i0,j0,k0;
3626: int w;
3627: unsigned int *fd;
3628:
3629: /*
3630: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
3631: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
3632: * otherwise i0,j0,k0 is set to 0.
3633: */
3634:
3635: fd = f->b;
3636: w = (d>>5)+1;
3637: if ( f->w && (d==degup2(f)) ) {
3638: for ( nz = 0, i = d; i >= 0; i-- )
3639: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
3640: switch ( nz ) {
3641: case 3:
3642: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
3643: /* reset i0-th bit */
3644: fd[i0>>5] &= ~(1<<(i0&31));
3645: j0 = k0 = 0;
3646: break;
3647: case 5:
3648: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
3649: /* reset i0-th bit */
3650: fd[i0>>5] &= ~(1<<(i0&31));
3651: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
3652: /* reset j0-th bit */
3653: fd[j0>>5] &= ~(1<<(j0&31));
3654: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
3655: /* reset k0-th bit */
3656: fd[k0>>5] &= ~(1<<(k0&31));
3657: break;
3658: default:
3659: f->w = 0; break;
3660: }
3661: } else
3662: f->w = 0;
3663:
3664: if ( !f->w ) {
3665: fd = f->b;
3666: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
3667: i0 = j0 = k0 = 0;
3668: }
3669: /* if j0 > 0 then f is already a pentanomial */
3670: if ( j0 > 0 ) goto PENTA;
1.1 noro 3671:
1.76 noro 3672: /* searching for an irreducible trinomial */
3673:
3674: for ( i = 1; 2*i <= d; i++ ) {
3675: /* skip the polynomials 'before' f */
3676: if ( i < i0 ) continue;
3677: if ( i == i0 ) { i0 = 0; continue; }
3678: /* set i-th bit */
3679: fd[i>>5] |= (1<<(i&31));
3680: ret = irredcheck_dddup2(f);
3681: if ( ret == 1 ) return 0;
3682: /* reset i-th bit */
3683: fd[i>>5] &= ~(1<<(i&31));
3684: }
3685:
3686: /* searching for an irreducible pentanomial */
1.1 noro 3687: PENTA:
1.76 noro 3688: for ( i = 1; i < d; i++ ) {
3689: /* skip the polynomials 'before' f */
3690: if ( i < i0 ) continue;
3691: if ( i == i0 ) i0 = 0;
3692: /* set i-th bit */
3693: fd[i>>5] |= (1<<(i&31));
3694: for ( j = i+1; j < d; j++ ) {
3695: /* skip the polynomials 'before' f */
3696: if ( j < j0 ) continue;
3697: if ( j == j0 ) j0 = 0;
3698: /* set j-th bit */
3699: fd[j>>5] |= (1<<(j&31));
3700: for ( k = j+1; k < d; k++ ) {
3701: /* skip the polynomials 'before' f */
3702: if ( k < k0 ) continue;
3703: else if ( k == k0 ) { k0 = 0; continue; }
3704: /* set k-th bit */
3705: fd[k>>5] |= (1<<(k&31));
3706: ret = irredcheck_dddup2(f);
3707: if ( ret == 1 ) return 0;
3708: /* reset k-th bit */
3709: fd[k>>5] &= ~(1<<(k&31));
3710: }
3711: /* reset j-th bit */
3712: fd[j>>5] &= ~(1<<(j&31));
3713: }
3714: /* reset i-th bit */
3715: fd[i>>5] &= ~(1<<(i&31));
3716: }
3717: /* exhausted */
3718: return 1;
1.1 noro 3719: }
3720:
3721: /*
3722: * f = an irreducible trinomial or pentanomial of degree d 'after' f
3723: *
3724: * searching strategy:
3725: * trinomial x^d+x^i+1:
3726: * i is as small as possible.
3727: * trinomial x^d+x^i+x^j+x^k+1:
3728: * i is as small as possible.
3729: * For such i, j is as small as possible.
3730: * For such i and j, 'k' is as small as possible.
3731: *
3732: * return value : 0 --- exists
3733: * 1 --- does not exist (exhaustion)
3734: */
3735:
3736: int _generate_good_irreducible_polynomial(UP2 f,int d)
3737: {
1.76 noro 3738: int ret,i,j,k,nz,i0,j0,k0;
3739: int w;
3740: unsigned int *fd;
3741:
3742: /*
3743: * if f = x^d+x^i+1 then i0 <- i, j0 <- 0, k0 <-0.
3744: * if f = x^d+x^k+x^j+x^i+1 (k>j>i) then i0 <- i, j0 <- j, k0 <-k.
3745: * otherwise i0,j0,k0 is set to 0.
3746: */
3747:
3748: fd = f->b;
3749: w = (d>>5)+1;
3750: if ( f->w && (d==degup2(f)) ) {
3751: for ( nz = 0, i = d; i >= 0; i-- )
3752: if ( fd[i>>5]&(1<<(i&31)) ) nz++;
3753: switch ( nz ) {
3754: case 3:
3755: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
3756: /* reset i0-th bit */
3757: fd[i0>>5] &= ~(1<<(i0&31));
3758: j0 = k0 = 0;
3759: break;
3760: case 5:
3761: for ( i0 = 1; !(fd[i0>>5]&(1<<(i0&31))) ; i0++ );
3762: /* reset i0-th bit */
3763: fd[i0>>5] &= ~(1<<(i0&31));
3764: for ( j0 = i0+1; !(fd[j0>>5]&(1<<(j0&31))) ; j0++ );
3765: /* reset j0-th bit */
3766: fd[j0>>5] &= ~(1<<(j0&31));
3767: for ( k0 = j0+1; !(fd[k0>>5]&(1<<(k0&31))) ; k0++ );
3768: /* reset k0-th bit */
3769: fd[k0>>5] &= ~(1<<(k0&31));
3770: break;
3771: default:
3772: f->w = 0; break;
3773: }
3774: } else
3775: f->w = 0;
3776:
3777: if ( !f->w ) {
3778: fd = f->b;
3779: f->w = w; fd[0] |= 1; fd[d>>5] |= (1<<(d&31));
3780: i0 = j0 = k0 = 0;
3781: }
3782: /* if j0 > 0 then f is already a pentanomial */
3783: if ( j0 > 0 ) goto PENTA;
1.1 noro 3784:
1.76 noro 3785: /* searching for an irreducible trinomial */
3786:
3787: for ( i = 1; 2*i <= d; i++ ) {
3788: /* skip the polynomials 'before' f */
3789: if ( i < i0 ) continue;
3790: if ( i == i0 ) { i0 = 0; continue; }
3791: /* set i-th bit */
3792: fd[i>>5] |= (1<<(i&31));
3793: ret = irredcheck_dddup2(f);
3794: if ( ret == 1 ) return 0;
3795: /* reset i-th bit */
3796: fd[i>>5] &= ~(1<<(i&31));
3797: }
3798:
3799: /* searching for an irreducible pentanomial */
1.1 noro 3800: PENTA:
1.76 noro 3801: for ( i = 3; i < d; i++ ) {
3802: /* skip the polynomials 'before' f */
3803: if ( i < i0 ) continue;
3804: if ( i == i0 ) i0 = 0;
3805: /* set i-th bit */
3806: fd[i>>5] |= (1<<(i&31));
3807: for ( j = 2; j < i; j++ ) {
3808: /* skip the polynomials 'before' f */
3809: if ( j < j0 ) continue;
3810: if ( j == j0 ) j0 = 0;
3811: /* set j-th bit */
3812: fd[j>>5] |= (1<<(j&31));
3813: for ( k = 1; k < j; k++ ) {
3814: /* skip the polynomials 'before' f */
3815: if ( k < k0 ) continue;
3816: else if ( k == k0 ) { k0 = 0; continue; }
3817: /* set k-th bit */
3818: fd[k>>5] |= (1<<(k&31));
3819: ret = irredcheck_dddup2(f);
3820: if ( ret == 1 ) return 0;
3821: /* reset k-th bit */
3822: fd[k>>5] &= ~(1<<(k&31));
3823: }
3824: /* reset j-th bit */
3825: fd[j>>5] &= ~(1<<(j&31));
3826: }
3827: /* reset i-th bit */
3828: fd[i>>5] &= ~(1<<(i&31));
3829: }
3830: /* exhausted */
3831: return 1;
1.3 noro 3832: }
3833:
1.24 noro 3834: void printqmat(Q **mat,int row,int col)
1.3 noro 3835: {
1.76 noro 3836: int i,j;
1.3 noro 3837:
1.76 noro 3838: for ( i = 0; i < row; i++ ) {
3839: for ( j = 0; j < col; j++ ) {
3840: printnum((Num)mat[i][j]); printf(" ");
3841: }
3842: printf("\n");
3843: }
1.3 noro 3844: }
3845:
1.24 noro 3846: void printimat(int **mat,int row,int col)
1.3 noro 3847: {
1.76 noro 3848: int i,j;
1.3 noro 3849:
1.76 noro 3850: for ( i = 0; i < row; i++ ) {
3851: for ( j = 0; j < col; j++ ) {
3852: printf("%d ",mat[i][j]);
3853: }
3854: printf("\n");
3855: }
1.36 noro 3856: }
3857:
3858: void Pnd_det(NODE arg,P *rp)
3859: {
1.76 noro 3860: if ( argc(arg) == 1 )
3861: nd_det(0,ARG0(arg),rp);
3862: else
3863: nd_det(QTOS((Q)ARG1(arg)),ARG0(arg),rp);
1.1 noro 3864: }
1.59 ohara 3865:
1.62 ohara 3866: void Pmat_col(NODE arg,VECT *rp)
1.59 ohara 3867: {
1.76 noro 3868: int i,j,n;
3869: MAT mat;
3870: VECT vect;
3871:
3872: asir_assert(ARG0(arg),O_MAT,"mat_col");
3873: asir_assert(ARG1(arg),O_N,"mat_col");
3874: mat = (MAT)ARG0(arg);
3875: j = QTOS((Q)ARG1(arg));
3876: if ( j < 0 || j >= mat->col) {
3877: error("mat_col : Out of range");
3878: }
3879: n = mat->row;
3880: MKVECT(vect,n);
3881: for(i=0; i<n; i++) {
3882: BDY(vect)[i] = BDY(mat)[i][j];
3883: }
3884: *rp = vect;
1.59 ohara 3885: }
1.71 noro 3886:
3887: NODE triangleq(NODE e)
3888: {
3889: int n,i,k;
3890: V v;
3891: VL vl;
3892: P *p;
3893: NODE r,r1;
3894:
3895: n = length(e);
3896: p = (P *)MALLOC(n*sizeof(P));
3897: for ( i = 0; i < n; i++, e = NEXT(e) ) p[i] = (P)BDY(e);
3898: i = 0;
3899: while ( 1 ) {
3900: for ( ; i < n && !p[i]; i++ );
3901: if ( i == n ) break;
3902: if ( OID(p[i]) == O_N ) return 0;
3903: v = p[i]->v;
3904: for ( k = i+1; k < n; k++ )
3905: if ( p[k] ) {
3906: if ( OID(p[k]) == O_N ) return 0;
3907: if ( p[k]->v == v ) p[k] = 0;
3908: }
3909: i++;
3910: }
3911: for ( r = 0, i = 0; i < n; i++ ) {
3912: if ( p[i] ) {
3913: MKNODE(r1,p[i],r); r = r1;
3914: }
3915: }
3916: return r;
3917: }
3918:
3919: void Ptriangleq(NODE arg,LIST *rp)
3920: {
3921: NODE ret;
3922:
3923: asir_assert(ARG0(arg),O_LIST,"sparseleq");
3924: ret = triangleq(BDY((LIST)ARG0(arg)));
3925: MKLIST(*rp,ret);
3926: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>