Computing the norm of an array of vectors

I have an array of (say, row) vectors, v = [ [ a1, a2, a3 ], [ b1, b2, b3 ], [ c1, c2, c3 ], ... ] What is the optimal way to compute the norm of each vector, norm(v)**2 = [ [ a1**2 + a2**2 + a3**2 ], [ b1**2 + b2**2 + b3**2 ], ... ] It seems clear that numpy.norm does not handle the multidimensional case as needed. The best thing I can think of is to do something like, sum(v**2, axis=0) * ones(3) but surely there must be a better way. Any ideas? Cheers, - Ben

On Tue, Feb 8, 2011 at 10:44, Ben Gamari <bgamari.foss@gmail.com> wrote:
I have an array of (say, row) vectors,
v = [ [ a1, a2, a3 ], [ b1, b2, b3 ], [ c1, c2, c3 ], ... ]
What is the optimal way to compute the norm of each vector, norm(v)**2 = [ [ a1**2 + a2**2 + a3**2 ], [ b1**2 + b2**2 + b3**2 ], ... ]
It seems clear that numpy.norm does not handle the multidimensional case as needed. The best thing I can think of is to do something like,
sum(v**2, axis=0) * ones(3)
but surely there must be a better way. Any ideas?
(v*v).sum(axis=1)[:,np.newaxis] You can leave off the newaxis bit if you don't really need a column vector. -- 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

On Tue, 8 Feb 2011 10:46:34 -0600, Robert Kern <robert.kern@gmail.com> wrote:
(v*v).sum(axis=1)[:,np.newaxis]
You can leave off the newaxis bit if you don't really need a column vector.
Fair enough, I unfortunately neglected to mention that I ultimately want to normalize these vectors, hence the *ones(3) in my original proposal (although looking back, the shapes would clearly be incompatible). Is there an elegant way to achieve this? Thanks again, - Ben

On Tue, Feb 8, 2011 at 11:55, Ben Gamari <bgamari.foss@gmail.com> wrote:
On Tue, 8 Feb 2011 10:46:34 -0600, Robert Kern <robert.kern@gmail.com> wrote:
(v*v).sum(axis=1)[:,np.newaxis]
You can leave off the newaxis bit if you don't really need a column vector.
Fair enough, I unfortunately neglected to mention that I ultimately want to normalize these vectors, hence the *ones(3) in my original proposal (although looking back, the shapes would clearly be incompatible). Is there an elegant way to achieve this?
v / np.hypot.reduce(v, axis=1)[:,np.newaxis] -- 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

On 08/02/2011 16:44, Ben Gamari wrote:
I have an array of (say, row) vectors,
v = [ [ a1, a2, a3 ], [ b1, b2, b3 ], [ c1, c2, c3 ], ... ]
What is the optimal way to compute the norm of each vector, norm(v)**2 = [ [ a1**2 + a2**2 + a3**2 ], [ b1**2 + b2**2 + b3**2 ], ... ]
It seems clear that numpy.norm does not handle the multidimensional case as needed. The best thing I can think of is to do something like,
sum(v**2, axis=0) * ones(3)
For this shape=(N,3) vector, this is not what you mean: as Robert Kern also has it you want axis=1, which produces a shape=(N,) (or the [:,newaxis] version which produces shape=(N,1). But what is the point of the ones(3)? I think you intend to make a new (N,3) array where each row duplicates the norm, so that you can then divide out the norms. But through the magic of broadcasting, that's not necessary: v/np.sqrt(sum(v**2, axis=1)[:,newaxis]) does what you want. Andrew

On Tue, 08 Feb 2011 18:06:48 +0000, Andrew Jaffe <a.h.jaffe@gmail.com> wrote:
For this shape=(N,3) vector, this is not what you mean: as Robert Kern also has it you want axis=1, which produces a shape=(N,) (or the [:,newaxis] version which produces shape=(N,1).
But what is the point of the ones(3)? I think you intend to make a new (N,3) array where each row duplicates the norm, so that you can then divide out the norms. But through the magic of broadcasting, that's not necessary:
v/np.sqrt(sum(v**2, axis=1)[:,newaxis])
does what you want.
Thanks! I've since realized the error in my ways. I had completely forgotten that newaxis existed. Robert Kern's proposal is perfect. - Ben
participants (3)
-
Andrew Jaffe
-
Ben Gamari
-
Robert Kern