# ticket: 305 from cpython.object cimport Py_EQ, Py_NE cimport cython DEF C21 = 2-1j 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 def test_object_conversion(o): """ >>> test_object_conversion(2) ((2+0j), (2+0j)) >>> test_object_conversion(2j - 0.5) ((-0.5+2j), (-0.5+2j)) """ cdef float complex a = o cdef double complex b = o return (a, b) def test_arithmetic(double complex z, double complex w): """ >>> test_arithmetic(2j, 4j) (2j, -2j, 6j, -2j, (-8+0j), (0.5+0j)) >>> test_arithmetic(6+12j, 3j) ((6+12j), (-6-12j), (6+15j), (6+9j), (-36+18j), (4-2j)) >>> test_arithmetic(5-10j, 3+4j) ((5-10j), (-5+10j), (8-6j), (2-14j), (55-10j), (-1-2j)) """ return +z, -z+0, z+w, z-w, z*w, z/w 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) def test_pow(double complex z, double complex w, tol=None): """ Various implementations produce slightly different results... >>> a = complex(3, 1) >>> test_pow(a, 1, 1e-15) True >>> 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 >>> test_pow(-0.5, 1j, tol=1e-15) True """ if tol is None: return z**w else: return abs(z**w /