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)
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 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}]
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}]
Have fun!
TraditionalForm[(-1+a)(-1+b)]returns(a-1)(b-1)but be very careful if you try to use the output of any*Formfunction.*Formfunctions are intended to make something pretty to look at, not something for further calculations.MatrixFormmay be pretty to look at butmymat=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