How can I remove the negative sign to make sure the positive-signed term comes first?

I found the default dealing of the terms often display a negative sign at the beginning, including those beneath a square-root sign , which is a little bit annoying since for most cases, there exists at least one positive-signed term in the result and the negative sign can be adjusted by directly being moved backward after the positive-signed term or being multiplied into one difference term, swapping the two numbers in the difference term and thus avoiding a negative sign at the beginning. How can I remove the negative sign to make sure the term with positive sign comes first(if there is any)?

Example1:
-(a+b)(c-d)

Current result:
(-a - b) (c - d)

What I want:

(a+b)(d-c)

Example2:
Integrate[2 g y Sqrt[2 r y - y^2] \[Rho], {y, 0, h}, Assumptions -> (r | h | g) \[Element] PositiveReals && h <= 2 r] // FullSimply

Current Result:
ConditionalExpression[ 1/3 g \[Rho] (2 Sqrt[-h^5 (h - 2 r)] - r (Sqrt[-h^3 (h - 2 r)] + 3 Sqrt[-h (h - 2 r)] r) + 6 r^3 ArcCsc[Sqrt Sqrt[r/h]]), h < 2 r]
i.e.
$$\frac{1}{3} g \rho \left(2 \sqrt{-h^5 (h-2 r)}-r \left(\sqrt{-h^3 (h-2 r)}+3 r \sqrt{-h (h-2 r)}\right)+6 r^3 \text{arccsc}\left(\sqrt{2} \sqrt{\frac{r}{h}}\right)\right)\text{ if }h<2 r$$

The result I want:
ConditionalExpression[ 1/3 g \[Rho] (2 Sqrt[h^5 (2 r-h)] - r (Sqrt[h^3 (2 r-h)] + 3 Sqrt[h (2 r-h)] r) + 6 r^3 ArcCsc[Sqrt Sqrt[r/h]]), h < 2 r]
i.e.
$$\frac{1}{3} g \rho \left(2 \sqrt{h^5 (2 r-h)}-r \left(\sqrt{h^3 (2 r-h)}+3 r \sqrt{h (2 r-h)}\right)+6 r^3 \text{arccsc}\left(\sqrt{2} \sqrt{\frac{r}{h}}\right)\right)\text{ if }h<2 r$$

Example 3:

Integrate[Sin[x]^2, {x, a, b}]

Current result:
1/2 (-a + b + Cos[a] Sin[a] - Cos[b] Sin[b])

What I want:

1/2 (b -a + Cos[a] Sin[a] - Cos[b] Sin[b])

Example 4:
D[1/2 Log[1 - x] - 1/2 Log[1 + x], x] // Simplify

Current result:
1/(-1 + x^2)

What I want:

1/(x^2 - 1)

Example 5:
Integrate[1/(x^4 - a^4), x]

Current result:
Log[a - x]/(4 a^3)-(ArcTan[x/a]/(2 a^3)) - Log[a + x]/(4 a^3)

• You can always try applying Simplify to the resulting expression, but note that Mathematica outputs expressions in default forms. For instance, if you evaluate b + a, it will output as a + b, and there's no way around this unless you apply functions like HoldForm to the expression: HoldForm[a + b], but then you can't do anything with it afterwards until you apply Releasehold. Sep 4 '21 at 23:07
• Why is this important anyway? It's something you can get used to, and it's probably not worth the trouble to force Mathematica to do those kinds of things in general! Sep 4 '21 at 23:10
• Expressions involving trigonometric terms may change sign every period; e.g. FunctionPeriod[Sin[x], x], so rearranging terms of a trig expr is not useful.
– Syed
Sep 5 '21 at 5:18
• @march. I think it meaningful because I feel after rearranging the terms that way, it will be more concise, convenient and aesthetic for potential readers to review the result. Just like the Example2, even after I explicitly made the assumption 0<h<2r, the system still gives the result in terms like $\sqrt{-h^5 (h - 2 r)}$, it might be correct, but compared with $\sqrt{h^5 (2 r-h)}$ , which one do you think is clearer and more convenient and aesthetic to lay your eyes on? Sep 8 '21 at 4:28
• @march I understand what you meant by “getting used to”, currently I am rearranging the terms manually. But what worries me is results needing adjusting might get more and more in the future, at that time the workload might be very heavy, so I’d better seeking a way to do it automatically. Sep 8 '21 at 4:29

Achieving this task is not at all trivial, since Mathematica has its internal order. It forces this order to all results in the OutputForm. As @march has already noticed the rearrangement often requires using such operations as those of the Hold group. The latter, however, prevents the expression from the possibility to make further transformations.

For this reason, if you are going to make some further Mathematica transformations with your result I recommend leaving it as it is, that is, in the order generated by Mathematica.

Nevertheless, the rearrangement of the result is an important task. To address the question asked by @march, the important reason to rearrange the results is the necessity to look at the result written in a convenient form. This often helps its better understanding and getting ideas of what to do with it during possible further calculations. For this reason, I often do such rearrangements after finalizing my calculations, or at least, after finalizing the current step of the calculations.

To do such rearrangements I have written a few functions two of which I share below with the explanations. After that, I address your examples to show how these functions work.

Rearrange terms in a sum

The function rearrange[expr,listOfFinalPositions] rewrites a sum of several terms in the order prescribed by the list entitled "listOfFinalPositions." Arguments: expr is the expression with the head Plus. listOfFinalPositions is a list. Its length is equal to the length of the expression (Important). It indicates the positions that the terms of the sum must take in the end.

For example, for the sum a+b+c the listOfFinalPositions {2,3,1} means that a must go to the second position, b - to the third, and c - to the first one resulting in c+a+b.

The function wraps the result by the HoldForm function to forbid an undesired reordering. Therefore, to use the resulting expression further one needs to first apply ReleaseHold to it.

rearrange[expr_, finalPositions_List] := Module[{lst, newlst},
lst = List @@ expr;
newlst =
Table[lst[[Position[finalPositions, i][[1, 1]]]], {i, 1, Length[lst]}];
];

factorMinus

The function factorMinus takes the sign "-" out of the expression expr, and (if needed) applies a function fun to -expr. By default fun is Identity.

The function wraps the result by the HoldForm function to forbid an undesired reordering. Therefore, to use the resulting expression further one needs to first apply ReleaseHold to it.

factorMinus[expr_, fun_ : Identity] := (-1)*HoldForm[Evaluate[fun[(-1)*expr]]]

Examples of applications

Here I demonstrate the way to apply these two functions to the expressions you gave in the examples to your question

Example 1

Clear[expr1, expr2, expr3, expr4];
expr1 = (-a - b) (c - d)

(*  (-a - b) (c - d)  *)

expr2 = MapAt[factorMinus, expr1, {{1}, {2}}] // ReleaseHold

(*  (a + b) (-c + d)  *)

expr3 = MapAt[rearrange[#, {2, 1}] &, expr2, {2}] Done. The last result here I show as an image. It is because it is wrapped by HoldForm which uses Boxes when showing it here. The latter prevents from clearly seeing it. However, if one applies the ReleaseHold to this final result Mathematica immediately rearranges it according to its internal order which is not desired. However, if you plan to further use this result in your calculations, do not forget to apply the ReleaseHold first.

Example 2

Clear[expr1, expr2, expr3, expr4]

expr1 = ConditionalExpression[
1/3 g \[Rho] (2 Sqrt[-h^5 (h - 2 r)] -
r (Sqrt[-h^3 (h - 2 r)] + 3 Sqrt[-h (h - 2 r)] r) +
6 r^3 ArcCsc[Sqrt Sqrt[r/h]]), h < 2 r]

Let us first get rid of the ConditionalExpression

expr2 = Simplify[expr1, h < 2 r]

(*  1/3 g \[Rho] (2 Sqrt[-h^5 (h - 2 r)] -
r (Sqrt[-h^3 (h - 2 r)] + 3 Sqrt[-h (h - 2 r)] r) +
6 r^3 ArcCsc[Sqrt Sqrt[r/h]])  *)

expr3 = MapAt[factorMinus[#] &,
expr2, {{4, 1, 2, 1, 3}, {4, 2, 3, 1, 1, 3}, {4, 2, 3, 2, 2, 1,
3}}] // ReleaseHold

(* 1/3 g \[Rho] (2 Sqrt[h^5 (-h + 2 r)] -
r (3 r Sqrt[h (-h + 2 r)] + Sqrt[h^3 (-h + 2 r)]) +
6 r^3 ArcCsc[Sqrt Sqrt[r/h]]) *)

expr4 = MapAt[rearrange[#, {2, 1}] &,
expr3, {{4, 1, 2, 1, 2}, {4, 2, 3, 1, 3, 1, 2}, {4, 2, 3, 2, 1, 2}}] Done. This expression is again held.

Example 3

Clear[expr1];

expr1 = 1/2 (-a + b + Cos[a] Sin[a] - Cos[b] Sin[b])

1/2 (-a + b + Cos[a] Sin[a] - Cos[b] Sin[b])

MapAt[rearrange[#, {2, 1, 3, 4}] &, expr1, {2}] Example 4

Clear[expr1];
expr1 = 1/(-1 + x^2)

(*  1/(-1 + x^2)  *)

MapAt[rearrange[#, {2, 1}] &, expr1, {1}] Example 5

Clear[expr1];
expr1 = Log[a - x]/(4 a^3) - (ArcTan[x/a]/(2 a^3)) - Log[a + x]/(4 a^3)

(*   -(ArcTan[x/a]/(2 a^3)) + Log[a - x]/(4 a^3) - Log[a + x]/(4 a^3)   *)

rearrange[expr1, {3, 1, 2}] Have fun!

• Thanks, I am happy to read your code and learn from it. Although at first glance it seems a little complicated, but reading through it with your explanation and exhibition is fun. Thanks again and have a nice day! Sep 8 '21 at 5:32
• @AlbertLew Yes, the functions and the way I apply them may cause questions. Do not hesitate to ask. Sep 8 '21 at 11:15