Expand¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Expand[expr] expands out products and powers in expr.
Expand[expr, patt] leaves unexpanded any parts of expr that are free of the pattern patt.
Examples¶
All examples below are verified against the current Mathilda build.
In[1]:= Expand[(x+3)(x+2)]
Out[1]= 6 + 5 x + x^2
In[2]:= Expand[(x+y)^2 (x-y)^2]
Out[2]= x^4 - 2 x^2 y^2 + y^4
In[3]:= Expand[(x+1)^2 + (y+1)^2, x]
Out[3]= 1 + 2 x + x^2 + (1 + y)^2
Implementation notes¶
Algorithm. builtin_expand (in src/expand.c) calls expr_expand_patt(e, patt), a structural recursion that distributes products over sums. The dispatch by head:
- Plus — expand each summand and rebuild via
eval_and_free. - Times — expand each factor, then multiply them pairwise with
multiply_all, a divide-and-conquer fold whose leaf operationmultiply_twohandles the fourPlus×Plus / Plus×atom / atom×Plus / atom×atomcases, producing every cross term (a_i · b_j) and re-summing them. - Power[base, k] with
ka positive integer< 100— expand the base, thenpower_expandraises it by repeated squaring (binary exponentiation), each multiply going through the distributingmultiply_two. (Note: there is no explicit binomial-coefficient formula; the binomial expansion of(a+b)^kfalls out of the iterated distribution.) Negative or non-integer exponents are left untouched. - List / equations / inequalities / And / Or / Not — threads into each argument (passing operator-symbol slots of
Inequalitythrough verbatim).
A two-argument Expand[expr, patt] only expands subexpressions that contain patt (gated by expr_contains_patt, which uses the pattern matcher); everything else is copied unchanged. Expand does not descend into the arguments of arbitrary function heads.
Data structures. Plain Expr* trees; transient Expr** argument buffers per node. All arithmetic rebuilding goes back through the evaluator (eval_and_free) so Plus/Times canonicalisation (Flat/Orderless, like-term collection) applies after each step.
Complexity / limits. Worst case is the full cross-product blow-up of distribution: expanding (x_1+...+x_m)^k produces O(m^k)-sized output, and Power is capped at exponent < 100 to bound it.
Protected.- Works only on positive integer powers and distributes products over sums.
- Threads over equations, inequalities, and lists.
- Implements an efficient binary-splitting algorithm for distributing products and repeated squaring for powers.
Expand[expr, patt]leaves unexpanded any parts ofexprthat are free of the patternpatt.
Attributes: Protected.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Geddes, Czapor & Labahn, "Algorithms for Computer Algebra" (1992), Ch. 3 (normal forms and the distributive expansion of polynomials).
- Source:
src/expand.c - Specification:
docs/spec/builtins/structural-manipulation.md
Notes & additional examples¶
Worked examples¶
In[1]:= Expand[(1 + x + y)^3]
Out[1]= 1 + 3 x + 3 x^2 + x^3 + 3 y + 6 x y + 3 x^2 y + 3 y^2 + 3 x y^2 + y^3
In[1]:= Expand[(1 + x)^10]
Out[1]= 1 + 10 x + 45 x^2 + 120 x^3 + 210 x^4 + 252 x^5 + 210 x^6 + 120 x^7 + 45 x^8 + 10 x^9 + x^10
Notes¶
Expand applies the distributive law to products and integer powers,
producing a flat sum of monomials in canonical order (ascending total
degree in the leading variable). Like terms are combined automatically, so
(x + 2)^2 (x - 1) collapses the x^1 coefficient to zero and it drops out
of the result. Expand only multiplies out — it does not factor or cancel —
and a second argument Expand[expr, patt] leaves alone any parts free of the
pattern patt.