I'm looking for a way to factor a polynomial recursively. Consider the following example:
expr = Expand[(a + (b + c)^2)^2];
Factor@expr
Simplify@expr
While the last Simplify
statement gives back the original form (a + (b + c)^2)^2
, Factor
only factors the outer level and gives (a + b^2 + 2 b c + c^2)^2
. Is there a way to factor recursively such as to produce the same output as after Simplify
?
EDIT The previous statement of the question might not be completely right, and as Daniel pointed out in his comment, I'm looking for a way to factorise polynomials for which only parts factorise.
I just wrote a very crude function in Mathematica that does the job only for the simplest examples (the third example below is already not working), but it's in fact even slower than Simplify
:
myFactor[s_Plus] := With[{l = List @@ s},
attempts = (Factor@*Plus @@ # +
Factor@*Plus @@ Complement[l, #] &) /@
Subsets[l, Floor[Length[l]/2]];
DeleteDuplicates@MinimalBy[ByteCount]@attempts
]
expr1 = a + (b + c)^2 // Expand
expr2 = a + (b + c) (d + e) // Expand
expr3 = (a + b)^2 + (c + d)^3 + e + f + 2 d + 3 a // Expand
(* gives *)
(* a + b^2 + 2 b c + c^2 *)
(* a + b d + c d + b e + c e *)
(* 3 a + a^2 + 2 a b + b^2 + c^3 + 2 d + 3 c^2 d + 3 c d^2 + d^3 + e + f *)
myFactor /@ {expr1, expr2, expr3} // ColumnForm
(* gives *)
(* {a + (b + c)^2} *)
(* {a + (b + c) (d + e)} *)
(* {3 a + a^2 + 2 a b + b^2 + 2 d + (c + d)^3 + e + f} *)
This is a very simple approach where I just split the sum into two parts, try to factorise those, and then sum again. For the last example to work one would probably need something recursive, but given that the performance doesn't seem great, I'm wondering if this approach is worth exploring at all. The only reason I want to avoid Simplify
is for performance reasons, as I assume it's going to try a lot of different simplification strategies, which I know I don't want to apply.
(a + (b + c)^2)
is not a factored exporession. I mention this to indicate that the problem you are trying to solve is probably much harder than the problem you state (since e.g.Map
can be used to effect factorization at multiple levels). $\endgroup$Simplify
. $\endgroup$