cmath.py 4.4 KB

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