ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 07 May 2013 05:11:14 +0200example for numpy.mgrid doesn't workhttps://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/Hello!
I type in sage notebook 5.8 on my local computer:
import numpy as np
np.mgrid?
....
example:
>>> np.mgrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])
But when I type in my notebook:
np.mgrid[-1:1:5j]
and press shift-enter, I get mistake:
TypeError: Unable to convert -0.200000000000000*I to float; use abs() or real_part() as desired
It seems to me it's the bug... Fri, 03 May 2013 04:39:59 +0200https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/Comment by kcrisman for <p>Hello!
I type in sage notebook 5.8 on my local computer:</p>
<pre><code>import numpy as np
np.mgrid?
....
example:
>>> np.mgrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])
</code></pre>
<p>But when I type in my notebook:</p>
<pre><code>np.mgrid[-1:1:5j]
</code></pre>
<p>and press shift-enter, I get mistake:
TypeError: Unable to convert -0.200000000000000*I to float; use abs() or real_part() as desired</p>
<p>It seems to me it's the bug... </p>
https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?comment=17775#post-id-17775Indeed, in the documentation for `mgrid`: "However, if the step length is a **complex number** (e.g. 5j)," so basically this is a numpy hack, in my opinion.Fri, 03 May 2013 11:51:57 +0200https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?comment=17775#post-id-17775Comment by kcrisman for <p>Hello!
I type in sage notebook 5.8 on my local computer:</p>
<pre><code>import numpy as np
np.mgrid?
....
example:
>>> np.mgrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])
</code></pre>
<p>But when I type in my notebook:</p>
<pre><code>np.mgrid[-1:1:5j]
</code></pre>
<p>and press shift-enter, I get mistake:
TypeError: Unable to convert -0.200000000000000*I to float; use abs() or real_part() as desired</p>
<p>It seems to me it's the bug... </p>
https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?comment=17776#post-id-17776That's the preparser. Well, in Python typically `1j**2` becomes `(-1+0j)`; `j` is a complex number ala electrical engineering. What surprises me is that numpy would use j for some other purpose.Fri, 03 May 2013 11:51:27 +0200https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?comment=17776#post-id-17776Answer by calc314 for <p>Hello!
I type in sage notebook 5.8 on my local computer:</p>
<pre><code>import numpy as np
np.mgrid?
....
example:
>>> np.mgrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])
</code></pre>
<p>But when I type in my notebook:</p>
<pre><code>np.mgrid[-1:1:5j]
</code></pre>
<p>and press shift-enter, I get mistake:
TypeError: Unable to convert -0.200000000000000*I to float; use abs() or real_part() as desired</p>
<p>It seems to me it's the bug... </p>
https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?answer=14876#post-id-14876Sage is interpreting this as complex for some reason. But, `np.mgrid[-1:1:0.15]` seems to be working for me.
Fri, 03 May 2013 10:42:37 +0200https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?answer=14876#post-id-14876Comment by kcrisman for <p>Sage is interpreting this as complex for some reason. But, <code>np.mgrid[-1:1:0.15]</code> seems to be working for me.</p>
https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?comment=17774#post-id-17774In the notebook, the user could also just evaluate all cells in pure Python, if so desired.Fri, 03 May 2013 11:53:00 +0200https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?comment=17774#post-id-17774Answer by slelievre for <p>Hello!
I type in sage notebook 5.8 on my local computer:</p>
<pre><code>import numpy as np
np.mgrid?
....
example:
>>> np.mgrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])
</code></pre>
<p>But when I type in my notebook:</p>
<pre><code>np.mgrid[-1:1:5j]
</code></pre>
<p>and press shift-enter, I get mistake:
TypeError: Unable to convert -0.200000000000000*I to float; use abs() or real_part() as desired</p>
<p>It seems to me it's the bug... </p>
https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?answer=14889#post-id-14889You can bypass the Sage preparser by appending `r` (for *raw*) to numerical input.
sage: import numpy as np
sage: np.mgrid[-1:1:5jr]
array([-1. , -0.5, 0. , 0.5, 1. ])
Note that some preparsing is still going on with -1 and 1 which are converted to Sage integers by the preparser before going to numpy's mgrid. If you want to feed numpy's mgrid with raw Python integers:
sage: np.mgrid[-1r:1r:5jr]
array([-1. , -0.5, 0. , 0.5, 1. ])
It's the same result, except less conversion was done on the way.
On a related note, you can get a Python integer by doing
sage: int(1)
but when you do this, the preparsing stage means what is really done is
int(Integer(1))
so the Python integer 1 is converted to a Sage integer and back to a Python integer.
Bypassing the Sage preparser with
sage: 1r
gets you the Python integer straight away.Tue, 07 May 2013 05:11:14 +0200https://ask.sagemath.org/question/10085/example-for-numpymgrid-doesnt-work/?answer=14889#post-id-14889