0

I have a data type numpy.complex128 for added to matrix 3D (3 Dimensional Array). I write script like below. when I run the script there was a warning like "ComplexWarning: Casting complex values to real discards the imaginary part", and the result of 3D matrix was just real value, not include imaginary value. How can I fix it if I want to add both real and imaginary in that matrix 3D?

for i in range (0, nper):
    zz = []
    x = np.zeros((nl,2,2))
    for j in range (0, nl):
        z = np.sqrt(phi*amu*res[j]/per[i])
        zz.append(complex(z,z))
        exp0 = np.exp((-2)*zz[j]/res[j]*thi[j])
        exp1 = complex(1,0)+exp0
        exp2 = complex(1,0)-exp0
    #matrix 3D
        x[j,0,0] = exp1
        x[j,0,1] = zz[j]*exp2
        x[j,1,0] = exp2/zz[j]
        x[j,1,1] = exp1
3
  • 1
    Your code is incomplete. There are all kinds of undefined variables. Nevertheless, I guess the problem is that you did not initialize the array as complex. Commented Nov 14, 2017 at 7:56
  • actually I have the complete code in my script, and the problem is just when I run the matrix 3D. if there is oke, may you give me an example how to initialize the array as complex? Commented Nov 14, 2017 at 8:00
  • Possible duplicate of Assigning complex values to numpy arrays? Commented Nov 14, 2017 at 8:02

1 Answer 1

0

You probably missed to tell numpy that your x array is complex:

x = np.zeros((nl, 2, 2), dtype=np.complex128)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.