Re: Rearranging terms - user-defined

*To*: mathgroup at smc.vnet.net*Subject*: [mg127111] Re: Rearranging terms - user-defined*From*: "djmpark" <djmpark at comcast.net>*Date*: Sun, 1 Jul 2012 02:06:07 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <1953698.53615.1341048723591.JavaMail.root@m06>

Mathematica does a number of automatic rearrangements of expressions and when using Simplify uses canonical forms so the simplifications will actually take place. This is good for computation but often makes it difficult to obtain desired display forms. There are things you can do about it. One technique is to use TraditionalForm, which often helps - but not with your examples. Your first case is rather straight forward. expr1 = a + b x - b y; Collect[expr1, {a, b}] a + b (x - y) Simplify[expr1] a + b (x - y) In general, Simplify is difficult to control if you want a specific form of the expression. Sometimes, in expressions, you want a sub-expression to be treated as a unit and not split apart or rearranged when using various Mathematica operations. The standard method for doing this is to use HoldForm. So you might use: oneMinusp = HoldForm[1 - p]; -oneMinusp - (1 - p) And in various operations the (1 - p) is kept together. Such as: (a - 5 - p oneMinusp)/a; Apart[%] (-5 + a)/a - (p (1 - p))/a The same method can be used to keep x-y together in your first expression. (expr1 // Simplify) /. x - y -> HoldForm[x - y] The problem with using HoldForm is that if you want to use Mathematica routines that access the held variables, such as numerical routines or Integrate, then you have to remember that HoldForm was in the expression and release it. Sometimes the expressions are not so easy to manipulate. The general technique is to hold subexpressions and/or to operate on only selected parts of an expression. The Presentations Application has a number of routines that are useful in manipulating expressions to particular forms. Some of these operations are: CompleteTheSquare, FactorOut, FactorMinusOne, AddZero, MultiplyByOne, LinearBreakout, PushOnto, CreateSybexpression, ReleaseSubexpressions, OperateSubexpressions, HoldOp, EvaluateAt, EvaluateAtPattern, MapLevelParts, MapLevelPatterns. For example, the following factors b out of the last two terms (even though b is not a perfect factor) and makes the factored result a protected Subexpression with a Tooltip that indicates it is a held expression. expr3 = a + b x + y; expr3 // MapLevelPatterns[ MapAt[CreateSubexpression, FactorOut[b][#], 2] &, {{_. (x | y)}}] a + b (x + y/b) where the (x + y/b) is a Subexpression with "held" as the Tooltip. I believe that native Mathematica lacks the capability of applying some function to a selected subset of level parts - except perhaps by using a Rule. (It always applies the function to each part individually.) MapLevelParts and MapLevelPatterns overcomes that limitation. David Park djmpark at comcast.net http://home.comcast.net/~djmpark/index.html From: drehstuhl2 at googlemail.com [mailto:drehstuhl2 at googlemail.com] Does anyone know how to tell mathematica the following: I have an expression like: a + bx -by and I want to factorise it like: a + b(x-y) BUT: I want Mathematica to show the difference (x-y) whenever possible (in the different calculations - not just place b outside the bracket). That's because equations are easier to discuss if they are written in a special way. Another example would be: using probabilities, I always want to write mathematica -(1-p) instead of -p+1 throughout the notebook. Sorry, for postig such a beginners question. I know there were similar questions, but it did't help. Thanks a lot!