# Is it possible to use Composition for polynomial composition?

I want to do this:

$P = (x^3+x)$

$Q = (x^2+1)$

$P \circ Q = P \circ (x^2+1) = (x^2+1)^3+(x^2+1) = x^6+3x^4+4x^2+2$

I used Composition for testing if that could be done then :

Composition[p, q][x^3 + x, x^2 + 1]

p[ q[ x + x^3, 1 + x^2]]


I've also tried to use Expand on it but It didn't work. The nearest thing I could do was this:

a = x^2 + 1;  Expand[ a^3 + a]

2 + 4 x^2 + 3 x^4 + x^6


I was thinking that doing it by hand on longer compositions would be tedious, is there a function or way to do that?

-

This seems to work :

p[x_] = x^3 + x;
q[x_] = x^2 + 1;

Composition[p, q][x]

(* 1 + x^2 + (1 + x^2)^3 *)

Composition[Expand, p, q][x]

(* 2 + 4 x^2 + 3 x^4 + x^6 *)

-
If you don't want to define separate functions: Composition[Function[x, x^3 + x], Function[x, x^2 + 1]][x] // Expand. – J. M. Aug 18 '12 at 16:40

Assuming p, q had been defined this way :

p[x_] := x^3 + x
q[x_] := x^2 + 1


then Composition[p, q][x^3 + x, x^2 + 1] couldn't have worked because q had been defined as a function of one variable, this however would have worked :

Composition[p, q][x]


but to get what you intended (i.e. $g = p \circ q\;$) you would rather use :

g[x_]:= Composition[ Expand, p, q][x]
g[x]

2 + 4 x^2 + 3 x^4 + x^6


Another way would be to use pure functions, e.g.

Clear[ p, q, g]
p = #^3 + # & ;
q = #^2 + 1 &;


now we can use it like this :

g = Composition[ Expand, p, q];
g[x]

2 + 4 x^2 + 3 x^4 + x^6


In such a simple case as you consider you can also get rid of Composition defining g like this (it doesn't matter if p and q are pure functions or not):

Clear[ g]
g[x_] := p[ q[x]] // Expand
g[x]

2 + 4 x^2 + 3 x^4 + x^6


Composition is especially useful when you have a list of functions, e.g.

lf = {f1, f2, f3, f4, f5, f6, f7, f8, f9, f10};


then apply it to this list :

(Composition @@ lf)[x]

f1[f2[f3[f4[f5[f6[f7[f8[f9[f10[x]]]]]]]]]]


Edit

The original example in the question isn't especially interesting to demonstrate how Composition can be useful. Consider e.g. Laguerre polynomials, we define :

L[k_] := LaguerreL[k, #] &


e.g. :

L[5][x]

1/120 (120 - 600 x + 600 x^2 - 200 x^3 + 25 x^4 - x^5)

Plot[ L[#][x] & /@ Range[5], {x, 0, 3}, Evaluated -> True, PlotStyle -> Thick]


Now we can find compositions of subsequent Laguerre polynomials starting from 2- nd to n- th, e.g. :

GraphicsGrid[
Partition[
Table[ Plot[ (Composition @@ (L[#] & /@ Range[k, 2, -1]))[x], {x, 0, 1.5},
PlotLabel -> Range[2, k], PlotStyle -> Thick],
{k, 3, 6}], 2]]


We could get the same with other Mathematica functions like e.g. Fold, FoldList etc. but Composition makes the task simpler and more straightforward.

-

Another possibility is to employ rules

x /. x -> p  /. x -> q

1 + x^2 + (1 + x^2)^3


You can also Expand it

x /. x -> p  /. x -> q // Expand

2 + 4 x^2 + 3 x^4 + x^6


## Edit

Expanding on @J.M. comment, this approach can be packaged in a function easily:

polyComposition[polyList_] := Fold[ReplaceAll, x, Map[x -> # &, polyList]] // Expand
polyComposition[{p, q}]

2 + 4 x^2 + 3 x^4 + x^6

-
Or, Fold[ReplaceAll, x, {x -> x^3 + x, x -> x^2 + 1}]. – J. M. Aug 18 '12 at 17:13