Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 61

Need an argument function. Sage has a built-in arg for complex types, but let's roll our own here to show it can be done:

def arg(x, y): if x > 0: # arctan works in Quad 1, 4 return atan(y/x) elif x <0: # quadrants 2, 3 at = atan(y/x) if at > 0: return at-pi else: return pi + at else: if y>0: return pi/2 elif y<0: return -pi/2 else: # at (0, 0), return 0, just for aesthetics return 0

A little testing:

n(arg(real(exp(2.5*I)), imag(exp(2.5*I))))
2.50000000000000
(3-2j).arg()
-0.588002603547568
n(arg(3, -2))
-0.588002603547568

Now let's try to find the square root of a complex number:

def csr(x, y): rad = sqrt(sqrt(x^2 + y^2)) ang = arg(x, y)/2 return (rad*cos(ang), rad*sin(ang))

Testing:

csr(2, 3)
(13^(1/4)*cos(1/2*arctan(3/2)), 13^(1/4)*sin(1/2*arctan(3/2)))
n(_)
1.67414922803554 + 0.895977476129838*I
_^2
2.00000000000000 + 3.00000000000000*I

This is peculiar and unexpected: Apparently, when you call n on a 2-tuple, it returns a complex number. (Can't use on an ordered triple, etc.)

n((2, 3))
2.00000000000000 + 3.00000000000000*I
n(csr(-3, 2))^2
-3.00000000000000 + 2.00000000000000*I

Here's an inverse to z^2 + c:

def qinv(z, c=0): x = real(z-c) y = imag(z-c) return n(csr(x, y))
-qinv(1+1j, c=1)
-0.707106781186548 - 0.707106781186548*I
_^2 + 1
1.00000000000000 + 1.00000000000000*I
︠a1ca3a9c-2a6f-4b84-a7b5-79f1af54447d︠