3 def test_object_conversion(o):
5 >>> test_object_conversion(2)
6 ((2+0j), (2+0j), (2+0j))
7 >>> test_object_conversion(2j - 0.5)
8 ((-0.5+2j), (-0.5+2j), (-0.5+2j))
10 cdef float complex a = o
11 cdef double complex b = o
12 cdef long double complex c = o
15 def test_arithmetic(double complex z, double complex w):
17 >>> test_arithmetic(2j, 4j)
18 (2j, -2j, 6j, -2j, (-8+0j), (0.5+0j))
19 >>> test_arithmetic(6+12j, 3j)
20 ((6+12j), (-6-12j), (6+15j), (6+9j), (-36+18j), (4-2j))
21 >>> test_arithmetic(5-10j, 3+4j)
22 ((5-10j), (-5+10j), (8-6j), (2-14j), (55-10j), (-1-2j))
24 return +z, -z+0, z+w, z-w, z*w, z/w
26 def test_pow(double complex z, double complex w, tol=None):
28 Various implementations produce slightly different results...
33 >>> test_pow(a, 2, 1e-15)
35 >>> test_pow(a, a, 1e-15)
37 >>> test_pow(complex(0.5, -.25), complex(3, 4), 1e-15)
43 return abs(z**w / <object>z ** <object>w - 1) < tol
45 def test_int_pow(double complex z, int n, tol=None):
47 >>> [test_int_pow(complex(0, 1), k, 1e-15) for k in range(-4, 5)]
48 [True, True, True, True, True, True, True, True, True]
49 >>> [test_int_pow(complex(0, 2), k, 1e-15) for k in range(-4, 5)]
50 [True, True, True, True, True, True, True, True, True]
51 >>> [test_int_pow(complex(2, 0.5), k, 1e-15) for k in range(0, 10)]
52 [True, True, True, True, True, True, True, True, True, True]
55 return z**n + <object>0 # add zero to normalize zero sign
57 return abs(z**n / <object>z ** <object>n - 1) < tol
59 @cython.cdivision(False)
60 def test_div_by_zero(double complex z):
62 >>> test_div_by_zero(4j)
64 >>> test_div_by_zero(0)
65 Traceback (most recent call last):
67 ZeroDivisionError: float division
71 def test_coercion(int a, float b, double c, float complex d, double complex e):
73 >>> test_coercion(1, 1.5, 2.5, 4+1j, 10j)
87 return z + a + b + c + d + e
89 def test_compare(double complex a, double complex b):
91 >>> test_compare(3, 3)
93 >>> test_compare(3j, 3j)
95 >>> test_compare(3j, 4j)
97 >>> test_compare(3, 4)
100 return a == b, a != b
102 def test_compare_coerce(double complex a, int b):
104 >>> test_compare_coerce(3, 4)
106 >>> test_compare_coerce(4+1j, 4)
108 >>> test_compare_coerce(4, 4)
111 return a == b, a != b
120 def test_real_imag(double complex z):
122 >>> test_real_imag(1-3j)
124 >>> test_real_imag(5)
126 >>> test_real_imag(1.5j)
129 return z.real, z.imag
131 def test_real_imag_assignment(object a, double b):
133 >>> test_real_imag_assignment(1, 2)
135 >>> test_real_imag_assignment(1.5, -3.5)
138 cdef double complex z
143 def test_conjugate(float complex z):
145 >>> test_conjugate(2+3j)
150 def test_conjugate_double(double complex z):
152 >>> test_conjugate_double(2+3j)
157 ctypedef double complex cdouble
158 def test_conjugate_typedef(cdouble z):
160 >>> test_conjugate_typedef(2+3j)
165 ## cdef extern from "complex_numbers_T305.h":
166 ## ctypedef double double_really_float "myfloat"
167 ## ctypedef float float_really_double "mydouble"
168 ## ctypedef float real_float "myfloat"
169 ## ctypedef double real_double "mydouble"
171 ## def test_conjugate_nosizeassumptions(double_really_float x,
172 ## float_really_double y,
173 ## real_float z, real_double w):
175 ## >>> test_conjugate_nosizeassumptions(1, 1, 1, 1)
176 ## (-1j, -1j, -1j, -1j)
177 ## >>> ["%.2f" % x.imag for x in test_conjugate_nosizeassumptions(2e300, 2e300, 2e300, 2e300)]
178 ## ['-inf', '-2e+300', '-inf', '-2e+300']
180 ## cdef double complex I = 1j
181 ## return ((x*I).conjugate(), (y*I).conjugate(), (z*I).conjugate(), (w*I).conjugate())
183 ctypedef double mydouble
184 def test_coerce_typedef_multiply(mydouble x, double complex z):
186 >>> test_coerce_typedef_multiply(3, 1+1j)
192 def test_coerce_typedef_multiply_int(myint x, double complex z):
194 >>> test_coerce_typedef_multiply_int(3, 1+1j)
199 cpdef double complex complex_retval():