6.11.4 Mathematical functions

Asymptote has built-in versions of the standard libm mathematical real(real) functions sin, cos, tan, asin, acos, atan, exp, log, pow10, log10, sinh, cosh, tanh, asinh, acosh, atanh, sqrt, cbrt, fabs, expm1, log1p, as well as the identity function identity. Asymptote also defines the order n Bessel functions of the first kind Jn(int n, real) and second kind Yn(int n, real), as well as the gamma function gamma, the error function erf, and the complementary error function erfc. The standard real(real, real) functions atan2, hypot, fmod, remainder are also included.

The functions degrees(real radians) and radians(real degrees) can be used to convert between radians and degrees. The function Degrees(real radians) returns the angle in degrees in the interval [0,360). For convenience, Asymptote defines variants Sin, Cos, Tan, aSin, aCos, and aTan of the standard trigonometric functions that use degrees rather than radians. We also define complex versions of the sqrt, sin, cos, exp, log, and gamma functions.

The functions floor, ceil, and round differ from their usual definitions in that they all return an int value rather than a real (since that is normally what one wants). The functions Floor, Ceil, and Round are respectively similar, except that if the result cannot be converted to a valid int, they return intMax for positive arguments and intMin for negative arguments, rather than generating an integer overflow. We also define a function sgn, which returns the sign of its real argument as an integer (-1, 0, or 1).

There is an abs(int) function, as well as an abs(real) function (equivalent to fabs(real)), an abs(pair) function (equivalent to length(pair)).

Random numbers can be seeded with srand(int) and generated with the int rand() function, which returns a random integer between 0 and the integer randMax. The unitrand() function returns a random number uniformly distributed in the interval [0,1]. A Gaussian random number generator Gaussrand and a collection of statistics routines, including histogram, are provided in the module stats. The functions factorial(int n), which returns n!, and choose(int n, int k), which returns n!/(k!(n-k)!), are also defined.

When configured with the GNU Scientific Library (GSL), available from https://www.gnu.org/software/gsl/, Asymptote contains an internal module gsl that defines the airy functions Ai(real), Bi(real), Ai_deriv(real), Bi_deriv(real), zero_Ai(int), zero_Bi(int), zero_Ai_deriv(int), zero_Bi_deriv(int), the Bessel functions I(int, real), K(int, real), j(int, real), y(int, real), i_scaled(int, real), k_scaled(int, real), J(real, real), Y(real, real), I(real, real), K(real, real), zero_J(real, int), the elliptic functions F(real, real), E(real, real), and P(real, real), the Jacobi elliptic functions real[] sncndn(real,real), the exponential/trigonometric integrals Ei, Si, and Ci, the Legendre polynomials Pl(int, real), and the Riemann zeta function zeta(real). For example, to compute the sine integral Si of 1.0:

import gsl;
write(Si(1.0));

Asymptote also provides a few general purpose numerical routines:

real newton(int iterations=100, real f(real), real fprime(real), real x, bool verbose=false);

Use Newton-Raphson iteration to solve for a root of a real-valued differentiable function f, given its derivative fprime and an initial guess x. Diagnostics for each iteration are printed if verbose=true. If the iteration fails after the maximum allowed number of loops (iterations), realMax is returned.

real newton(int iterations=100, real f(real), real fprime(real), real x1, real x2, bool verbose=false);

Use bracketed Newton-Raphson bisection to solve for a root of a real-valued differentiable function f within an interval [x1,x2] (on which the endpoint values of f have opposite signs), given its derivative fprime. Diagnostics for each iteration are printed if verbose=true. If the iteration fails after the maximum allowed number of loops (iterations), realMax is returned.

real simpson(real f(real), real a, real b, real acc=realEpsilon, real dxmax=b-a)

returns the integral of f from a to b using adaptive Simpson integration.