| Author |
Comment/Response |
Eric Harley
|
10/14/05 09:19am
OK, one more improvement I thought I should post.
Clear[polyOrder, poly, varlist, ord, polyList, termsList, orderList, selTerms];
polyList[poly_Plus]:=List@@poly;
polyList[poly_]:={poly}/;\[Not]MatchQ[Head[poly],_Plus];
polyOrder=Function[{poly,varlist,ord},Block[{termsList,orderList,selTerms},
termsList=polyList[poly];
orderList=Exponent[#,varlist,List]& /@ termsList;
orderList=Map[Flatten,orderList,2];
orderList=Apply[Plus,orderList,2];
termsList=Transpose[{termsList,orderList}];
selTerms=
Select[termsList,#\[LeftDoubleBracket]2\[RightDoubleBracket]\
\[LessEqual]ord&];
Plus@@(#\[LeftDoubleBracket]1\[RightDoubleBracket]&/@selTerms)
]];
This fixes a problem the last one had when presented with a "polynomial" that was, in fact, only one term long. This can happen in the context in which I am using this, since I don't know beforehand what the polynomial is going to look like.
The version in my previous post assumed that the head of the polynomial was Plus, and just changed this head to List. This caused problems if the polynomial only had one term, such that the head of the polynomial could be Times or Power, for example.
This version only changes the head to List if the head is Plus. It's accomplished by the new function polyList, which is defined so that it either replaces Plus by List, or if the head is not Plus, just puts brackets around it to turn it into a one-element list.
URL: , |
|