Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have some 2-variable Laurent polynomials in $q$ and $t$, and I'd like to collect powers of $q$ and display them without using fractions, and to sort the terms by the power of $q$. For example, I'd like to display $(t-t^{-1})q^{-1} + (t^{-1})q$, instead of $(t-\frac 1 t)\frac 1 q + \frac q t$ which is what Mathematica wants to output.

Is there a nice way to do this? Right now I'm applying the function

StandardForm[# /. Power[expr_, r_?Negative] :> Superscript[expr, r]] &

to the polynomials I'd like to display, which makes negative powers display as exponents. But the problem is that Mathematica then doesn't sort the terms correctly, since it doesn't recognize the "Superscript" as a power.

share|improve this question

1 Answer 1

f[subexpr_, var_] := Sort@Which[
  NumberQ[subexpr], {subexpr, {var, 0}},
  Head[subexpr] === Power && subexpr[[1]] == var, {1, List @@ subexpr},
  MemberQ[subexpr, var], List @@ subexpr /. var -> {var, 1},
  True, List @@ # /. Power[var, r_] :> List[var, r] &@subexpr
 ]
f2[expr_, var_] := Plus @@ (#1 Superscript[var, #2[[2]]] & @@@ 
    Sort[f[#, var] & /@ (List @@ expr), #1[[2, 2]] < #2[[2, 2]] &])
expr1 = Series[1/Sin[x]^10, {x, 0, 2}] // Normal
expr2 = (t - t^-1) q^-1 + (t^-1) q
f2[expr1, x]
f2[expr2, q]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.