Cython has moved to github.

cython-devel

view tests/run/complex_numbers_T305.pyx @ 2654:76e7121cbdc7

Fix #441
author Dag Sverre Seljebotn <dagss@student.matnat.uio.no>
date Tue Nov 03 16:35:19 2009 +0100 (2 years ago)
parents c6a27fd42d87
children 9d06f312c5b7
line source
1 cimport cython
3 def test_object_conversion(o):
4 """
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))
9 """
10 cdef float complex a = o
11 cdef double complex b = o
12 cdef long double complex c = o
13 return (a, b, c)
15 def test_arithmetic(double complex z, double complex w):
16 """
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))
23 """
24 return +z, -z, z+w, z-w, z*w, z/w
26 @cython.cdivision(False)
27 def test_div_by_zero(double complex z):
28 """
29 >>> test_div_by_zero(4j)
30 -0.25j
31 >>> test_div_by_zero(0)
32 Traceback (most recent call last):
33 ...
34 ZeroDivisionError: float division
35 """
36 return 1/z
38 def test_coercion(int a, float b, double c, float complex d, double complex e):
39 """
40 >>> test_coercion(1, 1.5, 2.5, 4+1j, 10j)
41 (1+0j)
42 (1.5+0j)
43 (2.5+0j)
44 (4+1j)
45 10j
46 (9+21j)
47 """
48 cdef double complex z
49 z = a; print z
50 z = b; print z
51 z = c; print z
52 z = d; print z
53 z = e; print z
54 return z + a + b + c + d + e
56 def test_compare(double complex a, double complex b):
57 """
58 >>> test_compare(3, 3)
59 (True, False)
60 >>> test_compare(3j, 3j)
61 (True, False)
62 >>> test_compare(3j, 4j)
63 (False, True)
64 >>> test_compare(3, 4)
65 (False, True)
66 """
67 return a == b, a != b
69 def test_compare_coerce(double complex a, int b):
70 """
71 >>> test_compare_coerce(3, 4)
72 (False, True)
73 >>> test_compare_coerce(4+1j, 4)
74 (False, True)
75 >>> test_compare_coerce(4, 4)
76 (True, False)
77 """
78 return a == b, a != b
80 def test_literal():
81 """
82 >>> test_literal()
83 (5j, (1-2.5j))
84 """
85 return 5j, 1-2.5j
87 def test_real_imag(double complex z):
88 """
89 >>> test_real_imag(1-3j)
90 (1.0, -3.0)
91 >>> test_real_imag(5)
92 (5.0, 0.0)
93 >>> test_real_imag(1.5j)
94 (0.0, 1.5)
95 """
96 return z.real, z.imag
98 def test_real_imag_assignment(object a, double b):
99 """
100 >>> test_real_imag_assignment(1, 2)
101 (1+2j)
102 >>> test_real_imag_assignment(1.5, -3.5)
103 (1.5-3.5j)
104 """
105 cdef double complex z
106 z.real = a
107 z.imag = b
108 return z
110 def test_conjugate(float complex z):
111 """
112 >>> test_conjugate(2+3j)
113 (2-3j)
114 """
115 return z.conjugate()
117 def test_conjugate_double(double complex z):
118 """
119 >>> test_conjugate_double(2+3j)
120 (2-3j)
121 """
122 return z.conjugate()
124 ctypedef double complex cdouble
125 def test_conjugate_typedef(cdouble z):
126 """
127 >>> test_conjugate_typedef(2+3j)
128 (2-3j)
129 """
130 return z.conjugate()
132 ## cdef extern from "complex_numbers_T305.h":
133 ## ctypedef double double_really_float "myfloat"
134 ## ctypedef float float_really_double "mydouble"
135 ## ctypedef float real_float "myfloat"
136 ## ctypedef double real_double "mydouble"
138 ## def test_conjugate_nosizeassumptions(double_really_float x,
139 ## float_really_double y,
140 ## real_float z, real_double w):
141 ## """
142 ## >>> test_conjugate_nosizeassumptions(1, 1, 1, 1)
143 ## (-1j, -1j, -1j, -1j)
144 ## >>> ["%.2f" % x.imag for x in test_conjugate_nosizeassumptions(2e300, 2e300, 2e300, 2e300)]
145 ## ['-inf', '-2e+300', '-inf', '-2e+300']
146 ## """
147 ## cdef double complex I = 1j
148 ## return ((x*I).conjugate(), (y*I).conjugate(), (z*I).conjugate(), (w*I).conjugate())
150 ctypedef double mydouble
151 def test_coerce_typedef_multiply(mydouble x, double complex z):
152 """
153 >>> test_coerce_typedef_multiply(3, 1+1j)
154 (3+3j)
155 """
156 return x * z
158 ctypedef int myint
159 def test_coerce_typedef_multiply_int(myint x, double complex z):
160 """
161 >>> test_coerce_typedef_multiply_int(3, 1+1j)
162 (3+3j)
163 """
164 return x * z
166 cpdef double complex complex_retval():
167 """
168 >>> complex_retval()
169 1j
170 """
171 return 1j