Let's consider a function of two variables f(x1, x2)
, where x1
spans over a vector v1
and x2
spans over a vector v2
.
If f(x1, x2) = np.exp(x1 + x2)
, we can represent this function in Python as a matrix by means of the command numpy.meshgrid like this:
xx, yy = numpy.meshgrid(v1, v2)
M = numpy.exp(xx + yy)
This way, M
is a representation of the function f
over the cartesian product "v1
x v2
", since M[i,j] = f(v1[i],v2[j])
.
But this works because both sums and exponential work in parallel componentwise. My question is:
if my variable is x = numpy.array([x1, x2])
and f
is a quadratic function f(x) = x.T @ np.dot(Q, x)
, where Q
is a 2x2 matrix, how can I do the same thing with the meshgrid function (i.e. calculating all the values of the function f on "v1 x v2" at once)?
Please let me know if I should include more details!