Skip to content

Variable Multiply

import mathy_core.rules.variable_multiply
The Variable Multiplication rule restates x^b * x^d as x^(b + d), which isolates the exponents attached to the variables so they can be combined.

Note

This rule can only be applied when the nodes have matching variable bases. This means that x * y cannot be combined, but x * x can be.

Transformations

Both implicit and explicit variable powers are recognized in this transformation.

Help Wanted

The current variable multiply rule leaves out a case where there is a power raised to another power, they can be combined by multiplying the exponents together.

For example: x^(2^2) = x^4

If you would like to help out with by updating this rule open an issue here

Explicit powers

In the simplest case, both variables have explicit exponents.

Examples: x^b * x^d = x^(b+d)

  • 42x^2 * x^3 becomes 42x^(2 + 3)
  • x^1 * x^7 becomes x^(1 + 8)
            *
           / \
          /   \          ^
         /     \    =   / \
        ^       ^      x   +
       / \     / \        / \
      x   b   x   d      b   d

Implicit powers

When not explicitly stated, a variable has an implicit power of being raised to the 1, and this form is identified.

Examples: x * x^d = x^(1 + d)

  • 42x * x^3 becomes 42x^(1 + 3)
  • x * x becomes x^(1 + 1)
            *
           / \
          /   \          ^
         /     \    =   / \
        x       ^      x   +
               / \        / \
              x   d      1   d

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
x * x x^(1 + 1)
(36c^6 * u^3) * 7u^3 36c^6 * 7u^(3 + 3)
4x * 2p^2 * 2p^3 * 12x^2 4x * 2 * 2 * p^(2 + 3) * 12x^2
324u * u 324u^(1 + 1)
17k * 23k 17 * 23k^(1 + 1)
p * p * 12 p^(1 + 1) * 12
4x * p^2 * p^3 * 12x^2 4x * p^(2 + 3) * 12x^2
4 * p * p^3 * 12x^2 4 * p^(1 + 3) * 12x^2
4 * p * p * 12x^2 4 * p^(1 + 1) * 12x^2
4 * p * p * 12 4 * p^(1 + 1) * 12
x * x^3 x^(1 + 3)
y^11 * y y^(11 + 1)
x^2 * x^7 x^(2 + 7)
(8y * 4y) 8 * 4y^(1 + 1)
x * z --- ---
x * y^2 --- ---
2x * 1y^3 --- ---

API

VariableMultiplyRule

VariableMultiplyRule(self, args, kwargs)

This restates x^b * x^d as x^(b + d) which has the effect of isolating the exponents attached to the variables, so they can be combined.

1. When there are two terms with the same base being multiplied together, their
   exponents are added together. "x * x^3" = "x^4" because "x = x^1" so
   "x^1 * x^3 = x^(1 + 3) = x^4"

TODO: 2. When there is a power raised to another power, they can be combined by
         multiplying the exponents together. "x^(2^2) = x^4"

The rule identifies terms with explicit and implicit powers, so the following transformations are all valid:

Explicit powers: x^b * x^d = x^(b+d)

      *
     / \
    /   \          ^
   /     \    =   / \
  ^       ^      x   +
 / \     / \        / \
x   b   x   d      b   d

Implicit powers: x * x^d = x^(1 + d)

    *
   / \
  /   \          ^
 /     \    =   / \
x       ^      x   +
       / \        / \
      x   d      1   d

get_type

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

Support two types of tree configurations: - Simple is where the node's left and right children are exactly terms that can be multiplied together. - Chained is where the node's left child is a term, but the right child is a continuation of a more complex term, as indicated by the presence of another Multiply node. In this case the left child of the next multiply node is the target.

Structure: - Simple node(mult),node.left(term),node.right(term) - Chained node(mult),node.left(term),node.right(mult),node.right.left(term)