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