MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Reverse order terms and 0-origin array.

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1695] Re: [mg1601] Reverse order terms and 0-origin array.
  • From: mike johnson <johnson56 at popcorn.llnl.gov>
  • Date: Mon, 17 Jul 1995 02:44:26 -0400
  • Organization: Lawrence Livermore Nat'l Lab

Richard Mercer <richard at seuss.math.wright.edu> wrote:
>>  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... 

Sorry for missing this query the first time around. I have used the following
for several years when working with angular momentum selection rules, which
involve products and ratios of polynomials. The principles are pretty simple:
(1) Change only the output format, not the internal one, (2) get a list of the
terms in each factor (if a product, etc.), (3) Reorder the terms based on the
size of their exponents. I have never taken the time to put this into a real
MMA package.

(* 12/7/93 maj (with a lot of help from Robby Villegas @ WRI tech support)
    Routines for reordering (output format only) polynomials,
      products of polynomials, and ratios of polynomials so that
      highest exponents occur first. 

reOrderPoly[expr,var]
reOrderPolyProd[expr,var] (includes above)
reOrderPolyRatio[expr,var] (includes both above) *)

 
Clear[DegreeSortTerms]
Clear[OrderlessPlus]
Clear[reOrderPolyRatio]
Clear[reOrderPoly]
Clear[OrderlessTimes]
Clear[reOrderPolyProd]
Clear[termsList]
Clear[facList]

termsList[sum_Plus] := Level[sum, {1}]
termsList[singleton_] := {singleton}

facList[product_Times] := Level[product, {1}]
facList[singleton_] := {singleton}

DegreeSortTerms[expr_,var_] :=
Module[{terms = termsList[expr]},
	Sort[terms, (Exponent[#1, var] >
		Exponent[#2,var]) &]
]

Format[ OrderlessPlus[terms___] ] := terms /; Length[{terms}] == 1 
Format[ OrderlessPlus[terms___] ] :=
	 HoldForm[Plus[terms]] /; Length[{terms}] > 1 

reOrderPoly[oneTerm_,var_] := oneTerm /; !PolynomialQ[#, var]
reOrderPoly[oneTerm_,var_] := 
	OrderlessPlus @@ DegreeSortTerms[oneTerm, var] /;
	 PolynomialQ[#, var]

Format[ OrderlessTimes[terms___] ] := terms /; Length[{terms}] == 1
Format[ OrderlessTimes[terms___] ] := HoldForm[Times[terms]] /;
		Length[{terms}] > 1 
		
reOrderPolyProd[product_,var_] := 
	OrderlessTimes @@ Map[reOrderPoly[#,var] &, facList[product]] 
		
reOrderPolyRatio[rational_,var_] :=
	reOrderPolyProd[rational,var] /; Denominator[rational] == 1
reOrderPolyRatio[rational_,var_] :=
	Divide @@ Map[reOrderPolyProd[#,var] &,
		Through[{Numerator,Denominator}[rational]] ] 

SetAttributes[reOrderPoly,Listable]
SetAttributes[reOrderPolyProd,Listable]
SetAttributes[reOrderPolyRatio,Listable]

(* EXAMPLE *)

expr1 = (j+3j^2+4)(4j+5j^5+j^3)/(3(j^3+2+j^2)(5+j))

            2          3      5
(4 + j + 3 j ) (4 j + j  + 5 j )
--------------------------------
                    2    3
    3 (5 + j) (2 + j  + j )
    
reOrderPolyRatio[expr1,j]

    2              5    3
(3 j  + j + 4) (5 j  + j  + 4 j)
--------------------------------
                3    2
    3 (j + 5) (j  + j  + 2)
    
Mike Johnson, Lawrence Livermore National Lab




  • Prev by Date: Help with a symbolic sum.....
  • Next by Date: How to simplify Sqrt[x^2]?
  • Previous by thread: Re: Reverse order terms and 0-origin array.
  • Next by thread: NDSolve algorithm