complex_numbers_T305.pyx 6.69 KB
Newer Older
1 2
# ticket: 305

3 4
from cpython.object cimport Py_EQ, Py_NE

5 6
cimport cython

7 8
DEF C21 = 2-1j

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

cdef class Complex3j:
    """
    >>> Complex3j() == 3j
    True
    >>> Complex3j() == Complex3j()
    True
    >>> Complex3j() != 3j
    False
    >>> Complex3j() != 3
    True
    >>> Complex3j() != Complex3j()
    False
    """
    def __richcmp__(a, b, int op):
        if op == Py_EQ or op == Py_NE:
            if isinstance(a, Complex3j):
                eq = isinstance(b, Complex3j) or b == 3j
            else:
                eq = isinstance(b, Complex3j) and a == 3j
            return eq if op == Py_EQ else not eq
        return NotImplemented


33 34
def test_object_conversion(o):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
35
    >>> test_object_conversion(2)
36
    ((2+0j), (2+0j))
Robert Bradshaw's avatar
Robert Bradshaw committed
37
    >>> test_object_conversion(2j - 0.5)
38
    ((-0.5+2j), (-0.5+2j))
39 40 41
    """
    cdef float complex a = o
    cdef double complex b = o
42
    return (a, b)
43

44

45 46
def test_arithmetic(double complex z, double complex w):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
47
    >>> test_arithmetic(2j, 4j)
48
    (2j, -2j, 6j, -2j, (-8+0j), (0.5+0j))
Robert Bradshaw's avatar
Robert Bradshaw committed
49
    >>> test_arithmetic(6+12j, 3j)
50
    ((6+12j), (-6-12j), (6+15j), (6+9j), (-36+18j), (4-2j))
Robert Bradshaw's avatar
Robert Bradshaw committed
51
    >>> test_arithmetic(5-10j, 3+4j)
52
    ((5-10j), (-5+10j), (8-6j), (2-14j), (55-10j), (-1-2j))
53
    """
Lisandro Dalcin's avatar
Lisandro Dalcin committed
54
    return +z, -z+0, z+w, z-w, z*w, z/w
Robert Bradshaw's avatar
Robert Bradshaw committed
55

56

57 58 59 60 61 62 63 64 65 66
def test_div(double complex a, double complex b, expected):
    """
    >>> big = 2.0**1023
    >>> test_div(1 + 1j, 1 + big*1j, 1/big - 1j/big)
    >>> test_div(1 + 1j, 1/big + 1j/big, big)
    """
    # Can't count on good c99 complex division :(
    if '_c99_' not in __name__:
        assert a / b == expected, (a / b, expected)

67

Robert Bradshaw's avatar
Robert Bradshaw committed
68 69 70
def test_pow(double complex z, double complex w, tol=None):
    """
    Various implementations produce slightly different results...
71

Robert Bradshaw's avatar
Robert Bradshaw committed
72
    >>> a = complex(3, 1)
73 74
    >>> test_pow(a, 1, 1e-15)
    True
Robert Bradshaw's avatar
Robert Bradshaw committed
75 76 77 78 79 80
    >>> test_pow(a, 2, 1e-15)
    True
    >>> test_pow(a, a, 1e-15)
    True
    >>> test_pow(complex(0.5, -.25), complex(3, 4), 1e-15)
    True
81 82
    >>> test_pow(-0.5, 1j, tol=1e-15)
    True
Robert Bradshaw's avatar
Robert Bradshaw committed
83 84 85 86 87 88
    """
    if tol is None:
        return z**w
    else:
        return abs(z**w / <object>z ** <object>w - 1) < tol

89

Robert Bradshaw's avatar
Robert Bradshaw committed
90 91 92 93 94 95
def test_int_pow(double complex z, int n, tol=None):
    """
    >>> [test_int_pow(complex(0, 1), k, 1e-15) for k in range(-4, 5)]
    [True, True, True, True, True, True, True, True, True]
    >>> [test_int_pow(complex(0, 2), k, 1e-15) for k in range(-4, 5)]
    [True, True, True, True, True, True, True, True, True]
96
    >>> [test_int_pow(complex(2, 0.5), k, 1e-14) for k in range(0, 10)]
Robert Bradshaw's avatar
Robert Bradshaw committed
97
    [True, True, True, True, True, True, True, True, True, True]
98 99
    >>> test_int_pow(-0.5, 5, tol=1e-15)
    True
Robert Bradshaw's avatar
Robert Bradshaw committed
100 101 102 103 104 105
    """
    if tol is None:
        return z**n + <object>0 # add zero to normalize zero sign
    else:
        return abs(z**n / <object>z ** <object>n - 1) < tol

106

107 108 109 110 111 112 113 114 115 116 117
@cython.cdivision(False)
def test_div_by_zero(double complex z):
    """
    >>> test_div_by_zero(4j)
    -0.25j
    >>> test_div_by_zero(0)
    Traceback (most recent call last):
    ...
    ZeroDivisionError: float division
    """
    return 1/z
Robert Bradshaw's avatar
Robert Bradshaw committed
118

119

120 121
def test_coercion(int a, float b, double c, float complex d, double complex e):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
122 123 124 125 126 127 128
    >>> test_coercion(1, 1.5, 2.5, 4+1j, 10j)
    (1+0j)
    (1.5+0j)
    (2.5+0j)
    (4+1j)
    10j
    (9+21j)
129 130 131 132 133 134 135 136 137
    """
    cdef double complex z
    z = a; print z
    z = b; print z
    z = c; print z
    z = d; print z
    z = e; print z
    return z + a + b + c + d + e

138

139 140
def test_compare(double complex a, double complex b):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
141
    >>> test_compare(3, 3)
142
    (True, False, False, False, False, True, False)
Robert Bradshaw's avatar
Robert Bradshaw committed
143
    >>> test_compare(3j, 3j)
144
    (True, False, True, True, True, False, False)
Robert Bradshaw's avatar
Robert Bradshaw committed
145
    >>> test_compare(3j, 4j)
146
    (False, True, True, False, True, True, False)
Robert Bradshaw's avatar
Robert Bradshaw committed
147
    >>> test_compare(3, 4)
148 149 150
    (False, True, False, False, False, True, False)
    >>> test_compare(2-1j, 4)
    (False, True, False, False, False, True, True)
151
    """
152
    return a == b, a != b, a == 3j, 3j == b, a == Complex3j(), Complex3j() != b, a == C21
153

154 155 156

def test_compare_coerce(double complex a, int b):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
157
    >>> test_compare_coerce(3, 4)
158
    (False, True, False, False, False, True)
Robert Bradshaw's avatar
Robert Bradshaw committed
159
    >>> test_compare_coerce(4+1j, 4)
160
    (False, True, False, True, False, True)
Robert Bradshaw's avatar
Robert Bradshaw committed
161
    >>> test_compare_coerce(4, 4)
162 163 164
    (True, False, False, False, False, True)
    >>> test_compare_coerce(3j, 4)
    (False, True, True, False, True, False)
165
    """
166 167
    return a == b, a != b, a == 3j, 4+1j == a, a == Complex3j(), Complex3j() != a

168 169 170

def test_literal():
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
171
    >>> test_literal()
172
    (5j, (1-2.5j), (2-1j))
173
    """
174 175
    return 5j, 1-2.5j, C21

176 177 178

def test_real_imag(double complex z):
    """
179 180 181 182 183 184
    >>> test_real_imag(1-3j)
    (1.0, -3.0)
    >>> test_real_imag(5)
    (5.0, 0.0)
    >>> test_real_imag(1.5j)
    (0.0, 1.5)
185 186 187 188 189
    """
    return z.real, z.imag

def test_real_imag_assignment(object a, double b):
    """
190 191 192 193
    >>> test_real_imag_assignment(1, 2)
    (1+2j)
    >>> test_real_imag_assignment(1.5, -3.5)
    (1.5-3.5j)
194
    """
195 196 197 198
    cdef double complex z
    z.real = a
    z.imag = b
    return z
Robert Bradshaw's avatar
Robert Bradshaw committed
199

200
def test_conjugate(float complex z):
201 202 203 204
    """
    >>> test_conjugate(2+3j)
    (2-3j)
    """
205 206 207
    return z.conjugate()

def test_conjugate_double(double complex z):
208 209 210 211
    """
    >>> test_conjugate_double(2+3j)
    (2-3j)
    """
212
    return z.conjugate()
213 214 215

ctypedef double complex cdouble
def test_conjugate_typedef(cdouble z):
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
216 217 218 219
    """
    >>> test_conjugate_typedef(2+3j)
    (2-3j)
    """
220 221
    return z.conjugate()

222 223 224 225 226
cdef cdouble test_conjugate_nogil(cdouble z) nogil:
    # Really just a compile test.
    return z.conjugate()
test_conjugate_nogil(0) # use it

Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
## cdef extern from "complex_numbers_T305.h":
##     ctypedef double double_really_float "myfloat"
##     ctypedef float float_really_double "mydouble"
##     ctypedef float real_float "myfloat"
##     ctypedef double real_double "mydouble"

## def test_conjugate_nosizeassumptions(double_really_float x,
##                                      float_really_double y,
##                                      real_float z, real_double w):
##     """
##     >>> test_conjugate_nosizeassumptions(1, 1, 1, 1)
##     (-1j, -1j, -1j, -1j)
##     >>> ["%.2f" % x.imag for x in test_conjugate_nosizeassumptions(2e300, 2e300, 2e300, 2e300)]
##     ['-inf', '-2e+300', '-inf', '-2e+300']
##     """
##     cdef double complex I = 1j
##     return ((x*I).conjugate(), (y*I).conjugate(), (z*I).conjugate(), (w*I).conjugate())

ctypedef double mydouble
def test_coerce_typedef_multiply(mydouble x, double complex z):
    """
    >>> test_coerce_typedef_multiply(3, 1+1j)
    (3+3j)
    """
    return x * z

ctypedef int myint
def test_coerce_typedef_multiply_int(myint x, double complex z):
    """
    >>> test_coerce_typedef_multiply_int(3, 1+1j)
    (3+3j)
    """
    return x * z
260 261

cpdef double complex complex_retval():
262 263 264 265
    """
    >>> complex_retval()
    1j
    """
266
    return 1j