cmath.py 4.0 KB

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