# Solve for coefficients to express polynomial in terms of another polynomial

If I have a polynomial, say $$p(x) = 6x^3 - x^2 + x$$, and I want to express that in terms of a sum of other polynomials how may I do that in Mathematica? Specifically I would like to say that $$p(x) = \sum_{i = 0}^3 \alpha_iL_i(x)$$ where $$L_i(x)$$ is the $$i^{th}$$ Legendre Polynomial (not super important what exactly the other polynomial is).

I have attempted the following:

p = 6*x^3 - x^2 + x;
Solve[p == (C3*LegendreP[3, x] + C2*LegendreP[2, x] +
C1*LegendreP[1, x] + C0*LegendreP[0, x]), {C3, C2, C1, C0}]


but it gives

 {{C0 -> x - C1 x - x^2 + 6 x^3 - 1/2 C2 (-1 + 3 x^2) -
1/2 C3 (-3 x + 5 x^3)}}


as the output when I know (for this example) the C's are real numbers.

Please see if this does what you want. If not, will delete this answer.

p      = 6*x^3 - x^2 + x;
coeff  = CoefficientList[p, x]
p0     = Expand[C3*LegendreP[3, x] + C2*LegendreP[2, x] + C1*LegendreP[1, x] +
C0*LegendreP[0, x]]; coeff0 = CoefficientList[p0, x]; sol = First@Solve[eqs, {C0, C1, C2, C3}] To verify

 p0 /. sol • @C.Fuhrman - You have four unknowns so Solve needs four equations to determine them. As shown by @Nasser, the four equations are obtained by equating the corresponding coefficients. – Bob Hanlon Sep 30 at 15:21

Here's a different method using SolveAlways:

poly = 6 x^3 - x^2 + x
SolveAlways[poly == Sum[C[i] LegendreP[i, x], {i, 0, 3}], x]


{{C -> -(1/3), C -> -(2/3), C -> 23/5, C -> 12/5}}

This is the job of PolynomialReduce; see the documentation here.

PolynomialReduce[p, Table[LegendreP[i, x], {i, 0, 3}], {x}]

is the idiomatic way. The output is of the form {coefficientList, remainder} where in the cases where the reduction is possible, remainder is zero.
• This won't work though, because the multipliers can be (and are, in this case) polynomials in 'x'. One can homogenize everything to degree 3 however; that seems to work. Multiplying each degree in x by a distinct new variable is another way to go about this. – Daniel Lichtblau Oct 2 at 16:47