1

enter image description here

Simplify[(1 - a) (1 - b)]
(-1 + a) (-1 + b)

How to modify the order of symbols to make the result more logical. (1-a)(1-b)

  • The question arises: what for? – user64494 Nov 23 '23 at 15:40
  • 1
    TraditionalForm[(-1+a)(-1+b)] returns (a-1)(b-1) but be very careful if you try to use the output of any *Form function. *Form functions are intended to make something pretty to look at, not something for further calculations. MatrixForm may be pretty to look at but mymat=MatrixForm[...];Solve[mymat==...] may fail. – Bill Nov 23 '23 at 16:20
  • (-1 + a) (-1 + b) seems more logical (one multiplication instead of three). (Is (1-a)(1-b) the more logical output you want? It's just there, not in a sentence.) – Goofy Nov 23 '23 at 22:25

2 Answers2

2

The function "rearrange"

The function rearrange[expr,listOfFinalPositions] rewrites a sum in the order prescribed by the list entitled "listOfFinalPositions."

Arguments:

expr is the expression with the head Plus.

listOfFinalPositions is a list. It is important that its length must be equal to the length of the expression. It indicates the positions that the terms of the sum must take in the end. For example, for the expression a+b+c the listOfFinalPositions {2,3,1} means that a must go 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 obtained expression further one needs to apply ReleaseHold

rearrange[expr_, finalPositions_List] := Module[{lst, newlst},
   lst = List @@ expr;
   newlst = 
    Table[lst[[Position[finalPositions, i][[1, 1]]]], {i, 1, Length[lst]}];
   HoldForm[Evaluate[expr]] /. MapThread[Rule, {lst, newlst}]
   ];

Examples

Example 1:

Rearrange the sum

Clear[expr];
expr = a + b + c + d;

such that a stays on the second place, b- on the fourth, c - on the third and d - on the first one.

rearrange[expr, {2, 4, 3, 1}]

enter image description here

Example 2: transform (-a-b) (c-d) into (a+b) (d-c)

Clear[expr1, expr2, expr3];
expr1 = (-a - b) (c - d);
expr2 = MapAt[(-1)*HoldForm[Evaluate[(-1)*#]] &, expr1, {{1}, {2}}] //ReleaseHold;
expr3 = MapAt[rearrange[#, {2, 1}] &, expr2, {2}]

enter image description here

Have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
0

The warning by @Bill in a comment about *Form functions is well made.

The following works, but it appears that PolymonialForm is still undocumented?

PolynomialForm[Simplify[(1-a)(1-b)],TraditionalOrder->True]

(* (a-1) (b-1) *)

user1066
  • 17,923
  • 3
  • 31
  • 49