Generating Machin-like formulaeΒΆ
This demo shows how arctans can be used to generate Machin-like formulae.
"""Generating Machin-like formulae.
Machin's formula is the formula pi = 16arctan(1/5) - 4arctan(1/239).
This demo shows how arctans can be used to generate other Machin-like
formulae for pi.
"""
from arctans import arctan, generate, Rational
# Machin's formule can be represented as an arctans formula as follows.
machin = 16 * arctan(Rational(1, 5)) - 4 * arctan(Rational(1, 239))
# You can print a formula. When printing a formula, [n] will be printed
# as shorthand for arctan(1/n) (or equivalently arccotan(n)), so in this case
# this will print 16*[5] - 4*[239]
print(machin)
print()
# The generate function can be used to generate a set of Machin-like formulae.
# The list of formulae that this function returns will all be different and not
# equal to Machin's formula.
formulae = generate(machin)
# We print the first five generated formulae.
print("# Machin-like formulae")
for f in formulae[:5]:
print(f)
print()
# If a list of formulae is passed into generate, all of the generated formulae will
# be ones not included in the input list. New formulae are generated by substituting
# values into the input formulae, so this will generate more formulae that the first
# call of generate did not generate.
new_formulae = generate([machin] + formulae)
print("# More Machin-like formulae")
for f in new_formulae[:5]:
print(f)
print()
# Some additional keyword arguements can be passed into generate to control some
# properties of the formulae it will create:
#
# - min_denominator and max_denominator can be used to control the minimum and
# maximum denominator of an arctan argument that can appear in the formulae.
# By default, 1 is used for the minimum and 100 for the maximum.
# - min_numerator and max_numerator can be used to control the minimum and
# maximum numerator of an arctan argument that can appear in the formulae.
# By default, 1 is used for the minimum and 1 for the maximum.
# - max_terms can be used to control the maximum number of terms that each
# formula can include. By default this is unlimited.
# - max_coefficient_denominator can be used to control the maximum denominator
# that a coefficient in a formula can have. By default this is unlimited.
# - printing can be set to True to print the numerators and denominators currently
# being tested
#
# For example, if you wanted to generate formulae with 3 or fewer terms you could run
# the following.
formulae = generate([machin], max_terms=3)
print("# Machin-like formulae with at most 3 terms")
for f in formulae:
print(f)
print()
# If you wanted to generate formulae that may include arccotans with non-integer
# arguments, you could run the following.
formulae = generate([machin], max_denominator=30, max_numerator=4)
print("# Machin-like formulae, including some with non-integer arccotan arguments")
for f in formulae:
print(f)
print()