gfun[rectoproc] - convert a recurrence into a function
|
Calling Sequence
|
|
rectoproc(eqns,u(n), remember, list,params=[a,b,...],<options>)
|
|
Parameters
|
|
eqns
|
-
|
single equation or set of equations
|
u
|
-
|
name; recurrence name
|
n
|
-
|
name; index of recurrence u
|
remember
|
-
|
(optional) name; specify that returned procedure uses option remember
|
list
|
-
|
(optional) name; specify that returned procedure computes the list [u(0), ..., u(n)]
|
params = [a, b, ...]
|
-
|
(optional) names; argument names
|
evalfun = fname
|
-
|
(optional) a function applied at each iteration
|
preargs=[c, d, ...]
|
-
|
(optional) first arguments to this function
|
postargs=[e, f, ...]
|
-
|
(optional) last arguments to this function
|
evalinicond
|
-
|
(optional)
|
whilecond=boolean_expr
|
-
|
(optional) while condition
|
errorcond=boolean_expr
|
-
|
(optional) error condition
|
index
|
-
|
(optional)
|
rhs=expression
|
-
|
(optional)
|
copyright=string
|
-
|
(optional) copyright string to be added to the options
|
extralocal=[g, h, ...]
|
-
|
(optional) names for extra local variables
|
nosymbolsums
|
-
|
(optional)
|
|
|
|
|
Description
|
|
•
|
The rectoproc(eqns, u(n)) command returns a Maple procedure that, given a non-negative integer n as input, returns the nth term u(n) of the linear recurrence.
|
•
|
If remember is specified, the function returns a procedure that uses option remember.
|
•
|
If the optional argument remember is supplied, the procedure returned will use option remember.
|
•
|
If list is specified, the returned procedure computes the list . In this case it runs in a linear number of arithmetic operations.
|
•
|
One of the optional arguments remember or list should be given each time one needs a large number of values of the sequence. When the first terms of the recurrence are not explicitly supplied, they are represented symbolically.
|
•
|
If is specified, the function returns a procedure that accepts n, a, b,... as input.
|
•
|
If the coefficients of the recurrence are nonconstant polynomials, the returned procedure runs in a linear number of arithmetic operations without using extra space if the remember option is not specified.
|
•
|
If the coefficients are constant, the returned procedure runs in a logarithmic number of arithmetic operations without using extra space if the remember option is not specified.
|
•
|
If the optional argument evalfun=fname is given, then the specified procedure is used in the generated code as an evaluation rule. Extra arguments can be passed to the procedure with options preargs= and postargs= . Names declared in preargs and postargs may appear in the argument sequence of the generated procedure if they are also declared with the option params. The function is mapped over initial conditions and it is evaluated before procedure generation if the option evalinicond is supplied.
|
•
|
The option whilecond=boolean_expr defines a condition that is checked at each iteration of the loop of the generated procedure. The condition is represented by a boolean expression that may be function of n, for any positive integer k and function of the names declared in params, preargs and postargs. Execution stops when the condition turns true. This option does not impact initial conditions.
|
•
|
The option errorcond=boolean_expr defines a condition that is checked at each iteration of the loop of the generated procedure. The condition is represented by a boolean expression that may be function of n, for any positive integer k in - where ord is the order of the recurrence - and function of the names declared in params, preargs and postargs. An error is thrown when the condition turns true.
|
•
|
The option extralocal= allows to declare and initialize extra local variables. g, h, ... must be either symbols or equations in the form symbol=expression, in which case the expression is used for initialization.
|
•
|
The option nosymbolsubs disables the name substitution that occurs for the parameters (declared with the option params). This means that the symbols that are used in the generated procedures are the same as the symbols used in the input recurrence. By default, the symbols that are used in the generated procedures are gathered in the global table gfun/rectoproc/symbol.
|
•
|
The option index makes the generated procedure return the list . This is useful in conjunction with whilecond.
|
•
|
You should specify remember or list each time a large number of values for the sequence is required.
|
•
|
The option rhs=expression allows to specify a right hand side of the recurrence that does not have to be a polynomial. The right hand side may be function of n and of the names declared in the options params, preargs and postargs.
|
|
|
Examples
|
|
Fibonacci numbers
You can create different types of programs to generate Fibonacci numbers that meet certain memory or time requirements.
The most obvious procedure is recursive and "remembers" values that are already computed.
>
|
|
| (1) |
>
|
|
>
|
fib1:=rectoproc(fiborec,f(i),remember);
|
| (2) |
>
|
|
| (3) |
To create a program which computes and lists the first terms of the recurrence, we use the list option.
>
|
fib2:=rectoproc(fiborec,f(i),list);
|
![fib2 := proc (n) local i1, loc; loc[0] := 1; loc[1] := 1; if n < 0 then error "index must be %1 or greater", 0 elif n = 0 then return loc[0] elif n = 1 then return loc[1] end if; for i1 to -1+n do loc[i1+1] := loc[-1+i1]+loc[i1] end do; [seq(loc[i1], i1 = 0 .. n)] end proc](/support/helpjp/helpview.aspx?si=6518/file04168/math419.png)
| (4) |
>
|
|
| (5) |
To create a program that is more space conscious we avoid the remember option. In this case the program exploits the fact that the recurrence has constant coefficients for extra efficiency.
>
|
fib3:=rectoproc(fiborec,f(i));
|

| (6) |
>
|
|
| (7) |
>
|
|

| (8) |
An example of right-hand side: nested procedures
This example shows how terms of nested recurrences can be computed. The first sequence is first converted into a procedure:
>
|
|
| (9) |
>
|
u_n := rectoproc(rec_u,u(n),remember);
|
![u_n := proc (n) option remember; table( [( 0 ) = 1, ( 1 ) = 2 ] ) (2*procname(-2+n)+4*procname(-1+n)-(procname(-2+n)+procname(-1+n))*n)/(3+(-3+n)*n) end proc](/support/helpjp/helpview.aspx?si=6518/file04168/math467.png)
| (10) |
The second sequence is defined by
>
|
|
| (11) |
>
|
|
| (12) |
The procedure computing is now generated
>
|
v_n:=rectoproc({op(1,rec_v)}union ini_v,v(n),rhs=subs(u=u_n,op(2,rec_v)),list);
|
![v_n := proc (n) local i1, loc; loc[0] := 1; loc[1] := 2; loc[2] := 3; if n < 0 then error "index must be %1 or greater", 0 elif n = 0 then return loc[0] elif n = 1 then return loc[1] elif n = 2 then return loc[2] end if; for i1 from 2 to -1+n do loc[i1+1] := (u_n(-1+i1)-u_n(-2+i1)*(-2+i1)-(3+(-3+i1)*(i1+1))*loc[-1+i1])/(i1+2) end do; [seq(loc[i1], i1 = 0 .. n)] end proc](/support/helpjp/helpview.aspx?si=6518/file04168/math494.png)
| (13) |
Computation of 10 first terms:
>
|
|
| (14) |
Applying a function at each iteration
We illustrate several ways to compute numerical values of the sequence defined by the following recurrence:
>
|
|
| (15) |
If hardware floating point numbers give a sufficient accuracy, then it is best to produce the procedure and then call evalhf on it:
>
|
p:=subsop(4=NULL,rectoproc(rec,u(n)));
|

| (16) |
>
|
|
| (17) |
If more precision is required, then evalf can be applied at each iteration using
>
|
p2:=rectoproc(rec,u(n),evalfun='evalf');
|
![p2 := proc (n) local i1, loc0, loc1, loc2; table( [( 0 ) = evalf(sin(1)), ( 1 ) = evalf(cos(1)) ] ) loc0 := evalf(sin(1)); loc1 := evalf(cos(1)); if n < 0 then error "index must be %1 or greater", 0 elif n = 0 then return loc0 elif n = 1 then return loc1 end if; for i1 to -1+n do loc2 := evalf(-(5+(-3+i1)*(i1+1))*loc0/i1); loc0 := loc1; loc1 := loc2 end do; loc1 end proc](/support/helpjp/helpview.aspx?si=6518/file04168/math545.png)
| (18) |
>
|

|
| (19) |
It is also possible to give the precision as an argument:
>
|
p3 := rectoproc(rec,u(n),evalfun='evalf',params=[d],postargs=[d]);
|
![p3 := proc (n, b1) local i1, loc0, loc1, loc2; table( [( 0 ) = evalf(sin(1), b1), ( 1 ) = evalf(cos(1), b1) ] ) loc0 := evalf(sin(1), b1); loc1 := evalf(cos(1), b1); if n < 0 then error "index must be %1 or greater", 0 elif n = 0 then return loc0 elif n = 1 then return loc1 end if; for i1 to -1+n do loc2 := evalf(-(5+(-3+i1)*(i1+1))*loc0/i1, b1); loc0 := loc1; loc1 := loc2 end do; loc1 end proc](/support/helpjp/helpview.aspx?si=6518/file04168/math562.png)
| (20) |
>
|
|
| (21) |
Extra local variables
The option extralocal is useful when the values of some parameters are not known until the procedure is executed. In the example below, the recurrence is provided with generic initial conditions. The values of these initial conditions are computed at execution-time.
To create a procedure which computes and lists the first terms of the recurrence, use the list option.
>
|
|
>
|
rectoproc(rec,u(n),params=[p],extralocal=[A='f'(p),B='g'(A,p)]);
|
![proc (n, b1) local i1, loc0, loc1, loc2, xloc1, xloc2; table( [( 0 ) = xloc1, ( 1 ) = xloc2 ] ) xloc1 := f(b1); xloc2 := g(xloc1, b1); loc0 := xloc1; loc1 := xloc2; if n < 0 then error "index must be %1 or greater", 0 elif n = 0 then return loc0 elif n = 1 then return loc1 end if; for i1 to -1+n do loc2 := -(-1+i1)*loc0; loc0 := loc1; loc1 := loc2 end do; loc1 end proc](/support/helpjp/helpview.aspx?si=6518/file04168/math592.png)
| (22) |
|
|