3
$\begingroup$

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.

$\endgroup$
5
  • 1
    $\begingroup$ 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. $\endgroup$
    – kcr
    Jan 17 at 3:56
  • 2
    $\begingroup$ @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. $\endgroup$
    – Kagaratsch
    Jan 17 at 4:09
  • $\begingroup$ 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.... $\endgroup$
    – kcr
    Jan 17 at 4:14
  • 1
    $\begingroup$ 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. $\endgroup$
    – kcr
    Jan 17 at 4:18
  • $\begingroup$ Similar to 95993. Try: q^2 Collect[expr2/q^2, q] where expr2 = a^2 q^2 + q^4 (1 + c)^2. $\endgroup$
    – Syed
    Jan 17 at 7:39

3 Answers 3

7
$\begingroup$

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!

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – Kagaratsch
    Jan 17 at 17:13
  • $\begingroup$ OK, have success in this hopeless matter. $\endgroup$ Jan 17 at 19:04
  • $\begingroup$ 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. $\endgroup$
    – Kagaratsch
    Jan 17 at 20:24
  • 1
    $\begingroup$ @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. $\endgroup$ Jan 18 at 3:05
  • 1
    $\begingroup$ @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. $\endgroup$ Jan 19 at 12:17
5
$\begingroup$

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) *)
$\endgroup$
1
$\begingroup$

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)$$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.