#!/usr/bin/env python2
# Implementation of the de Vries algorithm in python 2
# Copyright (C) 2014 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
# Licensed under the GPLv3+ Gnu General Public License
# http://www.gnu.org/licenses/gpl-3.0.html
#
# This script and information about it is maintained at:
# http://lkcl.net/reports/fine_structure_constant/
#
# Last Updated: 6 Apr 2014
#
# this program prints out the fine structure constant according to the
# de vries algorithm, to well within the uncertainty of the 2010 CODATA value.
# 2010 CODATA uncertainty is 3.2e-10.  when PROPERLY EXECUTED ACCURATELY
# the de vries algorithm gets it to within 1.6e-10.  it's the only algorithm
# still "in the running" and it has the benefit of making any kind of
# theoretical sense as well as very low kolmogorov complexity.
#
# the implications of the de vries algorithm, through its similarity
# to the infinite series which give us a means to calculate pi and e,
# may actually show that alpha is a fundamental (universal) constant.
# we say this with the proviso that it may *actually* represent
# "Alpha-Infinity" in the same corresponding manner that "Rydberg-Infinity"
# reprsents a relationship to a 2nd particle of effectively infinite mass,
# the de vries algorithm *may* represent the "perfect" mathematical
# relationship of a particle of zero mass (zero radius) with its
# own outwardly-propagating electro-magnetic field.  As such, the de
# Vries algorithm may not be considered to be "the last word".
#
# cursory studies show a potential link to non-paraxial airy beam 1/3
# order bessel functions as well as to Hermite Polynomials as related
# to Schroedinger Harmonic Oscillator solutions.  both involve the term
# exp(-x^2/2) as a fundamental part of the equations.  there are many
# other areas where this exponential term occurs: the scientific community
# is invited to help investigate them.
#
# as it is an iterative algorithm based on *inverse* stability to a
# power series it is hyper-sensitive to floating-point accuracy and
# the number of iterations.  if the number of triangular power series
# additions is too low (below about 20) or the number of iterations
# too small (below about 15) the algorithm simply does not give accurate
# results.  single-precision floating point is without a shadow of
# doubt completely useless.
#
# standard python 2.7 with 25 triangular powers and 30 iterations is just
# about within the limit of python 2.7 and still producing accurate results.
# for absolute paranoia run with the python BigFloat library to numerical
# precision of 150 decimal places, 70 triangular power additions and 30
# iterations.  as this settles at around the 30th decimal place it is not
# possible to argue that it is inaccurate at only the 10th.  to do so is
# to call into question the CODATA 2010 accuracy, which is an entirely
# different matter [to whit: rather unwise].
#
# an intuitive understanding of this algorithm is that it is a reflection of
# a particle's stability with its own electro-magnetic field (and in the case
# of electron shells, a reflection of the stability between the fields
# of an electron and an atom).  standing wave patterns, in effect.  for
# a theoretical paper which best describes this phenomenon see G Poelz's
# paper: http://arxiv.org/abs/1206.0620 - the only proviso being that
# Poelz makes the same approximations that are normally done in these
# circumstances, taking only the first approximation (1 + alpha / (2 pi))
# as the maths is at present not fully understood.
#
# if this algorithm is a true reflection of the process by which alpha
# comes about then it has some fascinating implications, not least of which
# is that any newly-created particle would need to make a few revolutions
# (at its compton wavelength) before properly settling down.
#
# http://www.chip-architect.com/news/...
#     ...2004_10_04_The_Electro_Magnetic_coupling_constant.html

from math import pi, e
import math
try:
    # use bigfloat library, uses Multi-Point Floating Precision library
    from bigfloat import cos, sin, acos, asin, pow, precision, BigFloat, log
    iterations = 70 # paranoid number of iterations
except:
    from math import cos, acos, asin, sin, log
    # bigfloat library not available, use standard python (less accurate)
    BigFloat = float
    class precision: # stub class, does nothing
        def __init__(self, ignore): pass
        def __enter__(self): pass
        def __exit__(self, ignore1, ignore2, ignore3): pass
    iterations = 25 # standard python, pow(2*pi, 70) is too big for float

def alphafunc(g_2_mfactor, mult_factor=2.0):
    with precision(150):
        a = BigFloat(0.007)
        for x in range(1, 30):
            t = 0.0
            g = 0.0
            for i in range(iterations):
                g = g + pow(a, i)/pow(2*pi, t)
                t = t + i
            a = pow(g, 2)/(pow(e, (pow(pi, 2)/mult_factor)))
            a = a * g_2_mfactor
            fsc = 1/a
            print a, g
    return a, g

alpha, g = alphafunc(1.0)
print "alpha infinity (and gamma) %.50g" % alpha, g
print "inverse %.50g" % (1/alpha)

