Re: Reverse order terms and 0-origin array.
- To: mathgroup at christensen.cybernetics.net
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1686] Re: [mg1601] Reverse order terms and 0-origin array.
- From: Bob Hanlon <hanlonbo at sgate.com>
- Date: Mon, 17 Jul 1995 01:14:20 -0400
Following is a brute force method of outputing a polynomial in standard
form. It hasn't been thoroughly tested but should offer a starting point.
Bob Hanlon
_______________
stdPoly::usage = "stdPoly[poly, var] prints a polynomial in
standard form.";
stdPoly[poly_, var_] :=
Module[{coef = CoefficientList[poly, var], pwrs, elem},
pwrs = NestList[Times[#, var]&, 1, Length[coef]-1];
elem = Flatten[
If[ MatchQ[#, Times[a_?Negative, x__]] ||
(NumberQ[#] && Negative[#]),
{" - ", -#}, {" + ", #} ]& /@
( Select[coef pwrs, # =!= 0&] // Reverse )];
elem = If[elem[[1]] == " + ",
Rest[elem], Join[{"- "}, Rest[elem]]];
Print[Sequence @@ elem] ];
p1[x_] := a x^4 + b x^2 + c; p2[x_] := -a x^2 + b x + c;
p3[x_] := a x^4 - b x^2 + c; p4[x_] := a x^2 + b x - c;
stdPoly[p1[x], x]; Print[""]; stdPoly[p2[x], x]; Print[""];
stdPoly[p3[x], x]; Print[""]; stdPoly[p4[x], x]; Print[""];
stdPoly[-7 x^5 + 6.3 x^4 - 28 x^3 - 6, x];
4 2
a x + b x + c
2
- a x + b x + c
4 2
a x - b x + c
2
a x + b x - c
5 4 3
- 7 x + 6.3 x - 28 x - 6
> From: Richard Mercer <richard at seuss.math.wright.edu>
> Newsgroups: comp.soft-sys.math.mathematica
> Subject: Re: [mg1601] Reverse order terms and 0-origin array.
> Date: 7 Jul 1995 01:18:06 GMT
>
> > 1) When I type Expand[(x+1)^2], Mathematica returns 1 +
> > 2x + x^2. Can receive the result in the reverse order,
> > i.e., x^2 + 2x + 1 ?
>
> Although I have made Mathematica jump through many hoops in 5 years of
> programming, the most frustrating failure has been my inability to
provide the
> option to have polynomials print out in traditional order. I know in
principle
> how it apparently could be done, but as I have not yet been able to
make it
> work there is no point in providing any details.
>
> The default sorting in Mathematica is difficult to turn off without side
> effects because it is so closely linked with the aggressive evaluation
scheme.
> Oh sure you can try
> Unprotect[Plus]
> ClearAttributes[Plus,Orderless]
> but that is begging for trouble and I promise you many undesirable side
> effects.
>
> I have tried to establish a custom format for printing polynomials, but
> whenever I sort the polynomial my way, somehow Mathematica always
manages to
> resort it in the default order before it appears as output. Very
frustrating,
> even by Mathematica standards! I haven't given up and someday will take
> another crack at it.
>
> Richard Mercer