cmath.py 4.2 KB

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