Python Complex Number Operations

75 Views Asked by At

I have an equation

(0.125-(1j*(mu1)*0.125))/(0.125 - cmath.sqrt((0.125**2)-(0.125**2)-((mu1**2)*(0.125**2))))

This is suppose to be 1 for any value of mu1 but it seems I am getting 1 only if mu1 is real... what am I missing?

For mu1 = 1, the above expression calculates 1 (see 3rd line)
(0.125-(1j*(mu1)*0.125))/(0.125 - cmath.sqrt((0.125**2)-(0.125**2)-((mu1**2)*(0.125**2))))
(1+0j)
For mu1 = 2, the above expression calculates 1 (see 3rd line)
(0.125-(1j*(mu1)*0.125))/(0.125 - cmath.sqrt((0.125**2)-(0.125**2)-((mu1**2)*(0.125**2))))
(1-0j)  ANSWER I AM LOOKING FOR
For mu1 = 2+2j, the above expression calculates
(0.125-(1j*(mu1)*0.125))/(0.125 - cmath.sqrt((0.125**2)-(0.125**2)-((mu1**2)*(0.125**2))))
(-1.4-0.8j) I WAS EXPECTING 1 because NUMERATOR and DENOMINATOR are same

For the last one, the bottom half evaluates to

1                     1
-- - sqrt(j^2 (2+2j)^2 ---^2 )
8                     8

1              1
-- - j (2+2j) ---
8              8

1 -2j -2j^2
-----------
8

3 - 2j
------
8
3

There are 3 best solutions below

11
Barmar On

After cancelling out the common terms, the result will only be 1 when

1j*mu1*0.125 == cmath.sqrt(-(mu1**2 * 0.125**2))

I haven't analyzed it carefully, but it seems to be true only the real part of mu1 is positive or zero and the imaginary part is negative or zero. I.e. it's true in the lower-right quadrant of the complex plane.

So I think you may have been misinformed about the equation, or you copied it wrong.

5
mjsqu On

In your case of 2+2j the top half of the fraction resolves to:

 1       j(2 + 2j)
---  -  -----------
 8           8

 1    -   (2j + (2j)^2)
---------------------
          8

 1    -   (2j + 2*-1)
---------------------
          8

 1    -   2j + 2
---------------------
          8

     3    -   2j 
---------------------
          8

Which only happens with the complex input due to j^2 = -1

The bottom half is:

(-0.125+0.25j)

# or
 
 -1 + 2j
--------
   8

# Because:

 1        sqrt(0 -      (2+2j)^2      )
---   -       -------------------
 8                 8^2


 1         sqrt(   -       8j
---  -                  -------     )
 8                         8^2

 1       2      2j
---  - (--- - -----)
 8       8      8

 1       2      2j
---  -  --- +  -----
 8       8      8

 -1 + 2j
--------
   8

It's worth noting this difference:

>>> cmath.sqrt(8j/64)
(0.25+0.25j)
>>> cmath.sqrt(-8j/64)
(0.25-0.25j)
0
user3873617 On

The answer lies in using appropriate branch cuts of sqrt function.