Distributive Factor Out
import mathy_core.rules.distributive_factor_out
Distributive Property
of numbers says that we can factor out common values from terms connected with an addition operator. This rule is expressed by the equation ab + ac = a(b + c)
Note
This is a core transformation used in combining like terms, though we usually skip over it mentally because humans are pretty intelligent.
Consider that the 9y + 9y
example from above becomes (9 + 9) * y
. If you apply a constant simplification rule, you end up with 18y
, which results from combining the two like y
terms.
Transformations¶
Given a common parent node, this rule extracts the common value from both sides, leaving an addition and a multiplication.
Addition¶
ab + ac = a(b + c)
+ *
/ \ / \
/ \ / \
/ \ -> / \
* * a +
/ \ / \ / \
a b a c b c
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 |
---|---|---|
4x + -3x | (4 + -3) * x | ✔ |
g + -x^3 + 4x^3 + 19p^4 + -1y | g + (-1 + 4) * x^3 + 19p^4 + -1y | ✔ |
5.8c + (3393c + 6o + -8614k) | (5.8 + 3393) * c + (6o + -8614k) | ✔ |
8c + (4v + 9n) + 7n + 4r | 8c + 4v + (9 + 7) * n + 4r | ✔ |
11 + 11d + (1d + d^2) | 11 + (11 + 1) * d + d^2 | ✔ |
5a + o + (o + y) + 4s | 5a + (1 + 1) * o + y + 4s | ✔ |
15 + 20j + 9j | 15 + (20 + 9) * j | ✔ |
23f + (5f + 5d) | (23 + 5) * f + 5d | ✔ |
9y + 9y | (9 + 9) * y | ✔ |
14x + 7x | (14 + 7) * x | ✔ |
6 + 4 | (3 + 2) * 2 | ✔ |
7 + 7 | (1 + 1) * 7 | ✔ |
4 + (z + 4) | --- | --- |
6 + 4 | --- | --- |
(z * 4 + z * 84x) + 1 | --- | --- |
API¶
DistributiveFactorOutRule¶
DistributiveFactorOutRule(self, constants: bool = False)
ab + ac = a(b + c)
The distributive property can be used to expand out expressions to allow for simplification, as well as to factor out common properties of terms.
Factor out a common term
This handles the ab + ac
conversion of the distributive property, which factors out a common term from the given two addition operands.
+ *
/ \ / \
/ \ / \
/ \ -> / \
* * a +
/ \ / \ / \
a b a c b c
get_type¶
DistributiveFactorOutRule.get_type(
self,
node: mathy_core.expressions.MathExpression,
) -> Optional[Tuple[str, mathy_core.util.TermEx, mathy_core.util.TermEx]]
Support the three types of tree configurations:
- Simple is where the node's left and right children are exactly terms linked by an add operation.
- Chained Left is where the node's left child is a term, but the right child is another add operation. In this case the left child of the next add node is the target.
- Chained Right is where the node's right child is a term, but the left child is another add operation. In this case the right child of the child add node is the target.
Structure:
- Simple
- node(add),node.left(term),node.right(term)
- Chained Left
- node(add),node.left(term),node.right(add),node.right.left(term)
- Chained Right
- node(add),node.right(term),node.left(add),node.left.right(term)