MultiplyNum , CachedConstant , NewtonNum , SumTaylorNum .

Arbitrary-precision numerical programming

This chapter contains functions that help programming numerical calculations with arbitrary precision.

MultiplyNum optimized numerical multiplication
CachedConstant precompute multiple-precision constants
NewtonNum low-level optimized Newton's iterations
SumTaylorNum optimized numerical evaluation of Taylor series


MultiplyNum -- optimized numerical multiplication

Standard library
Calling format:
MultiplyNum(x,y)
MultiplyNum(x,y,z,...)
MultiplyNum({x,y,z,...})

Parameters:
x, y, z -- integer, rational or floating-point numbers to multiply

Description:
The function MultiplyNum is used to speed up multiplication of floating-point numbers with rational numbers. Suppose we need to compute p/q*x where p, q are integers and x is a floating-point number. At high precision, it is faster to multiply x by an integer p and divide by an integer q than to compute p/q to high precision and then multiply by x. The function MultiplyNum performs this optimization.

The function accepts any number of arguments (not less than two) or a list of numbers. The result is always a floating-point number (even if Numeric is not set).

See also:
MathMultiply .


CachedConstant -- precompute multiple-precision constants

Standard library
Calling format:
CachedConstant(cache, Cname, Cfunc)

Parameters:
cache -- atom, name of the cache

Cname -- atom, name of the constant

Cfunc -- expression that evaluates the constant

Description:
This function is used to create precomputed multiple-precision values of constants. Caching these values will save time if they are frequently used.

The call to CachedConstant defines a new function named Cname() that returns the value of the constant at given precision. If the precision is increased, the value will be recalculated as necessary, otherwise calling Cname() will take very little time.

The parameter Cfunc must be an expression that can be evaluated and returns the value of the desired constant at the current precision. (Most arbitrary-precision mathematical functions do this by default.)

The associative list cache contains elements of the form {Cname, prec, value}, as illustrated in the example. If this list does not exist, it will be created.

This mechanism is currently used by N() to precompute the values of Pi and gamma. The name of the cache for N() is CacheOfConstantsN. The code in the function N() assigns unevaluated calls to Pi() and gamma() to the atoms Pi and gamma and declares them LazyGlobal. The result is that the constants will be recalculated only when they are used in the expression under N(). In other words, the code in N() does the equivalent of
Pi := Hold(Pi());
gamma := Hold(gamma());
LazyGlobal(Pi);
LazyGlobal(gamma);
After this, evaluating an expression such as 1/2+gamma will call the function gamma() but not the function Pi().

Example:
In> CachedConstant( my'cache, Ln2, LnNum(2) )
Out> 0.6931471806;
In> Ln2
Out> Ln2;
In> Ln2()
Out> 0.6931471806;
In> [ Precision(20); V( Ln2() ); ]
CachedConstant: Info: constant Ln2 is being
  recalculated at precision 20 
Out> 0.69314718055994530942;
In> my'cache
Out> {{"Ln2",20,0.69314718055994530942}};

See also:
N , Pi() , Precision , gamma .


NewtonNum -- low-level optimized Newton's iterations

Standard library
Calling format:
NewtonNum(func, x0, prec0, order)
NewtonNum(func, x0, prec0)
NewtonNum(func, x0)

Parameters:
func -- a function specifying the iteration sequence

x0 -- initial value (must be close enough to the root)

prec0 -- initial precision (at least 4, default 5)

order -- convergence order (typically 2 or 3, default 2)

Description:
This function is an optimized interface for computing Newton's iteration sequences for numerical solution of equations in arbitrary precision.

NewtonNum will iterate the given function starting from the initial value, until the sequence converges within current precision. Initially, up to 5 iterations at the initial precision prec0 is performed (the low precision is set for speed). The initial value x0 must be close enough to the root so that the initial iterations converge. If the sequence does not produce even a single correct digit of the root after these initial iterations, an error message is printed. The default value of the initial precision is 5.

The order parameter should give the convergence order of the scheme. Normally, Newton iteration converges quadratically (so the default value is order=2) but some schemes converge faster and you can speed up this function by specifying the correct order. (Caution: if you give order=3 but the sequence is actually quadratic, the result will be silently incorrect. It is safe to use order=2.)

Example:
In> Precision(20)
Out> True;
In> NewtonNum({{x}, x+Sin(x)}, 3, 5, 3)
Out> 3.14159265358979323846;

See also:
Newton .


SumTaylorNum -- optimized numerical evaluation of Taylor series

Standard library
Calling format:
SumTaylorNum(x, NthTerm, order)
SumTaylorNum(x, NthTerm, TermFactor, order)

Parameters:
NthTerm -- a function specifying n-th coefficient of the series

x -- number, value of the expansion variable

TermFactor -- a function specifying the ratio of n-th term to the previous one

order -- power of x in the last term

Description:
SumTaylorNum computes a Taylor series Sum(k,0,n,a[k]*x^k) numerically. This function allows very efficient computations of functions given by Taylor series, although some tweaking of the parameters is required for good results.

The coefficients a[k] of the Taylor series are given as functions of one integer variable (k). It is convenient to pass them to SumTaylorNum as closures. For example, if a function a(k) is defined, then
SumTaylorNum(x, {{k}, a(k)}, n)
computes the series Sum(k,0,n,a(k)*x^k).

Often a simple relation between successive coefficients a[k-1], a[k] of the series is available; usually they are related by a rational factor. In this case, the second form of SumTaylorNum should be used because it will compute the series faster. The function TermFactor applied to an integer k>=1 must return the ratio a[k]/a[k-1]. (If possible, the function TermFactor should return a rational number and not a floating-point number.) The function NthTerm must also be given, but the current implementation only calls NthTerm(0) and obtains all other coefficients by using TermFactor.

The algorithm is described in the essays. The number of terms order+1 must be specified and a sufficiently high precision must be set in advance to achieve the desired accuracy.

Example:
To compute 20 digits of Exp(1) using the Taylor series, one needs 21 digits of working precision and 21 terms of the series:

In> Precision(21)
Out> True;
In> SumTaylorNum(1, {{k},1/k!}, {{k},1/k}, 21)
Out> 2.71828182845904523535;
In> RoundTo(N(Ln(%)),20)
Out> 1;

See also:
Taylor .