[Numpy-discussion] broacasting question
Thomas K Gamble
tkgamble at windstream.net
Thu Jun 30 17:57:58 EDT 2011
> On 30.06.2011, at 7:32PM, Thomas K Gamble wrote:
> > I'm trying to convert some IDL code to python/numpy and i'm having some
> > trouble understanding the rules for boradcasting during some operations.
> > example:
> >
> > given the following arrays:
> > a = array((2048,3577), dtype=float)
> > b = array((256,25088), dtype=float)
> > c = array((2048,3136), dtype=float)
> > d = array((2048,3136), dtype=float)
> >
> > do:
> > a = b * c + d
> >
> > In IDL, the computation is done without complaint and all array sizes are
> > preserved. In ptyhon I get a value error concerning broadcasting. I can
> > force it to work by taking slices, but the resulting size would be a =
> > (256x3136) rather than (2048x3577). I admit that I don't understand IDL
> > (or python to be honest) well enough to know how it handles this to be
> > able to replicate the result properly. Does it only operate on the
> > smallest dimensions ignoring the larger indices leaving their values
> > unchanged? Can someone explain this to me?
>
> If IDL does such operations silently I'd probably rather be troubled about
> it... Assuming you actually meant something like "a =
> np.ndarray((2048,3577))" (because np.array((2048,3577), dtype=float) would
> simply be the 2-vector [ 2048. 3577.]), the shape of a indeed matches in
> no way the other ones. While b,c,d do have the same total size, thus
> something like
>
> b.reshape((2048,3136) * c + d
>
> will work, meaning the first 8 rows of b b[:8] would be concatenated to the
> first row of the output, and so on... Since the total size is still
> smaller than a, I could only venture something is done like
>
> np.add(b.reshape(2048,3136) * c, d, out=a[:,:3136])
>
> But to say whether this is really the equivalent result to what IDL does,
> one would have to study the IDL manual in detail or directly compare the
> output (e.g. check what happens to the values in a[:,3136:]...)
>
> Cheers,
> Derek
Your post gave me the cluse I needed.
I had my shapes slightly off in the example I gave, but if I try:
a = reshape(b.flatten('F') * c.flatten('F') + d.flatten('F'), b.shape, order='F')
I get a result in line with the IDL result.
Another example with different total size arrays:
b = np.ndarray((2048,3577), dtype=float)
c = np.ndarray((256,25088), dtype=float)
a= reshape(b.flatten('F')[:size(c)]/c.flatten('F'), c.shape, order='F')
This also gives a result like that of IDL.
Thanks for the help.
--
Thomas K. Gamble tkgamble at windstream.net
Receive my instruction, and not silver; and knowledge rather than choice gold.
For wisdom is better than rubies; and all the things that may be desired are
not to be compared to it. (Proverbs 8:10,11)
More information about the NumPy-Discussion
mailing list