An answer to How does Mathematica calculate $\sin(\pi/5)$? by @J.M. points out that in the Notes on Internal Implementation, it is implied that FunctionExpand
is the way to expand trigonometric functions in terms of radicals when possible:
FunctionExpand
uses an extension of Gauss's algorithm to expand trigonometric functions with arguments that are rational multiples of π.
FunctionExpand[Tan[Pi/8]]
(* Sqrt[(2 - Sqrt[2])/(2 + Sqrt[2])] *)
The internal function that does what the OP wants is called Simplify`TrigToRealRadicals[]
, which is called by FunctionExpand
and ToRadicals
in this case. It in turn calls a function like System`TrigToRadicalsDump`tan[]
, which computes the conversion. These function memoize their results, which makes timing them a challenge. However, the timings below show that FunctionExpand
does a lot of unnecessary work. We give a complicated example. At first, FunctionExpand
does nothing, and ToRadicals
returns the simplified expansion in terms of the complex exponential function.
FunctionExpand[Cos[π/257]]
(* Cos[π/257] <-- takes <0.02s *)
ToRadicals[Cos[π/257]]
(* -(1/2) (-1)^(256/257) (1 + (-1)^(2/257)) <-- takes 0.004s *)
If the trig-to-radicals functionality has been loaded, then we get a different behavior. I cannot say for certain what functions cause the loading of the package. The first time I tested, it had been loaded and I got results like those below. The second, I retested with a fresh kernel and got the above, which confused me for a while.
Simplify`TrigToRealRadicals; (* load/initialize *)
System`TrigToRadicalsDump`cos[π/257]; // AbsoluteTiming
(* {146.975, Null} <-- the expansion is now memoized *)
ToRadicals[Cos[π/257]] // LeafCount
(* 263365 <-- in terms of (real) radicals; takes only 0.06s *)
Here's a speed comparison, assuming the initialization of System`TrigToRadicalsDump`cos[π/257]
has been done.
Simplify`TrigToRealRadicals[Cos[π/257]]; // RepeatedTiming
ToRadicals[Cos[π/257]]; // RepeatedTiming
RootReduce[Cos[π/257]]; // RepeatedTiming
FunctionExpand[Cos[π/257]]; // AbsoluteTiming
(*
{ 0.0579771, Null}
{ 0.0593911, Null}
{ 1.28168, Null}
{37.3825, Null}
*)
Tan[Pi/8]
is an exact numerical constant. $\endgroup$Tan[Pi/8] // N // RootApproximant
$\endgroup$12.3.1 for Microsoft Windows (64-bit) (June 19, 2021)
,FullSimplify[Tan[Pi/8] + 1 - Sqrt[2]]
yields0
. $\endgroup$