-1

I am trying to calculate Rij = Aij * Bij/Cij by Numpy broadcasting.

B1 * np.linalg.inv(C1) gives a singular matrix error.

I have also tried doing this. It gave me some values but I am not super sure if it is correct. D = B1 / C1[..., None]

import numpy as np
from numpy.linalg import inv


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 numpy_task3_part_A(A,B,C):
    A1 = np.array(A)
    B1 = np.array(B)
    C1 = np.array(C)
    D = B1 / C1[..., None]
    Rij = A1 @ D
    return Rij


A1 = np.array(A)
B1 = np.array(B)
C1 = np.array(C)
print(C1.shape)
print(B1.shape)
print(A1.shape)

numpy_task3_part_A(A,B,C)

How can I solve this issue?

The Myth
  • 1,090
  • 1
  • 4
  • 16
sansa
  • 21
  • 4
  • Can you perform a test? Make 3 small matrices (small, 3x3) where you know what the result would be. Then check if the result is what you expect. – MSH Nov 11 '22 at 17:19

1 Answers1

1

you want to do element-wise matrix multiplication and division, the normal * and / operators do the element-wise operation.

numpy @ operator does matrix product as studied in any algebra course, and dividing by the inv of the matrix actually compute the matrix inverse which is not an element-wise divsion.

you just need to do this.

R = A * B / C
Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23