-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp.py
52 lines (44 loc) · 1.41 KB
/
exp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import TypeVar
import math
Numeric = TypeVar('Numeric', int, float, complex)
def exp(z: Numeric) -> Numeric:
"""Complex exponential function using math.exp
AZEXP subroutine ported from FORTRAN 77.
SUBROUTINE AZEXP(AR, AI, BR, BI)
C***BEGIN PROLOGUE AZEXP
C***REFER TO ZBESH,ZBESI,ZBESJ,ZBESK,ZBESY,ZAIRY,ZBIRY
C
C DOUBLE PRECISION COMPLEX EXPONENTIAL FUNCTION B=EXP(A)
C
C***ROUTINES CALLED (NONE)
C***END PROLOGUE AZEXP
DOUBLE PRECISION AR, AI, BR, BI, ZM, CA, CB
ZM = DEXP(AR)
CA = ZM*DCOS(AI)
CB = ZM*DSIN(AI)
BR = CA
BI = CB
RETURN
END
"""
result = complex(math.exp(z.real)*math.cos(z.imag), math.exp(z.real)*math.sin(z.imag))
if not result.imag:
if int(result.real) == result.real:
return int(result.real)
else:
return result.real
return result
def arg(number: complex) -> float:
"""Calculate arg function of a complex number."""
return math.atan2(number.imag, number.real)
def ln(z: Numeric) -> Numeric:
"""Complex natural logarithm using math.log"""
result = complex(math.log(abs(z)), arg(z))
if not result.imag:
if int(result.real) == result.real:
return int(result.real)
else:
return result.real
return result
if __name__ == '__main__':
pass