Complex square root

From testwiki
Revision as of 16:19, 7 February 2024 by imported>ThaniosAkro (Examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

Template:RoundBoxTop

File:0114TwoComplexNumbers01.png
Complex number W = complex number w².
Origin at point (0,0).
wreal,Wreal parallel to X axis. wimag,Wimag parallel to Y axis.
wreal=2.4; wimag=0.7; wmod=2.42+0.72=2.5.
Wreal=5.27; Wimag=3.36;  Wmod=5.272+3.362=6.25.
Wmod=wmod2=2.52=6.25.
Wϕ=2wϕ.
By cosine double angle formula:
cosWϕ=cos(2wϕ)=2cos2wϕ1=22.42.52.42.51=0.8432=5.276.25

Let complex numbers W=a+bi and w=p+qi.

Let W=w2.


When given a,b, aim of this page is to calculate p,q.


In the diagram complex number w=p+qi=wreal+iwimag=wmod(coswϕ+isinwϕ), where

  • wreal,wimag are the real and imaginary components of w, the rectangular components.
  • wmod,wϕ are the modulus and phase of w, the polar components.

Similarly, Wreal,Wimag,Wmod,Wϕ are the corresponding components of W.

Wreal,Wimag are given. Wreal,Wimag=a,b.


W=w2=(wmod(coswϕ+isinwϕ))2

=wmod2(cos(2wϕ)+isin(2wϕ))

=Wmod(cos(Wϕ)+isin(Wϕ))

=W


There are 3 significant calculations:

Wmod=a2+b22

wmod=Wmod2 and

coswϕ=cosWϕ2.

  • It is not necessary to calculate actual values of wϕ,Wϕ.
  • As with real math, there are 2 complex square roots, (p+qi), (pqi).

Template:RoundBoxBottom

Implementation

In the python programming language a complex square root is available for floating point numbers with a precision of 15.

# python code:
>>> (5.27 + 3.36*1j) ** 0.5
(2.4+0.7000000000000001j)
>>> 
>>> (-18j) ** .5
(3-2.9999999999999996j)
>>>

Function cmath.sqrt() provides clean output:

# python code:
>>> import cmath
>>> cmath.sqrt (5.27 + 3.36*1j)
(2.4+0.7j)
>>> cmath.sqrt (-18j)
(3-3j)
>>>

If it is desired to calculate complex square root with precision greater than that available for python's floating point math, the following code using python's decimal module will do the job. The following code also shows how complex square root is calculated.

# python code:
import decimal

dD = decimal.Decimal
dgt = decimal.getcontext()
Precision = dgt.prec = 100   # Adjust as necessary.

def ComplexSquareRoot (v1, v2 = None) :
    '''
p,q = ComplexSquareRoot (a,b) or
p,q = ComplexSquareRoot ((a,b))
a,b are the real and imaginary parts of complex number W = (a+bi)
p,q are the real and imaginary parts of complex number w = (p+qi)
where W = w ** 2
This function preserves +/- 0 as calculated by python function cmath.sqrt().
'''
    thisName = 'ComplexSquareRoot (v1, v2 = None) :'
    if v2 == None : a,b = v1
    else : a,b = v1,v2

    dgt.prec += 3
    a,b = [ dD(str(v)) for v in (a,b) ]

    if b == 0 :
        if a == 0 : p,q = dD(0),b
        else :
            root = abs(a).sqrt()
            if a > 0: p,q = root, b
            else : p,q = dD(0), root.copy_sign(b)
    elif a == 0 :
        root = abs(b/2).sqrt()
        if b > 0 : p,q = root, root
        else : p,q = root, -root
    else :
        Wmod = (a**2 + b**2).sqrt()
        wmod = Wmod.sqrt()
        cosWφ = a/Wmod
#             2
# cos2A = 2cos A - 1
        coswφ = ((cosWφ + 1) / 2).sqrt()
        p = wmod * coswφ
        q = b /(2*p)

    dgt.prec -= 3
    return [ s.normalize() for s in (p,q) ]

Examples

# python code:

import cmath
sqrt = cmath.sqrt

for (a,b) in (
        (0.,0.), (0.,-0.), (-0.,0.), (-0.,-0.),
        (4,0.),  (4,-0.),  (-4,0.),  (-4,-0.),
        (0.,50), (0.,-50), (-0.,50), (-0.,-50),
        (-394200411798404114010884279663511687236816, 157994206778295991363266285626991662856270),
):
    result1 = ComplexSquareRoot (a,b)
    result2 = sqrt (complex(a,b))
    str1 = 'result1, result2' ; print (str1, eval(str1))
result1, result2 ([Decimal('0'), Decimal('0')], 0j)
result1, result2 ([Decimal('0'), Decimal('-0')], -0j)
result1, result2 ([Decimal('0'), Decimal('0')], 0j)
result1, result2 ([Decimal('0'), Decimal('-0')], -0j)

result1, result2 ([Decimal('2'), Decimal('0')], (2+0j))
result1, result2 ([Decimal('2'), Decimal('-0')], (2-0j))
result1, result2 ([Decimal('0'), Decimal('2')], 2j)
result1, result2 ([Decimal('0'), Decimal('-2')], -2j)

result1, result2 ([Decimal('5'), Decimal('5')], (5+5j))
result1, result2 ([Decimal('5'), Decimal('-5')], (5-5j))
result1, result2 ([Decimal('5'), Decimal('5')], (5+5j))
result1, result2 ([Decimal('5'), Decimal('-5')], (5-5j))

# Function ComplexSquareRoot() preserves precision of large numbers:
result1, result2 ([Decimal('123456789012345678935'), Decimal('639876543210987654321')], (1.2345678901234568e+20+6.398765432109876e+20j))

Method #2. Algebraic

Introduction

Let W=a+bi and w=p+qi.

Let W=w2=(p+qi)2=p2q2+2pqi.

Then:

a=PQ  (1) where P,Q=p2,q2.

b=2pq  (2)

Square (2): B=4PQ  (3) where B=b2

(1)*4P: 4Pa=4PP4PQ  (4)

From (3),(4): 4Pa=4PPB  (5)

From (5): 4PP4aPB=0  (6)

From (6): P=4a±16a24(4)(B)2(4) =a±a2+b22 =a±Wmod2

Implementation

# python code:

def ComplexSquareRoot_al (v1, v2=None) :
    '''
ComplexSquareRoot algebraic
p, q = ComplexSquareRoot_al (a, b) 
'''
    if v2 == None : a,b = v1
    else : a,b = v1,v2

    if 0 in (a,b) : return  ComplexSquareRoot (a,b) 

    dgt.prec += 3
    a,b = [ dD(str(v)) for v in (a,b) ]

    Wmod = (a**2 + b**2).sqrt()
    P1 = (a+Wmod)/2
    # P1 must be > 0.
    p = P1.sqrt() ; q = b / (2*p)
    
    dgt.prec -= 3
    return [ s.normalize() for s in (p,q) ]

Template:RoundBoxTop Because function ComplexSquareRoot_al() contains only two calculations of .sqrt() and function ComplexSquareRoot() contains three, function ComplexSquareRoot_al() is significantly faster than function ComplexSquareRoot(). Template:RoundBoxBottom

Links to related topics

The Python Standard Library: cmath.sqrt