[Numpy-discussion] sum of two arrays with different shape?

Robert Kern robert.kern at gmail.com
Sun Dec 17 20:39:30 CST 2006


zhang yunfeng wrote:
> Hi, I'm newbie to Numpy.
> 
> When reading tutorials at
> http://www.scipy.org/Tentative_NumPy_Tutorial
> <http://www.scipy.org/Tentative_NumPy_Tutorial>, I found a snippet about
> addition of two arrays with different shape, Does it make sense? If
> array shapes are not same, why it doesn't throw out an error?

When two arrays of different shapes are operated against each other, numpy tries
to "broadcast" them to a compatible shape according to certain rules. This is a
fairly powerful concept, and it provides quite a lot of convenience. The
following wiki page has an explanation of the broadcasting rules:

  http://www.scipy.org/EricsBroadcastingDoc

It still refers to Numeric, numpy's predecessor, but the concepts still apply
(change "Numeric" to "numpy" and "NewAxis" to "newaxis", and I believe all of
the code examples will be correct).

-- 
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 Numpy-discussion mailing list