On Sep 9, 2011, at 17:05 , Renan Birck Pinheiro wrote:
> I have the following Sage code (it comes from a larger Python program):
>
> from scipy.stats import norm, randint
>
> signalD = randint(0,2);
> signal = signalD.rvs(10);
>
> It successfully generates the array "signal" with 10 random elements.
>
> However, this fails:
>
> list_plot(signal)
> Traceback (click to the left of this block for traceback)
> ...
> ValueError: The truth value of an array with more than one element
> is ambiguous. Use a.any() or a.all()
I *think* that the problem is the type of 'signal': it's a "numpy.ndarray". Evidently, such a gizmo can be coerced to a list or tuple, but not a dictionary.
Your "work-around" (or "list(signal)") should make it work, as you've noticed.
I don't know whether this can be called a bug, or that other thing. Anyone have a thought?
HTH
Justin
--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
--
Democracy is two wolves and a lamb
voting on what to have for lunch.
Liberty is a well-armed lamb contesting
the vote.
It's a bug in how list_plot checks for an empty list. We can see in the
error message (below):
sage: import numpy as np
sage: x=np.array(range(10))
sage: x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
sage: list_plot(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/Users/grout/<ipython console> in <module>()
/Users/grout/sage/local/lib/python2.6/site-packages/sage/plot/plot.pyc
in list_plot(data, plotjoined, **kwargs)
3561 """
3562 from sage.plot.all import line, point
-> 3563 if data == {} or data == () or data == []:
3564 return Graphics()
list_plot is trying to check to see if there is an empty
list/tuple/dict. I think it should just do len(data). The real root of
the error we are seeing is that numpy arrays compare element-by-element
(IIRC) for the dictionary comparison:
sage: x=={}
array([False, False, False, False, False, False, False, False, False,
False], dtype=bool)
Then trying to do bool(resulting array) throws the error.
Anyways, your workaround works great for now. I've opened
http://trac.sagemath.org/sage_trac/ticket/11787 to track this issue.
Thanks,
Jason