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 a math instructor.
Here is a list of the functions this file defines:
sgn
abs
heavyside
int
ln
log
logb
pow
rad2deg
deg2rad
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
newton
integrate
prime
Alongside the ubiquitous mathematical functions in this list, this file contains implementations of the following:
- Newton's Method
(
newton(x)
) which approximates a zero of a smooth functionf
nearx
. Note thatf
and its derivativeff
must be globally defined. - Numerical Integration
(
integrate(a,b,n)
) which numerically computes the value of a definite integral of a functionf
betweena
andb
using Boole's Rule (boole_
) withn
subdivisions. Note thatf
must be globally defined.
Here is a list of the functions this file defines:
- Pythagorean Triple Generator
(
pythagtriple(m,n)
) which prints the Pythagorean triple generated by two parametersm
andn
. - Pythagorean Quadruple Generator
(
pythagquadruple(m,n,p,q)
) which prints the primitive Pythagorean quadruple generated by four parametersm
,n
,p
, andq
. - Newton's Method
(
newtoniter(x,n)
) which iteratively approximates a zero of a smooth functionf
nearx
a total ofn
times, printing each successive approximation. Note thatf
and its derivativeff
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 tox
— the convergents of its continued fraction presentation — until finding the first one equal tox
up toscale
. - Different Base Expression
(
bases(n)
) which displays a numbern
in 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.
- 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. - In the file
routines.bc
if a function changes/defines a variable globally, the function willprint
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 is named
f
. If there must be two functions, the other will be namedg
. If a function wants to know the derivative off
, it'll expect that derivative to be namedff
(e.g.newton
).
- Add
cubic
andquartic
functions that prints the details of a cubic and quartic polynomial. - Address issues with the numerical integration
integrate
function: There must be a way to remove the need for the parametern
, Also, I'm concerned this function is inaccurate for large values of |b – a|. - Add a function that finds constructable algebraic approximations to real numbers. (see this).
- Add a numerical derivative function (see this).