Why is Mathematica's expansion of expressions with square roots so slow?
Looks like a computation that rightfully should be slow, you end up with close to 5 million sequences, and I suppose it's checking each constantly throughout to see if the products of the squares lead to square-able numbers.
Why is Maple and Mupad faster? I can't say, but looking at the timing I would suspect they are just using approximate floats, leading to it calculating a single number and then taking that to 100. In Mathematica the second line is treated in a sense just as symbolically as the first, for instance Sqrt(2)
will not be converted to the approximate real value: 1.41421...
, but it does check along the calculations and convert when the root can be found exactly such as Sqrt[4]
evaluating to 2
. If you convert to approximate numerical values, then it is faster:
Expand[N[Sqrt[2] + Sqrt[3] + Sqrt[5] + Sqrt[6] + Sqrt[7]]^100]; // AbsoluteTiming
{0., Null}
Note the N
function, which converts to floating point approximations.
Update
Interesting it would seem the reason for the large difference in performance is that Mathematica expands and calculates the product at once rather than iteratively. This means that if some common subsequences can be simplified it will carry out those simplifications multiple times rather than once while iterating through it.
This mush faster code runs through with one term at a time expanding each time:
term = (Sqrt[2] + Sqrt[3] + Sqrt[5] + Sqrt[6] + Sqrt[7]);
Nest[Expand[# term] & , term, 100-1]; // AbsoluteTiming
(* {0.120012,Null} *)
To see how Mathematica expands the series you can use
f /: Times[a___, b_f, c___] := (Print[a, b, c]; Times @@ (First /@ {a}))
(f[a] + f[b] + f[c])^6 // Expand
(* f[a] f[b] f[c] *)
(* f[b] 6 f[a]^5 *)
(* f[a] 6 f[b]^5 *)
(* ... etc. *)