[Numpy-discussion] Vector magnitude?

2007-09-05 Thread Robert Dailey
Hi,

I have two questions:

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

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

Thanks.
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vector magnitude?

2007-09-05 Thread Gael Varoquaux
On Wed, Sep 05, 2007 at 11:55:36AM -0500, Robert Dailey wrote:
1) Is there any way in numpy to represent vectors? Currently I'm using
'array' for vectors.

What do you call a vector ? For me a vector is an element of an linear
space. In numerical methods what is comonly called a vector is a 1D array
of arbitrary length. I suspect you mean something different, given your
question

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

I am being dumb. What do you mean by magnitude (or length) ? Maybe it is
just because I am not a native English speaker. If you are talking about
the euclidien norm, I don't know a built in way of doing it, but it is
very easy to define a norm function:

import numpy as N

a = N.arange(3)
norm = lambda x: N.sqrt(N.square(x).sum())
norm(a)
- 2.2360679775

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vector magnitude?

2007-09-05 Thread Matthieu Brucher
2007/9/5, Robert Dailey [EMAIL PROTECTED]:

 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


Re: [Numpy-discussion] Vector magnitude?

2007-09-05 Thread Zachary Pincus
Hello,

'len' is a (pretty basic) python builtin function for getting the  
length of anything with a list-like interface. (Or more generally,  
getting the size of anything that is sized, e.g. a set or dictionary.)

Numpy arrays offer a list-like interface allowing you to iterate  
along their first dimension, etc. (*) Thus, len(numpy_array) is  
equivalent to numpy_array.shape[0], which is the number of elements  
along the first dimension of the array.

Zach


(*) For example, this is useful if you've got numerous data vectors  
packed into an array along the first dimension, and want to iterate  
across the different vectors.

a = numpy.ones((number_of_data_elements, size_of_data_element))
for element in a:
# element is a 1-D array with a length of 'size_of_data_element'

Note further that this works even if your data elements are multi- 
dimensional; i.e. the above works the same if:
element_shape = (x,y,z)
a = numpy.ones((number_of_data_elements,)+element_shape)
for element in a:
# element is a 3-D array with a shape of (x,y,z)





On Sep 5, 2007, at 2:40 PM, Robert Dailey wrote:

 Thanks for your response.

 I was not able to find len() in the numpy documentation at the  
 following link:
 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 [EMAIL PROTECTED]  wrote:

 2007/9/5, Robert Dailey  [EMAIL PROTECTED]: 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

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vector magnitude?

2007-09-05 Thread Matthieu Brucher
2007/9/5, Robert Dailey [EMAIL PROTECTED]:

 Thanks for your response.

 I was not able to find len() in the numpy documentation at the following
 link:
 http://www.scipy.org/doc/numpy_api_docs/namespace_index.html

 Perhaps I'm looking in the wrong location?


Yes, it's a Python function ;)

Matthieu
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vector magnitude?

2007-09-05 Thread Robert Dailey
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 [EMAIL PROTECTED] wrote:

 Thanks for your response.

 I was not able to find len() in the numpy documentation at the following
 link:
 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 [EMAIL PROTECTED]  wrote:

 
 
  2007/9/5, Robert Dailey  [EMAIL PROTECTED]:
  
   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


Re: [Numpy-discussion] Vector magnitude?

2007-09-05 Thread Robert Kern
Robert Dailey wrote:
 Thanks for your response.
 
 I was not able to find len() in the numpy documentation at the following
 link:
 http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
 http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
 
 Perhaps I'm looking in the wrong location?

It's a Python builtin function, but it doesn't do what you want. It returns the
number of elements in a sequence (any sequence, not just arrays) not the
magnitude of the vector.

Besides constructing the Euclidean norm itself (as shown by others here), you
can also use numpy.linalg.norm() to calculate any of several different norms of
a vector or a matrix:

In [7]: numpy.linalg.norm?
Type: function
Base Class:   type 'function'
Namespace:Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-1.0.4.dev4025-py2.5-macosx-10.3-fat.egg/numpy/linalg/linalg.py
Definition:   numpy.linalg.norm(x, ord=None)
Docstring:
norm(x, ord=None) - n

Matrix or vector norm.

Inputs:

  x -- a rank-1 (vector) or rank-2 (matrix) array
  ord -- the order of the norm.

 Comments:
   For arrays of any rank, if ord is None:
 calculate the square norm (Euclidean norm for vectors,
 Frobenius norm for matrices)

   For vectors ord can be any real number including Inf or -Inf.
 ord = Inf, computes the maximum of the magnitudes
 ord = -Inf, computes minimum of the magnitudes
 ord is finite, computes sum(abs(x)**ord,axis=0)**(1.0/ord)

   For matrices ord can only be one of the following values:
 ord = 2 computes the largest singular value
 ord = -2 computes the smallest singular value
 ord = 1 computes the largest column sum of absolute values
 ord = -1 computes the smallest column sum of absolute values
 ord = Inf computes the largest row sum of absolute values
 ord = -Inf computes the smallest row sum of absolute values
 ord = 'fro' computes the frobenius norm sqrt(sum(diag(X.H * X),axis=0))

   For values ord  0, the result is, strictly speaking, not a
   mathematical 'norm', but it may still be useful for numerical purposes.

-- 
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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vector magnitude?

2007-09-05 Thread lorenzo bolla
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 [EMAIL PROTECTED] 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 [EMAIL PROTECTED] wrote:
 
  Thanks for your response.
 
  I was not able to find len() in the numpy documentation at the following
  link:
  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  [EMAIL PROTECTED]  wrote:
 
  
  
   2007/9/5, Robert Dailey  [EMAIL PROTECTED]:
   
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


___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vector magnitude?

2007-09-05 Thread Lou Pecora

--- Robert Kern [EMAIL PROTECTED] wrote:

 
 Besides constructing the Euclidean norm itself (as
 shown by others here), you
 can also use numpy.linalg.norm() to calculate any of
 several different norms of
 a vector or a matrix:

Right.  linalg.norm also gives the proper magnitude of
complex vectors



-- Lou Pecora,   my views are my own.
---
Great spirits have always encountered violent opposition from mediocre minds. 
-Albert Einstein


   

Moody friends. Drama queens. Your life? Nope! - their life, your story. Play 
Sims Stories at Yahoo! Games.
http://sims.yahoo.com/  
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion