Complex powers.
[cython.git] / tests / run / complex_numbers_T305.pyx
1 cimport cython
2
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)
14
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+0, z+w, z-w, z*w, z/w
25
26 def test_pow(double complex z, double complex w, tol=None):
27     """
28     Various implementations produce slightly different results...
29     
30     >>> a = complex(3, 1)
31     >>> test_pow(a, 1)
32     (3+1j)
33     >>> test_pow(a, 2, 1e-15)
34     True
35     >>> test_pow(a, a, 1e-15)
36     True
37     >>> test_pow(complex(0.5, -.25), complex(3, 4), 1e-15)
38     True
39     """
40     if tol is None:
41         return z**w
42     else:
43         return abs(z**w / <object>z ** <object>w - 1) < tol
44
45 def test_int_pow(double complex z, int n, tol=None):
46     """
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]
53     """
54     if tol is None:
55         return z**n + <object>0 # add zero to normalize zero sign
56     else:
57         return abs(z**n / <object>z ** <object>n - 1) < tol
58
59 @cython.cdivision(False)
60 def test_div_by_zero(double complex z):
61     """
62     >>> test_div_by_zero(4j)
63     -0.25j
64     >>> test_div_by_zero(0)
65     Traceback (most recent call last):
66     ...
67     ZeroDivisionError: float division
68     """
69     return 1/z
70
71 def test_coercion(int a, float b, double c, float complex d, double complex e):
72     """
73     >>> test_coercion(1, 1.5, 2.5, 4+1j, 10j)
74     (1+0j)
75     (1.5+0j)
76     (2.5+0j)
77     (4+1j)
78     10j
79     (9+21j)
80     """
81     cdef double complex z
82     z = a; print z
83     z = b; print z
84     z = c; print z
85     z = d; print z
86     z = e; print z
87     return z + a + b + c + d + e
88
89 def test_compare(double complex a, double complex b):
90     """
91     >>> test_compare(3, 3)
92     (True, False)
93     >>> test_compare(3j, 3j)
94     (True, False)
95     >>> test_compare(3j, 4j)
96     (False, True)
97     >>> test_compare(3, 4)
98     (False, True)
99     """
100     return a == b, a != b
101
102 def test_compare_coerce(double complex a, int b):
103     """
104     >>> test_compare_coerce(3, 4)
105     (False, True)
106     >>> test_compare_coerce(4+1j, 4)
107     (False, True)
108     >>> test_compare_coerce(4, 4)
109     (True, False)
110     """
111     return a == b, a != b
112
113 def test_literal():
114     """
115     >>> test_literal()
116     (5j, (1-2.5j))
117     """
118     return 5j, 1-2.5j
119
120 def test_real_imag(double complex z):
121     """
122     >>> test_real_imag(1-3j)
123     (1.0, -3.0)
124     >>> test_real_imag(5)
125     (5.0, 0.0)
126     >>> test_real_imag(1.5j)
127     (0.0, 1.5)
128     """
129     return z.real, z.imag
130
131 def test_real_imag_assignment(object a, double b):
132     """
133     >>> test_real_imag_assignment(1, 2)
134     (1+2j)
135     >>> test_real_imag_assignment(1.5, -3.5)
136     (1.5-3.5j)
137     """
138     cdef double complex z
139     z.real = a
140     z.imag = b
141     return z
142
143 def test_conjugate(float complex z):
144     """
145     >>> test_conjugate(2+3j)
146     (2-3j)
147     """
148     return z.conjugate()
149
150 def test_conjugate_double(double complex z):
151     """
152     >>> test_conjugate_double(2+3j)
153     (2-3j)
154     """
155     return z.conjugate()
156
157 ctypedef double complex cdouble
158 def test_conjugate_typedef(cdouble z):
159     """
160     >>> test_conjugate_typedef(2+3j)
161     (2-3j)
162     """
163     return z.conjugate()
164
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"
170
171 ## def test_conjugate_nosizeassumptions(double_really_float x,
172 ##                                      float_really_double y,
173 ##                                      real_float z, real_double w):
174 ##     """
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']
179 ##     """
180 ##     cdef double complex I = 1j
181 ##     return ((x*I).conjugate(), (y*I).conjugate(), (z*I).conjugate(), (w*I).conjugate())
182
183 ctypedef double mydouble
184 def test_coerce_typedef_multiply(mydouble x, double complex z):
185     """
186     >>> test_coerce_typedef_multiply(3, 1+1j)
187     (3+3j)
188     """
189     return x * z
190
191 ctypedef int myint
192 def test_coerce_typedef_multiply_int(myint x, double complex z):
193     """
194     >>> test_coerce_typedef_multiply_int(3, 1+1j)
195     (3+3j)
196     """
197     return x * z
198
199 cpdef double complex complex_retval():
200     """
201     >>> complex_retval()
202     1j
203     """
204     return 1j