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"
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 functionfatx. - Newton's Method
(
newton(x)) which approximates a zero of a global functionfnearx. - Numerical Integration
(
integral(a,b)) which numerically computes the value of a definite integral of a global functionfbetweenaandb.
Here is a list of the functions this file defines:
- Pythagorean Triple Generator
(
pythagtriple(m,n)) which prints the Pythagorean triple generated by two parametersmandn. - Pythagorean Quadruple Generator
(
pythagquadruple(m,n,p,q)) which prints the primitive Pythagorean quadruple generated by four parametersm,n,p, andq. - Degrees/Minutes/Seconds (DMS)
(
dd2dms(x)) which prints the anglex, 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 functionfnearxa total ofntimes, printing each successive approximation. Note thatfand its derivativeffmust 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 tox— the convergents of its continued fraction presentation — until finding the first one equal toxup toscale. - Different Base Expression
(
bases(n)) which displays a numbernin bases 2, 3, …, 36. - Prime Integer Factorization
(
factor(n)) which displays the prime integer factors ofn. - 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 thatncan be expressed as a sum of consecutive integral powers. - Zeckendorf Presentation
(
zeckendorf(n)) which prints the unique sum of non-adjacent Fibonacci numbers equal ton.
- If a function in
routines.bcupdates/defines 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.