def unify(constraints, broadcasting=None): """ Unify a set of constraints and return a concrete solution >>> import blaze >>> d1 = blaze.dshape('10, int32') >>> d2 = blaze.dshape('T, float32') >>> [result], constraints = unify([(d1, d2)], [True]) >>> result dshape("10, float32") >>> constraints [] """ S = IdentityDict() constraints = [(simplify(ds1, S), simplify(ds2, S)) for ds1, ds2 in constraints] # Compute a solution to a set of constraints constraints, b_env = normalize(constraints, broadcasting) logger.debug("Normalized constraints: %s", constraints) solution, remaining = unify_constraints(constraints, S) logger.debug("Initial solution: %s", solution) seed_typesets(remaining, solution) merge_typevar_sets(remaining, solution) # Compute a type substitution with concrete types from the solution # TODO: incorporate broadcasting environment during reification substitution = reify(solution) logger.debug("Substitution: %s", substitution) # Reify and promote the datashapes result = [substitute(substitution, ds2) for ds1, ds2 in constraints] return result, remaining
def test_normalize_ellipses_2_ellipses2(self): ds1 = dshape('A, ..., B, int32') ds2 = dshape('M, N, ..., S, T, float32') res1, res2 = normalize(ds1, ds2) self.assertEqual(str(res1), 'A, N, ..., S, B, int32') self.assertEqual(str(res2), 'M, N, ..., S, T, float32')
def test_normalize_ellipses_2_ellipses(self): ds1 = dshape('..., A, int32') ds2 = dshape('X, ..., Y, Z, float32') res1, res2 = normalize(ds1, ds2) self.assertEqual(str(res1), 'X, ..., Y, A, int32') self.assertEqual(str(res2), 'X, ..., Y, Z, float32')
def test_normalize_ellipses5(self): ds1 = dshape('..., A, B, int32') ds2 = dshape('..., X, Y, float32') res1, res2 = normalize(ds1, ds2) self.assertEqual(str(res1), '..., A, B, int32') self.assertEqual(str(res2), '..., X, Y, float32')
def test_normalize_ellipses1(self): ds1 = dshape('..., T') ds2 = dshape('X, Y, T') res1, res2 = normalize(ds1, ds2) self.assertEqual(str(res1), 'X, Y, T') self.assertEqual(str(res2), 'X, Y, T')
def test_normalize_ellipsis_broadcasting(self): ds1 = dshape('T, int32 -> T, T, int32') ds2 = dshape('A..., int32 -> A..., float32') [(x, y)], _ = normalize([(ds1, ds2)]) self.assertEqual(str(y), 'T, int32 -> T, T, float32')