[Numpy-discussion] Re: reshape and ravel methods of arrays now return views or raise error

Robert Kern robert.kern at gmail.com
Tue Mar 28 22:11:01 CST 2006


Charles R Harris wrote:
> 
> 
> On 3/28/06, *Zachary Pincus* <zpincus at stanford.edu
> <mailto:zpincus at stanford.edu>> wrote:
> 
>     Let me give a more concrete example, pulled from some code I was
>     actually using.
> 
>     The code is obviously a bit flawed, but it worked fine with numpy up
>     until today. Now it breaks when a and b aren't both c-strided:
>     def distance_squared(a, b):
>          return numpy.sum( (a.ravel() - b.ravel())**2 )
> 
> def distance_squared(a,b)
>     return ((a - b)**2).sum()
> 
> The function should probably throw an error if a and b don't have the
> same shape and order and now it does.

Does it?

In [55]: def distance_squared(a, b):
   ....:     return ((a - b) ** 2).sum()
   ....:

In [56]: distance_squared(arange(5), arange(3)[:,newaxis])
Out[56]: 55

-- 
Robert Kern
robert.kern at gmail.com

"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