# Vector magnitude?

Hi,

I have two questions:

1. Is there any way to represent vectors? Currently I’m using ‘array’ for vectors.

2. Is there a way to calculate the magnitude (length) of a vector?

Thanks.

Robert Dailey wrote:

Hi,

I have two questions:

1) Is there any way to represent vectors? Currently I'm using 'array' for
vectors.

I suppose you mean vectors in the Matlab way? Then you should have a look at
In short: of course you can do all linalg with numpy.array. With numpy.matrix,
some operations (like dot products) can be written more conveniently.

2) Is there a way to calculate the magnitude (length) of a vector?

a = array([3,1,4])
len(a)
a.size # only for a rank-1 array
a.shape[0]

For some basics on numpy, you may also check out:
http://scipy.org/Documentation

HTH

···

--
cheers,
steve

I love deadlines. I like the whooshing sound they make as they fly by. --

Robert Dailey wrote:

1) Is there any way to represent vectors? Currently I'm using 'array'
for vectors.

There is no explicit 'vector' representation. You may view vectors as
either column or row vectors and represent them as 1xN or Nx1 matrices.
In that case, the linear algebra like matrix products etc. can be used
rather elegantly. Otherwise, you just use 1D-arrays and take care of the
vector semantics yourself.

2) Is there a way to calculate the magnitude (length) of a vector?

numpy.linalg.norm(X)

(works for vectors as well as matrices)

Norbert Nemec wrote:

2) Is there a way to calculate the magnitude (length) of a vector?

numpy.linalg.norm(X)

This makes more sense than len(). I've got confused by "length"

···

--
cheers,
steve

I love deadlines. I like the whooshing sound they make as they fly by. --