Re: (x - 1) or (-1 + x)
- To: mathgroup at smc.vnet.net
- Subject: [mg121557] Re: (x - 1) or (-1 + x)
- From: "David Park" <djmpark at comcast.net>
- Date: Mon, 19 Sep 2011 07:07:38 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <3941015.158363.1316331430213.JavaMail.root@m06>
This is a perennial problem, which occurs because Mathematica puts expressions in a canonical form in order to perform simplifications. Nevertheless, we will often wish to put expressions in a "nice" form for readers or to compare with textbook forms. This can be done by doing "surgery on expressions". Let's use two slightly more complicated expressions for which simple measures do not work well. expr1 = -4 a (-1 + q); expr2 = -4 a/(-1 + q); TraditionalForm does not always work. The following leaves us with one minus sign. expr1 // TraditionalForm -4 a (q-1) One strategy is to map Minus on two of the factors in the expression using MapAt. (One slight downside of these methods is that you have to find the positions of the factors.) MapAt[Minus, expr1, {{1}, {3}}] 4 a (1 - q) MapAt[Minus, expr2, {{1}, {3, 1}}] (4 a)/(1 - q) Another strategy is to factor -1 out of the complicated factor, Hold the quotient, let the -1 combine with the remaining factors and ReleaseHold. The Presentations application has a FactorOut routine that allows us to do this. << Presentations` MapAt[FactorOut[-1, Identity, Hold], expr1, 3] // ReleaseHold 4 a (1 - q) MapAt[FactorOut[-1, Identity, Hold], expr2, {3, 1}] // ReleaseHold (4 a)/(1 - q) We could write a routine to automate this. (I'll probably add this to the next Presentations.) FactorMinusOne::usage = "FactorMinusOne[position][expr] will pull -1 out of the \ subexpression at position in expr and Hold the quotient until the -1 \ combines with the remaining factors."; SyntaxInformation[FactorMinusOne] = {"ArgumentsPattern" -> {_}}; FactorMinusOne[position_][expr_] := MapAt[FactorOut[-1, Identity, Hold], expr, {position}] // ReleaseHold expr1 // FactorMinusOne[3] 4 a (1 - q) expr2 // FactorMinusOne[{3, 1}] (4 a)/(1 - q) Or we could let Mathematica find the position (if there is only one): expr2 // FactorMinusOne @@ Position[expr2, q - 1] (4 a)/(1 - q) David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ From: Ralph Dratman [mailto:ralph.dratman at gmail.com] I feel like a small child asking this, but how can I get Mathematica to say (x - 1) instead of (-1 + x), or to give me 4 (1 - q) instead of the doubly annoying -4 (-1 + q) ? In brief, given the choice, I prefer to look at subtraction rather than negation. The binary operator instead of the unary. I'm not asking Mathematica to read my mind about the way I want every possible expression to look, rather I am hoping to find out how to transform something I don't like into something I like, in a way that is guaranteed not to change the value of the expression. Ralph Dratman