# Shaping/simplifying equations in a certain way

A problem I am occasionally facing is to simplify an equation not to it's shortest form but to a form that is simple by other means. Often, this is grouping the term according to certain functions, for example exponential functions from a Fourier series.

For example,

1 Exp[i k t] + 2 x Exp[i k t] + (2 x + 1) Exp[i k t]^2 // Simplify


will give E^(i k t) (1 + E^(i k t)) (1 + 2 x). Instead,

(2 x + 1) Exp[i k t] + (2 x + 1) Exp[i 2 k t]


is often desired.

Is there any simple[1] way to achieve that?

[1] I once achieved that using the FourierTransform and replacing the DiracDelta with 1 or 0 to get the coefficients, but that is neither elegant nor always possible.

-
The ComplexityFunction option of simplify is the way to go, but constructing the relevant function may not be straightforward –  acl May 23 '12 at 12:12
Although it does not seem to be the way to go, this was an interesting thing to learn. Thanks –  mcandril May 23 '12 at 12:51
I edited the answer to include the usage of Simplify in this case. You would rather use ExcludedForms more than ComplexityFunction. –  Artes May 23 '12 at 16:41

There is no need to play around with Simplify, since to achive what you need one can use Collect, e.g.

expr = Exp[i k t] + 2 x Exp[i k t] + (2 x + 1) Exp[i k t]^2;
Collect[expr, Exp[i k t]]

E^(i k t) (1 + 2 x) + E^(2 i k t) (1 + 2 x)


If there are more variables you can use a list of them as the second argument, look also at Simplify as the third argument in Collect. As J.M. pointed out one could use also PolynomialForm, to transform the expression into a more expected form, e.g.

Collect[expr, {Exp[i k t], x}] // PolynomialForm[#, TraditionalOrder -> True] &

 E^(i k t)(2 x + 1) + E^(2 i k t)(2 x + 1)


Although it seems that Collect is more appropriate for your task than Simplify, you can still take advantage of the latter if you make use of options like ExcludedForms to get what you would like, e.g.

Simplify[expr, ExcludedForms -> Exp[_]]

(E^(i k t) + E^(2 i k t)) (1 + 2 x)


or if you prefer an expanded form

Expand[%, E^(i k t)]

E^(i k t) (1 + 2 x) + E^(2 i k t) (1 + 2 x)


For the sake of completeness there is also Apart available (it seems the simplest way) :

Apart[expr]

E^(i k t) (1 + 2 x) + E^(2 i k t) (1 + 2 x)


in more general cases, there is also the second argument, e.g. Apart[expr, Exp[i k t]] returns the result as above.

To sum up there is no way to decide what is the best method, since all of them have their advantages, but as said before I suggest to use Collect.

-
...and if you need to display the coefficients "properly": Collect[1 Exp[i k t] + 2 x Exp[i k t] + (2 x + 1) Exp[i k t]^2, Exp[i k t], PolynomialForm[Simplify[#], TraditionalOrder -> True] &] –  Guess who it is. May 23 '12 at 12:20
@J.M. This is a good complement ! –  Artes May 23 '12 at 12:24
First, thanks a lot, I got it! However, and that may sound stupid: The only option in my documentation is Modulus? o_O Whats the "Simplify option?" Additionally as another complement, that was not evident to me: Collect[expr,Exp[q_]] also works. This is very important for mè because due to the complexity, I do not know x in Collect[expr,x]. Why is Collect not mentioned in the manual page for Coefficients, for example? –  mcandril May 23 '12 at 12:44
What is your Mathematica version ? There is in Basic Examples e.g. Collect[(1 + a + x)^4, x, Simplify] at least in ver.7 and 8. –  Artes May 23 '12 at 12:48
Oh :) I just looked at "Options" :) Thanks again that made a huge difference again. –  mcandril May 23 '12 at 12:49

The original question may be considered from a more general perspective. Namely, the analytical expression may be more complex than the expression:

expr1 = Exp[i k t] + 2 x Exp[i k t] + (2 x + 1) Exp[i k t]^2;


discussed above, and the transformation may concern not the coefficients of the expression expr1 (e.g. the level 1), but may be located on deeper levels. In addition one may wish not to rely on automatic simplification, but to transform the expression on the screen the same way one might do this by hands. This represents advantages with respect to doing the same by hands, since it enormously accelerates the process and is much less prone to errors. Which again accelerates the process.

For such cases Mathematica unfortunately lacks few built-in functions that would enable one to easily make transformations. One finds, however, such functions in the Presentation package of David Park, in its part entitled "Manipulations".

However, also without this package one can achieve the same goal, the penalty is a somewhat longer code. Though the present example admits simpler solutions already shown above, I will show the way using which I usually achieve custom transformations in more complex cases. The general idea is to first turn the expression into a list of subexpressions down to the level needed to be customly transformed (in this case it is already the level 1):

expr2 = List @@ expr
(* {E^(i k t), 2 E^(i k t) x, E^(2 i k t) (1 + 2 x)} *)


and then operate with the elements of the list, which is to extent easier, and assemble it back into the final expression:

expr3 = (Plus @@ Drop[expr2, -1] // Factor) + (expr2 // Last)
(* E^(i k t) (1 + 2 x) + E^(2 i k t) (1 + 2 x) *)

% // Factor
(* E^(i k t) (1 + E^(i k t)) (1 + 2 x)  *)

-