1

I have problem to understand what happens in the next code:

A=np.array([[3.+4.j, 2.+8.j, 6.+5.j, 8.+3.j, 4.+8.j], [0.+3.j, 4.+5.j, 3.+9.j, 0.+6.j, 0.+8.j], [2.+7.j, 3.+5.j, 2.+3.j, 3.+9.j, 3.+6.j], [6.+0.j, 1.+2.j, 0.+1.j, 6.+5.j, 2.+7.j], [6.+1.j, 3.+4.j, 2.+2.j, 3.+8.j, 7.+6.j]]) 

I am trying to store the next coeficients

B=np.identity(5) for i in range(1,l): B[i][0]=-(A[i][0]/A[0][0]) 

when I print the matrix B I obtain

B=array([[ 1. , 0. , 0. , 0. , 0. ], [-0.48, 1. , 0. , 0. , 0. ], [-1.36, 0. , 1. , 0. , 0. ], [-0.72, 0. , 0. , 1. , 0. ], [-0.88, 0. , 0. , 0. , 1. ]]) 

but of course I know that for example

-A[1][0]/A[0][0]=-0.48-0.36j 

and I don't know why the loop just takes the real part.

Any help is welcome. Thank you very much for your advice.

3
  • 3
    You need to change the dtype of B to complex. Commented Sep 7, 2021 at 19:59
  • 2
    np.identity(5) creates a float array. Commented Sep 7, 2021 at 19:59
  • What are you trying to do? Can you please include your expected output? Commented Sep 7, 2021 at 20:03

1 Answer 1

2

Use:

B = np.identity(5, dtype=np.complex) 

To avoid:

ComplexWarning: Casting complex values to real discards the imaginary part B[i][0]=-(A[i][0]/A[0][0]) 

And get:

>>> B array([[ 1. +0.j , 0. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [-0.48-0.36j, 1. +0.j , 0. +0.j , 0. +0.j , 0. +0.j ], [-1.36-0.52j, 0. +0.j , 1. +0.j , 0. +0.j , 0. +0.j ], [-0.72+0.96j, 0. +0.j , 0. +0.j , 1. +0.j , 0. +0.j ], [-0.88+0.84j, 0. +0.j , 0. +0.j , 0. +0.j , 1. +0.j ]]) 
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.