[SciPy-dev] numpy.broadcast

Robert Kern robert.kern@gmail....
Wed Aug 5 16:01:32 CDT 2009


On Wed, Aug 5, 2009 at 15:40, David Goldsmith<d_l_goldsmith@yahoo.com> wrote:
> --- On Wed, 8/5/09, David Goldsmith <d_l_goldsmith@yahoo.com> wrote:
>
>> > It is necessarily == D. Broadcasting is associative.
>> > The
>> > (x*y*z).shape
>> > == (x*(y*z)).shape == ((x*y)*z).shape.
>
> Um:
>
>>>> x = np.array((1, 2, 3))
>>>> y = np.array([[4], [5], [6]])
>>>> z = x * y
>>>> x; y; z
> array([1, 2, 3])
> array([[4],
>       [5],
>       [6]])
> array([[ 4,  8, 12],
>       [ 5, 10, 15],
>       [ 6, 12, 18]])
>>>> B = np.broadcast
>>>> X = B(x, y, z)
>>>> Y = B(x, B(y, z))
>>>> Z = B(B(x, y), z)
>>>> X.numiter, Y.numiter, Z.numiter
> (3, 2, 2)
>>>> X.nd, Y.nd, Z.nd
> (2, 1, 2)
>>>> X.shape, Y.shape, Z.shape
> ((3, 3), (3,), (3, 3))
>
> Am I doing something wrong?

You are passing a broadcast iterator as an argument to broadcast. The
broadcast iterator iterates over the inputs in-parallel. This is
entirely different from actually operating on the inputs.

In [1]: x = array([1, 2, 3])

In [2]: y = array([[4], [5], [6]])

In [3]: z = x * y

In [4]: x*y*z
Out[4]:
array([[ 16,  64, 144],
       [ 25, 100, 225],
       [ 36, 144, 324]])

In [5]: (x*y)*z
Out[5]:
array([[ 16,  64, 144],
       [ 25, 100, 225],
       [ 36, 144, 324]])

In [6]: x*(y*z)
Out[6]:
array([[ 16,  64, 144],
       [ 25, 100, 225],
       [ 36, 144, 324]])

In [8]: array(list(broadcast(x,y)))
Out[8]:
array([[1, 4],
       [2, 4],
       [3, 4],
       [1, 5],
       [2, 5],
       [3, 5],
       [1, 6],
       [2, 6],
       [3, 6]])

-- 
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