# Reverse an expression in the output?

I have a recurrence relation in output and Mathematica give me in output the reverse order of what I want, for example

{-5 (2 n - 7) (3 n - 10) (3 n - 7) , 0,0,(6 n - 19) (6 n - 13), (6 n -7),(462722852867 n^3 -962875190792 n^2 + 1166698625652 n - 627172524720)}

in the output I get

-5 (2 n - 7) (3 n - 10) (3 n - 7)a[n-5]+(6 n - 19) (6 n - 13)a[n-2]+ (6 n -7)a[n-1]+( 462722852867 n^3 -962875190792 n^2 + 1166698625652 n - 627172524720)a[n]

but I want

( 462722852867 n^3 -962875190792 n^2 + 1166698625652 n - 627172524720)a[n]+ (6 n -7)a[n-1]+(6 n - 19) (6 n - 13)a[n-2]-5 (2 n - 7) (3 n - 10) (3 n - 7)a[n-5]+

I tried these commands

Reverse[]
RotateLeft[]
RotateRight[]


but the output still the same

Thank you.

A larger example, not in the desired order:

-576 (-9 + n) (-8 + n) (-7 + n) (-35 + 3 n) (-31 + 3 n) (-28 +
3 n) (-23 + 3 n) (-20 + 3 n) (-19 + 3 n) (-17 + 3 n) (-16 +
3 n) (-14 + 3 n) (-13 + 3 n) (-10 + 3 n) (-7 + 3 n) a[-14 + n] +
5 (-8 + n) (-7 + n) (-5 + n) (-28 + 3 n) (-20 + 3 n) (-17 +
3 n) (-16 + 3 n) (-14 + 3 n) (-13 + 3 n) (-10 + 3 n) (-7 +
3 n) (-57 + 5 n) (-49 + 5 n) (-41 + 5 n) (-33 + 5 n) a[-6 + n] -
5 (-7 + n) (-17 + 3 n) (-14 + 3 n) (-13 + 3 n) (-10 + 3 n) (-7 +
3 n) (-7132966283520 + 9021347689536 n - 5006624101088 n^2 +
1601473586668 n^3 - 325587246373 n^4 + 43652805122 n^5 -
3861437250 n^6 + 217395000 n^7 - 7070625 n^8 + 101250 n^9) a[-5 +
n] + 5 (-14 + 3 n) (-10 + 3 n) (-7 + 3 n) (23357124762753600 -
43564137549957360 n + 36775008279260204 n^2 -
18593224893612441 n^3 + 6274748858601497 n^4 -
1489842040904040 n^5 + 255310266461696 n^6 - 31828995825234 n^7 +
2865910980978 n^8 - 181809090000 n^9 + 7715209500 n^10 -
196678125 n^11 + 2278125 n^12) a[-4 + n] -
10 (-19 + 3 n) (-7 + 3 n) (-91605709055952000 +
211605862175890560 n - 221164472227884248 n^2 +
138679631057141024 n^3 - 58274719204472912 n^4 +
17346580037124671 n^5 - 3766312275861722 n^6 +
604367447829986 n^7 - 71720894959443 n^8 + 6220126970109 n^9 -
383392845000 n^10 + 15911397000 n^11 - 398671875 n^12 +
4556250 n^13) a[-3 + n] +  5 (-23 + 3 n) (-19 + 3 n) (-16 + 3 n) (1868204798460000 -
4872003910332720 n + 5568683824220146 n^2 -
3705906207430371 n^3 + 1605611503276213 n^4 -
478711029627885 n^5 + 100995656823529 n^6 - 15227970369864 n^7 +
1631911034772 n^8 - 121434130125 n^9 + 5964951375 n^10 -
173896875 n^11 + 2278125 n^12) a[-2 + n] -
5 (-9 + n) (-23 + 3 n) (-20 + 3 n) (-19 + 3 n) (-16 + 3 n) (-13 +
3 n) (-172642368360 + 467904529078 n - 471632902377 n^2 +
247446259858 n^3 - 76729280622 n^4 + 14835851470 n^5 -
1808908500 n^6 + 135189000 n^7 - 5653125 n^8 + 101250 n^9) a[-1 +
n] - 5 (-9 + n) (-8 + n) n (-31 + 3 n) (-23 + 3 n) (-20 +
3 n) (-19 + 3 n) (-17 + 3 n) (-16 + 3 n) (-13 + 3 n) (-10 +
3 n) (-56 + 5 n) (-42 + 5 n) (-28 + 5 n) (-14 + 5 n) a[n]

• Just out of curiosity, why do you want the output to be in a given order? – Kiro Feb 3 '17 at 13:21
• @kiro because I have many recurrences relations and to find the general recurrence I need to write all these recurrences in specific order to understand the general case. ^_^ – OAMAZF Feb 3 '17 at 13:26
• actually it's ( 462722852867 n^3 -962875190792 n^2 + 1166698625652 n- 627172524720), sorry. – OAMAZF Feb 3 '17 at 14:28

Is this close enough?

in = -5 (2 n - 7) (3 n - 10) (3 n - 7) a[n - 5] + (6 n - 19) (6 n - 13) a[
n - 2] + (6 n - 7) a[
n - 1] + (462722852867 n^3 - 962875190792 n^2 + 1166698625652 n -
627172524720) a[n];

in /. Plus -> (Defer[+##] ~Reverse~ {2} &)

a[n] (462722852867 n^3 - 962875190792 n^2 + 1166698625652 n - 627172524720) +
a[n - 1] (6 n - 7) + a[n - 2] (6 n - 19) (6 n - 13) -
5 a[n - 5] (3 n - 10) (2 n - 7) (3 n - 7)

TraditionalForm[in] /. Plus -> (Defer[+##]~Reverse~{2} &)


For your larger example using HoldForm in place of Defer seems to get the main order right, but the suborder is not as desired.

in = (* big input *)

TraditionalForm[in] /. Plus -> (HoldForm[+##] ~Reverse~ {2} &)


I am still seeking a simple way to get this all correct.

This is pretty ugly but after half an hour I haven't come up with anything better.

in /. Plus -> (HoldForm[+##] ~Reverse~ {2} &)
First[List @@@ Map[ToString[#, TraditionalForm] &, %, {2}]];
HoldForm[+##] & @@ % // TraditionalForm


• Thank you for the answer, but when I convert it to TraditionalForm I lost the order again??? – OAMAZF Feb 3 '17 at 14:54
• @OAMAZF Please see update. – Mr.Wizard Feb 3 '17 at 14:56
• It works for small recurrence but if the number of terms became big then I lost the order for example: Example. – OAMAZF Feb 3 '17 at 15:17
• @OAMAZF I added that larger example to your Question for others to reference. Please describe the ordering that you desire. – Mr.Wizard Feb 3 '17 at 15:24
• @OAMAZF Thank you for clarifying; I shall see if I can make my code work more reliably. – Mr.Wizard Feb 3 '17 at 16:47

Defining your polynomial as q, you can get the coefficients of the a[n] using CoefficientList.

q = -5 (2 n - 7) (3 n - 10) (3 n - 7) a[n - 5] +
(6 n - 19) (6 n - 13) a[n - 2] + (6 n - 7) a[n - 1] +
(462722852867 n^3 - 962875190792 n^2 + 1166698625652 - 627172524720) a[n]

CoefficientList[q, {a[n-5], a[n - 4], a[n - 3], a[n - 2], a[n - 1], a[n]}]

• I want to modify the order of the output as the example in the question, the output give me this order (a[n-5],a[n-4],a[n-3,a[n-2],a[n-1],a[n]) and I want to display it like this (a[n],a[n-1],a[n-2],a[n-3],a[n-4],a[n-5]) – OAMAZF Feb 3 '17 at 14:32
• The order of the coefficients is pretty easy to rearrange. See edit.. – bill s Feb 3 '17 at 17:44