My working collection of functions for
GNU bc.
The functions are split into two files:
functions.bc containing pure functions that only return a value,
and routines.bc containing functions that I’ve found useful in practice as an instructor
that, generally, print information about some input.
Files like these can be loaded automatically by bc at startup
by setting this environment variable:
export BC_ENV_ARGS="-lq /PATH/TO/functions.bc /PATH/TO/routines.bc"
This file defines the following functions:
sgn
abs
heavyside
max
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)
Returns the derivative of the (global) function f at x using a finite central distance with eight points. - Newton's Method
newton(x)
Returns the zero of the (global) function f that results from iteratively applying Newton's Method with initial parameter x. - Numerical Integration
integral(a,b)
Returns the value of the definite integral of the (global) function f between a and b.
This file defines the following routines:
- Pythagorean Triple Generator
(
pythagtriple(m,n))
Print the Pythagorean triple generated by two parameters m and n, and return the hypotenuse. - Pythagorean Quadruple Generator
(
pythagquadruple(m,n,p,q))
Print the Pythagorean quadruple generated by parameters m, n, p, and q, and return the hypotenuse. - Degrees/Minutes/Seconds (DMS)
(
dd2dms(x))
Prints the angle x, presumed as a decimal number of degrees, in DMS (degrees° minutes′ seconds″) notation. - Newton's Method
(
newtoniter(x))
Iteratively applies Newton's Method, printing each result, with initial guess x and (global) function f. - Quadratic Polynomial Solver
(
quadratic(a,b,c))
Print the roots and vertex coordinates of ax²+bx+c. - Simple Continued Fraction
(
contfrac(x))
Print the coefficients of the simple continued fraction representation of x as well as each convergent obtained by truncating at that coefficient, which are successively better rational approximations to the number x. - Different Base Expression
(
bases(n))
Printnexpressed in bases 2, 3, ..., 36. - Prime Integer Factorization
(
factor(n))
Print the prime integer factorization of n. - Rectangular/Polar Conversion
(
rect2pol(x,y)) and (pol2rect(r,θ))
Convert two-dimensional rectangular coordinates to polar coordinates and vice-versa respectively. - Rectilinear/Cylindrical Conversion
(
rect2cyl(x,y,z)) and (cyl2rect(r,θ,z))
Convert three-dimensional rectangular coordinates to cylindrical coordinates and vice-versa respectively. - Rectilinear/Spherical Conversion
(
rect2sphere(x,y,z)) and (sphere2rect(ρ,θ,φ))
Convert three-dimensional rectangular coordinates to spherical coordinates and vice-versa respectively, following the mathematician’s convention with the zenith angle θ in the range [0,2π) with θ = 0 along the positive x-axis, and the azimuth angle φ in the range [0,π] with φ = 0 along the positive z-axis. - Collazt (Hailstone) Sequence
(
collatz(n))
Print the sequence of positive integers that results from iteratively applying the function prescribed by the Collatz Conjecture. - Sum of Consecutive Powers
(
sumofpowers(m))
Print every way that m can be written as a sum of powers of consecutive positive integers. - Zeckendorf Presentation
(
zeckendorf(n))
Prints the unique sum of non-adjacent Fibonacci numbers equal to n.
- If a function in
routines.bcdefines or updates the value of a variable globally, it willprintthat 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.
- Add
cubicandquarticfunctions 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.