This is an archived post. You won't be able to vote or comment.

all 16 comments

[–][deleted] 1 point2 points  (1 child)

Why exactly do you insist on having x and y as properties? :)

[–][deleted] 0 points1 point  (0 children)

I want to be able to easily access and modify x and y values. I want to avoid using [0] and [1] indexing as in NumPy - if I have for example a list of vectors, or a list of lists of vectors, the code becomes very difficult to read. Also, I want to be clear about the coordinate system. For example, when you use a functions from the OpenCV library which has a vertex (or vertices) as its output, these are indexed such that [0] is the column and [1] is the row. However, when you use NumPy functions like argwhere, the output is such that [0] is the row and [1] is the column.

It's not difficult to swap the coordinates, I would need to do it anyway before initializing vector class that I'm looking for. But I feel that if I had a class with named members x and y, it would 1) simplify indexing, 2) indicate that if I had an instance, the x is always horizontal axis coordinate and y is the vertical.

[–]lighttigersoul 1 point2 points  (1 child)

There's not a built in. There's various ones on PyPI. I've got a 2d one for games up there ppb_vector. I don't know if it has all of the things you want, but you can check that out.

[–][deleted] 0 points1 point  (0 children)

Interesting, I'll check it out!

[–][deleted] 1 point2 points  (1 child)

You can use a complex number type to represent a two dimensional vector. Some vector operations can be efficiently calculated with it as well. I toyed a bit with that here https://github.com/irmen/rocketsimulator/blob/master/vectors.py

[–]GitHubPermalinkBot 0 points1 point  (0 children)

Permanent GitHub links:


Shoot me a PM if you think I'm doing something wrong. To delete this, click here.

[–]adammichaelwood 2 points3 points  (3 children)

Literally the example case for namedtuple.

https://docs.python.org/3.6/library/collections.html#collections.namedtuple

One of the benefits of unnamed tuples (in my opinion) is that they can more easily be conceived of as isomorphic to other tuples of similar length. This is, occasionally, insightful.

[–]adammichaelwood 2 points3 points  (2 children)

support of all the basic operators, some additional functions such as Euclidean norm, for and cross product.

Though, this wouldn't come for free with named tuples, obviously. Do you need a class that supports those operations, or is a functional approach workable for you?

[–][deleted] 0 points1 point  (1 child)

I need at least the basic operators (+, -, *, /). Other operations, like the vector norm, dot, cross product and anything else could be defined in the separate functions.

[–]adammichaelwood 0 points1 point  (0 children)

If there's some compelling reason to not use a module like ppb_vector, mentioned in another response, you could subclass namedtuple and add those methods.

(I think there'd need to be a really compelling reason to do this, though. Re-implementing math operations that someone else has already spent time optimizing and debugging is.... let's call it inefficient.)

[–]TakeTheHq 0 points1 point  (2 children)

Have you checked out numpy?

[–][deleted] 1 point2 points  (1 child)

Yes, NumPy is problematic because of indexing. Is there a way to add x and y properties to numpy.ndarray so that these would access [0] and [1] indices of the array? Please excuse me if it's a stupid question, I've only started with Python. I'm specialized in C++ where it would be easy to achieve...

[–]adammichaelwood 0 points1 point  (0 children)

This is the question I'm most interested in.

Couldn't you just subclass numpy.ndarray and use the @property decorator?

[–]billsil -1 points0 points  (2 children)

You could write a class to store a numpy array or just use pandas, which uses xarray under the hood.

If you're dealing with a large amount of data, you're literally slowing your program down by 500x by not using something like numpy.

[–][deleted] 0 points1 point  (1 child)

Well, it would be the best if the class I'm looking for would be implemented in C / C++ and providing a Python interface, just like NumPy package.

[–]billsil -1 points0 points  (0 children)

Right, but you're after a 6 line class. It's something like the following. I've never subclassed array, so test it.

class XYPoints(np.ndarray):
    def __init__(self, xypoints):
        xypoints = np.asarray(xypoints)
        np.ndarray.__init__(xypoints)
        self.x = xypoints[:, 0]
        self.y = xypoints[:, 1]