0

I have an array like below:

  poles = numpy.array([[-1+1j], [-1-1j], [-2+3j], [-2-3j]])

Its shape is (4,1).

When I use the numpy.diag like below:

LA = numpy.diag(poles)

The output is [-1.+1.j] while I'm expecting to see a diagonal matrix. Can someone explain what is going on and what should be done to see a diagonal matrix? I also tried to change the shape to (1,4) but the result didn't change.

2
  • What are you expecting? np.diag will take the diagonal values of your array, which there is only one item since one of your axes is only length 1. Commented Oct 7, 2019 at 22:39
  • @busybear. I expected to get a diagonal array (4*4 matrix) where the diagonal elements are the elements of poles and all other elements to be zero. Commented Oct 7, 2019 at 22:41

1 Answer 1

1

The function you are looking for is np.fill_diagonal. This will set the diagonal values of an array. You'll have to create the array first:

arr = np.zeros((4, 4), dtype=np.complex64))
np.fill_diagonal(arr, poles)

arr is now:

array([[-1.+1.j,  0.+0.j,  0.+0.j,  0.+0.j],
       [ 0.+0.j, -1.-1.j,  0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j, -2.+3.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j, -2.-3.j]], dtype=complex64)

np.diagonal, on the other hand, retrieves the values in the diagonal. Output of np.diagonal(arr) is:

array([-1.+1.j, -1.-1.j, -2.+3.j, -2.-3.j], dtype=complex64)

In your example, you are retrieving the diagonal of poles, which only has one value in the diagonal since one of the axes is only length 1.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.