8
$\begingroup$

The trigonometric functions for half angles have well-defined and simple rules. For example $$\tan\left(\frac x2\right)=\frac{\sin x}{1+\cos x}=\frac1{\csc x+\cot x}$$ But I can't get the exact value for some expressions as simple as $\tan(\pi/8)$. No matter what I tried, Mathematica wouldn't understand that it's equal to $\sqrt2-1$, for example:

Simplify[Tan[Pi/8] + 1 - Sqrt[2]]

does nothing. I tried TrigReduce, TrigExpand, TrigFactor, FullSimplify and some other unrelated functions and they all failed to give the exact value of $\tan(\pi/8)$. This became ironic when I checked some other angles such as $\pi/12$ and turned out it does know e.g. $\sin(\pi/12)$ whose only way of calculation is apparently, through half angle formulas.

Why this happens and how can I get Mathematica to calculate such values when they do have an exact expression?

$\endgroup$
6
  • 1
    $\begingroup$ Of course, the question is a bit flawed because Tan[Pi/8] is an exact numerical constant. $\endgroup$
    – John Doty
    Oct 29, 2021 at 15:36
  • $\begingroup$ @JohnDoty please feel free to edit it if you can think of better words. My vocabulary is not that great. $\endgroup$
    – polfosol
    Oct 29, 2021 at 15:38
  • 1
    $\begingroup$ Well, what is it that you want, really? What problem are you trying to solve? $\endgroup$
    – John Doty
    Oct 29, 2021 at 15:40
  • 1
    $\begingroup$ Tan[Pi/8] // N // RootApproximant $\endgroup$
    – Bob Hanlon
    Oct 29, 2021 at 16:25
  • $\begingroup$ With version 12.3.1 for Microsoft Windows (64-bit) (June 19, 2021), FullSimplify[Tan[Pi/8] + 1 - Sqrt[2]] yields 0. $\endgroup$
    – bbgodfrey
    Oct 30, 2021 at 13:26

3 Answers 3

11
$\begingroup$

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}
*)
$\endgroup$
2
  • $\begingroup$ The formatting of third paragraph makes it hard to read/understand. By the way, is there anything special about number 257 or is it chosen randomly? $\endgroup$
    – polfosol
    Oct 30, 2021 at 19:28
  • $\begingroup$ @polfosol n=257 is a prime such that a regular n-gon can be constructed by ruler and compass. That comes down to writing Cos[Pi/257] in terms of square roots, more or less. The example is borrowed from the linked question. Such primes have to be Fermat primes, which have the form 2^(2^k)+1. (And thanks for alerting me to a missing backtick.) $\endgroup$
    – Michael E2
    Oct 30, 2021 at 20:26
10
$\begingroup$
TrigToExp[Tan[Pi/8]] // FullSimplify
-1 + Sqrt[2]

Or, even better:

Tan[Pi/8] // RootReduce
-1 + Sqrt[2]
$\endgroup$
2
  • $\begingroup$ Who would have thought, of all the functions, RootReduce would do the job? a strange world we live in... $\endgroup$
    – polfosol
    Oct 29, 2021 at 15:36
  • 6
    $\begingroup$ @polfosol You apparently wanted an explicit algebraic number, so RootReduce made perfect sense to me. $\endgroup$
    – John Doty
    Oct 29, 2021 at 15:38
9
$\begingroup$

Try to use ToRadicals[Tan[Pi/8]]//FullSimplify

$\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.