Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
/* Copyright (C) 2016 The PARI group.12This file is part of the PARI/GP package.34PARI/GP is free software; you can redistribute it and/or modify it under the5terms of the GNU General Public License as published by the Free Software6Foundation; either version 2 of the License, or (at your option) any later7version. It is distributed in the hope that it will be useful, but WITHOUT8ANY WARRANTY WHATSOEVER.910Check the License for details. You should have received a copy of it, along11with the package; see the file 'COPYING'. If not, write to the Free Software12Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */1314#include "pari.h"15#include "paripriv.h"1617/********************************************************************/18/** **/19/** LOW-RES PLOT **/20/** **/21/********************************************************************/22#define ISCR 6423#define JSCR 222425INLINE long26DTOL(double t) { return (long)(t + 0.5); }2728static char29PICT(long j) {30switch(j%3) {31case 0: return '_';32case 1: return 'x';33default: return '"';34}35}36static char37PICTZERO(long j) {38switch(j%3) {39case 0: return ',';40case 1: return '-';41default: return '`';42}43}4445static char *46dsprintf9(double d, char *buf)47{48int i = 10;4950while (--i >= 0) {51sprintf(buf, "%9.*g", i, d);52if (strlen(buf) <= 9) break;53}54return buf;55}5657typedef unsigned char screen[ISCR+1][JSCR+1];5859static void60fill_gap(screen scr, long i, int jnew, int jpre)61{62int mid, i_up, i_lo, up, lo;6364if (jpre < jnew - 2) {65up = jnew - 1; i_up = i;66lo = jpre + 1; i_lo = i - 1;67} else if (jnew < jpre - 2) {68up = jpre - 1; i_up = i - 1;69lo = jnew + 1; i_lo = i;70} else return; /* if gap < 2, leave it as it is. */7172mid = (jpre+jnew)/2;73if (mid>JSCR) mid=JSCR; else if (mid<0) mid=0;74if (lo<0) lo=0;75if (lo<=JSCR) while (lo <= mid) scr[i_lo][lo++] = ':';76if (up>JSCR) up=JSCR;77if (up>=0) while (up > mid) scr[i_up][up--] = ':';78}7980static double81todbl(GEN x) { return rtodbl(gtofp(x, LOWDEFAULTPREC)); }8283void84pariplot(void* E, GEN (*fun)(void *E, GEN x), GEN a, GEN b, GEN ysmlu,GEN ybigu, long prec)85{86const char BLANK = ' ', YY = '|', XX_UPPER = '\'', XX_LOWER = '.';87long jz, j, i, sig;88pari_sp av = avma;89int jnew, jpre = 0; /* for lint */90GEN x, dx;91double diff, dyj, ysml, ybig, y[ISCR+1];92screen scr;93char buf[80], z;9495sig=gcmp(b,a); if (!sig) return;96if (sig<0) { x=a; a=b; b=x; }97x = gtofp(a, prec);98dx = divru(gtofp(gsub(b,a),prec), ISCR-1);99for (j=1; j<=JSCR; j++) scr[1][j]=scr[ISCR][j]=YY;100for (i=2; i<ISCR; i++)101{102scr[i][1] = XX_LOWER;103scr[i][JSCR]= XX_UPPER;104for (j=2; j<JSCR; j++) scr[i][j] = BLANK;105}106ysml = ybig = 0.; /* -Wall */107for (i=1; i<=ISCR; i++)108{109pari_sp av2 = avma;110y[i] = gtodouble( fun(E, x) );111set_avma(av2);112if (i == 1)113ysml = ybig = y[1];114else115{116if (y[i] < ysml) ysml = y[i];117if (y[i] > ybig) ybig = y[i];118}119x = addrr(x,dx);120}121set_avma(av);122if (ysmlu) ysml = gtodouble(ysmlu);123if (ybigu) ybig = gtodouble(ybigu);124diff = ybig - ysml;125if (!diff) { ybig += 1; diff= 1.; }126dyj = ((JSCR-1)*3+2) / diff;127/* work around bug in gcc-4.8 (32bit): plot(x=-5,5,sin(x)))) */128jz = 3 - (long)(ysml*dyj + 0.5); /* 3 - DTOL(ysml*dyj) */129z = PICTZERO(jz); jz /= 3;130for (i=1; i<=ISCR; i++)131{132if (0<=jz && jz<=JSCR) scr[i][jz]=z;133j = 3 + DTOL((y[i]-ysml)*dyj);134jnew = j/3;135if (i > 1) fill_gap(scr, i, jnew, jpre);136if (0<=jnew && jnew<=JSCR) scr[i][jnew] = PICT(j);137jpre = jnew;138}139pari_putc('\n');140pari_printf("%s ", dsprintf9(ybig, buf));141for (i=1; i<=ISCR; i++) pari_putc(scr[i][JSCR]);142pari_putc('\n');143for (j=(JSCR-1); j>=2; j--)144{145pari_puts(" ");146for (i=1; i<=ISCR; i++) pari_putc(scr[i][j]);147pari_putc('\n');148}149pari_printf("%s ", dsprintf9(ysml, buf));150for (i=1; i<=ISCR; i++) pari_putc(scr[i][1]);151pari_putc('\n');152{153char line[10 + 32 + 32 + ISCR - 9];154sprintf(line, "%10s%-9.7g%*.7g\n"," ",todbl(a),ISCR-9,todbl(b));155pari_printf(line);156}157}158159void160pariplot0(GEN a, GEN b, GEN code, GEN ysmlu,GEN ybigu, long prec)161{162push_lex(gen_0, code);163pariplot((void*)code, &gp_eval, a, b, ysmlu, ybigu, prec);164pop_lex(1);165}166167168