# How to make FullSimplify always factor out terms?

Consider the following two FullSimplify examples:

a^2 q^2 + q^4 c^2 // FullSimplify


q^2 (a^2 + c^2 q^2)

and

a^2 q^2 + q^4 (1 + c)^2 // FullSimplify


a^2 q^2 + (1 + c)^2 q^4

I would like the output of the second example to factor out the q^2 just like in the first example. Is there a way to make FullSimplify do that?

Note that the examples above are very simple just to illustrate the point. Ideally, I would like to apply the simplification to very large (rational) expressions that cannot be inspected visually.

• Am I correct to assume that you don't want to use Factor for specific reasons? I am mentioning this because a^2 q^2 + q^4 (1 + c)^2 // Factor factors the desired q^2 term.
– kcr
Jan 17 at 3:56
• @DiSp0sablE_H3r0 That's right, Factor is very limited. Even in this example, even though it factors out q^2 it actually unfolds all other terms, which makes the expression less simplified overall. Jan 17 at 4:09
• Did you try using the ExcludedForms options of FullSimplify? For example if you consider q^2 an excluded form that is not to be touched....
– kcr
Jan 17 at 4:14
• And another comment, in this particular example the following a^2 q^2 + q^4 (1 + c)^2 // Factor // FullSimplify gives the nice q^2 (a^2 + (1 + c)^2 q^2) which is of the form you're after. Not sure how much more complicated examples you want to consider.
– kcr
Jan 17 at 4:18
• Similar to 95993. Try: q^2 Collect[expr2/q^2, q] where expr2 = a^2 q^2 + q^4 (1 + c)^2.
– Syed
Jan 17 at 7:39

One way of doing this is to apply the following function:

factor[expr_, fact_, funExpr_ : Expand, funFact_ : Identity] :=
Module[{a = fact, b = expr/fact},
funFact[Evaluate[a]]*funExpr[Evaluate[b]]];


Here expr is the expression you intend to factor, fact is the term you intend to take out of parentheses. optionally you can apply a function funExpr to the expression left within the parentheses and the function funFact to the factor. In the case of your example, the straightforward application yields

factor[a^2 q^2 + q^4 (1 + c)^2, q^2]

(*  q^2 (a^2 + q^2 + 2 c q^2 + c^2 q^2)   *)


However, if we need to have the content of the parentheses in the compact form we apply the function Simplify to the parentheses content:

factor[a^2 q^2 + q^4 (1 + c)^2, q^2, Simplify]

(*  q^2 (a^2 + (1 + c)^2 q^2)  *)


Have fun!

• That's an interesting approach! However, it narrows down the type of objects to very simple ones (polynomials) which one can inspect visually. Ideally, I am looking for a solution that would work on messy rational expressions that are hundreds of pages long and cannot be inspected visually, to reduce them to a readable output. Jan 17 at 17:13
• OK, have success in this hopeless matter. Jan 17 at 19:04
• I suspect that there is a certain tweak to the FullSimplify complexity function one can make to achieve this. It may be not as hopeless as it seems, but we for sure have to wait for someone who knows a lot about this stuff. Jan 17 at 20:24
• @Kagaratsch ComplexityFunction -> (LeafCount[#] + Total[Cases[#, _^x_Integer -> x, Infinity]] &) works for your original example. The Total[Cases[...]] can be modified to favor more smaller powers or fewer larger powers as needed. Also, a scalar multiplier can be added to weigh it against the LeafCount. Jan 18 at 3:05
• @Kagaratsch By the way, in some cases, mapping of the functions like Factor, of Simplify onto expression may help. Like this: Map[Factor, expr]. It will try to take a common factor out of the parentheses in each term of your long-expression separately. Also this: Map[Simplify[Factor[#]]&, expr] may sometimes occur useful. Jan 19 at 12:17

In many cases, a simple solution is to apply FullSimplify to the factors of your expression, rather than the full expression itself. This is very easily done:

FullSimplify /@ Factor[a^2 q^2 + q^4 (1 + c)^2]
(* q^2 (a^2 + (1 + c)^2 q^2) *)


Modifying the approach given here by IPoiler

q^2 FullSimplify[(a^2 q^2 + q^4 (1 + c)^2)/q^2]


$$q^2\left(a^2+(c+1)^2q^2\right)$$

q^2 FullSimplify[(a^2 q^2 + q^4 c^2)/q^2]


$$q^2\left(a^2+c^2q^2\right)$$