2015-03-02 12:47:44 +00:00
|
|
|
# test basic complex number functionality
|
|
|
|
|
|
|
|
# constructor
|
|
|
|
print(complex(1))
|
|
|
|
print(complex(1.2))
|
|
|
|
print(complex(1.2j))
|
2022-06-21 13:45:27 +00:00
|
|
|
print(complex("j"))
|
|
|
|
print(complex("J"))
|
2015-03-02 12:47:44 +00:00
|
|
|
print(complex("1"))
|
|
|
|
print(complex("1.2"))
|
|
|
|
print(complex("1.2j"))
|
2022-06-21 13:45:27 +00:00
|
|
|
print(complex("1+j"))
|
2021-08-06 16:16:59 +00:00
|
|
|
print(complex("1+2j"))
|
|
|
|
print(complex("-1-2j"))
|
|
|
|
print(complex("+1-2j"))
|
|
|
|
print(complex(" -1-2j "))
|
|
|
|
print(complex(" +1-2j "))
|
2022-06-21 13:45:27 +00:00
|
|
|
print(complex("nanj"))
|
|
|
|
print(complex("nan-infj"))
|
2015-03-02 12:47:44 +00:00
|
|
|
print(complex(1, 2))
|
|
|
|
print(complex(1j, 2j))
|
|
|
|
|
|
|
|
# unary ops
|
|
|
|
print(bool(1j))
|
|
|
|
print(+(1j))
|
2015-03-14 22:56:02 +00:00
|
|
|
print(-(1 + 2j))
|
2015-03-02 12:47:44 +00:00
|
|
|
|
|
|
|
# binary ops
|
2015-03-25 23:10:09 +00:00
|
|
|
print(1j + False)
|
|
|
|
print(1j + True)
|
2015-03-02 12:47:44 +00:00
|
|
|
print(1j + 2)
|
|
|
|
print(1j + 2j)
|
|
|
|
print(1j - 2)
|
|
|
|
print(1j - 2j)
|
|
|
|
print(1j * 2)
|
|
|
|
print(1j * 2j)
|
|
|
|
print(1j / 2)
|
2015-04-04 22:16:22 +00:00
|
|
|
print((1j / 2j).real)
|
2015-03-14 22:56:02 +00:00
|
|
|
print(1j / (1 + 2j))
|
2022-02-02 05:11:25 +00:00
|
|
|
ans = 0j**0
|
2020-03-23 02:26:08 +00:00
|
|
|
print("%.5g %.5g" % (ans.real, ans.imag))
|
2022-02-02 05:11:25 +00:00
|
|
|
ans = 0j**1
|
2020-03-23 02:26:08 +00:00
|
|
|
print("%.5g %.5g" % (ans.real, ans.imag))
|
2022-02-02 05:11:25 +00:00
|
|
|
ans = 0j**0j
|
2020-03-23 02:26:08 +00:00
|
|
|
print("%.5g %.5g" % (ans.real, ans.imag))
|
2022-02-02 05:11:25 +00:00
|
|
|
ans = 1j**2.5
|
2020-03-23 02:26:08 +00:00
|
|
|
print("%.5g %.5g" % (ans.real, ans.imag))
|
2022-02-02 05:11:25 +00:00
|
|
|
ans = 1j**2.5j
|
2020-03-23 02:26:08 +00:00
|
|
|
print("%.5g %.5g" % (ans.real, ans.imag))
|
2015-03-02 17:21:10 +00:00
|
|
|
|
2015-04-04 22:16:22 +00:00
|
|
|
# comparison
|
|
|
|
print(1j == 1)
|
|
|
|
print(1j == 1j)
|
2020-02-10 11:22:12 +00:00
|
|
|
print(0 + 0j == False, 1 + 0j == True)
|
|
|
|
print(False == 0 + 0j, True == 1 + 0j)
|
2015-04-04 22:16:22 +00:00
|
|
|
|
2017-09-04 04:16:27 +00:00
|
|
|
# comparison of nan is special
|
2020-03-23 02:26:08 +00:00
|
|
|
nan = float("nan") * 1j
|
2017-09-04 04:16:27 +00:00
|
|
|
print(nan == 1j)
|
|
|
|
print(nan == nan)
|
|
|
|
|
2015-03-02 17:21:10 +00:00
|
|
|
# builtin abs
|
|
|
|
print(abs(1j))
|
2015-03-14 22:56:02 +00:00
|
|
|
print("%.5g" % abs(1j + 2))
|
2015-03-25 23:10:09 +00:00
|
|
|
|
2017-04-04 02:14:34 +00:00
|
|
|
# builtin hash
|
|
|
|
print(hash(1 + 0j))
|
|
|
|
print(type(hash(1j)))
|
|
|
|
|
2015-04-04 21:05:30 +00:00
|
|
|
# float on lhs should delegate to complex
|
|
|
|
print(1.2 + 3j)
|
|
|
|
|
2017-09-26 02:57:51 +00:00
|
|
|
# negative base and fractional power should create a complex
|
2020-03-23 02:26:08 +00:00
|
|
|
ans = (-1) ** 2.3
|
|
|
|
print("%.5g %.5g" % (ans.real, ans.imag))
|
|
|
|
ans = (-1.2) ** -3.4
|
|
|
|
print("%.5g %.5g" % (ans.real, ans.imag))
|
2017-09-26 02:57:51 +00:00
|
|
|
|
2015-05-13 19:35:58 +00:00
|
|
|
# check printing of inf/nan
|
2020-03-23 02:26:08 +00:00
|
|
|
print(float("nan") * 1j)
|
|
|
|
print(float("-nan") * 1j)
|
|
|
|
print(float("inf") * (1 + 1j))
|
|
|
|
print(float("-inf") * (1 + 1j))
|
2015-05-13 19:35:58 +00:00
|
|
|
|
2021-08-06 16:16:59 +00:00
|
|
|
# malformed complex strings
|
|
|
|
for test in ("1+2", "1j+2", "1+2j+3", "1+2+3j", "1 + 2j"):
|
|
|
|
try:
|
|
|
|
complex(test)
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError", test)
|
|
|
|
|
2017-01-19 12:37:21 +00:00
|
|
|
# can't assign to attributes
|
|
|
|
try:
|
|
|
|
(1j).imag = 0
|
|
|
|
except AttributeError:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("AttributeError")
|
2017-01-19 12:37:21 +00:00
|
|
|
|
2015-03-25 23:10:09 +00:00
|
|
|
# can't convert rhs to complex
|
|
|
|
try:
|
|
|
|
1j + []
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
2015-04-04 22:16:22 +00:00
|
|
|
|
|
|
|
# unsupported unary op
|
|
|
|
try:
|
|
|
|
~(1j)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
# unsupported binary op
|
|
|
|
try:
|
|
|
|
1j // 2
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
# unsupported binary op
|
|
|
|
try:
|
|
|
|
1j < 2j
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
# small int on LHS, complex on RHS, unsupported op
|
2016-12-21 03:47:02 +00:00
|
|
|
try:
|
|
|
|
print(1 | 1j)
|
|
|
|
except TypeError:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("TypeError")
|
2016-12-21 03:47:02 +00:00
|
|
|
|
2015-04-04 22:16:22 +00:00
|
|
|
# zero division
|
|
|
|
try:
|
|
|
|
1j / 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
|
|
|
|
|
|
|
# zero division via power
|
2017-02-02 13:04:13 +00:00
|
|
|
try:
|
2022-02-02 05:11:25 +00:00
|
|
|
0j**-1
|
2017-02-02 13:04:13 +00:00
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
2015-04-04 22:16:22 +00:00
|
|
|
try:
|
2022-02-02 05:11:25 +00:00
|
|
|
0j**1j
|
2015-04-04 22:16:22 +00:00
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|