# [Numpy-discussion] Vector magnitude?

lorenzo bolla lbolla@gmail....
Wed Sep 5 13:59:17 CDT 2007

```maybe numpy.vdot is good for you.

In [3]: x = numpy.random.rand(4)

In [4]: x
Out[4]: array([ 0.45426898,  0.22369238,  0.98731244,  0.7758774 ])

In [5]: numpy.sqrt(numpy.vdot(x,x))
Out[5]: 1.35394615117

hth,
lorenzo

On 9/5/07, Robert Dailey <rcdailey@gmail.com> wrote:
>
> Oh I think I get it.
>
> You mean the built-in len() function? This isn't what I am looking for.
> len() returns the number of components in the vector (e.g. whether it is a
> 2D, 3D, etc vector). I found that magnitude can be calculated using hypot()
> in the math module that comes with python. However, this method only appears
> to work with 2D vectors. And yes, by magnitude I mean euclidean norm:
>
> sqrt( x*x + y*y ) = magnitude (length) of a vector
>
> On 9/5/07, Robert Dailey <rcdailey@gmail.com> wrote:
> >
> > Thanks for your response.
> >
> > I was not able to find len() in the numpy documentation at the following
> > http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
> >
> > Perhaps I'm looking in the wrong location?
> >
> > On 9/5/07, Matthieu Brucher < matthieu.brucher@gmail.com > wrote:
> >
> > >
> > >
> > > 2007/9/5, Robert Dailey < rcdailey@gmail.com>:
> > > >
> > > > Hi,
> > > >
> > > > I have two questions:
> > > >
> > > > 1) Is there any way in numpy to represent vectors? Currently I'm
> > > > using 'array' for vectors.
> > >
> > >
> > >
> > > A vector is an array with one dimension, it's OK. You could use a
> > > matrix of dimension 1xn or nx1 as well.
> > >
> > >
> > > 2) Is there a way to calculate the magnitude (length) of a vector in
> > > > numpy?
> > >
> > >
> > > Yes, len(a)
> > >
> > > Matthieu
> > >
> > > _______________________________________________
> > > Numpy-discussion mailing list
> > > Numpy-discussion@scipy.org
> > > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> > >
> > >
> >
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070905/81c40346/attachment.html
```