%mathpiper,def="Coef"

5 # Coef(uv_IsUniVar,order_IsList) <--
[
  Local(result);
  result:={};
  ForEach(item,order)
  [
    DestructiveAppend(result,Coef(uv,item));
  ];
  result;
];

10 # Coef(uv_IsUniVar,order_IsInteger)_(order<uv[2]) <-- 0;
10 # Coef(uv_IsUniVar,order_IsInteger)_(order>=uv[2]+Length(uv[3])) <-- 0;
20 # Coef(uv_IsUniVar,order_IsInteger) <-- uv[3][(order-uv[2])+1];
30 # Coef(uv_CanBeUni,_order)_(IsInteger(order) Or IsList(order)) <-- Coef(MakeUni(uv),order);

Function("Coef",{expression,var,order})
    NormalForm(Coef(MakeUni(expression,var),order));



%/mathpiper



%mathpiper_docs,name="Coef",categories="User Functions;Polynomials (Operations)"
*CMD Coef --- coefficient of a polynomial
*STD
*CALL
	Coef(expr, var, order)

*PARMS

{expr} -- a polynomial

{var} -- a variable occurring in "expr"

{order} -- integer or list of integers

*DESC

This command returns the coefficient of "var" to the power "order"
in the polynomial "expr". The parameter "order" can also be a list
of integers, in which case this function returns a list of
coefficients.

*E.G.

In> e := Expand((a+x)^4,x)
Result: x^4+4*a*x^3+(a^2+(2*a)^2+a^2)*x^2+
	(a^2*2*a+2*a^3)*x+a^4;
In> Coef(e,a,2)
Result: 6*x^2;
In> Coef(e,a,0 .. 4)
Result: {x^4,4*x^3,6*x^2,4*x,1};

*SEE Expand, Degree, LeadingCoef
%/mathpiper_docs