I want a function that takes in 2 numbers and a matrix and does a multiplication operation using them and then sums every element of the matrix after multiplication (see code below).
I am trying to do this operation for all possible combinations of x and y. What is the best way to do this without using loops, since I know I could loop over the function with different values of x and y but this doesn't seem like an efficient way to do it.
I tried using a meshgrid as input but that didnt work due to the way the input is broadcasted.
import numpy as np
def my_func(num1, num2, matrix2):
return np.sum(matrix*num1*num2)
x = np.linspace(0,5)
y = np.linspace(0,1)
X, Y = np.meshgrid(x, y)
matrix = np.array([[1],[2],[3]])
a = my_func(X,Y,matrix)
I get the following error:
ValueError: operands could not be broadcast together with shapes (50,50) (3,1)
I would like a to equal a meshgrid of values where each value in the array corresponds to the output of my_func for every possible combination of x and y.