Confusion on contour plots for different types of functions

40 views
Skip to first unread message

Dylan Thurston

unread,
Sep 6, 2009, 8:51:48 PM9/6/09
to sage-support
I'm trying to improve the complex_plot facilities to include real and
complex contour lines, in the style of the book Visual Complex
Analysis. I'm getting rather confused by some of the behaviour of
contour_plot in SAGE 4.1.1.

Specifically, if I run

sage: f(z) = z^2; p1 = complex_plot(f, (-3,3), (-3,3))
sage: p2 = contour_plot(lambda x, y: imag(f(x+y*I)), (-3, 3), (-3, 3),
fill=False, cmap=[(1,0,0)], contours=[-24,-22..24])
sage: p1+p2

I get something that I expect.

On the other hand, if I then try

sage: p3 = contour_plot(lambda x, y: real(f(x+y*I)), (-3, 3), (-3, 3),
fill=False, cmap=[(1,0,0)], contours=[-24,-22..24])

I get an error message:




Traceback (click to the left for traceback)
...
ValueError: setting an array element with a sequence.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/dpt/.sage/sage_notebook/worksheets/admin/1/code/24.py",
line 7, in <module>
contour_plot(lambda x, y: real(f(x+y*I)), (-_sage_const_3 ,
_sage_const_3 ), (-_sage_const_3 , _sage_const_3 ), fill=False, cmap=
[(_sage_const_1 ,_sage_const_0 ,_sage_const_0 )], contours=
(ellipsis_range(-_sage_const_24 ,-
_sage_const_22 ,Ellipsis,_sage_const_24 )))
File "", line 1, in <module>

File "sage_object.pyx", line 101, in
sage.structure.sage_object.SageObject.__repr__ (sage/structure/
sage_object.c:1387)
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
sage/plot/plot.py", line 873, in _repr_
self.show()
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
sage/plot/plot.py", line 1364, in show
self.save(**options)
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
sage/plot/plot.py", line 1592, in save
g._render_on_subplot(subplot)
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
sage/plot/contour_plot.py", line 162, in _render_on_subplot
subplot.contour(self.xy_data_array, contours, cmap=cmap, extent=
(x0,x1,y0,y1))
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
matplotlib/axes.py", line 6538, in contour
return mcontour.ContourSet(self, *args, **kwargs)
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
matplotlib/contour.py", line 567, in __init__
x, y, z = self._contour_args(*args) # also sets
self.levels,
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
matplotlib/contour.py", line 751, in _contour_args
z = ma.asarray(args[0], dtype=np.float64)
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
numpy/ma/core.py", line 5536, in asarray
return masked_array(a, dtype=dtype, copy=False, keep_mask=True,
subok=False)
File "/usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/
numpy/ma/core.py", line 2295, in __new__
_data = np.array(data, dtype=dtype, copy=copy, subok=True,
ndmin=ndmin)
ValueError: setting an array element with a sequence.

What's the difference between these two cases? Surely they should
have the same behaviour?

I can work around this (just multiply by I rather than taking the real
part), but I'd like to understand what's going on.

William Stein

unread,
Sep 6, 2009, 11:25:59 PM9/6/09
to sage-s...@googlegroups.com

There is a bug in the float function on symbolic expressions. I
narrowed the problem down to this simple example:

sage: a = real((-I*float(1))^2); a
-1.00000000000000
sage: float(a)


Traceback (most recent call last):

...
TypeError: can't convert complex to float; use abs(z)
sage: b = a.simplify(); b
-1.0
sage: float(b)
-1.0

See http://trac.sagemath.org/sage_trac/ticket/6899

You could do

lambda x, y: real(f(x+y*I)).simplify()

as a temporary workaround, but doing that will be quite slow.

William

P.S. Hi Dylan!

Reply all
Reply to author
Forward
0 new messages