4

First, I'd like to know if it's good practice in general to use an OO approach in numerical work.

Second, a possible use case for OO would be to encapsulate in some object the parameters of some model. Say, I want to study parabolas of the form ax^2 + bx +c. So I would encapsulate a, b, c in some Parabola object. I can plot it and so on. Now, let's say, I want to explore the location of the vertical axis of parabolas. Basically, without OO, I could just plot say a surface of all vertical axis locations w.r.t. a and b (that would be two numpy arrays) for a few given value of c's.

My question is, how do I do such surface plot with the extra OO layer without sacrificing (too much) on numpy performance?

Extra explanation

A way to go with the OO approach would be to create a matrix of Parabola objects for a range of values of parameters a and b. But this way would handle possibly very big objects instead of plain numpy arrays of parameter range.

green diod
  • 1,399
  • 3
  • 14
  • 29

2 Answers2

4

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 -= distance

x = 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.

Bi Rico
  • 25,283
  • 3
  • 52
  • 75
  • Hi, Bi Rico. The problem with your approach is that you would start from the very beginning by implementing your model objects with numpy arrays. In the use case I mentioned, you would have first a simple OO implementation of the model. And then you would need to explore sensitivities or do other static analysis – green diod Sep 27 '12 at 14:21
2

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 np

class 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.c

vpara = 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])
  • I mean, if I wanted to plot, say, on x and y which are just variables and not part of the model, it would be fine. But then, should I just get rid of the OO encapsulation for the model part whenever I want to do heavy stuff on the model parameters? Even if that would mean rewriting the whole stuff in a procedural way? – green diod Sep 20 '12 at 00:48
  • See my edit, It may be helpful to you. Sadly numpy does not allow you the syntax of truly array oriented languages, like Fortran, where you can simply write `objs[556:824:-5].c` and you will get array of those c`s. If someone knows how to do it, please let me know. – Vladimir F Героям слава Sep 20 '12 at 12:50
  • @greendiod, in short yes. See my answer below. – Bi Rico Sep 20 '12 at 19:34