I have been searching all weekend for a more elegant (read: no manual for-loop programming) solution to the following problem:
Lets assume we have a custom function f() with an indefinite number of inputs. For simplicity, let us start with two:
def f(x,y):
return x + y
Now I pass an array with the correct number of variables to this function:
x = np.array([x0, x1, x2, ..., xn])
y = np.array([y0, y1, y2, ..., yn])
The answer I look for is:
z = np.array([[x0 + y0, x0 + y1, x0 + y2, ..., x0 + yn],
[x1 + y0, x1 + y1, x1 + y2, ..., x1 + yn],
[x2 + y0, x2 + y1, x2 + y2, ..., x2 + yn],
...])
So, in summary, I am looking for a function, that I can pass another custom function to, which then calculates all possible combinations, without me having to program a ridiculous number of for-loops.
Please help me, hive mind!
Edit 1: the custom function may be of arbitrary complexity. From my real world problem, this is one example:
def f(x, y):
return 1 - (x/2)**y*binom(y, y/2)
Edit 2: the accepted answer works as intended. The answer of Dishin H Goyani linking to stackoverflow.com/a/32742943/6075699 produces the same result using a different path.
Thanks everyone! Stackoverflow rules!