TangentNumbers.frink

View or download TangentNumbers.frink in plain text format


// Calculation of Tangent Numbers, which can be used to also calculate
// Bernoulli numbers.
//
//  These can be used to derive the tangent of a number by:
//
//  tan[z] = sum[n>0, Tn * z^(2n-1)/(2n-1)!]
//
// See:
//  http://arxiv.org/abs/1108.0286 (Brent)
//  http://mathworld.wolfram.com/TangentNumber.html
//  http://oeis.org/A000182
//  https://www.petervis.com/mathematics/maclaurin_series/maclaurin_series_tanx.html
//
//  See:
//   Fast Algorithms for High-Precision Computation of Elementary Functions,
//   Richard P. Brent, 2006
//   https://pdfs.semanticscholar.org/bf5a/ce09214f071251bfae3a09a91100e77d7ff6.pdf
//
// This implements the algorithm in figure 2 of the Brent paper.
//
//  The main problem for using this to calculate tangents to arbitrary
// precision is that this algorithm alters numbers in-place in several passes,
// and doesn't allow easy calculation of more tangent numbers if you decide
// you need more.
//
//  Another problem with using Tangent Numbers around 90 degrees is that
// this converges very slowly and may require way too many terms.
//
//  We could also try using Newton's method to invert arctan[x] which
//  has a simple series expansion,
//  arctan[x] = sum[(-1)^k x^(2k+1) / (2k + 1),  {k, 0, infinity}]
//  but this only converges for abs[x] <= 1, x != +/- i
// 
tangentNumbers[n] :=
{
   t = new array
   t@1 = 1

   for k = 2 to n
      t@k = (k-1) t@(k-1)

   for k = 2 to n
      for j = k to n
          t@j = (j-k) t@(j-1) + (j-k+2) t@j

   return t 
}


View or download TangentNumbers.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 18350 days, 2 hours, 21 minutes ago.