Skip to content

Commit 96febb1

Browse files
committed
Enable hi-res SVG plotting in IPython
1 parent bc60822 commit 96febb1

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

autogen/generator.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,24 @@ def __call__(self):
302302
self.decl_file = io.open(self.decl_filename + '.tmp', 'w', encoding='utf-8')
303303
self.decl_file.write(decl_banner)
304304

305+
# Check for availability of hi-res SVG plotting. This requires
306+
# PARI-2.10 or later.
307+
have_plot_svg = False
308+
305309
for v in D:
306-
if not self.can_handle_function(**v):
307-
sys.stdout.write(" (%s)" % v["function"])
308-
else:
309-
sys.stdout.write(" %s" % v["function"])
310+
func = v["function"]
311+
if self.can_handle_function(**v):
312+
sys.stdout.write(" %s" % func)
310313
sys.stdout.flush()
311314
self.handle_pari_function(**v)
315+
if func == "plothraw":
316+
have_plot_svg = True
317+
else:
318+
sys.stdout.write(" (%s)" % func)
312319
sys.stdout.write("\n")
313320

321+
self.instance_file.write("DEF HAVE_PLOT_SVG = {}".format(have_plot_svg))
322+
314323
self.gen_file.close()
315324
self.instance_file.close()
316325
self.decl_file.close()

cypari2/pari_instance.pyx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,11 @@ cdef class Pari(Pari_auto):
579579
self.PARI_TWO = new_gen_noclear(gen_2)
580580
sig_off()
581581

582+
IF HAVE_PLOT_SVG:
583+
# If we are running under IPython, setup for displaying SVG plots.
584+
if "IPython" in sys.modules:
585+
pari_set_plot_engine(get_plot_ipython)
586+
582587
def _close(self):
583588
"""
584589
Deallocate the PARI library.
@@ -1427,3 +1432,24 @@ cdef long get_var(v) except -2:
14271432
varno = fetch_user_var(s)
14281433
sig_off()
14291434
return varno
1435+
1436+
1437+
IF HAVE_PLOT_SVG:
1438+
cdef void get_plot_ipython(PARI_plot* T):
1439+
# Values copied from src/graph/plotsvg.c in PARI sources
1440+
T.width = 480
1441+
T.height = 320
1442+
T.hunit = 3
1443+
T.vunit = 3
1444+
T.fwidth = 9
1445+
T.fheight = 12
1446+
1447+
T.draw = draw_ipython
1448+
1449+
cdef void draw_ipython(PARI_plot *T, GEN w, GEN x, GEN y):
1450+
global avma
1451+
cdef pari_sp av = avma
1452+
cdef char* svg = rect2svg(w, x, y, T)
1453+
from IPython.core.display import SVG, display
1454+
display(SVG(svg))
1455+
avma = av

cypari2/paridecl.pxd

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,6 +3754,46 @@ cdef extern from *: # PARI headers already included by types.pxd
37543754
void pari_mt_init()
37553755
void pari_mt_close()
37563756

3757+
# plotport.c
3758+
3759+
void color_to_rgb(GEN c, int *r, int *g, int *b)
3760+
void colorname_to_rgb(const char *s, int *r, int *g, int *b)
3761+
void pari_plot_by_file(const char *env, const char *suf, const char *img)
3762+
void pari_set_plot_engine(void (*plot)(PARI_plot *))
3763+
void pari_kill_plot_engine()
3764+
void plotbox(long ne, GEN gx2, GEN gy2)
3765+
void plotclip(long rect)
3766+
void plotcolor(long ne, long color)
3767+
void plotcopy(long source, long dest, GEN xoff, GEN yoff, long flag)
3768+
GEN plotcursor(long ne)
3769+
void plotdraw(GEN list, long flag)
3770+
GEN ploth(void *E, GEN(*f)(void*,GEN), GEN a, GEN b, long flags,long n, long prec)
3771+
GEN plothraw(GEN listx, GEN listy, long flag)
3772+
GEN plothsizes(long flag)
3773+
void plotinit(long ne, GEN x, GEN y, long flag)
3774+
void plotkill(long ne)
3775+
void plotline(long ne, GEN gx2, GEN gy2)
3776+
void plotlines(long ne, GEN listx, GEN listy, long flag)
3777+
void plotlinetype(long ne, long t)
3778+
void plotmove(long ne, GEN x, GEN y)
3779+
void plotpoints(long ne, GEN listx, GEN listy)
3780+
void plotpointsize(long ne, GEN size)
3781+
void plotpointtype(long ne, long t)
3782+
void plotrbox(long ne, GEN x2, GEN y2)
3783+
GEN plotrecth(void *E, GEN(*f)(void*,GEN), long ne, GEN a,GEN b, ulong flags,long n, long prec)
3784+
GEN plotrecthraw(long ne, GEN data, long flags)
3785+
void plotrline(long ne, GEN x2, GEN y2)
3786+
void plotrmove(long ne, GEN x, GEN y)
3787+
void plotrpoint(long ne, GEN x, GEN y)
3788+
void plotscale(long ne, GEN x1, GEN x2, GEN y1, GEN y2)
3789+
void plotstring(long ne, char *x, long dir)
3790+
void psdraw(GEN list, long flag)
3791+
GEN psploth(void *E, GEN(*f)(void*,GEN), GEN a, GEN b, long flags, long n, long prec)
3792+
GEN psplothraw(GEN listx, GEN listy, long flag)
3793+
char* rect2ps(GEN w, GEN x, GEN y, PARI_plot *T)
3794+
char* rect2ps_i(GEN w, GEN x, GEN y, PARI_plot *T, int plotps)
3795+
char* rect2svg(GEN w, GEN x, GEN y, PARI_plot *T)
3796+
37573797
# polarit1.c
37583798

37593799
GEN ZX_Zp_root(GEN f, GEN a, GEN p, long prec)
@@ -4348,10 +4388,6 @@ cdef extern from *: # PARI headers already included by types.pxd
43484388
int equaliu(GEN x, ulong y)
43494389
int equalsi(long x, GEN y)
43504390
int equalui(ulong x, GEN y)
4351-
long evalexpo(long x)
4352-
long evallg(long x)
4353-
long evalprecp(long x)
4354-
long evalvalp(long x)
43554391
long expi(GEN x)
43564392
long expu(ulong x)
43574393
void fixlg(GEN z, long ly)

cypari2/types.pxd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ cdef extern from "pari/pari.h":
6565
long evalexpo(long x)
6666
long evallgefint(long x)
6767

68+
ctypedef struct PARI_plot:
69+
long width
70+
long height
71+
long hunit
72+
long vunit
73+
long fwidth
74+
long fheight
75+
void (*draw)(PARI_plot *T, GEN w, GEN x, GEN y)
76+
6877
# Various structures that we don't interface but which need to be
6978
# declared, such that Cython understands the declarations of
7079
# functions using these types.

0 commit comments

Comments
 (0)