Skip to content

mikepierce/GNU-bc-Functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GNU Basic Calculator (bc) Functions

My working collection of functions for GNU bc. The functions are split into two files: functions.bc containing simple “pure” functions, and routines.bc containing functions that I’ve found useful in practice as an instructor. Generically, the functions in functions.bc print nothing and return a value, and the functions in routines.bc print out stuff. Note that files like these can be loaded automatically by bc by setting this environment variable:

export BC_ENV_ARGS="-lq /PATH/TO/functions.bc /PATH/TO/routines.bc"

functions.bc

Here is a list of the functions this file defines:

sgn abs heavyside int frac ln log logb pow rad2deg deg2rad dms2dd cos sin tan sec csc cot arccos arcsin arctan atan2 arcsec arccsc arccot cosh sinh tanh sech csch coth arcosh arsinh artanh arsech arcsch arcoth factorial pick choose fibonacci derivative newton integral prime

Alongside the ubiquitous mathematical functions in this list, this file contains implementations of the following:

  • Numerical Differentiation (derivative(x)) which numerically computes the value of a derivative of a global function f at x.
  • Newton's Method (newton(x)) which approximates a zero of a global function f near x.
  • Numerical Integration (integral(a,b)) which numerically computes the value of a definite integral of a global function f between a and b.

routines.bc

Here is a list of the functions this file defines:

  • Pythagorean Triple Generator (pythagtriple(m,n)) which prints the Pythagorean triple generated by two parameters m and n.
  • Pythagorean Quadruple Generator (pythagquadruple(m,n,p,q)) which prints the primitive Pythagorean quadruple generated by four parameters m, n, p, and q.
  • Degrees/Minutes/Seconds (DMS) (dd2dms(x)) which prints the angle x, presumed to be measured in degrees, in terms of degrees° minutes′ seconds″.
  • Newton's Method (newtoniter(x,n)) which iteratively approximates a zero of a smooth function f near x a total of n times, printing each successive approximation. Note that f and its derivative ff must be globally defined.
  • Quadratic Polynomial Solver (quadratic(a,b,c)) which prints the roots and vertex of a degree-two polynomial given its coefficients as input.
  • Rational Approximation (rational(x)) which displays subsequently better rational approximations to x — the convergents of its continued fraction presentation — until finding the first one equal to x up to scale.
  • Different Base Expression (bases(n)) which displays a number n in bases 2, 3, …, 36.
  • Prime Integer Factorization (factor(n)) which displays the prime integer factors of n.
  • Rectangular/Polar Conversion (rect2pol(x,y)) and (pol2rect(r,θ)) which convert two-dimensional rectangular coordinates to polar coordinates and vice-versa respectively.
  • Rectilinear/Cylindrical Conversion (rect2cyl(x,y,z)) and (cyl2rect(r,θ,z)) which convert three-dimensional rectilinear coordinates to cylindrical coordinates and vice-versa respectively.
  • Rectilinear/Spherical Conversion (rect2sphere(x,y,z)) and (sphere2rect(ρ,θ,φ)) which convert three-dimensional rectilinear coordinates to spherical coordinates and vice-versa respectively.
  • Collazt (Hailstone) Sequence (collatz(n)) which prints the hailstone sequence obtained by iteratively applying the transformation prescribed by the Collatz Conjecture.
  • Sum of Consecutive Powers (sumofpowers(n)) which prints every way that n can be expressed as a sum of consecutive integral powers.
  • Zeckendorf Presentation (zeckendorf(n)) which prints the unique sum of non-adjacent Fibonacci numbers equal to n.

Conventions

  • If a function in routines.bc updates/defines a variable globally, it will print that variable assignment explicitly.
  • Since bc doesn't accept functions as parameters to other functions, any functions that morally should be a parameter must be defined globally. Such a function will be named f.
  • Function names ending in _ are helper functions not intended to be called directly.
  • Some functions that return the nth number in a sequence (e.g. fibonacci, prime) create an array of the same name as the function containing all previous terms in the sequence used to compute the nth term.
  • There are certain things bc is not designed for — linear algebra, statistics, and complex arithmetic among others — and should not be implemented in bc. These, if one so desires, should be implemented upstream within a fork of the bc program itself.

Aspirations

  • Add cubic and quartic functions that prints the details of a cubic and quartic polynomial.
  • Replace the discrete combinatorics functions (factorial, pick, choose, etc) with continuous (analytic?) analogous so I can remove the PRINT statements.
  • Add a function that finds constructable algebraic approximations to real numbers. (see this).
  • Browse the NIST Digital Library of Mathematical Functions for thoughts.

Allusions