I am trying to set up a system in which users can submit an equation that needs to be solved. I have a class, DP, which represents a complete parameterization of the problem. Another class, dpsolver, takes a dp object, builds matrices, and solves the problem.
When creating a DP object, one of the attributes is a reference to a function, where the function looks like this:
def equation1(r, tx, p, p_prime, k, k_prime, z, z_prime ): """ it is assumed all arguments are same sized ndarray or scalar""" inv = k_prime - 0.8 * k v = np.power(z_prime * inv, 0.75) * (1-tx) - p*tx + p_prime return(v)
r and tx are scalars defined in the DP object. p, p_prime, k, k_prime, z, z_prime are all matrix matrices that are built on the basis of other information contained in an instance of the DP class. The DP class contains an OrderedDict that has p, k, and z as keys (in that order). p_prime, k_prime and z_prime are all inferred by the solver. My question is how to properly call the equation1 function from the solver. I want this function to be as simple as possible, so other people can imagine equations that can be easily integrated into this system.
I thought using eval could work, or alternatively using lists and then using eval to unpack the list into equation1. What is the best way to do this without using eval? Or will eval be ok in terms of performance?
source share