Skip to content Skip to sidebar Skip to footer

How Does Numpy Multiply Matrices Of Complex Numbers?

I've been trying to figure out the algorithm behind NumPy's matrix multiplication for complex numbers: import numpy as np A = np.array([[17.+0.j, -3.+0.j], [-7.+0.j,

Solution 1:

When you compute A*B it's actually multiplying the matrices elementwise, giving you what is called the hadamard product. It isn't matmul. For example (17.+0.j) * (60.+0.j) = 1020.+0.j, which is the first element in the output. For matrix multiplication use np.dot or simply the @ operator, ie, A@B.

Solution 2:

Found it! It seems NumPy uses np.multiply() (element-wise multiplication), hence the different results.

Here is a naive implementation of this function using for loops:

defnp_multiply(X, Y):
    height = X.shape[0]
    width = X.shape[1]
    output = np.empty((height, width), dtype=np.complex128)

    for i inrange(height):
        for j inrange(width):
            output[i,j] = X[i, j] * Y[i, j]

    return output

This post has an interesting discussion on its performance.

Post a Comment for "How Does Numpy Multiply Matrices Of Complex Numbers?"