Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python numpy vector math

Tags:

python

numpy

What is the numpy equivalent to euclid's 2d vector classes / operations ? ( like: euclid.Vector2 )

So far I have this. Create two vectors

import numpy as np

loc = np.array([100., 100.])
vel = np.array([30., 10])

loc += vel

# reseting speed to a default value, maintaining direction
vel.normalize()
vel *= 200

loc += vel
like image 806
ninMonkey Avatar asked Aug 21 '12 06:08

ninMonkey


People also ask

How to create a vector in NumPy?

In order to create a vector, we use np.array method. Note: We can create vector with other method as well which return 1-D numpy array for example np.arange (10), np.zeros ( (4, 1)) gives 1-D array, but most appropriate way is using np.array with the 1-D list.

What is vector algebra in Python?

Vector are built from components, which are ordinary numbers. We can think of a vector as a list of numbers, and vector algebra as operations performed on the numbers in the list. In other words vector is the numpy 1-D array. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

What is the use of NumPy in scientific computing?

It is the fundamental package for scientific computing with Python. Numpy is basically used for creating array of n dimensions. Vector are built from components, which are ordinary numbers. We can think of a vector as a list of numbers, and vector algebra as operations performed on the numbers in the list.

How to perform scalar multiplication in NumPy?

To perform scalar multiplication, we need to multiply the scalar by each component of the vector. How to get the magnitude of a vector in NumPy? Writing code in comment?


1 Answers

You can just use numpy arrays. Look at the numpy for matlab users page for a detailed overview of the pros and cons of arrays w.r.t. matrices.

As I mentioned in the comment, having to use the dot() function or method for mutiplication of vectors is the biggest pitfall. But then again, numpy arrays are consistent. All operations are element-wise. So adding or subtracting arrays and multiplication with a scalar all work as expected of vectors.

Edit2: Starting with Python 3.5 and numpy 1.10 you can use the @ infix-operator for matrix multiplication, thanks to pep 465.

Edit: Regarding your comment:

  1. Yes. The whole of numpy is based on arrays.

  2. Yes. linalg.norm(v) is a good way to get the length of a vector. But what you get depends on the possible second argument to norm! Read the docs.

  3. To normalize a vector, just divide it by the length you calculated in (2). Division of arrays by a scalar is also element-wise.

    An example in ipython:

    In [1]: import math
    
    In [2]: import numpy as np
    
    In [3]: a = np.array([4,2,7])
    
    In [4]: np.linalg.norm(a)
    Out[4]: 8.3066238629180749
    
    In [5]: math.sqrt(sum([n**2 for n in a]))
    Out[5]: 8.306623862918075
    
    In [6]: b = a/np.linalg.norm(a)
    
    In [7]: np.linalg.norm(b)
    Out[7]: 1.0
    

    Note that In [5] is an alternative way to calculate the length. In [6] shows normalizing the vector.

like image 170
Roland Smith Avatar answered Sep 20 '22 04:09

Roland Smith