I have two numpy arrays a, b and a string of the form s = "1.5 * b if 2*a > 7 else a + b"
I would like to create an array c, which will evaluate the string on the arrays in an efficient manner.
Example of a desired behavior:
a = np.array([1, 4])
b = np.array([3, 1])
s = "1.5 * b if 2*a > 7 else a + b"
print(my_eval(a, b, s))
[4, 1.5]
Was thinking of something like
f = np.vectorize(eval(s)); map(a, b, f)
What's the best way to do it? The number of arrays in the expression can be larger (but bounded by something reasonable.