Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test various binary operators

  • Loading branch information...
commit dd455de1cbdba8fed77f8e190ffd6fd171f49999 1 parent e413b45
@markflorisson authored
View
12 Cython/Compiler/ExprNodes.py
@@ -8115,6 +8115,13 @@ def analyse_memoryview_operation(self, env):
ndim = max(ndim1, ndim2)
dtype = self.result_type(dtype1, dtype2)
+ if dtype is None:
+ if dtype1.same_as(dtype2):
+ dtype = dtype1
+ else:
+ error(self.pos,
+ "Unsupported types in binary operation (%s, %s)" %
+ (dtype1, dtype2))
axes = [('direct', 'strided')] * ndim
self.type = PyrexTypes.MemoryViewSliceType(dtype, axes)
self.is_elemental = True
@@ -8399,7 +8406,7 @@ def analyse_operation(self, env):
else:
self.ctruedivision = self.truedivision
NumBinopNode.analyse_operation(self, env)
- if self.is_cpp_operation():
+ if self.is_cpp_operation() or self.is_elemental:
self.cdivision = True
if not self.type.is_pyobject:
self.zerodivision_check = (
@@ -9826,6 +9833,9 @@ def __init__(self, arg, dst_type, env):
CoercionNode.__init__(self, arg)
dst_type.create_declaration_utility_code(env)
+ def analyse_types(self, env):
+ self.arg.analyse_types(env)
+
def calculate_result_code(self):
if self.arg.type.is_complex:
real_part = "__Pyx_CREAL(%s)" % self.arg.result()
View
1  Cython/Compiler/Vector.py
@@ -191,6 +191,7 @@ def declare_type(self, type):
def may_error(self, node):
return (node.type.resolve().is_pyobject or
+ node.type.resolve().is_complex or
(node.type.is_memoryviewslice and node.type.dtype.is_pyobject))
class CythonCCodeWriter(Code.CCodeWriter):
2  Cython/minivect
@@ -1 +1 @@
-Subproject commit d706122424d7367f2702d9a9f297b3d61e5132c0
+Subproject commit 54e7b7925c1d18391f0a849ed568be284d53c2c7
View
141 tests/array_expressions/elementwise.pyx
@@ -170,3 +170,144 @@ def test_unop_simple(fused_dtype_t[:] m):
"""
m[:] = -m --m - 2
return np.asarray(m)
+
+#
+### Test binary operators
+#
+cdef fused dt:
+ double complex
+ object
+ double
+
+cdef fused dt2:
+ object
+ double
+ np.int32_t
+
+c_a = np.arange(4*5*6, dtype=np.complex128).reshape(4, 5, 6)
+c_b = np.arange(4*5*1, dtype=np.complex128).reshape(4, 5, 1)
+
+o_a = np.arange(4*5*6, dtype=np.object).reshape(4, 5, 6)
+o_b = np.arange(4*5*1, dtype=np.object).reshape(4, 5, 1)
+
+d_a = np.arange(4*5*6, dtype=np.double).reshape(4, 5, 6)
+d_b = np.arange(4*5*1, dtype=np.double).reshape(4, 5, 1)
+
+i_a = np.arange(4*5*6, dtype=np.int32).reshape(4, 5, 6)
+i_b = np.arange(4*5*1, dtype=np.int32).reshape(4, 5, 1)
+
+def equal(our_result, numpy_result):
+ our_result = np.asarray(our_result).astype(int)
+ numpy_result = numpy_result.astype(int)
+ if not np.all(our_result == numpy_result):
+ print our_result
+ print
+ print 'Expected'
+ print
+ print numpy_result
+
+@testcase
+def test_operator_plus(dt[:, :, :] a, dt[:, :, :] b, result):
+ """
+ >>> r = d_a + d_b
+ >>> test_operator_plus(c_a, c_b, r)
+ >>> test_operator_plus(c_a, c_b.copy(order='F'), r)
+ >>> test_operator_plus(o_a, o_b, r)
+ >>> test_operator_plus(o_a, o_b.copy(order='F'), r)
+ >>> test_operator_plus(d_a, d_b, r)
+ >>> test_operator_plus(d_a, d_b.copy(order='F'), r)
+ """
+ equal(a + b, result)
+
+@testcase
+def test_operator_mul(dt[:, :, :] a, dt[:, :, :] b, result):
+ """
+ >>> r = d_a * d_b
+ >>> test_operator_mul(c_a, c_b, r)
+ >>> test_operator_mul(c_a, c_b.copy(order='F'), r)
+ >>> test_operator_mul(o_a, o_b, r)
+ >>> test_operator_mul(o_a, o_b.copy(order='F'), r)
+ >>> test_operator_mul(d_a, d_b, r)
+ >>> test_operator_mul(d_a, d_b.copy(order='F'), r)
+ """
+ equal(a * b, result)
+
+@testcase
+def test_operator_sub(dt[:, :, :] a, dt[:, :, :] b, result):
+ """
+ >>> r = d_a - d_b
+ >>> test_operator_sub(c_a, c_b, r)
+ >>> test_operator_sub(c_a, c_b.copy(order='F'), r)
+ >>> test_operator_sub(o_a, o_b, r)
+ >>> test_operator_sub(o_a, o_b.copy(order='F'), r)
+ >>> test_operator_sub(d_a, d_b, r)
+ >>> test_operator_sub(d_a, d_b.copy(order='F'), r)
+ """
+ equal(a - b, result)
+
+@testcase
+def test_operator_div(dt[:, :, :] a, dt[:, :, :] b, result):
+ """
+ c_b = c_b + 1; o_b = o_b + 1; d_b = d_b + 1
+ >>> r = d_a / (d_b + 1)
+ >>> test_operator_div(c_a, c_b, r)
+ >>> test_operator_div(c_a, c_b.copy(order='F'), r)
+ >>> test_operator_div(o_a, o_b, r)
+ >>> test_operator_div(o_a, o_b.copy(order='F'), r)
+ >>> test_operator_div(d_a, d_b, r)
+ >>> test_operator_div(d_a, d_b.copy(order='F'), r)
+ """
+ equal(a / (b + 1), result)
+
+@testcase
+def test_operator_mod(dt2[:, :, :] a, dt2[:, :, :] b, result):
+ """
+ >>> r = d_a % (d_b + 5)
+ >>> test_operator_mod(i_a, i_b, r)
+ >>> test_operator_mod(i_a, i_b.copy(order='F'), r)
+ >>> test_operator_mod(o_a, o_b, r)
+ >>> test_operator_mod(o_a, o_b.copy(order='F'), r)
+ >>> test_operator_mod(d_a, d_b, r)
+ >>> test_operator_mod(d_a, d_b.copy(order='F'), r)
+ """
+ equal(a % (b + 5), result)
+
+#
+### Bitwise binary operators
+#
+cdef fused bt:
+ object
+ np.int32_t
+
+@testcase
+def test_operator_and(bt[:, :, :] a, bt[:, :, :] b, result):
+ """
+ >>> r = i_a & i_b
+ >>> test_operator_and(o_a, o_b, r)
+ >>> test_operator_and(o_a, o_b.copy(order='F'), r)
+ >>> test_operator_and(i_a, i_b, r)
+ >>> test_operator_and(i_a, i_b.copy(order='F'), r)
+ """
+ equal(a & b, result)
+
+@testcase
+def test_operator_or(bt[:, :, :] a, bt[:, :, :] b, result):
+ """
+ >>> r = i_a | i_b
+ >>> test_operator_or(o_a, o_b, r)
+ >>> test_operator_or(o_a, o_b.copy(order='F'), r)
+ >>> test_operator_or(i_a, i_b, r)
+ >>> test_operator_or(i_a, i_b.copy(order='F'), r)
+ """
+ equal(a | b, result)
+
+@testcase
+def test_operator_xor(bt[:, :, :] a, bt[:, :, :] b, result):
+ """
+ >>> r = i_a ^ i_b
+ >>> test_operator_xor(o_a, o_b, r)
+ >>> test_operator_xor(o_a, o_b.copy(order='F'), r)
+ >>> test_operator_xor(i_a, i_b, r)
+ >>> test_operator_xor(i_a, i_b.copy(order='F'), r)
+ """
+ equal(a ^ b, result)
Please sign in to comment.
Something went wrong with that request. Please try again.