[SciPy-dev] numpy.broadcast

Robert Kern robert.kern@gmail....
Wed Aug 5 13:51:25 CDT 2009


On Wed, Aug 5, 2009 at 13:48, David Goldsmith<d_l_goldsmith@yahoo.com> wrote:
> I guess I don't really understand this too well - is the below correct behavior, and if so, why?
>
>>>> x = np.array([1, 2, 3])
>>>> y = np.array([[4], [5], [6]])
>>>> b = np.broadcast(x, y)
>>>> b.nd # returns what I'd expect
> 2
>>>> b = np.broadcast(x, y, x, y)
>>>> b.nd # doesn't return what I'd expect
> 2

Why don't you expect this? It's the correct answer. (x*y*x*y).shape == (3,3).

>>>> del b # maybe problem is that I have to "clear" b first?
>>>> # or maybe it's that all args have to be different?
> ...
>>>> b = np.broadcast(x, y, x * y)
>>>> b.nd
> 2
>>>> z = x * y # grasping at straws now
>>>> z
> array([[ 4,  8, 12],
>       [ 5, 10, 15],
>       [ 6, 12, 18]])

Yup. z.shape == (3,3). Broadcasting that with a (3,1) array or a (3,)
array still gives a (3,3) array.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the Scipy-dev mailing list