Sums, products, integrals and similar functions.

Although the GP calculator is programmable, it is useful to have preprogrammed a number of loops, including sums, products, and a certain number of recursions. Also, a number of functions from numerical analysis like numerical integration, summation of series or plotting will be described here.

One of the parameters in these loops must be the control variable, hence a simple variable name. The last parameter must be any legal PARI expression, including of course expressions using loops. Since it is much easier to program directly the loops in library mode, these functions are mainly useful for GP programming. The use of these functions in library mode is a little tricky and its explanation will be omitted, although the reader can try and figure it out by himself by reading the source code. A brief explanatory sketch is given in section 3.9.7. Hence in this section we do not give the library syntax.

The letter X will always denote any simple variable name, and represents the formal parameter used in the function.

divsum (n, X, expr): sum of expression expr over the positive divisors of n.

In the present version 1.35, n is restricted to being less than 231.

hvector (n, X, expr): creates a row vector with n components whose components are the expression expr evaluated at the integral points between 1 and n.

(numerical) integration:numerical integration A number of Romberg-like integration methods are implemented. The user should not require too much accuracy: 18 or 28 decimal digits is OK, but not much more. In addition, analytical cleanup of the integral must have been done: there must be no singularities in the interval or at the boundaries. In practice this can be accomplished with a simple change of variable. Furthermore, for improper integrals, where one or both of the limits of integration are plus or minus infinity, the function must decrease sufficiently rapidly at infinity. This can often be accomplished through integration by parts.

Note that [*] can be represented with essentially no loss of accuracy by 1e4000. However beware of real underflow when dealing with rapidly decreasing functions. For example, if one wants to compute the $\int_{0}^{\infty}$e-x2 dx to 28 decimal digits, one should set infinity equal to 10 for example, and cetrainly not to 1e4000.

The integrand may have values belonging to a vector space over the real numbers; in particular, it can be complex-valued or vector-valued.

intgen (X = a, b, expr): general driver routine for doing numerical integration from a to b of the expression expr, with respect to the formal variable X.

intinf (X = a, b, expr): numerical integration from a to b, tailored for being used when a or b are infinite. One must have ab > 0, and in fact if for example b = ∞, then it is preferable to have a as large as possible, at least a≥1.

intnum (X = a, b, expr): simple numerical integration from a to b. This is the fastest method, and should be used when a and b are not too large, the function is smooth, and can be evaluated exactly everywhere on the interval [a, b].

intopen (X = a, b, expr): numerical integration from a to b. This method should be used, again when a and b are not too large, but when the function is now allowed to be undefined (but continuous) at a or b, for example the function sin(x)/x at x = 0.

Two summation methods are also implemented, for alternating series, and for series with terms of the same sign (see 3.8.18 and 3.8.20).

matrix (m, n, X, Y, expr): creation of the m×n matrix whose coefficients are given by the expression expr. There are two formal parameters in expr, the first one (X) corresponding to the rows, the second (Y) to the columns, and X goes from 1 to m, Y goes from 1 to n.

plot (X = a, b, expr): crude plot of the function represented by expression expr from a to b.

ploth (X = a, b, expr): high precision plot of the function y = f (x) represented by the expression expr, x going from a to b. Since this involves around 1000 function calls, it is advised to keep the current precision to a minimum (i.e. 9) before calling ploth.

Note that this function is very much implementation dependent and has been written for the moment for the Macintosh, for the SUN under Sunview/Suntools and for the X-window system (release X11). An Atari/gem version is also available upon request.

ploth2 (T = a, b, expr): high precision plot of the curve represented in parametric form by x = f (t), y = g(t), represented by the expression expr, t going from a to b. The expression must be a two component vector. Since this involves around 2000 function calls, it is advised to keep the current precision to a minimum (i.e. 9) before calling ploth2.

Note that, as for ploth above, this function has been written for the moment only for the Macintosh, for the SUN under Sunview/Suntools and for the X-window system (release X11). An Atari/gem version is also available upon request.

prod (x, X = a, b, expr): product of expression expr, initialized at x, the formal parameter X going from a to b. As for sum, the initialization parameter x must be given: its main purpose is to force the type of the operations being performed. For example if it is put equal to the integer 1, operations will start being done exactly. If it is put equal to the real 1., they will be done using real numbers having the default precision. If it is put equal to the power series 1 + O(Xk) for a certain k, they will be done using power series of precision at most k. These are the three most common initializations.

As an extreme example, compare

prod(1, j = 1, 100,(1 - X$\displaystyle \hat{{\ }}$j))

with

prod(1 + O(X$\displaystyle \hat{{\ }}$101), j = 1, 100,(1 - X$\displaystyle \hat{{\ }}$j)).

prodeuler (X = a, b, expr): product of expression expr, initialized at 1., the formal parameter X ranging over the prime numbers between a and b.Euler product

prodinf (X = a, expr): [*] of expression expr, the formal parameter X starting at a. The evaluation stops when the relative error of the expression minus 1 is less than the default precision. The expressions must always evaluate to an element of $\Bbb$C.

prodinf1 (X = a, expr): infinite product of expression 1 + expr, the formal parameter X starting at a. The evaluation stops when the relative error of the expression is less than the default precision. The expressions must always evaluate to an element of $\Bbb$C.

solve (X = a, b, expr): real root of expression expr between a and b, under the condition expr(X = a)*expr(X = b)≤ 0. The method used is Brent's method.

sum (x, X = a, b, expr): sum of expression expr, initialized at x, the formal parameter going from a to b. As for prod, the initialization parameter x must be given: its main purpose is to force the type of the operations being performed. For example if it is put equal to the integer 0, operations will start being done exactly. If it is put equal to the real 0., they will be done using real numbers having the default precision. If it is put equal to the power series O(Xk) for a certain k, they will be done using power series of precision at most k. These are the three most common initializations.

As an extreme example, compare

sum(0, j = 1, 1000, 1/j)

with

sum(0., j = 1, 1000, 1/j).

sumalt (X = a, expr): numerical summation of the series expr, which should be an [*], the formal variable X starting at a. The algorithm used is Euler's method of finite differences combined with a trick due to van Wijngaarden.

Divergent alternating series can sometimes be summed by this method, as well as series which are not exactly alternating (see for example section 3.9.5).

suminf (X = a, expr): [*] of expression expr, the formal parameter X starting at a. The evaluation stops when the relative error of the expression is less than the default precision. The expressions must always evaluate to an element of $\Bbb$C.

sumpos (X = a, expr): numerical summation of the series expr, which must be a series of terms having the same sign, the formal variable X starting at a. The algorithm used is van Wijngaarden's trick for converting such a series into an alternating one, and is quite slow.

vector (n, X, expr): creates a row vector with n components whose components are the expression expr evaluated at the integral points between 1 and n (identical with hvector).

vvector (n, X, expr): creates a column vector with n components whose components are the expression expr evaluated at the integral points between 1 and n.