I am currently having 2 following classes:
class plane():
def __init__(self):
self.points = {}
# Using dictionary is necessary, to be able refer a specific point by using its name (saved as dictionary key)
def all_points(self):
all_points = np.empty((3, len(self.points)))
for k, point in enumerate(self.points.values()): # Line A
all_points[k,:] = point.coordinate # Line B
return all_points
import numpy as np
class point():
def __init__(self, x, y, z):
self.coordinate = np.array((x, y, z))
My use case:
a = plane()
a.points[0] = point(0, 1, 2)
a.points[1] = point(1, 2, 3)
a.points[2] = point(2, 3, 4)
a.all_points()
Output:
array([[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]])
Is it possible to replace the for loop with numpy operation? Since Numpy allows following operation:
x = np.array([[0, 1, 2], [2, 3, 4]])
x + 1
Output:
array([[1, 2, 3],
[3, 4, 5]])
My idea was: a.points.values().coordniate. This obviously doesn't work as "dict value" object don't have "coordinate" methode.
Any suggestion is welcome. Thank you in advance for any feedback.
pointobject has acoordinateattribute, you can't avoid iterating in one way or other over allpoints. That's a Python level operation. The suggestions are just window dressing.