add an "ifCondition" parameter to rules, so you can programatically choose whether a rule applies or not#2901
add an "ifCondition" parameter to rules, so you can programatically choose whether a rule applies or not#2901ziktar wants to merge 2 commits into
Conversation
…hoose whether a rule applies or not
|
Thanks for your PR @ziktar Two thoughts:
|
|
Regarding option 1, one goal I had was to keep the existing powerful node matching that exists in the current rules, so I wouldn't want to use the existing provide-a-single-function rule option that exists today. An improvement to what I did here could be to instead of adding the The end goal for me is to be able to sort terms/factors consistently (such as by polynomial degree in the easy case of a bunch of polynomials and alphabetic for polynomials of the same degree, and using some pattern in the more complicated expressions) As such, regarding option 2, I imagine that I could get to normalized ordering by checking if the rules matched both current and opposite order, but it would still need some logic to be able to compare the two terms beyond just being c vs v vs ve. So we'd need to be able to use a method that compares two terms, so we'd have to extend the simplify rules logic to be able to specify that function being used to compare them. If we're not letting the user add whatever logic they want, then we'll need to make sure that whatever we expose here is comprehensive, so we'll need to make sure our comparison function can compare any expression; |
|
About point 2 It's indeed not trivial. It requires thorough thinking through, maybe best to park these ideas for now and focus on making the simplify rule system more flexible. About point 1 Just to be clear: I'm not ruling out your initial solution with Interesting idea to make the I was also thinking into a different direction: right now a rule can basically defined high level as function customRule(node) {
if (satisfiesCondition(node)) {
return applyRule(node, rule, context)
}
} |
|
Created discussion #3570 based on this PR, which I am closing as out of date. |
I found that when calling
simplifywith equations in different orders (such as "x + x^2" vs "x^2 + x"), I'd get different results. I wanted to make a consistent way to sort terms, and found there wasn't a conditional way to apply simplification rules; you can only either make a rule that always applies or make a change based on the entire node tree (which means I'd need to write my own matching code).As such, I propose adding an optional function, I called "ifCondition" , that you can place on a rule. It gets passed the matches that are being currently looked at, and if the function returns false, then the swap is not made, and if it returns true, then it is made.