I'm looking to build a numpy array as follows so that I don't have to hard code countless numpy arrays by hand:
def func_1(x):
return x**2
def func_2(x):
return x**3+1
So the array becomes:
| func_1(x) func_1(x) func_2(x) |
| func_1(x) func_1(x) func_2(x) |
A = | func_1(x) func_1(x) func_2(x) |
| func_1(x) func_1(x) func_2(x) |
now with this array filled with functions for each element create many versions of A:
| 1 1 2 |
| 1 1 2 |
A(x=1) = | 1 1 2 |
| 1 1 2 |
| 4 4 9 |
| 4 4 9 |
A(x=2) = | 4 4 9 |
| 4 4 9 |
Update
I implemented this as follows:
def h(x):
return np.exp(-((x - 1)**2/ (2*(0.25**2))))
def l(x):
return np.exp(-((x - 0)**2/ (2*(0.25**2))))
def m(x):
return np.exp(-((x - 0.5)**2/ (2*(0.25**2))))
def fuzzy_patterns(x):
return np.array([
# pattern_1
np.array ([
[l(x), l(x), h(x)],
[l(x), l(x), h(x)],
[l(x), l(x), h(x)]
]),
# pattern_2
np.array ([
[h(x), h(x), l(x)],
[h(x), h(x), l(x)],
[h(x), h(x), l(x)]
]),
# pattern_3
np.array ([
[h(x), h(x), h(x)],
[l(x), l(x), l(x)],
[l(x), l(x), l(x)]
]),
.
.
.,
# pattern_n
np.array ([
[m(x), m(x), m(x)],
[m(x), l(x), m(x)],
[m(x), m(x), m(x)]
]),
In the end, this seemed like the most straightforward way to go considering the readability of code. I'll accept hiro protagonist's answer as my implementation is most similar to their answer.
np.frompyfuncis a handy tool for working with object arrays, element by element.