8

This might be a dumb question, but say i want to build a program from bottom-up like so:

class Atom(object):
    def __init__(self):
        '''
        Constructor
        '''
    def atom(self, foo, bar):
        #...with foo and bar being arrays of atom Params of lengths m & n
        "Do what atoms do"
        return atom_out

...i can put my instances in a dictionary:

class Molecule(Atom):
    def __init__(self):


    def structure(self, a, b):
        #a = 2D array of size (num_of_atoms, m); 'foo' Params for each atom
        #b = 2D array of size (num_of_atoms, n); 'bar' Params for each atom

        unit = self.atom()
        fake_array = {"atom1": unit(a[0], b[0]),
                      "atom2": unit(a[1], b[1]),
                       :                      :                    :
                       :                      :                    :}

    def chemicalBonds(self, this, that, theother):
        :                         :                      :
        :                         :                      :

My question is, is there a way to do this with numpy arrays so that each element in "real_array" would be an instance of atom--i.e., the output of the individual computations of atom function? I can extend this to class Water(molecule): which would perform fast numpy operations on the large structure and chemicalBonds outputs, hence the need for arrays...Or is it the case that i'm going about this the wrong way?

Also if i am on the right track, i'd appreciate if you wanted to throw in any tips on how to structure a "hierarchical program" like this, as i'm not sure i'm doing the above correctly and recently discovered that i don't know what i'm doing.

Thanks in advance.

5
  • 2
    "recently discovered that i don't know what i'm doing". I wonder how it feels. Sounds like a true moment of enlightenment ;) Commented Mar 16, 2013 at 20:27
  • 2
    self.unit = self.atom() -- aren't you missing the args in the function call? what is real_array? what types are the elements in a, b, x, y, foo, bar? Commented Mar 16, 2013 at 20:32
  • @shx2 It's a trip. A crushing low made better by the fact that you can say "I learned something today." Commented Mar 16, 2013 at 20:46
  • @shx2: fake_array is a dictionary of instances so real_array would replace fake_array but be a numpy array of instances instead. self.unit is the local variable for the function, atom inherited from the lower class (which now i see should be called unit instead). The args are passed into unit through the structure function. So if i wanted to describe a water molecule, i'd have my structure as output an "array" containing [hydrogen1]: self.unit(hydrogen1 params),[hydrogen2]: self.unit(hydrogen2 params),[oxygen]: self.unit(oxygen params). Clearer? Commented Mar 16, 2013 at 20:54
  • @shx2: Oh! I think i got you. Is that a little clearer? Commented Mar 16, 2013 at 21:26

1 Answer 1

10

The path to hell is paved with premature optimization... As a beginner in python, focus on your program and what is supposed to do, once it is doing it too slowly you can ask focused questions about how to make it do it faster. I would stick with learning python's intrinsic data structures for managing your objects. You can implement your algorithms using using numpy arrays with standard data types if you are doing large array operations. Once you have some working code you can do performance testing to determine where you need optimization.

Numpy does allow you to create arrays of objects, and I will give you enough rope to hang yourself with below, but creating an ecosystem of tools to operate on those arrays of objects is not a trivial undertaking. You should first work with python data structures (buy Beazley's essential python reference), then with numpy's built in types, then creating your own compound numpy types. As a last resort, use the object type from the example below.

Good luck!

David

import numpy

class Atom(object):
    def atoms_method(self, foo, bar):
        #...with foo and bar being arrays of Paramsof length m & n
        atom_out = foo + bar
        return atom_out


array = numpy.ndarray((10,),dtype=numpy.object)

for i in xrange(10):
    array[i] = Atom()

for i in xrange(10):
    print array[i].atoms_method(i, 5)
Sign up to request clarification or add additional context in comments.

Comments

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.