New answers tagged factorization
1
fullFactor[f_, x_] := Roots[f == 0, x] /. Equal -> ((#1 - #2) &) /. Or -> Times
fullFactor[x^5 - 1, x]
(* (-1 + x) ((-1)^(1/5) + x) (-(-1)^(2/5) + x) ((-1)^(3/5) + x) (-(-1)^(4/5) + x) *)
3
A function from the article that cormullion linked is shorter and faster than what I proposed below. Transcribed in terse style:
uf[m_, 1] := {{}}
uf[1, n_] := {{}}
uf[m_, n_?PrimeQ] := If[m < n, {}, {{n}}]
uf[m_, n_] := uf[m, n] =
Join @@ Table[Prepend[#, d] & /@ uf[d, n/d], {d, Select[Rest@Divisors@n, # <= m &]}]
uf[n_] := uf[n, n]
...
15
The engine behind this inside Compile is a well-hidden function called
OptimizeExpression. it has two levels, 1 and 2. Setting to 2 makes it work harder to find CSEs.
e1 = (G u^2 (6 p (2 h + p) - 8 (h + p) u + 3 u^2))/(12 h^2);
e2 = (G (3 h + 3 p - 2 u) u^2)/(3 h^2);
Experimental`OptimizeExpression[{e1, e2},
OptimizationLevel -> 2]
(* Out[40]= ...
5
Level may provide a means of getting started. Level breaks down your polynomials into their constituent parts, with various "levels" of complexity. As @Rex Kerr noted in a comment, Level 1 happens to be interesting in the present example.
a=(G u^2 (6 p (2 h+p)-8 (h+p) u+3 u^2))/(12 h^2);
b=(G (3 h+3 p-2 u) u^2)/(3 h^2);
...
6
Since you are going to work with numeric functions, Compile will optimize your functions along those lines.
If you define :
f = Compile[{{g, _Real}, {u, _Real}, {h, _Real}, {p, _Real}},
{(g u^2 (6 p (2 h + p) - 8 (h + p) u + 3 u^2))/(12 h^2),
(g (3 h + 3 p - 2 u) u^2)/(3 h^2)}
]
you can already see some of it in the output.
To get an even more in ...
Top 50 recent answers are included
