matchpy icon indicating copy to clipboard operation
matchpy copied to clipboard

Show ReplacementRule while using `replace()` in ManyToOneReplacer

Open arihantparsoya opened this issue 8 years ago • 5 comments

Is it possible to know/display the ReplacementRule if the replace() is able apply the rule?

arihantparsoya avatar Jul 10 '17 05:07 arihantparsoya

You can use the underlying matcher to find out which rules match: replacer.matcher.match(subject). If you want to see the replacement steps that are taken, there is currently no function for that. A "debug" function sounds like a good idea though.

wheerd avatar Jul 10 '17 09:07 wheerd

I have used the following code:

    matches = rubi.matcher.match(expr)
    for matched_pattern, substitution in sorted(map(lambda m: (str(m[0]), str(m[1])), matches)):
        print('{} matched with {}'.format(matched_pattern, substitution))

which returns:

<function rubi_object.<locals>.<lambda> at 0x10bf93d08> matched with {x ↦ x}

What do I need to do in order to print the ReplacementRule?

arihantparsoya avatar Jul 10 '17 12:07 arihantparsoya

Sorry, I didn't realize that the labels used by the ManyToOneReplacer are the replacement lambda functions and do not contain the patterns. Here is a workaround you can try until I can provide debugging functionality:

matches = rubi.matcher.match(expr)
for _ in matches._match(rubi.matcher.root):
    for pattern_index in matches.patterns:
        renaming = rubi.matcher.pattern_vars[pattern_index]
        substitution = matches.substitution.rename({renamed: original for original, renamed in renaming.items()})
        pattern = rubi.matcher.patterns[pattern_index][0]
        print('{} matched with {}'.format(pattern , substitution))

I will try to add some debugging functionality to the ManyToOneReplacer as soon as possible.

wheerd avatar Jul 11 '17 16:07 wheerd

Thanks

arihantparsoya avatar Jul 11 '17 17:07 arihantparsoya

It would be nice to have API something like this:

result, applied_rule = replacer.replace(expression, showsteps=True)

Otherwise we would have to compute the result and applied_rule seperately.

arihantparsoya avatar Jul 11 '17 17:07 arihantparsoya