Pari/GP Reference Documentation  Contents - Index - Meta commands

Sums, products, integrals and similar functions


intnum   prod   prodeuler   prodinf   solve   sum   sumalt   sumdiv   suminf   sumpos  
 
intnum(X = a,b,expr,{flag = 0})  

numerical integration of expr (smooth in ]a,b[), with respect to X.

Set flag = 0 (or omit it altogether) when a and b are not too large, the function is smooth, and can be evaluated exactly everywhere on the interval [a,b].

If flag = 1, uses a general driver routine for doing numerical integration, making no particular assumption (slow).

flag = 2 is tailored for being used when a or b are infinite. One must have ab > 0, and in fact if for example b = + oo , then it is preferable to have a as large as possible, at least a >= 1.

If flag = 3, the function is allowed to be undefined (but continuous) at a or b, for example the function sin(x)/x at x = 0.

The library syntax is intnum0(entree*e,GEN a,GEN b,char*expr,long flag,long prec).

prod(X = a,b,expr,{x = 1})  

product of expression expr, initialized at x, the formal parameter X going from a to b. As for sum, the main purpose of the initialization parameter x is to force the type of the operations being performed. For example if it is set equal to the integer 1, operations will start being done exactly. If it is set equal to the real 1., they will be done using real numbers having the default precision. If it is set equal to the power series 1+O(X^k) 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(i=1, 100, 1 - X^i);  \\ this has degree 5050 !!

 time = 3,335 ms.  ? prod(i=1, 100, 1 - X^i, 1 + O(X^101))  time = 43 ms.  %2 = 1 - X - X^2 + X^5 + X^7 - X^12 - X^15 + X^22 + X^26 - X^35 - X^40 + \   X^51 + X^57 - X^70 - X^77 + X^92 + X^100 + O(X^101)

The library syntax is produit(entree *ep, GEN a, GEN b, char *expr, GEN x).

prodeuler(X = a,b,expr)  

product of expression expr, initialized at 1. (i.e.to a real number equal to 1 to the current realprecision), the formal parameter X ranging over the prime numbers between a and b.

The library syntax is prodeuler(entree *ep, GEN a, GEN b, char *expr, long prec).

prodinf(X = a,expr,{flag = 0})  

infinite product 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 C.

If flag = 1, do the product of the (1+expr) instead.

The library syntax is prodinf(entree *ep, GEN a, char *expr, long prec) (flag = 0), or prodinf1 with the same arguments (flag = 1).

solve(X = a,b,expr)  

find a real root of expression expr between a and b, under the condition expr(X = a) * expr(X = b) <= 0. This routine uses Brent's method and can fail miserably if expr is not defined in the whole of [a,b] (try solve(x = 1, 2, tan(x)).

The library syntax is zbrent(entree *ep, GEN a, GEN b, char *expr, long prec).

sum(X = a,b,expr,{x = 0})  

sum of expression expr, initialized at x, the formal parameter going from a to b. As for prod, the initialization parameter x may be given to force the type of the operations being performed.

As an extreme example, compare

? sum(i=1, 5000, 1/i); \\ rational number: denominator has 2166 digits.

 time = 1,241 ms.  ? sum(i=1, 5000, 1/i, 0.)  time = 158 ms.  %2 = 9.094508852984436967261245533

The library syntax is somme(entree *ep, GEN a, GEN b, char *expr, GEN x). This is to be used as follows: ep represents the dummy variable used in the expression expr

/* compute a^2 + ... + b^2 */
 {
   /* define the dummy variable "i" */
   entree *ep = is_entry("i");
   /* sum for a <= i <= b */
   return somme(ep, a, b, "i^2", gzero);
 }

sumalt(X = a,expr,{flag = 0})  

numerical summation of the series expr, which should be an alternating series, the formal variable X starting at a.

If flag = 0, use an algorithm of F.Villegas as modified by D.Zagier. This is much better than Euler-Van Wijngaarden's method which was used formerly. Beware that the stopping criterion is that the term gets small enough, hence terms which are equal to 0 will create problems and should be removed.

If flag = 1, use a variant with slightly different polynomials. Sometimes faster.

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

Important hint: a significant speed gain can be obtained by writing the (-1)^X which may occur in the expression as (1.- X%2*2).

The library syntax is sumalt(entree *ep, GEN a, char *expr, long flag, long prec).

sumdiv(n,X,expr)  

sum of expression expr over the positive divisors of n.

Arithmetic functions like sigma use the multiplicativity of the underlying expression to speed up the computation. In the present version 2.1.1, there is no way to indicate that expr is multiplicative in n, hence specialized functions should be prefered whenever possible.

The library syntax is divsum(entree *ep, GEN num, char *expr).

suminf(X = a,expr)  

infinite sum 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 a complex number.

The library syntax is suminf(entree *ep, GEN a, char *expr, long prec).

sumpos(X = a,expr,{flag = 0})  

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. Beware that the stopping criterion is that the term gets small enough, hence terms which are equal to 0 will create problems and should be removed.

If flag = 1, use slightly different polynomials. Sometimes faster.

The library syntax is sumpos(entree *ep, GEN a, char *expr, long flag, long prec).