complex_numbers_T305.pyx 5.16 KB
Newer Older
Robert Bradshaw's avatar
Robert Bradshaw committed
1 2
# ticket: 305

3 4 5 6
cimport cython

def test_object_conversion(o):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
7
    >>> test_object_conversion(2)
8
    ((2+0j), (2+0j))
Robert Bradshaw's avatar
Robert Bradshaw committed
9
    >>> test_object_conversion(2j - 0.5)
10
    ((-0.5+2j), (-0.5+2j))
11 12 13
    """
    cdef float complex a = o
    cdef double complex b = o
14
    return (a, b)
15 16 17

def test_arithmetic(double complex z, double complex w):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
18
    >>> test_arithmetic(2j, 4j)
19
    (2j, -2j, 6j, -2j, (-8+0j), (0.5+0j))
Robert Bradshaw's avatar
Robert Bradshaw committed
20
    >>> test_arithmetic(6+12j, 3j)
21
    ((6+12j), (-6-12j), (6+15j), (6+9j), (-36+18j), (4-2j))
Robert Bradshaw's avatar
Robert Bradshaw committed
22
    >>> test_arithmetic(5-10j, 3+4j)
23
    ((5-10j), (-5+10j), (8-6j), (2-14j), (55-10j), (-1-2j))
24
    """
Lisandro Dalcin's avatar
Lisandro Dalcin committed
25
    return +z, -z+0, z+w, z-w, z*w, z/w
Robert Bradshaw's avatar
Robert Bradshaw committed
26

Robert Bradshaw's avatar
Robert Bradshaw committed
27 28 29
def test_pow(double complex z, double complex w, tol=None):
    """
    Various implementations produce slightly different results...
30

Robert Bradshaw's avatar
Robert Bradshaw committed
31
    >>> a = complex(3, 1)
32 33
    >>> test_pow(a, 1, 1e-15)
    True
Robert Bradshaw's avatar
Robert Bradshaw committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    >>> 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
    """
    if tol is None:
        return z**w
    else:
        return abs(z**w / <object>z ** <object>w - 1) < tol

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]
52
    >>> [test_int_pow(complex(2, 0.5), k, 1e-14) for k in range(0, 10)]
Robert Bradshaw's avatar
Robert Bradshaw committed
53 54 55 56 57 58 59
    [True, True, True, True, True, True, True, True, True, True]
    """
    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

60 61 62 63 64 65 66 67 68 69 70
@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
71

72 73
def test_coercion(int a, float b, double c, float complex d, double complex e):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
74 75 76 77 78 79 80
    >>> test_coercion(1, 1.5, 2.5, 4+1j, 10j)
    (1+0j)
    (1.5+0j)
    (2.5+0j)
    (4+1j)
    10j
    (9+21j)
81 82 83 84 85 86 87 88 89 90 91
    """
    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

def test_compare(double complex a, double complex b):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
92 93 94 95 96 97 98 99
    >>> test_compare(3, 3)
    (True, False)
    >>> test_compare(3j, 3j)
    (True, False)
    >>> test_compare(3j, 4j)
    (False, True)
    >>> test_compare(3, 4)
    (False, True)
100 101 102 103 104
    """
    return a == b, a != b

def test_compare_coerce(double complex a, int b):
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
105 106 107 108 109 110
    >>> test_compare_coerce(3, 4)
    (False, True)
    >>> test_compare_coerce(4+1j, 4)
    (False, True)
    >>> test_compare_coerce(4, 4)
    (True, False)
111 112 113 114 115
    """
    return a == b, a != b

def test_literal():
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
116 117
    >>> test_literal()
    (5j, (1-2.5j))
118 119 120 121 122
    """
    return 5j, 1-2.5j

def test_real_imag(double complex z):
    """
123 124 125 126 127 128
    >>> 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)
129 130 131 132 133
    """
    return z.real, z.imag

def test_real_imag_assignment(object a, double b):
    """
134 135 136 137
    >>> test_real_imag_assignment(1, 2)
    (1+2j)
    >>> test_real_imag_assignment(1.5, -3.5)
    (1.5-3.5j)
138
    """
139 140 141 142
    cdef double complex z
    z.real = a
    z.imag = b
    return z
Robert Bradshaw's avatar
Robert Bradshaw committed
143

144
def test_conjugate(float complex z):
145 146 147 148
    """
    >>> test_conjugate(2+3j)
    (2-3j)
    """
149 150 151
    return z.conjugate()

def test_conjugate_double(double complex z):
152 153 154 155
    """
    >>> test_conjugate_double(2+3j)
    (2-3j)
    """
156
    return z.conjugate()
157 158 159

ctypedef double complex cdouble
def test_conjugate_typedef(cdouble z):
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
160 161 162 163
    """
    >>> test_conjugate_typedef(2+3j)
    (2-3j)
    """
164 165
    return z.conjugate()

166 167 168 169 170
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
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
## 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
204 205

cpdef double complex complex_retval():
206 207 208 209
    """
    >>> complex_retval()
    1j
    """
210
    return 1j