Mathematics.hel

From Rosetta Code

Introduction

Both Regina and ooRexx are supplied with a mathematical library, a DLL based on the C-library. This supports some basic math functions and a few constants, up to 16 digits precision. For many tasks on RosettaCode it's not sufficient.

This year I started posting on RosettaCode and found the copy-and-paste WoW to get a runnable program annoying. Therefore I'll gather all procedures I have until now and present them here in 7 libraries. Other REXX entries will link to this page.

Many sources were helpful while creating these libraries. Credits go to:

  • Math functions in REXX: John Brock, Patrick McPhee
  • Code snippets RosettaCode: Gerard Schildberger, Walter Pachl
  • Wikipedia International
  • Wolfram Mathworld
  • Geeks for Geeks
  • Online Encyclopedia of Integer Sequences
  • Google
  • ChatGPT
  • Numerical methods at work: Henrik Vestermark
  • Numerical recipes: Press et al
  • Various other books and internet math sources and online calculators
  • And my own research and try-outs

General remarks

For now, all procedures work only with real arguments and deliver real results. No complex arithmetic is supported yet.
REXX can work with arbitrary precision, thus also these procedures, using your current numeric digits setting. However, basic functions such as square root, pi or tangent start having problems at 1000 digits, and the more complex functions as gamma or zeta struggle already with 100 digits.
Basis parameter checks are present. In case of error, a procedure just returns 'X' (= forbidden) and your program will fail elsewhere. No fancy error trapping is present.
All routines are coded with the 'procedure' clause, meaning all variables within are local. They communicate via the parameters. However, sometimes it's handy or needed to use global variables (store multiple results, memorization). For this the stem 'glob.' is used, and every procedure has 'expose glob.' coded to make it available.
As you might guess, there are many cross references between the libraries.

Usage

Use the low profile pre processor (include pieces of code in a REXX program): Low profile REXX pre processor
or
Copy and paste (parts) of the libraries in your program

Mind the cross refs: if you copy Numbers, big chance you'll also need Functions and Constants.

Constants

A whole bunch of mathematical constants: Pi, E, Euler-Mascheroni and a lot more. Up to 100 digits, these constants are predefined and thus given directly. Above 100 digits, under availability of a acceptable fast algorithm, the constant is calculated. All procedures in this library have no parameters. The full list follows:

  • Apery: Apery constant
  • Artin: Artin constant
  • Backhouse: Backhouse constant
  • Bernstein: Bernstein constant
  • Bloch: Bloch constant
  • Bronze: Bronze ratio constant = positive root of x^2-3x-1
  • Brun: Brun constant
  • Cahen: Cahen constant
  • Capacity: Logarithmic capacity of the unit disk constant
  • Catalan1: Catalan constant
  • Chaitin: Chaitin constant
  • Champernowne: Champernowne constant
  • Conway2: Conway constant = real root of x^71-x^69-2x^68...-6x^2+3x-6
  • Copeland: Copeland-Erdos constant
  • Devicci: DeVicci constant = positive root of 4x^8-28x^6-7x^4+16x^2+16
  • Dottie: Dottie constant = root of Cos(x) - x
  • Dubois: Second Dubois-Reymond constant
  • E: Euler number
  • Embree: Embree-Trefethen constant
  • Erdos1: Erdos-Borwein constant
  • Erdos2: Erdos-Tenenbaum-Ford constant
  • Euler: Euler-Mascheroni constant
  • Feigenbaum1: Feigenbaum first constant
  • Feigenbaum2: Feigenbaum second constant
  • Feller: Feller-Tornier constant
  • Foias: Foias constant
  • Fraction: First continued fraction constant
  • Fransen: Fransen-Robertson constant
  • Gauss: Gauss constant
  • Gelfond1: Gelfond constant
  • Gelfond2: Gelfond-Schneider constant
  • Gieseking: Gieseking constant
  • Glaisher: Glaisher-Kinkelin constant
  • Golden: Golden ratio constant = root of x^2-x-1
  • Goldenangle: Golden angle constant
  • Golomb1: Golomb constant
  • Gompertz: Gompertz constant
  • Hafner: Hafner-Sarnak-McCurley constant
  • Heath: Heath-Brown-Moroz constant
  • Hermite: Second Hermite constant
  • Kepler: Kepler-Bouwkamp constant
  • Khinchin: Kinchin constant
  • Komornik: Komornik-Loreti constant
  • Landau1: Landau-Ramanujan constant
  • Landau2: Second Landau constant
  • Laplace: Laplace limit constant
  • Lebesgue2: Lebesgue constant
  • Lemniscate: Lemniscate constant
  • Levy1: Levy-Khinchin constant
  • Levy2: Second Levy constant
  • Lieb: Lieb constant
  • Ln2: Natural log of 2 constant
  • Ln4: Natural log of 4 constant
  • Ln8: Natural log of 8 constant
  • Ln10: Natural log of 10 constant
  • Loch: Loch constant
  • Magicangle: Magic angle constant
  • Meissel: Meissel-Mertens constant
  • Mills: Mills constant
  • Mrb: MRB constant
  • Nielsen: Nielsen-Ramanujan constant
  • Niven: Niven constant
  • Omega: Omega constant
  • Paperfolding: Paperfolding constant
  • Parabolic: Universal parabolic constant
  • Pi: Pi constant
  • Plastic: Plastic ratio constant = real root of x^3-x-1
  • Porter: Porter constant
  • Prouhet: Prouhet-Thue-Morse constant
  • Ramanujan: Ramanujan constant
  • Recifibo: Reciprocal Fibonacci constant
  • Robbins: Robbins constant
  • Salem: Salem constant = largest positive root of x^10+x^9-x^7-x^6-x^5-x^4-x^3+x+1
  • Sierpinski: Sierpinski constant
  • Silver: Silver ratio constant = positive root of x^2-2x-1
  • Soldner: Soldner constant
  • Somos: Somos quadratic recurrence constant
  • Sqrt2: Square root of 2 constant
  • Sqrt3: Square root of 3 constant
  • Sqrt5: Square root of 5 constant
  • Stephens: Stevens constant
  • Supergolden: Super golden ratio constant = real solution of x^3-x^2-1
  • Taniguchi: Taniguchi constant
  • Tau1: Tau constant = 2*pi
  • Tetranacci2: Tetranacci constant = root of x^4-x^3-x^2-x-1
  • Tribonacci2: Tribonacci constant = root of x^3-x^2-x-1
  • Twinprimes: Twin primes constant
  • Vanderpauw: Van der Pauw constant
  • Viswanath: Viswanath constant
  • Wallis: Wallis constant = real root of x^3-2x-5
  • Weierstrass: Weierstrass constant
  • Zscore: Z-score 97.5 percentile constant

Functions

Here we have the well known set of trigonometric, hyperbolic, exponential and logarithmic functions and their inverses. And of course functions like Square/Cubic/Quartic/Nth root. Some 'higher' functions are also included: Beta, Gamma, Zeta and a few more. Arguments for the trig functions is always in radians, maybe you like to use Deg or Rad to convert values. The full list:

  • Acos: Arc cosine function
  • Acosh: Arc cosine hyperbolic function
  • Acot: Arc cotangent function
  • Acoth: Arc cotangent hyperbolic function
  • Acsc: Arc cosecant function
  • Acsch: Arc cosecant hyperbolic function
  • Agmean: Arithmetic geometric mean function
  • Alog: Antilog function = 10^x
  • Amean: Arithmetic mean function
  • Asec: Arc secant function
  • Asech: Arc secant hyperbolic function
  • Asin: Arc sine function
  • Asinh: Arc sine hyperbolic function
  • Atan: Arc tangent function
  • Atanh: Arc tangent hyperbolic function
  • Atan2: Arc tangent correct quadrant function
  • Beta: Beta function
  • Cbrt: Cubic root function = x^(1/3)
  • Ceil: Ceiling function
  • Cos: Cosine function
  • Cosh: Cosine hyperbolic function
  • Cot: Cotangent function
  • Coth: Cotangent hyperbolic function
  • Csc: Cosecant function
  • Csch: Cosecant hyperbolic function
  • Deg: Radians->degrees function
  • Digamma: Digamma function
  • Dxgamma: First derivative of Gamma function
  • Dxzeta: First derivative of Zeta function
  • Erf: Error function
  • Erfc: Error complementary function
  • Exp: Exponential function = e^x
  • Factorial: Factorial function = x!
  • Floor: Floor function
  • Frac: Fractional part function
  • Gamma: Gamma function
  • Gmean: Geometric mean function
  • Isqrt: Integer square root function = Floor(x^(1/2))
  • Lambda: Dirichlet lambda function
  • Lambertw0: Lambert W0 function
  • Lambertwm1: Lambert W-1 function
  • Lcm: Least common multiple function
  • Ln: Natural logarithm function
  • Log: Briggs logarithm function
  • Logxy: Logarithm x base y function
  • Mant: Mantissa function
  • Nroot: Nth root function = x^(1/n)
  • Power: Power function function = x^y
  • Powermod: Power modulus function = x^(y mod z)
  • Qtrt: Quartic root function = x^(1/4)
  • Rad: Degrees->radians function
  • Rand: Random number function in 12 digits precision
  • Round: Round to nearest integer function
  • Sec: Secant function
  • Sech: Secant hyperbolic function
  • Sin: Sine function
  • Sinh: Sine hyperbolic function
  • Sqrt: Square root function = x^(1/2)
  • Tan: Tangent function
  • Tanh: Tangent hyperbolic function
  • Tau2: Ramanujan tau function
  • Trigamma: Trigamma function
  • Xpon: Exponent function
  • Zeta: Zeta function

Numbers

These are procedures belonging to the field 'number theory': all kinds of numbers, permutations, combinations, factors, divisors and more. Many of these procedures come in pairs: i.e. Prime(number) checks if number is prime and Primes(threshold) gives the number of primes <= threshold and stores all these primes in glob. Most procedures expect a number as parameter, some two numbers like Gcd or Comb. Many of these procedures become quite slow as the numbers grow. The full list:

  • Abundant: Is a number abundant? function
  • Abundants: Abundant numbers
  • Ackerman: Ackerman's function
  • Additiveprime: Is a number additive prime? function
  • Additiveprimes: Additive prime numbers
  • Almostprime: Is a number almost k-prime?
  • Amicable: Amicable number pairs
  • Arithmetic: Is a number arithmetic? function
  • Arithmetics: Arithmetic numbers
  • Comb: Combinations function = binomial coefficients
  • Composite: Is a number composite? function
  • Composites: Composite numbers = not prime
  • Deficient: Is a number deficient? function
  • Deficients: Deficient numbers
  • Dfact: Double factorial function = n!!
  • Digitsum: Digitsum function = sum(digits of a number)
  • Divisor: Divisor function = sum(Divisors(x)^y)
  • Divisors: Divisors of an integer
  • Euclids: Euclid-Mullin numbers
  • Even: Is a number even? function
  • Fact: Factorial function = n!
  • Factors: Prime factors of an integer
  • Fareys: Farey fraction numbers
  • Ffact: Falling factorial function
  • Gcd: Greatest common divisor function
  • Giugas: Giuga numbers
  • Half: Is a number half integer? function
  • Kaprekar: Is a number Kaprekar? function
  • Kaprekars: Kaprekar numbers
  • Lcm: Least common multiple function
  • Odd: Is a number odd? function
  • Perfect: Is a number perfect? function
  • Perfects: Perfect numbers
  • Perm: Permutations function
  • Phi: Euler's totient function
  • Practical: Is a number practical? function
  • Practicals: Practical numbers
  • Prim: Primorial function = n#
  • Prime: Is a number prime? function
  • Primes: Prime numbers
  • Radical: Radical function = product of unique prime factors
  • Rfact: Rising factorials function
  • Semiprime: Is a number semi prime? function
  • Sigma: Sigma function = Sum of all divisors of x including 1 and x
  • Ufactors: Unique prime factors of an integer
  • Whole: Is a number integer? function

Polynomial

Two procedures, used elsewhere, deal with the first derivative or value of a polynomial. They both have a string with coefficients as parameter, highest power first, zero entries included:2x^3+x+3 = '2 0 1 3'. Pdiff returns as string in this format, which than may be evaluated using Peval. The list:

  • Pdiff: Polynomial differentiation function
  • Peval: Polynomial evaluation function

Rational

One of the tasks on RosettaCode is 'rational arithmetic'. The most important procedures from this field are included here. Besides the second parameter of Rpower, all arguments in and out have the form 'numerator/denominator'. The list:

  • Rabs: Rational absolute value function
  • Radd: Rational add function
  • Rdivide: Rational divide function
  • Rinvert: Rational invert function
  • Rmultiply: Rational multiply function
  • Rnegate: Rational negate function
  • Rpower: Rational power function
  • Rrational: Integer->Rational function
  • Rsimplify: Rational simplify function
  • Rsubtract: Rational subtract function

Roots

For equations of the second to fourth degree solutions in closed form (formulas) are possible. Procedures for these equations are present here. The input follows the convention as described in Polynomial: so solving x^2-3x+2=0 is represented by parameter '1 -3 2'. The list:

  • Cbeq: Cubic roots of ax^3+bx^2+cx+d = 0
  • Qteq: Quartic roots of ax^4+bx^3+cx^2+dx+e = 0
  • Sqeq: Square roots of ax^2+bx+c = 0

Sequences

These procedures deliver the nth element of a given sequences. Unlike the library Numbers, previous elements are not given. All procedure expect a whole number, often > 0, as parameter. The most important is Bernoulli. The full list:

  • Alcuin: Alcuin sequence
  • Bell: Bell sequence
  • Beraha: Beraha sequence
  • Bernoulli: Bernoulli sequence
  • Cake: Cake sequence
  • Catalan2: Catalan sequence
  • Caterer: Lazy caterer's sequence
  • Central: Central binomial coefficient sequence
  • Conway1: Conway look-and-say sequence
  • Cullen: Cullen sequence
  • Derangement: Derangement sequence
  • Favard: Favard sequence
  • Fermat: Fermat sequence
  • Fibonacci: Fibonacci sequence
  • Golomb2: Golomb-Silverman sequence
  • Gould: Gould sequence
  • Gregory1: Gregory sequence 1
  • Gregory2: Gregory sequence 2
  • Harmonic: Harmonic sequence
  • Jacobsthal: Jacobsthal sequence
  • Lah: Lah sequence
  • Lebesgue1: Lebesgue sequence
  • Leonardo: Leonardo sequence
  • Lucas: Lucas sequence
  • Magic: Magic sequence
  • Metallic: Metallic ratio sequence
  • Motzkin: Motzkin sequence
  • Moebius: Moebius sequence
  • Narayana: Narayana cows sequence
  • Padovan: Padovan sequence
  • Partition: Partition sequence
  • Pell1: Pell sequence
  • Pell2: Pell-Lucas companion sequence
  • Perrin: Perrin sequence
  • Pronic: Pronic sequence
  • Sorting: Sorting sequence
  • Recaman: Recaman sequence
  • Stirling1: Stirling first kind sequence
  • Stirling2: Stirling second kind sequence
  • Sylvester: Sylvester sequence
  • Tetranacci1: Tetranacci sequence
  • Thue: Thue-Morse numbers
  • Tribonacci1: Tribonacci sequence
  • Ulam: Ulam sequence
  • Wedderburn: Wdderburn sequence
  • Woodall: Woodall sequence
  • Zigzag: Zigzag or alternating permutations sequence