MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Rearranging terms - user-defined

  • To: mathgroup at
  • Subject: [mg127111] Re: Rearranging terms - user-defined
  • From: "djmpark" <djmpark at>
  • Date: Sun, 1 Jul 2012 02:06:07 -0400 (EDT)
  • Delivered-to:
  • 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

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) 

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]; 

- (1 - p)

And in various operations the (1 - p) is kept together. Such as:

(a - 5 - p oneMinusp)/a;
(-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,

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 

From: drehstuhl2 at [mailto:drehstuhl2 at] 

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

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!

  • Prev by Date: Re: Approximate Zero Times A Symbol
  • Next by Date: Re: Varying a constant in an ODE to Manipulate solution
  • Previous by thread: Re: Rearranging terms - user-defined
  • Next by thread: Re: Rearranging terms - user-defined