# How to preserve the order of expressions in the asymptotic expansion?

I have a complicated expression involving logarithms.

 ex = (1/2)*(-4*n + n^2 - n*Log[4] + 4*n*Log[n] - 2*n*Log[n - n/(2*Log[n])] + (-1 + 2*n)*Log[1 + n - 2*Log[n]] - 2*n*Log[Log[n]] + (1/Log[n])*n*(1 + Log[4] + Log[n - n/(2*Log[n])] - (1 + n)*Log[1 + n - 2*Log[n]] + 2*Log[Log[n]]) - Log[(Pi*(-1 + 2*Log[n]))/(2*Log[n]^2)]);


I wanted to determine the asymptotic expansion in order of the weight of each term. Unfortunately, Asymptotic command is only reasonably usable up to level 1.

 Asymptotic[ex, n -> Infinity]
(* 2 n Log[n] *)


If I add SeriesTermGoal -> 2, the result is completely unusable.

 Asymptotic[ex, n -> Infinity, SeriesTermGoal -> 2]


The problem can be easily solved by the following procedure with repeated use of the Asymptotic as we successively subtract partial results.

 asylog[f_, steps_] := (asyp = 0; Do[asyp += Asymptotic[f - asyp, n -> Infinity];, {j, 1, steps}]; asyp);


But Mathematica rearranges the result into a form where the new order of the terms no longer corresponds to their weights.

 asylog[ex, 4]
(* -n - 1/2 n Log[4] + 2 n Log[n] - n Log[Log[n]] + (n Log[Log[n]])/Log[n] *)


I can work around this by storing the partial results in an auxiliary field where they are in the correct order (as appropriate for publishing).

 asylog2[f_, steps_] := (asyp = 0; asypub = {};
Do[asyp1 = Asymptotic[f - asyp, n -> Infinity];
asypub = Join[asypub, {asyp1}]; asyp += asyp1;, {j, 1, steps}];
asypub);

asylog2[ex, 4]
(* {2 n Log[n], -n Log[Log[n]], -n - 1/2 n Log[4], (n Log[Log[n]])/Log[n]} *)


Unfortunately, if I use Total[%] for the previous list, the terms order is again 3124 instead of the desired 1234.

 Total[%]
(* -n - 1/2 n Log[4] + 2 n Log[n] - n Log[Log[n]] + (n Log[Log[n]])/Log[n] *)


The question is: how to get the result with the expressions in the right order ? And ideally can't it directly do the Asymptotic command with some parameter ?

I can only answer the title of your question regarding the preservation of order, for which there are already several questions on this site. Working upon the answer from @march, you can use the following:

sumTerms[res_] :=
Block[{Plus},
DisplayForm@
ToBoxes[Plus @@ res] //. {a___, "+",
RowBox[{(RowBox[{"-", x___}] | "-"), b___}], c___} :> {a,
RowBox[{"-\[ThinSpace]", x, b}], c}
]

terms = asylog2[ex, 8];
sumTerms[terms]


• So the code is very unreadable, but it solves exactly what I wanted. Thank you! Commented Mar 13, 2023 at 13:35

First you might simplify your code using NestList

erg = NestList[# + Asymptotic[ex - # , n -> Infinity] &,Asymptotic[ex, n -> Infinity] , 4]

(*{2 n Log[n], 2 n Log[n] - n Log[Log[n]],
-n - 1/2 n Log[4] + 2 n Log[n] -n Log[Log[n]], -n - 1/2 n Log[4] + 2 n Log[n] - n Log[Log[n]] + (n Log[Log[n]])/Log[n],
-n - 1/2 n Log[4] + n/(2 Log[n]) + (n Log[4])/(2 Log[n]) + 2 nLog[n] - n Log[Log[n]] + (n Log[Log[n]])/Log[n]}
*)


which gives a list of asymptotic approximations.

Assuming an orderd list we now filter out successively the leading terms

fn[0] = 0;
Do[fn[k] = (erg - fn[k - 1])[[k]], {k, 1, 4}]
res=Table[fn[k], {k, 1, 4}]
(*{2 n Log[n], -n Log[Log[n]], -n - 1/2 n Log[4] +2 n Log[n], -n Log[Log[n]] + (n Log[Log[n]])/Log[n]}*)


Accumulation of the result res gives the list erg of asymptodes!

final solution

asy2[expr_, iter_ : 3] :=NestList[{Asymptotic[expr - #[[2 ]], n ->Infinity], #[[2 ]] +Asymptotic[expr - #[[2 ]] , n -> Infinity]} &, {Asymptotic[expr, n -> Infinity], Asymptotic[expr, n -> Infinity]} , iter]


Function gives a list containing {{a[k+1]-a[k], a[k+1]},...}' First entry shows the change between sukcessive iterations and gives the ordered list of asymptotic approximation!

asy2[ex, 3][[All, 1]]
(*{2 n Log[n], -n Log[Log[n]], -n - 1/2 n Log[4], (n Log[Log[n]])/Log[n]}*)
`
• Thanks, but again this requires a bit of manual work if I want to publish or move the result somewhere. Code by "Domen" gives a result that can be directly copied without modification. Commented Mar 13, 2023 at 13:41
• You only have to define max order you are interested in . Commented Mar 13, 2023 at 14:15