[Numpy-discussion] Computing the norm of an array of vectors

Ben Gamari bgamari.foss at gmail.com
Tue Feb 8 11:44:42 EST 2011


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



More information about the NumPy-Discussion mailing list