ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 08 Feb 2018 13:10:46 -0600Using Sage plotting capability on data from PARI/GPhttps://ask.sagemath.org/question/41029/using-sage-plotting-capability-on-data-from-parigp/I am trying to plot data in Sage that is generated by a function in PARI/GP, however keep getting type errors.
I use this simple example to illustrate the point:
f=(gp("Z(s)=zeta(s)"))
f(2)
This gives the correct output 1.6449340668482264364724151666460251892 and appears to be a 'normal' floating point number that I can indeed multiply/add with.
However, when I try the following:
plot(f,(2,6))
It keeps coming back with
TypeError: Error executing code in GP
I have studied most of the (limited) info on the Pari/GP interface and did read that PARI/GP always returns a string. I therefore tried to convert the returned value into a float, int etc., however nothing works. I very likely do something wrong (or not allowed) and would be grateful for any advice on what could be the issue here (esp. since f(2) gives the correct numerical floating point result).
Thanks!
Thu, 08 Feb 2018 05:49:41 -0600https://ask.sagemath.org/question/41029/using-sage-plotting-capability-on-data-from-parigp/Answer by slelievre for <p>I am trying to plot data in Sage that is generated by a function in PARI/GP, however keep getting type errors.</p>
<p>I use this simple example to illustrate the point:</p>
<pre><code>f=(gp("Z(s)=zeta(s)"))
f(2)
</code></pre>
<p>This gives the correct output 1.6449340668482264364724151666460251892 and appears to be a 'normal' floating point number that I can indeed multiply/add with.</p>
<p>However, when I try the following:</p>
<pre><code>plot(f,(2,6))
</code></pre>
<p>It keeps coming back with </p>
<pre><code>TypeError: Error executing code in GP
</code></pre>
<p>I have studied most of the (limited) info on the Pari/GP interface and did read that PARI/GP always returns a string. I therefore tried to convert the returned value into a float, int etc., however nothing works. I very likely do something wrong (or not allowed) and would be grateful for any advice on what could be the issue here (esp. since f(2) gives the correct numerical floating point result).</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/41029/using-sage-plotting-capability-on-data-from-parigp/?answer=41034#post-id-41034The workaround here is to use a lambda expression.
This is a very general trick for any plotting problem where there are problems with the function or expression.
Using the following should get you the plot you want.
sage: plot(lambda x: f(x), (2, 6))
Thu, 08 Feb 2018 13:06:09 -0600https://ask.sagemath.org/question/41029/using-sage-plotting-capability-on-data-from-parigp/?answer=41034#post-id-41034Comment by RuudH for <p>The workaround here is to use a lambda expression.</p>
<p>This is a very general trick for any plotting problem where there are problems with the function or expression.</p>
<p>Using the following should get you the plot you want.</p>
<pre><code>sage: plot(lambda x: f(x), (2, 6))
</code></pre>
https://ask.sagemath.org/question/41029/using-sage-plotting-capability-on-data-from-parigp/?comment=41035#post-id-41035Many thanks! This works very well indeed. I had seen the use of the lambda before, but never related it to calls with PARI/GP objects.Thu, 08 Feb 2018 13:10:46 -0600https://ask.sagemath.org/question/41029/using-sage-plotting-capability-on-data-from-parigp/?comment=41035#post-id-41035