avsill
avsill

Reputation: 1

Operating with complex numbers in python

I'm working with this equation:

import numpy as np
ld = 1.3
aut = -np.sqrt(-4*ld*(ld - 1)*np.log(ld) + (ld + np.log(ld) - 1)**2)/(2*(ld - 1)) + (ld + np.log(ld) - 1)/(2*(ld- 1))
aut

But when I try to get the result there is an error:

invalid value encountered in sqrt

I know that probably the solution is a complex number. Does anyone know how can I get a result from this equation??

Thank you!

Upvotes: 0

Views: 119

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150825

The issue is np.sqrt encounters a negative number. Since you are looking for complex solution, one (obvious) solution is to wrap the number in np.complex:

(-np.sqrt(np.complex(-4*ld*(ld - 1)*np.log(ld) 
         + (ld + np.log(ld) - 1)**2 + 0))/(2*(ld - 1)) 
+ (ld + np.log(ld) - 1)/(2*(ld- 1))
)

Output:

(0.937273774112485-0.5083597988171367j)

Upvotes: 1

Related Questions