'Creating graphs using code from Pari-GP, but using Sage's graphing tools

everyone!

I know this is probably a dumb question, but I'm having a lot of trouble with Sage, and it's frustrating the hell out of me. To give a bit of an explanation, I code entirely in pari-gp. I don't have a single problem with my code in pari-gp, I'm just having trouble with interfacing my code with Sage. Which, this is something Sage bolsters, that you can run pari-gp code within sage. I essentially want to run all my processes in pari-gp, but I want to exploit all of Sage's graphing protocols.

What I want to do couldn't be easier. I have a well working function test(x,y), which produces a real number. I don't have any problems running this in pari, everything is kosher. All I want to do is make a graph of test(x,y) = z. Now I know how to run this command in sage, but I don't know how to interface such that test is actually a program running from Pari code.

I'd love to program all my stuff in Sage, but that's just impossible. I need the language Pari, so before you say that I could just translate everything in Sage, it's impossible. I just can't wrap my head around how to interface Pari with Sage. I know it's possible; hell, they advertise it on the main website. I'm just very unclear about how to do the following:

1.) Read a file from my pari-gp root directory: gp.read("myfile.gp").

2.) Inherit the functions, such that gp("test(x,y)"), now runs as though it runs in the GP terminal.

3.) Graph said function test(x,y) using Sage's graphing protocols.

Any help is greatly appreciated. I know I'm just forgetting to do something dumb, or I'm missing a step in how I'm doing it.

Any help is greatly appreciated!



Solution 1:[1]

Let us start with:

sage: gp("test(x, y) = sin(x*y)")
(x,y)->sin(x*y)
sage: gp("test(1, 1)")
0.84147098480789650665250232163029899962
sage: type(_)
<class 'sage.interfaces.gp.GpElement'>

So the printed version of gp("test(1, 1)") looks like a number, but it is not a float instance. Note that py3 has some f-strings, so we can easily plug in values of variables x, y inside the gp("test(...)") . I will use f-strings to define the sage function...

And we are not far away from the final plot:

sage: f = lambda x, y:    float( gp(f"test({x}, {y})") )
sage: plot3d(f, [0, pi], [0, pi])
Launched html viewer for Graphics3d Object

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dan_fulea