Example #1
0
 def test_allow_anything_to_bool(self):
     # The cost should be large
     min_cost = coercion_cost(dshape('int8'), dshape('complex[float64]'))
     for ds in ['int8', 'int16', 'int32', 'int64', 'uint8', 'uint16',
                'uint32', 'uint64', 'float32', 'float64',
                'complex[float32]', 'complex[float64]']:
         self.assertGreater(coercion_cost(dshape(ds), dshape('bool')),
                            min_cost)
Example #2
0
 def test_allow_anything_to_bool(self):
     # The cost should be large
     min_cost = coercion_cost(dshape('int8'), dshape('complex[float64]'))
     for ds in [
             'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32',
             'uint64', 'float32', 'float64', 'complex[float32]',
             'complex[float64]'
     ]:
         self.assertGreater(coercion_cost(dshape(ds), dshape('bool')),
                            min_cost)
Example #3
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))
Example #4
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))
Example #5
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))
Example #6
0
def match_by_weight(func, argtypes, constraints=None):
    """
    Return all matched overloads for function `func` given `argtypes`.

    Parameters
    ----------
    func: Dispatcher
        Overloaded Blaze function

    argtypes: [Mono]
        List of input argument types

    constraints: [(TypeVar, Mono)]
        Optional set of constraints, see unification.py

    Returns
    -------
    { weight : [Overload] }
    """
    from datashape import coercion_cost
    overloads = func.overloads

    # -------------------------------------------------
    # Find candidates

    candidates = find_matches(overloads, argtypes, constraints or [])

    # -------------------------------------------------
    # Weigh candidates

    matches = defaultdict(list)
    for match in candidates:
        in_signature = T.Function(*list(argtypes) + [T.TypeVar('R')])
        signature = match.sig
        try:
            weight = coercion_cost(in_signature, signature)
        except error.CoercionError:
            pass
        else:
            matches[weight].append(match)

    return matches
Example #7
0
 def test_coercion_transitivity(self):
     a, b, c = dshapes('int8', 'complex128', 'float64')
     self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
Example #8
0
 def test_coerce_numeric(self):
     a, b = dshapes('float32', 'float64')
     self.assertGreater(coercion_cost(a, b), 0)
Example #9
0
 def test_coerce_ctype(self):
     a, b, c = dshapes('float32', 'float32', 'float64')
     self.assertLess(coercion_cost(a, b), coercion_cost(a, c))
Example #10
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))
Example #11
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))
Example #12
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))
Example #13
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))
Example #14
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))
Example #15
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))
Example #16
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))
Example #17
0
 def test_coercion_transitivity(self):
     a, b, c = dshapes('int8', 'complex128', 'float64')
     self.assertGreater(coercion_cost(a, b), coercion_cost(a, c))
Example #18
0
 def test_coerce_numeric(self):
     a, b = dshapes('float32', 'float64')
     self.assertGreater(coercion_cost(a, b), 0)
Example #19
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))
Example #20
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))
Example #21
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))
Example #22
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))
Example #23
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))