cmath.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import math
  2. class complex:
  3. def __init__(self, real, imag=0):
  4. self._real = float(real)
  5. self._imag = float(imag)
  6. @property
  7. def real(self):
  8. return self._real
  9. @property
  10. def imag(self):
  11. return self._imag
  12. def conjugate(self):
  13. return complex(self.real, -self.imag)
  14. def __repr__(self):
  15. s = ['(', str(self.real)]
  16. if self.imag >= 0:
  17. s.append('+')
  18. s.append(str(self.imag))
  19. s.append('j)')
  20. return ''.join(s)
  21. def __eq__(self, other):
  22. if type(other) is complex:
  23. return self.real == other.real and self.imag == other.imag
  24. if type(other) in (int, float):
  25. return self.real == other and self.imag == 0
  26. return NotImplemented
  27. def __add__(self, other):
  28. if type(other) is complex:
  29. return complex(self.real + other.real, self.imag + other.imag)
  30. if type(other) in (int, float):
  31. return complex(self.real + other, self.imag)
  32. return NotImplemented
  33. def __radd__(self, other):
  34. return self.__add__(other)
  35. def __sub__(self, other):
  36. if type(other) is complex:
  37. return complex(self.real - other.real, self.imag - other.imag)
  38. if type(other) in (int, float):
  39. return complex(self.real - other, self.imag)
  40. return NotImplemented
  41. def __rsub__(self, other):
  42. if type(other) is complex:
  43. return complex(other.real - self.real, other.imag - self.imag)
  44. if type(other) in (int, float):
  45. return complex(other - self.real, -self.imag)
  46. return NotImplemented
  47. def __mul__(self, other):
  48. if type(other) is complex:
  49. return complex(self.real * other.real - self.imag * other.imag,
  50. self.real * other.imag + self.imag * other.real)
  51. if type(other) in (int, float):
  52. return complex(self.real * other, self.imag * other)
  53. return NotImplemented
  54. def __rmul__(self, other):
  55. return self.__mul__(other)
  56. def __pow__(self, other: int | float):
  57. if type(other) in (int, float):
  58. return complex(self.__abs__() ** other * math.cos(other * phase(self)),
  59. self.__abs__() ** other * math.sin(other * phase(self)))
  60. return NotImplemented
  61. def __abs__(self) -> float:
  62. return math.sqrt(self.real ** 2 + self.imag ** 2)
  63. def __neg__(self):
  64. return complex(-self.real, -self.imag)
  65. def __hash__(self):
  66. return hash((self.real, self.imag))
  67. # Conversions to and from polar coordinates
  68. def phase(z: complex):
  69. return math.atan2(z.imag, z.real)
  70. def polar(z: complex):
  71. return z.__abs__(), phase(z)
  72. def rect(r: float, phi: float):
  73. return r * math.cos(phi) + r * math.sin(phi) * 1j
  74. # Power and logarithmic functions
  75. def exp(z: complex):
  76. return math.exp(z.real) * rect(1, z.imag)
  77. def log(z: complex, base=2.718281828459045):
  78. return math.log(z.__abs__(), base) + phase(z) * 1j
  79. def log10(z: complex):
  80. return log(z, 10)
  81. def sqrt(z: complex):
  82. return z ** 0.5
  83. # Trigonometric functions
  84. def acos(z: complex):
  85. return -1j * log(z + sqrt(z * z - 1))
  86. def asin(z: complex):
  87. return -1j * log(1j * z + sqrt(1 - z * z))
  88. def atan(z: complex):
  89. return 1j / 2 * log((1 - 1j * z) / (1 + 1j * z))
  90. def cos(z: complex):
  91. return (exp(z) + exp(-z)) / 2
  92. def sin(z: complex):
  93. return (exp(z) - exp(-z)) / (2 * 1j)
  94. def tan(z: complex):
  95. return sin(z) / cos(z)
  96. # Hyperbolic functions
  97. def acosh(z: complex):
  98. return log(z + sqrt(z * z - 1))
  99. def asinh(z: complex):
  100. return log(z + sqrt(z * z + 1))
  101. def atanh(z: complex):
  102. return 1 / 2 * log((1 + z) / (1 - z))
  103. def cosh(z: complex):
  104. return (exp(z) + exp(-z)) / 2
  105. def sinh(z: complex):
  106. return (exp(z) - exp(-z)) / 2
  107. def tanh(z: complex):
  108. return sinh(z) / cosh(z)
  109. # Classification functions
  110. def isfinite(z: complex):
  111. return math.isfinite(z.real) and math.isfinite(z.imag)
  112. def isinf(z: complex):
  113. return math.isinf(z.real) or math.isinf(z.imag)
  114. def isnan(z: complex):
  115. return math.isnan(z.real) or math.isnan(z.imag)
  116. def isclose(a: complex, b: complex):
  117. return math.isclose(a.real, b.real) and math.isclose(a.imag, b.imag)
  118. # Constants
  119. pi = math.pi
  120. e = math.e
  121. tau = 2 * pi
  122. inf = math.inf
  123. infj = complex(0, inf)
  124. nan = math.nan
  125. nanj = complex(0, nan)