コード例 #1
0
def replace_linsolve_right(equations):
    """Replaces linear systems with a LinSolve operator.

    This function can be used to generate Equations to generate naive and
    recommended code.
    """
    new_eqns = []
    for eqn in equations:
        eqn = matchpy.replace_all(eqn, [linsolveRnI, linsolveRTnI])
        eqn = matchpy.replace_all(eqn, [linsolveR, linsolveRT])
        new_eqns.append(eqn)
    return Equations(*new_eqns)
コード例 #2
0
ファイル: test_geom1d.py プロジェクト: traverseda/plycutter
    def equivalence_transform(self, name, idx, rules):
        expr = [e for e in self.exprs[name]]
        for rule in rules:
            expr[idx] = matchpy.replace_all(expr[idx], [rule], max_count=1)

        self.exprs[name] = expr
        self.check(name)
コード例 #3
0
 def replace_all(self, rules):
     equations = []
     for equation in self.equations:
         equation = matchpy.replace_all(equation, rules)
         equation = ar.to_normalform(equation)
         equations.append(equation)
     return Equations(*equations)
コード例 #4
0
ファイル: equations.py プロジェクト: as641651/PhD-Linnea
 def replace_all(self, rules):
     equations = []
     for equation in self.equations:
         equation = matchpy.replace_all(equation, rules)
         equation = ar.to_SOP(at.simplify(ar.to_SOP(equation)))
         equations.append(equation)
     return Equations(*equations)
コード例 #5
0
ファイル: equations.py プロジェクト: vishalbelsare/linnea
    def remove_identities(self):
        """Removes equations of the form tmpX = tmpY.

        There are three different cases:
        
        If both sides of the equation are the same temporary, the equation can
        simply be removed.

        If both sides are different temporaries, all occurrences of the
        temporary on the left-hand side in the other equations are replaced by
        the temporary on the right-hand side. This is necessary to ensure that
        code generation still works. This case can happen when setting
        equivalent expressions does not work perfectly.

        If the operand on the left-hand side is an intermediate, and the one on
        the right hand side is a temporary, all occurrences of the intermediate
        will be replace with the temporary.

        Replacing the temporary in the other equations only works if the
        temporary that is replaced is not part of any computation yet.

        Replacing temporaries even works if there are sequences of assignments
        such as:
        tmp2 = tmp3
        tmp1 = tmp2

        Returns:
            Equations: self without identities, and with replaced temporaries.
        """

        new_equations = []
        mapping = dict()
        for equation in self.equations:
            if temporaries.is_temporary(equation.rhs):
                if equation.lhs != equation.rhs:
                    try:
                        op = mapping[equation.rhs]
                    except KeyError:
                        mapping[equation.lhs] = equation.rhs
                    else:
                        mapping[equation.lhs] = op
                    if temporaries.is_temporary(equation.lhs):
                        continue
                else:
                    continue

            if mapping:
                rules = [
                    matchpy.ReplacementRule(matchpy.Pattern(rhs),
                                            Replacer(lhs))
                    for rhs, lhs in mapping.items()
                ]
                new_equations.append(
                    ae.Equal(equation.lhs,
                             matchpy.replace_all(equation.rhs, rules)))
            else:
                new_equations.append(equation)

        return Equations(*new_equations)
コード例 #6
0
ファイル: transformer.py プロジェクト: giguru/pyterrier
 def compile(self):
     '''
         Rewrites this pipeline by applying of the Matchpy rules in rewrite_rules.
     '''
     if not rewrites_setup:
         setup_rewrites()
     print("Applying %d rules" % len(rewrite_rules))
     return replace_all(self, rewrite_rules)
コード例 #7
0
ファイル: transformer.py プロジェクト: Xiao0728/pyterrier
 def compile(self):
     """
         Rewrites this pipeline by applying of the Matchpy rules in rewrite_rules. Pipeline
         optimisation is discussed in the `ICTIR 2020 paper on PyTerrier <https://arxiv.org/abs/2007.14271>`_.
     """
     if not rewrites_setup:
         setup_rewrites()
     print("Applying %d rules" % len(rewrite_rules))
     return replace_all(self, rewrite_rules)
コード例 #8
0
ファイル: equations.py プロジェクト: as641651/PhD-Linnea
    def remove_identities(self):
        """Removes equations where both sides are temporaries.

        There are three different cases.

        If both sides of an equation are the same, it can simply be removed.

        If both sides are different temporaries, all occurrences of the
        temporary on the left-hand side in the other equations are replaced by
        the temporary on the right-hand side. This is necessary to ensure that
        code generation still works. This case can happen when setting
        equivalent expressions does not work perfectly.

        If the operand on the left-hand side is an intermediate, and the one on
        the right hand side is a temporary, all occurrences of the intermediate
        will be replace with the temporary.

        Replacing the temporary in the other equations only works if the
        temporary that is replaced is not part of any computation yet.

        Furthermore, it is assumed that there are no cases such as
        tmp2 = tmp3
        tmp1 = tmp2

        Returns:
            Equations: self with equations removed.
        """
        remove = []
        replace_eqns = []
        equations = list(self.equations)
        for n, equation in enumerate(equations):
            if equation.rhs.name in temporaries._equivalent_expressions:
                if equation.lhs.name in temporaries._equivalent_expressions:
                    remove.append(n)
                    if equation.lhs != equation.rhs:
                        replace_eqns.append(equation)
                else:
                    # TODO it would be better to only do this if the operand is known to be an intermediate.
                    replace_eqns.append(equation)

        for idx in reversed(remove):
            del equations[idx]

        for replace_eqn in replace_eqns:
            rule = matchpy.ReplacementRule(matchpy.Pattern(replace_eqn.lhs),
                                           lambda: replace_eqn.rhs)
            equations_replaced = []
            for equation in equations:
                equations_replaced.append(
                    ae.Equal(equation.lhs,
                             matchpy.replace_all(equation.rhs, (rule, ))))
            equations = equations_replaced

        # TODO do we want to manipulate the table of temporaries here?
        return Equations(*equations)
コード例 #9
0
def optimize(algebra):
    rules = [
        # cross_reduction,
        # tautology,
        # and_tautology,
        cross_with_universe,
        remove_empty_select,
        convert_union_into_select,

        logic_and_true,
        logic_or_true,
        logic_or_false,
        intersect_true,

        select_tautology,
        equal_column_tautology,
        intersection_select_of_same_relation,
        intersection_of_same_relation,
        union_of_same_relation,
        union_of_equilvalent_selects,
        intersection_of_equilvalent_selects,
        union_of_same_relation_over_intersection,
        combine_selects,
        cross_plus_select_2_join,
        join2,
        # join3,

        # Convenience
        swap_comparison,

        merge_greater_thans,
        merge_less_thans,

        # set
        false_becomes_emptyset,
        union_of_empty_set_is_ignored,
        intersection_of_empty_set_is_empty_set,

        push_select_down_cross,
        # empty_union,
        # shift_joins,

        # IN (...)
        in_statement_2_ors,
        in_statement_false,

        coerce_to_int,
    ]
    new_algebra = replace_all(algebra, rules)
    new_algebra._tables = algebra._tables

    logger.debug("Optimized RA:\n{}".format(pretty_print(new_algebra)))

    return new_algebra
コード例 #10
0
ファイル: equations.py プロジェクト: vishalbelsare/linnea
    def remove_explicit_transposition(self, eqn_idx=None):
        """Removes equations of the form tmpX = tmpY^T.

        With common subexpression elimination and the application of tranposed
        operands, it is possible to reach assignments of the form tmpX = tmpY^T.
        To avoid that an explicit transposition is computed, this function
        replaces tmpX in all subsequent assignments with tmpY^T.

        Args:
            eqn_idx (int, optional): Only test if self.equations[eqn_idx] is an
                explicit transposition.

        Returns:
            Equations: self with explicit transposition removed.
        """

        indices = None
        if eqn_idx:
            indices = [eqn_idx]
        else:
            indices = range(len(self.equations))

        replacement_rules = []
        remove = set()
        for idx in indices:
            equation = self.equations[idx]
            if temporaries.is_temporary(equation.lhs) and isinstance(
                    equation.rhs, ae.Transpose) and isinstance(
                        equation.rhs.operand, ae.Symbol):
                if equation.lhs != equation.rhs.operand:
                    # In they are equal, the operand is symmetric, which means
                    # that this assignment is an identity and can be removed.
                    replacement_rules.append(
                        matchpy.ReplacementRule(matchpy.Pattern(equation.lhs),
                                                Replacer(equation.rhs)))
                remove.add(idx)

        if replacement_rules:
            new_equations = []
            for i, equation in enumerate(self.equations):
                if not i in remove:
                    if i < min(remove):
                        # It is sufficient to start replacing at the smallest index in remove.
                        new_equations.append(equation)
                    else:
                        new_equations.append(
                            ae.Equal(
                                equation.lhs,
                                matchpy.replace_all(equation.rhs,
                                                    replacement_rules)))
            return Equations(*new_equations)
        else:
            return self
コード例 #11
0
    def replace_auxiliaries(self):
        """Replaces auxiliaries.

        If there is an equation X = rhs where X has the property "auxiliary", the
        equation is removed and all occurences of X in other equations are
        replaced with rhs.
        """
        replacement_rules = []
        remove = []
        # IMPORTANT: eqn can NOT be renamed to equation. Otherwise,
        # "equation.rhs in the lambda function refers to the wrong equation.
        for eqn in self.equations:
            if eqn.lhs.has_property(Property.AUXILIARY):
                rule = (matchpy.Pattern(eqn.lhs), lambda **_: eqn.rhs)
                replacement_rules.append(rule)
                remove.append(eqn)

        for eqn in remove:
            self.equations.remove(eqn)

        self.equations = [matchpy.replace_all(equation, replacement_rules) for equation in self.equations]
コード例 #12
0
def remove_universe_set(algebra):
    rules = [
        cross_with_universe,
    ]
    return replace_all(algebra, rules)