20

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
4
  • Someone can correct me if I'm wrong, but I think what you're doing is pretty standard. Commented Aug 21, 2012 at 6:04
  • 1
    One thing to look out for is multiplication of arrays. Numpy array multiply by element. If you want to multiply two vectors, use the dot() method. Commented Aug 21, 2012 at 6:34
  • 1
    Judging by the comments and the answer, I think people completely misunderstood your question, and didn't realize you're talking about replacing the euclid module. Commented Aug 6, 2014 at 20:20
  • By the way, how was the migration? Was it motivated by performance? I'm planning on use euclid, so any tips are welcome :) Commented Aug 6, 2014 at 20:30

1 Answer 1

36

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.

Sign up to request clarification or add additional context in comments.

1 Comment

[1] Is this the common route to use? (Using numpy.array vs euclid or something else) [2] to get Vector2.magnitude() do I use linalg.norm(v) [3] What function do I use for Vector2.normalize() ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.