def test_coerce_ctype(self): a, b, c = dshapes('float32', 'float32', 'float64') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) a, b, c = dshapes('uint64', 'uint64', 'int64') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) a, b, c = dshapes('int64', 'int64', 'uint64') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) a, b, c = dshapes('float64', 'float64', 'complex[float32]') self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_ctype_float_vs_complex(self): # int -> float32 is preferred over int -> complex[float32] a, b, c = dshapes('int32', 'float32', 'complex[float32]') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) # int -> float64 is preferred over int -> complex[float64] a, b, c = dshapes('int32', 'float64', 'complex[float64]') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) # int -> float64 is preferred over int -> complex[float32] a, b, c = dshapes('int32', 'float64', 'complex[float32]') self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
def test_unify_datashape_promotion2(self): # LHS s1 = 'A, B, int32' s2 = 'B, 10, float32' # RHS s3 = 'X, Y, int16' s4 = 'X, X, Z' # Create proper equation d1, d2, d3, d4 = dshapes(s1, s2, s3, s4) constraints = [(d1, d3), (d2, d4)] # What we know from the above equations is: # 1) A coerces to X # 2) B coerces to Y # 3) 10 coerces to X # # From this we determine that X must be Fixed(10). We must retain # type variable B for Y, since we can only say that B must unify with # Fixed(10), but not whether it is actually Fixed(10) (it may also be # Fixed(1)) [arg1, arg2], remaining_constraints = unify(constraints, [True, True]) self.assertEqual(str(arg1), '10, B, int16') self.assertEqual(str(arg2), '10, 10, float32')
def test_best_match_float_int_complex(self): ores = OverloadResolver('j') ores.extend_overloads(['(A... * float64, A... * float64) -> A... * float64', '(A... * complex[float32], A... * complex[float32]) -> A... * complex[float32]']) d1, d2 = dshapes('3 * float64', 'int32') idx, match = ores.resolve_overload(coretypes.Tuple([d1, d2])) self.assertEqual(idx, 0) self.assertEqual(match, dshape('(3 * float64, float64) -> 3 * float64')[0])
def test_best_match_int32_float32_ufunc_promotion(self): ores = OverloadResolver('m') ores.extend_overloads(['(A... * int32, A... * int32) -> A... * int32', '(A... * float32, A... * float32) -> A... * float32', '(A... * float64, A... * float64) -> A... * float64']) d1, d2 = dshapes('3 * int32', '3 * float32') idx, match = ores.resolve_overload(coretypes.Tuple([d1, d2])) self.assertEqual(idx, 2) self.assertEqual(match, dshape('(3 * float64, 3 * float64) -> 3 * float64')[0])
def test_best_match_float_int_complex(self): ores = OverloadResolver('j') ores.extend_overloads([ '(A... * float64, A... * float64) -> A... * float64', '(A... * complex[float32], A... * complex[float32]) -> A... * complex[float32]' ]) d1, d2 = dshapes('3 * float64', 'int32') idx, match = ores.resolve_overload(coretypes.Tuple([d1, d2])) self.assertEqual(idx, 0) self.assertEqual(match, dshape('(3 * float64, float64) -> 3 * float64')[0])
def test_best_match_int32_float32_ufunc_promotion(self): ores = OverloadResolver('m') ores.extend_overloads([ '(A... * int32, A... * int32) -> A... * int32', '(A... * float32, A... * float32) -> A... * float32', '(A... * float64, A... * float64) -> A... * float64' ]) d1, d2 = dshapes('3 * int32', '3 * float32') idx, match = ores.resolve_overload(coretypes.Tuple([d1, d2])) self.assertEqual(idx, 2) self.assertEqual( match, dshape('(3 * float64, 3 * float64) -> 3 * float64')[0])
def test_coerce_ctype(self): a, b, c = dshapes('float32', 'float32', 'float64') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) a, b, c = dshapes('uint64', 'uint64', 'int64') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) a, b, c = dshapes('int64', 'int64', 'uint64') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) a, b, c = dshapes('float64', 'float64', 'complex[float32]') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) a, b, c = dshapes('int16', 'float64', 'complex[float32]') self.assertLess(coercion_cost(a, b), coercion_cost(a, c)) a, b, c = dshapes('int8', 'float64', 'complex[float32]') self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_src_ellipsis(self): a, b, c = dshapes('10, ..., float32', 'X, Y, float64', 'X, ..., float64') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_broadcasting3(self): a, b, c = dshapes('10, 10, float32', '10, 10, 10, float32', '1, 10, 10, float32') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coercion_transitivity(self): a, b, c = dshapes('int8', 'complex128', 'float64') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_ctype(self): a, b, c = dshapes('float32', 'float32', 'float64') self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_dst_ellipsis(self): a, b, c = dshapes('10 * 10 * float32', 'X * ... * float64', 'X * Y * float64') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_downcast(self): a, b = dshapes('float32', 'int32') self.assertRaises(error.CoercionError, coercion_cost, a, b)
def test_coerce_typevars(self): a, b, c = dshapes('10 * 11 * float32', 'X * Y * float64', '10 * Y * float64') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_broadcasting(self): a, b, c = dshapes('10 * 10 * float32', '10 * Y * Z * float64', 'X * Y * float64') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_numeric(self): a, b = dshapes('float32', 'float64') self.assertGreater(coercion_cost(a, b), 0)
def test_best_match_float_int_complex(self): d1, d2 = dshapes('3, float64', 'int32') match = best_match(j, [d1, d2]) self.assertEqual(str(match.sig), 'A..., float64 -> A..., float64 -> A..., float64') self.assertEqual(str(match.resolved_sig), '3, float64 -> float64 -> 3, float64')
def test_coerce_constrained_typevars(self): a, b, c = dshapes('10 * 10 * float32', 'X * Y * float64', 'X * X * float64') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_constrained_typevars(self): a, b, c = dshapes('10, 10, float32', 'X, Y, float64', 'X, X, float64') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_broadcasting3(self): a, b, c = dshapes('10 * 10 * float32', '10 * 10 * 10 * float32', '1 * 10 * 10 * float32') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_traits(self): a, b, c = dshapes('10, 10, float32', '10, X, A : floating', '10, X, float32') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_traits(self): a, b, c = dshapes('10 * 10 * float32', '10 * X * A : floating', '10 * X * float32') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
def test_coerce_src_ellipsis(self): a, b, c = dshapes('10 * ... * float32', 'X * Y * float64', 'X * ... * float64') self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))