Skip to content

Restate Subtraction

import mathy_core.rules.restate_subtraction
Subtraction operations aren't commutable, but addition of negative values are. This flips a subtraction to a plus negation, to "unlock" terms and allow moving them around, e.g. for like-terms simplification.

Examples

Info

All the examples shown below are drawn from the mathy test suite that verifies the expected input/output combinations for rule transformations.

Input Output Valid
-9 = -10 - -h -9 = -10 + h
-7 - -4 = -t -7 + 4 = -t
m - 3 = 3 m + -3 = 3
3u^2 - -1t^4 + -u^2 3u^2 + 1t^4 + -u^2
-4x^3 - -2x^3 -4x^3 + 2x^3
4x - -2x 4x + 2x
m^2 - (3m^2 - 12m^2 - g^2) m^2 + -(3m^2 - 12m^2 - g^2)
2x - 9 2x + -9
2x - 9x 2x + -9x
2x - 9x^3 2x + -9x^3
2x + -9 2x - 9
2x + -9x 2x - 9x
2x + -9x^3 2x - 9x^3
4 - 3x 4 + -3x

API

RestateSubtractionRule

RestateSubtractionRule(self, args, kwargs)
Convert subtract operators to plus negative to allow commuting

get_type

RestateSubtractionRule.get_type(
    self, 
    node: mathy_core.expressions.MathExpression, 
) -> Optional[str]
Determine the configuration of the tree for this transformation.

Support two types of tree configurations: - Subtraction is a subtract to be restate as a plus negation - PlusNegative is a plus negative const to be restated as subtraction