Noo*_*bot 4 python numpy inner-classes
这可能是一个愚蠢的问题,但是我想要自下而上地建立一个程序:
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
Run Code Online (Sandbox Code Playgroud)
...我可以将我的实例放在字典中:
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):
: : :
: : :
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有办法用numpy数组做这个,以便" real_array" 中的每个元素都是atom--ie 的实例,atom函数的各个计算的输出?我可以扩展这个class Water(molecule):将对大型structure和chemicalBonds输出执行快速numpy操作,因此需要数组...或者是我的错误方式的情况?
此外,如果我在正确的轨道上,我会很感激,如果你想提出如何构建这样的"分层程序"的任何提示,因为我不确定我是否正确地做了上述事情并且最近发现了我不知道我在做什么.
提前致谢.
通往地狱之路的过早优化......作为python的初学者,专注于你的程序和应该做的事情,一旦它做得太慢你就可以提出有关如何让它更快地完成它的重点问题.我会坚持学习python的内部数据结构来管理你的对象.如果您正在进行大型数组操作,则可以使用带有标准数据类型的numpy数组来实现算法.一旦有了一些工作代码,就可以进行性能测试,以确定需要优化的位置.
Numpy确实允许你创建对象数组,我会给你足够的绳索让自己挂在下面,但创建一个工具生态系统来操作这些对象数组并不是一件容易的事.你应该首先使用python数据结构(购买Beazley的基本python参考),然后使用numpy的内置类型,然后创建自己的复合numpy类型.作为最后的手段,请使用以下示例中的对象类型.
祝好运!
大卫
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6623 次 |
| 最近记录: |