I'd like to encapsulate my calc function and all its parameters inside an object, but vectorize the execution for millions of objects much like how numpy would do it. Any suggestions?
the calculation is still basic arithmetic which numpy should be able to vectorize.
Example code:
import numpy as np
myarray = np.random.rand(3, 10000000)
############################# This works fine: FAST ###################################
def calc(a,b,c):
return (a+b/c)**b/a
res1 = calc(*myarray) #0.7 seconds
############################# What I'd like to do (unsuccessfully): SLOW ###################################
class MyClass():
__slots__ = ['a','b','c']
def __init__(self, a,b,c):
self.a, self.b, self.c = a,b,c
def calc(self):
return (self.a + self.b / self.c) ** self.b / self.a
def classCalc(myClass:MyClass):
return myClass.calc()
vectorizedClassCalc = np.vectorize(classCalc)
myobjects = np.array([MyClass(*args) for args in myarray.transpose()])
res2 = vectorizedClassCalc(myobjects) #8 seconds no different from a list comprehension
res3 = [obj.calc() for obj in myobjects] #7.5 seconds
perhaps pandas has additional features?
np.frompyfuncfor past discussions on this topic.numpy/scipyheavy code) simply doesn't use OOP, or uses it sparingly when it's convenient or the right tool for the job. OOP is a tool: nothing more. But as an aside, consider just leaving yourcalcfunction as is and just pass it in your array: it looks fine as-is.