# Reason (and alternatives) for some min/max simplification rules

#1

We currently have simplification rules that changes min to max and max to min as follows where c1 and c2 are constants (let’s call them set 1)

``````min(c1 - x, c2) -> c1 - max(x, c1-c2)
max(c1 - x, c2) -> c1 - min(x, c1-c2)
``````

My guess about the motivation behind these rules is that they try to keep the coefficient of `x` positive. is that correct? If yes, (a) what if x itself has a negative sign (b) what do we achieve by this rule?

Now consider the following new rules (set 2)

``````max(a,b) - a -> max(0, b-a)
min(a,b) - a -> min(0, b-a)
a - max(a,b) -> min(0, a-b)
a - min(a,b) -> max(0, a-b)
...
``````

Note that we can’t have both sets because they lead to infinite recursion. I need set 2 because of the following simplification I have to make in some experimental code:

``````min (5-min(x,5), max(x,5)-5) --set2--> min(max(5-x,0), max(x-5,0)) --existing rules--> min(max(5-x,x-5),0) --new rule--> 0
``````

where new rule is `min(max(a,-a),0) -> 0`

I can think of two suggestions:

• Add set 2 and remove set 1 (if there is no good reason to keep set 1)

• Add set 2 and add restriction `c2 != 0` to set 1 to get rid of infinite recursion

It seems to me that if I don’t add set 2, then I have to come up with an overly complicated (and non-minimal) rule to take care of the specific example above.

What are your thoughts? @tqchen @sgrechanik-h @wweic

#2

I think we need a more thorough testing of simplifiers, so that we can tweak them and see if there are any regressions. I supposed the test data may be gathered from the existing tests by tracing every call to the simplification functions.

#3

Are you suggesting more testing the simplifiers in general or only after the changes I’m suggesting above?

I supposed the test data may be gathered from the existing tests by tracing every call to the simplification functions.

Do you mean that we should also write unit tests for intermediate functions called during simplification (instead of only testing `Simplify` and `CanonicalSimplify`)?

#4

Yes, more testing of the simplifiers in general. If we had a lot of tests, we could disable the rules in question and immediately see why they are needed by looking at the failing tests.

No, I misformulated. I mean, we can collect every expression that gets passed to Simplify and CanonicalSimplify together with the corresponding simplified results during the run of tvm tests. This will be a good approximation of the set of expressions we care about.