Exemplo n.º 1
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))
Exemplo n.º 2
0
 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))
Exemplo n.º 3
0
 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))
Exemplo n.º 4
0
    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')
Exemplo n.º 5
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])
Exemplo n.º 6
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])
Exemplo n.º 7
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])
Exemplo n.º 8
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])
Exemplo n.º 9
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))
Exemplo n.º 10
0
 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))
Exemplo n.º 11
0
 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))
Exemplo n.º 12
0
 def test_coercion_transitivity(self):
     a, b, c = dshapes('int8', 'complex128', 'float64')
     self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
Exemplo n.º 13
0
 def test_coerce_ctype(self):
     a, b, c = dshapes('float32', 'float32', 'float64')
     self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
Exemplo n.º 14
0
 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))
Exemplo n.º 15
0
 def test_downcast(self):
     a, b = dshapes('float32', 'int32')
     self.assertRaises(error.CoercionError, coercion_cost, a, b)
Exemplo n.º 16
0
 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))
Exemplo n.º 17
0
 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))
Exemplo n.º 18
0
 def test_coerce_numeric(self):
     a, b = dshapes('float32', 'float64')
     self.assertGreater(coercion_cost(a, b), 0)
Exemplo n.º 19
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')
Exemplo n.º 20
0
 def test_coercion_transitivity(self):
     a, b, c = dshapes('int8', 'complex128', 'float64')
     self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
Exemplo n.º 21
0
 def test_coerce_numeric(self):
     a, b = dshapes('float32', 'float64')
     self.assertGreater(coercion_cost(a, b), 0)
Exemplo n.º 22
0
 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))
Exemplo n.º 23
0
 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))
Exemplo n.º 24
0
 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))
Exemplo n.º 25
0
 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))
Exemplo n.º 26
0
 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))
Exemplo n.º 27
0
 def test_downcast(self):
     a, b = dshapes('float32', 'int32')
     self.assertRaises(error.CoercionError, coercion_cost, a, b)
Exemplo n.º 28
0
 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))