[SciPy-dev] numpy.broadcast

David Goldsmith d_l_goldsmith at yahoo.com
Wed Aug 5 18:15:43 EDT 2009


Sorry to be "dense", but can someone please show me how to exhibit associativity with the broadcast object - it's that (not broadcasting as a process) I'm trying to understand (and illustrate).

DG

--- On Wed, 8/5/09, Robert Kern <robert.kern at gmail.com> wrote:

> From: Robert Kern <robert.kern at gmail.com>
> Subject: Re: [SciPy-dev] numpy.broadcast
> To: "SciPy Developers List" <scipy-dev at scipy.org>
> Date: Wednesday, August 5, 2009, 2:01 PM
> On Wed, Aug 5, 2009 at 15:40, David
> Goldsmith<d_l_goldsmith at yahoo.com>
> wrote:
> > --- On Wed, 8/5/09, David Goldsmith <d_l_goldsmith at 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
> _______________________________________________
> Scipy-dev mailing list
> Scipy-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev
> 


      



More information about the SciPy-Dev mailing list