A mapping function for numpy matrices… here is a solution to the problem.
A mapping function for numpy matrices
I have two numpy matrices. One contains a lambda function. The other contains the value.
Is there a function like Python’s map function that allows me to get the expected result?
Is there a better way?
functionMatrix = np.array([[lambda x:x**2, lambda x:x**3],[lambda x: x**2,
lambda x: np.sqrt(x)]])
valueMatrix = np.array([[1,2],[3,4]])
expectedResult = np.array([[1,8],[9,2]])
Solution
It’s just grammatical sugar, but it gets the job done.
@np.vectorize
def apply_vec(f, x):
return f(x)
result = apply_vec(functionMatrix, valueMatrix)