Mixing numpy and OO in numerical work
I would advise against using an array of objects because you end up loosing nearly all of the performance benefits of using numpy. We structure or code more like this:
class Points: def __init__(self, x, y): self.x = np.asarray(x) self.y = np.asarray(y) def shift_left(self, distance): self.x -= distancex = np.zeros(10000)y = np.zeros(10000)points_obj = Points(x, y)
Now you can create functions, methods, and so on that operate on points_obj
knowing that points_obj.x
and point_obj.y
are numpy arrays (maybe with size 1, or possibly bigger). If you need to be able to index in to points_obj, you can always define a __getitem__
method on your class.
You can use the same numerical algorithm with or without object orientation. I do not really understand your question. OO is more about program structure and connetion between data. The numerics inside methods can be the same as in normal procedural program.--edit--
You can make array of your parabolas quite quickly when you vectorize its methods. You may of course vectorize even much more complicated ones.
import numpy as npclass parabola: a = 0.0 b = 0.0 c = 0.0 def __init__(self,a,b,c): self.a = a self.b = b self.c = c def set_a(self, new_a): self.a = new_a def set_b(self, new_b): self.b = new_b def set_c(self, new_c): self.c = new_c def get_a(self): return self.a def get_b(self): return self.b def get_c(self): return self.cvpara = np.vectorize(parabola)vgeta = np.vectorize(parabola.get_a)vgetb = np.vectorize(parabola.get_b)vgetc = np.vectorize(parabola.get_c)a = np.zeros(10000)b = np.zeros(10000)c = np.zeros(10000)a[:] = [i for i in xrange(10000)]b[:] = [2*i for i in xrange(10000)]c[:] = [i*i for i in xrange(10000)]objs = np.empty((10000), dtype=object)objs[:] = vpara(a,b,c)print vgeta(objs[1:10:2]),vgetc(objs[9900:9820:-3])