I am trying to calculate Rij = Aij x Bji/Cij with numPy broadcasting. Also raise an exception if matrices are not the same size (n × n).
I am not so sure if this is correct or if I should be doing element wise or matrix wise. could anyone tell me how to do it
A = [[(i+j)/2000 for i in range(500)] for j in range(500)]
B = [[(i-j)/2000 for i in range(500)] for j in range(500)]
C = [[((i+1)/(j+1))/2000 for i in range(500)] for j in range(500)]
def matrix_R(A,B,C):
A1 = np.array(A)
B1 = np.array(B)
C1 = np.array(C)
eq = (A1 @ np.transpose(B1))
Rij = np.divide(eq, C1)
if len(A1) != len(B1) or len(A1) != len(C1):
raise ArithmeticError('Matrices are NOT the same size.')
return Rij
matrix_R(A, B, C)
The @ is the matrix product operator for numpy arrays.
is
For element multiplication you may use * which does element-wise product for numpy arrays.
is
To answer your question, you can compute R the matrix of Rij = Aij x Bji/Cij with:
or equivalently and shorter: