Matlab vs Numpy inner product, different results

I’m translating some old Matlab code I was given to python. I’m experiencing some behavior I don’t understand and to which I didn’t find a proper answer:

I want to multiply two vectors containing complex numbers with an inner product, so I expect to get a complex scalar.
Here is a MWE for Matlab ( I ran it in Octave, so maybe that’s an issue?):

a = [-0.21+0.58i -0.02-0.23i 0.23-0.39i];
b = [ 1.41-1.63i -0.46+0.69i -1.11+1.08i];
a*b'
ans = -2.06750 + 0.77960i

Here the same thing in python:

a = np.array([-0.21+0.58j, -0.02-0.23j ,
        0.23-0.39j])
b = np.array([1.41-1.63j, -0.46 +0.69j,
       -1.11+1.08j])
a@b
(0.9830999999999999+1.9333999999999998j)

I don’t really know much about Matlab but I tried to find as much information about differences to python/numpy as possible and can’t find any way to get the same result in python. Does anybody know what I’m doing wrong here?

Best
Lucas

>Solution :

I believe b' in Matlab is conjugate in Python, so you want

a@b.conj()
# (-2.0675+0.7796000000000001j)

which is the correct inner product, while a@b is (mathematically) not an inner product.

Leave a Reply