2

Here the question with details and I think it's clearer,

suppose I have a matrix h of size 4 x 4 , and a vector of x of size 4 x 1, if we have y is the output of multiplication between h and x which means y = h * x; whose size is 1 x 4. So when I multiply again the inverse of every column in h by vector y, I should be able to get a vector equivalent of vector x which means $x = h^{-1} * y $. But unfortunately, I can't get that in python.

for example, let's first do that in MATLAB:

clear all 
clc 

h = (randn(4,4) + 1j*randn(4,4));  %any matrix of 4 x 4 
x = [1 + 1j ; 0; 0 ; 0];           % a vector of 4 x 1 

y = h * x ;                       % y is the output of multiplication 
x2 = [];
for ii = 1 : 4 
    x1 = pinv(h(:,ii))*y;        %multiply every column of h^(-1) with y  
    x2 = [x2 x1];                % the output
end

in that case, the output x2 is as expected, a vector 1 x 4 as below:

x2 =

   1.0000 + 1.0000i   0.7249 + 0.5054i  -0.0202 + 0.0104i   0.2429 + 0.0482i

In MATLAB, that's ok.

Now let's do that in python:

import numpy as np

h = np.random.randn(4,4) + 1j*np.random.randn(4,4)
x = [[1+1j],[0+0j],[0+0j],[0+0j]]
y = h.dot(x)

x2 = []
for ii in range(4):
    x1 = np.divide(y, h[:,ii])
    x2.append(x1)
    print(x2)

Although x2 is supposed to be a vector of dimension 1 x 4 similar as in output of above MATLAB code, but in that case, I get x2 a matrix of size 4 x 4 !!

please any help.

Gze
  • 398
  • 1
  • 12
  • Two issues you are getting wrong: `np.divide()` is element-wise and MATLAB is col major (FORTRAN-style), while NumPy is row major (C-style). – norok2 Aug 01 '19 at 09:59
  • Try `extend` instead of `append`, https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend – Renat Aug 01 '19 at 10:01
  • @Renat, I tried but I get the same issue. ! – Gze Aug 01 '19 at 10:17
  • @norok2 So, please how can I solve that ? you mean to use y = h * x directly? I get the final output also 4 x 4 matrix instead of vector of 1 x 4 ! – Gze Aug 01 '19 at 10:18

1 Answers1

1

There are two issues here:

  • np.divide() is for element-wise division, you may be looking for np.linalg.pinv() instead.
  • MATLAB is col major (FORTRAN-style), while NumPy is row major (C-style) so getting a list as a NumPy array will get you to a shape (n,) with n the length of the list and not an object of size (1, n) as MATLAB would.

The Python code equivalent (sort of, I'll do preallocation) to your MATLAB one, would be:

import numpy as np

h = np.random.randn(4, 4) + 1j * np.random.randn(4, 4)
x = np.array([[1 + 1j], [0 + 0j], [0 + 0j], [0 + 0j]])
# y = h.dot(x)  <-- now NumPy supports also `@` in place of `np.dot()`
y = h @ x

x2 = np.zeros((1, 4), dtype=np.complex)
for i in range(4):
    x2[0, i] = np.linalg.pinv(h[:, i:i + 1]) @ y

as you can see, the shape of the output is enforced right away.

norok2
  • 25,683
  • 4
  • 73
  • 99