PROC writeline(int y,int x,int start) { int endd,oxp; unsigned int size; char buf[MAXCOLS+1]; unsigned int bi = 0; endd = fseekeol(start); if (start==0 || core[start-1] == EOL) mvcur(y, 0); else mvcur(y, x); oxp = curpos.x; while (start < endd && curpos.x < COLS) { size = format(&buf[bi],core[start++]); bi += size; curpos.x += size; } if (list) { buf[bi++] = '$'; curpos.x++; } size = min(bi,COLS-oxp); if (size > 0) { zwrite(buf, size); } if (curpos.x < COLS) strput(CE); }
PROC printch(char c) { register int size; char buf[MAXCOLS]; size = min(format(buf,c),COLS-curpos.x); if (size > 0) { zwrite(buf, size); curpos.x += size; } }
PROC printi(int num) { char nb[10]; register int size; numtoa(nb,num); size = min(strlen(nb),COLS-curpos.x); if (size > 0) { zwrite(nb, size); curpos.x += size; } }
int main(void) { double delta = 0.15; long i, n = 16; verylong zP = 0, zQ = 0; verylong *zp = allocate_very_vector(1, n); verylong *zq = allocate_very_vector(1, n); zpstart2(); for (i = 1; i <= n; i++) zintoz(zpnext(), &zq[i]); zintoz(zpnext(), &zQ); printf("delta = %lf\n", delta); printf("n = %ld\n", n); if (n <= 16) { printf("q = "); zwriteln(zQ); printf("q[i] "); for (i = 1; i <= n; i++) { zwrite(zq[i]); printf(" "); } } printf("\n"); if (simultaneous_diophantine(delta, n, zQ, &zP, zp, zq)) { printf("p = "); zwriteln(zP); printf("p[i] "); for (i = 1; i <= n; i++) { zwrite(zp[i]); printf(" "); } } else printf("\nno simultaneous diophantine approximation\n"); free_very_vector(zp, 1, n); free_very_vector(zq, 1, n); return 0; }
PROC prints(char *s) { int size,oxp = curpos.x; char buf[MAXCOLS+1]; unsigned int bi = 0; while (*s && curpos.x < COLS) { size = format(&buf[bi],*s++); bi += size; curpos.x += size; } size = min(bi,COLS-oxp); if (size > 0) zwrite(buf, size); }
void refresh(void) { static int inlist; /* avoiding recursion */ int canscroll = 0, /* number of lines we are allowed to scroll */ ln = 0, /* current line we're working on */ more_status = 0, /* more stuff in status line */ nvcs = 0, nvln = -1, /* video cursor column and line */ t0 = -1, /* tmp */ tosln = 0; /* tmp in statusline stuff */ unsigned char *s, /* pointer into the video buffer */ *t, /* pointer into the real buffer */ *sen, /* pointer to end of the video buffer (eol) */ *scs; /* pointer to cursor position in real buffer */ char **qbuf; /* tmp */ /* If this is called from listmatches() (indirectly via trashzle()), and * * that was called from the end of refresh(), then we don't need to do * * anything. All this `inlist' code is actually unnecessary, but it * * improves speed a little in a common case. */ if (inlist) return; #ifdef HAVE_SELECT cost = 0; /* reset */ #endif #ifndef WINNT /* Nov 96: <mason> I haven't checked how complete this is. sgtty stuff may or may not work */ oxtabs = ((SGTTYFLAG & SGTABTYPE) == SGTABTYPE); #else WINNT oxtabs = 0; #endif WINNT cleareol = 0; /* unset */ more_start = more_end = 0; /* unset */ if (isset(SINGLELINEZLE) || lines < 3 || (termflags & (TERM_NOUP | TERM_BAD | TERM_UNKNOWN))) termflags |= TERM_SHORT; else termflags &= ~TERM_SHORT; if (resetneeded) { onumscrolls = 0; setterm(); #if defined( TIOCGWINSZ) || defined(WINNT) if (winchanged) { moveto(0, 0); t0 = olnct; /* this is to clear extra lines even when */ winchanged = 0; /* the terminal cannot TCCLEAREOD */ } #endif resetvideo(); resetneeded = 0; /* unset */ oput_rpmpt = 0; /* no right-prompt currently on screen */ /* we probably should only have explicitly set attributes */ tsetcap(TCALLATTRSOFF, 0); tsetcap(TCSTANDOUTEND, 0); tsetcap(TCUNDERLINEEND, 0); if (!clearflag) if (tccan(TCCLEAREOD)) tcout(TCCLEAREOD); else cleareol = 1; /* request: clear to end of line */ if (t0 > -1) olnct = t0; if (termflags & TERM_SHORT) vcs = 0; else if (!clearflag && lpptlen) zwrite(lpptbuf, lpptlen, 1, shout); if (clearflag) { zputc('\r', shout); vcs = 0; moveto(0, pptw); } fflush(shout); clearf = clearflag; } else if (winw != columns || rwinh != lines) resetvideo(); /* now winw equals columns and winh equals lines width comparisons can be made with winw, height comparisons with winh */ if (termflags & TERM_SHORT) { singlerefresh(); return; } if (cs < 0) { #ifdef DEBUG fprintf(stderr, "BUG: negative cursor position\n"); fflush(stderr); #endif cs = 0; } scs = line + cs; numscrolls = 0; /* first, we generate the video line buffers so we know what to put on the screen - also determine final cursor position (nvln, nvcs) */ /* Deemed necessary by PWS 1995/05/15 due to kill-line problems */ if (!*nbuf) *nbuf = (char *)zalloc(winw + 2); s = (unsigned char *)(nbuf[ln = 0] + pptw); t = line; sen = (unsigned char *)(*nbuf + winw); for (; t < line+ll; t++) { if (t == scs) /* if cursor is here, remember it */ nvcs = s - (unsigned char *)(nbuf[nvln = ln]); if (*t == '\n') { /* newline */ nbuf[ln][winw + 1] = '\0'; /* text not wrapped */ nextline } else if (*t == '\t') { /* tab */
void zbinary_ext_gcd(verylong zx, verylong zy, verylong *za, verylong *zb, verylong *zv) /* returns a * x + b * y = v, v = gcd(x, y) */ { verylong zA = 0, zB = 0, zC = 0, zD = 0; verylong zX = 0, zY = 0, zc = 0, zg = 0; verylong zu = 0; zone(&zg); zcopy(zx, &zX); zcopy(zy, &zY); while (!zodd(zX) && !zodd(zY)) { zrshift(zX, 1l, &zc); zcopy(zc, &zX); zrshift(zY, 1l, &zc); zcopy(zc, &zY); zlshift(zg, 1l, &zc); zcopy(zc, &zg); } zcopy(zX, &zu); zcopy(zY, zv); zone(&zA); zzero(&zB); zzero(&zC); zone(&zD); do { while (!zodd(zu)) { zrshift(zu, 1l, &zc); zcopy(zc, &zu); if (!zodd(zA) && !zodd(zB)) { zrshift(zA, 1l, &zc); zcopy(zc, &zA); zrshift(zB, 1l, &zc); zcopy(zc, &zB); } else { zadd(zA, zY, &zc); zrshift(zc, 1l, &zA); zsub(zB, zX, &zc); zrshift(zc, 1l, &zB); } } while (!zodd(*zv)) { zrshift(*zv, 1l, &zc); zcopy(zc, zv); if (!zodd(zC) && !zodd(zD)) { zrshift(zC, 1l, &zc); zcopy(zc, &zC); zrshift(zD, 1l, &zc); zcopy(zc, &zD); } else { zadd(zC, zY, &zc); zrshift(zc, 1l, &zC); zsub(zD, zX, &zc); zrshift(zc, 1l, &zD); } } if (zcompare(zu, *zv) >= 0) { zsub(zu, *zv, &zc); zcopy(zc, &zu); zsub(zA, zC, &zc); zcopy(zc, &zA); zsub(zB, zD, &zc); zcopy(zc, &zB); } else { zsub(*zv, zu, &zc); zcopy(zc, zv); zsub(zC, zA, &zc); zcopy(zc, &zC); zsub(zD, zB, &zc); zcopy(zc, &zD); } #ifdef DEBUG zwrite(zu); printf(" "); zwrite(*zv); printf(" "); zwrite(zA); printf(" "); zwrite(zB); printf(" "); zwrite(zC); printf(" "); zwriteln(zD); #endif } while (zscompare(zu, 0l) != 0); zcopy(zC, za); zcopy(zD, zb); zmul(zg, *zv, &zc); zcopy(zc, zv); zfree(&zA); zfree(&zB); zfree(&zC); zfree(&zD); zfree(&zX); zfree(&zY); zfree(&zc); zfree(&zg); zfree(&zu); }
int main(int ac, char **av) { for(av++ ; *av ; av++) zwrite(*av); return 0; }
int simultaneous_diophantine(double delta, long n, verylong zQ, verylong *zP, verylong *zp, verylong *zq) { double P, Q, l; int equal, found; long i, j, n1 = n + 1; verylong zd = 0, zl = 0, zr = 0, zs = 0, zt = 0; verylong **zA = allocate_very_matrix(1, n1, 1, n1); verylong **zh = allocate_very_matrix(1, n1, 1, n1); Q = zdoub(zQ); zintoz(pow(Q, delta), &zl); l = 1.0 / zdoub(zl); zmul(zl, zQ, &zd); for (i = 1; i <= n; i++) zcopy(zd, &zA[i][i]); znegate(&zl); for (i = 1; i <= n; i++) zmul(zl, zq[i], &zA[n1][i]); zone(&zA[n1][n1]); int_LLL(n1, zA, zh); found = 0; for (j = 1; !found && j <= n1; j++) { zcopy(zA[j][n1], zP); if (zcompare(*zP, zQ) != 0) { for (i = 1; i <= n; i++) { zdiv(zA[j][i], zl, &zr, &zs); zmul(*zP, zq[i], &zt); zadd(zr, zt, &zs); zdiv(zs, zQ, &zp[i], &zr); } P = zdoub(*zP); #ifdef DEBUG if (n <= 16) { printf("p = "); zwrite(*zP); printf(" p[i] "); for (i = 1; i <= n; i++) { zwrite(zp[i]); printf(" "); } printf("\n"); } #endif if (zcompare(*zP, 0) != 0) { equal = 1; for (i = 1; equal && i <= n; i++) equal = fabs(P * zdoub(zq[i]) / Q - zdoub(zp[i])) <= l; } else equal = 0; found = equal; } } free_very_matrix(zA, 1, n1, 1, n1); free_very_matrix(zh, 1, n1, 1, n1); zfree(&zd); zfree(&zl); zfree(&zr); zfree(&zs); zfree(&zt); return found; }
void int_LLL(long n, verylong **zb, verylong **zh) { double x, y; long i, j, k = 2, k1, kmax = 1, l; verylong zr = 0, zs = 0, zt = 0, zu = 0; verylong *zB = allocate_very_vector(1, n); verylong *zd = allocate_very_vector(0, n); verylong **zl = allocate_very_matrix(1, n, 1, n); zone(&zd[0]); scalar(n, zb[1], zb[1], &zd[1]); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) zzero(&zh[i][j]); zone(&zh[i][i]); } #ifdef DEBUG if (n <= 17) { printf("the basis to be reduced is:\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { zwrite(zb[i][j]); printf(" "); } printf("\n"); } } #endif L2: if (k <= kmax) goto L3; kmax = k; for (j = 1; j <= k; j++) { scalar(n, zb[k], zb[j], &zu); for (i = 1; i <= j - 1; i++) { zmul(zd[i], zu, &zr); zmul(zl[k][i], zl[j][i], &zs); zsub(zr, zs, &zt); zdiv(zt, zd[i - 1], &zu, &zr); } if (j < k) zcopy(zu, &zl[k][j]); else if (j == k) { zcopy(zu, &zd[k]); if (zscompare(zd[k], 0l) == 0) system_error("Failure in int_LLL."); } } L3: k1 = k - 1; RED(k, k1, n, zd, zb, zh, zl); zmul(zd[k], zd[k - 2], &zr); zsq(zd[k1], &zs); zsq(zl[k][k1], &zt); x = zdoub(zr); y = 3.0 * zdoub(zs) / 4.0 - zdoub(zt); if (x < y) { SWAP(k, k1, kmax, n, zd, zb, zh, zl); k = max(2, k1); goto L3; } for (l = k - 2; l >= 1; l--) RED(k, l, n, zd, zb, zh, zl); if (++k <= n) goto L2; #ifdef DEBUG if (n <= 17) { printf("the LLL-reduced basis is:\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { zwrite(zb[i][j]); printf(" "); } printf("\n"); } } #endif free_very_matrix(zl, 1, n, 1, n); free_very_vector(zB, 1, n); free_very_vector(zd, 0, n); zfree(&zr); zfree(&zs); zfree(&zt); zfree(&zu); }