rectoproc - Maple Help

gfun

 rectoproc
 convert a recurrence into a function

 Calling Sequence rectoproc(eqns,u(n), remember, list,params=[a,b,...],)

Parameters

 eqns - single equation, or list 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 the optional argument remember is supplied, the procedure returned will use option remember.
 • If list is specified, the returned procedure computes the list $[u\left(0\right),...,u\left(n\right)]$. 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 $\mathrm{params}=[a,b,...]$ 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= $[c,d,...]$ and postargs= $[ⅇ,f,...]$. 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, $u\left(n-k\right)$ 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, $u\left(n-k\right)$ for any positive integer k in $0..\mathrm{ord}$ - 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= $[g,h,...]$ 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 $\left[n,u\left(n\right)\right]$. 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.

 > $\mathrm{fiborec}≔\left\{f\left(0\right)=1,f\left(1\right)=1,f\left(i\right)=f\left(i-1\right)+f\left(i-2\right)\right\}$
 ${\mathrm{fiborec}}{≔}\left\{{f}{}\left({0}\right){=}{1}{,}{f}{}\left({1}\right){=}{1}{,}{f}{}\left({i}\right){=}{f}{}\left({i}{-}{1}\right){+}{f}{}\left({i}{-}{2}\right)\right\}$ (1)
 > $\mathrm{with}\left(\mathrm{gfun}\right):$
 > fib1:=rectoproc(fiborec,f(i),remember);
 ${\mathrm{fib1}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{remember}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{procname}}{}\left({−}{2}{+}{n}\right){+}{\mathrm{procname}}{}\left({n}{-}{1}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2)
 > $\mathrm{fib1}\left(100\right)$
 ${573147844013817084101}$ (3)

To create a program which computes and lists the first $n$ terms of the recurrence, we use the list option.

 > fib2:=rectoproc(fiborec,f(i),list);
 ${\mathrm{fib2}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{,}{\mathrm{loc}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{0}{]}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{1}{]}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{<}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{error}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{"index must be %1 or greater"}{,}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{0}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{1}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{-}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{\mathrm{i1}}{+}{1}{]}{≔}{\mathrm{loc}}{[}{\mathrm{i1}}{-}{1}{]}{+}{\mathrm{loc}}{[}{\mathrm{i1}}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left[{\mathrm{seq}}{}\left({\mathrm{loc}}{[}{\mathrm{i1}}{]}{,}{\mathrm{i1}}{=}{0}{..}{n}\right)\right]\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (4)
 > $\mathrm{fib2}\left(10\right)$
 $\left[{1}{,}{1}{,}{2}{,}{3}{,}{5}{,}{8}{,}{13}{,}{21}{,}{34}{,}{55}{,}{89}\right]$ (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));
 ${\mathrm{fib3}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{,}{\mathrm{loc0}}{,}{\mathrm{loc1}}{,}{\mathrm{loc2}}{,}{\mathrm{tmp2}}{,}{\mathrm{tmp1}}{,}{\mathrm{i2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{<=}{44}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{<}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{error}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{"index must be %1 or greater"}{,}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{-}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc2}}{≔}{\mathrm{loc0}}{+}{\mathrm{loc1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{loc1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{loc2}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{tmp2}}{≔}\left[\begin{array}{rr}1& 1\\ 1& 0\end{array}\right]{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{tmp1}}{≔}\left[\begin{array}{r}1\\ 1\end{array}\right]{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i2}}{≔}{\mathrm{convert}}{}\left({n}{-}{1}{,}{\mathrm{base}}{,}{2}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i2}}{[}{1}{]}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{tmp1}}{≔}\left[\begin{array}{r}2\\ 1\end{array}\right]\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{in}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{subsop}}{}\left({1}{=}\left({}\right){,}{\mathrm{i2}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{tmp2}}{≔}{\mathrm{LinearAlgebra}}{:-}{\mathrm{MatrixMatrixMultiply}}{}\left({\mathrm{tmp2}}{,}{\mathrm{tmp2}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{tmp1}}{≔}{\mathrm{LinearAlgebra}}{:-}{\mathrm{MatrixVectorMultiply}}{}\left({\mathrm{tmp2}}{,}{\mathrm{tmp1}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{tmp1}}{[}{1}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (6)
 > $\mathrm{fib3}\left(100\right)$
 ${573147844013817084101}$ (7)
 > $\mathrm{fib3}\left(1000\right)$
 ${70330367711422815821835254877183549770181269836358732742604905087154537118196933579742249494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125598767690091902245245323403501}$ (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:

 > $\mathrm{rec_u}≔\left\{u\left(n+2\right)\left({n}^{2}+n+1\right)+u\left(n+1\right)\left(n-2\right)+u\left(n\right)n,u\left(0\right)=1,u\left(1\right)=2\right\}$
 ${\mathrm{rec_u}}{≔}\left\{{u}{}\left({n}{+}{2}\right){}\left({{n}}^{{2}}{+}{n}{+}{1}\right){+}{u}{}\left({n}{+}{1}\right){}\left({-}{2}{+}{n}\right){+}{u}{}\left({n}\right){}{n}{,}{u}{}\left({0}\right){=}{1}{,}{u}{}\left({1}\right){=}{2}\right\}$ (9)
 > u_n := rectoproc(rec_u,u(n),remember);
 ${\mathrm{u_n}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{remember}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left({2}{*}{\mathrm{procname}}{}\left({−}{2}{+}{n}\right){+}{4}{*}{\mathrm{procname}}{}\left({n}{-}{1}\right){-}\left({\mathrm{procname}}{}\left({−}{2}{+}{n}\right){+}{\mathrm{procname}}{}\left({n}{-}{1}\right)\right){*}{n}\right){/}\left({3}{+}\left({−}{3}{+}{n}\right){*}{n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (10)

The second sequence is defined by

 > $\mathrm{rec_v}≔v\left(n+3\right)\left(n+4\right)+v\left(n+1\right)\left({n}^{2}+2n\right)=u\left(n+1\right)-nu\left(n\right)$
 ${\mathrm{rec_v}}{≔}{v}{}\left({n}{+}{3}\right){}\left({n}{+}{4}\right){+}{v}{}\left({n}{+}{1}\right){}\left({{n}}^{{2}}{+}{2}{}{n}\right){=}{u}{}\left({n}{+}{1}\right){-}{u}{}\left({n}\right){}{n}$ (11)
 > $\mathrm{ini_v}≔\left\{v\left(0\right)=1,v\left(1\right)=2,v\left(2\right)=3\right\}$
 ${\mathrm{ini_v}}{≔}\left\{{v}{}\left({0}\right){=}{1}{,}{v}{}\left({1}\right){=}{2}{,}{v}{}\left({2}\right){=}{3}\right\}$ (12)

The procedure computing $v\left(n\right)$ 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);
 ${\mathrm{v_n}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{,}{\mathrm{loc}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{0}{]}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{1}{]}{≔}{2}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{2}{]}{≔}{3}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{<}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{error}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{"index must be %1 or greater"}{,}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{0}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{1}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{2}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{from}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{-}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc}}{[}{\mathrm{i1}}{+}{1}{]}{≔}\left({\mathrm{u_n}}{}\left({\mathrm{i1}}{-}{1}\right){-}{\mathrm{u_n}}{}\left({−}{2}{+}{\mathrm{i1}}\right){*}\left({−}{2}{+}{\mathrm{i1}}\right){-}\left({3}{+}\left({−}{3}{+}{\mathrm{i1}}\right){*}\left({\mathrm{i1}}{+}{1}\right)\right){*}{\mathrm{loc}}{[}{\mathrm{i1}}{-}{1}{]}\right){/}\left({\mathrm{i1}}{+}{2}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left[{\mathrm{seq}}{}\left({\mathrm{loc}}{[}{\mathrm{i1}}{]}{,}{\mathrm{i1}}{=}{0}{..}{n}\right)\right]\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (13)

Computation of 10 first terms:

 > $\mathrm{v_n}\left(9\right)$
 $\left[{1}{,}{2}{,}{3}{,}\frac{{1}}{{2}}{,}{-}\frac{{7}}{{5}}{,}{-}\frac{{17}}{{9}}{,}\frac{{125}}{{49}}{,}\frac{{6803}}{{1092}}{,}{-}\frac{{169567}}{{17199}}{,}{-}\frac{{423697}}{{14105}}\right]$ (14)

Applying a function at each iteration

We illustrate several ways to compute numerical values of the sequence defined by the following recurrence:

 > $\mathrm{rec}≔\left\{u\left(n+2\right)\left(n+1\right)+u\left(n\right)\left({n}^{2}+1\right),u\left(0\right)=\mathrm{sin}\left(1\right),u\left(1\right)=\mathrm{cos}\left(1\right)\right\}$
 ${\mathrm{rec}}{≔}\left\{{u}{}\left({n}{+}{2}\right){}\left({n}{+}{1}\right){+}{u}{}\left({n}\right){}\left({{n}}^{{2}}{+}{1}\right){,}{u}{}\left({0}\right){=}{\mathrm{sin}}{}\left({1}\right){,}{u}{}\left({1}\right){=}{\mathrm{cos}}{}\left({1}\right)\right\}$ (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)));
 ${p}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{,}{\mathrm{loc0}}{,}{\mathrm{loc1}}{,}{\mathrm{loc2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{sin}}{}\left({1}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{cos}}{}\left({1}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{<}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{error}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{"index must be %1 or greater"}{,}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{-}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc2}}{≔}{−}\left({5}{+}\left({−}{3}{+}{\mathrm{i1}}\right){*}\left({\mathrm{i1}}{+}{1}\right)\right){*}{\mathrm{loc0}}{/}{\mathrm{i1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{loc1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{loc2}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (16)
 > $\mathrm{evalhf}\left(p\left(100\right)\right)$
 ${5.27739153869639746}{×}{{10}}^{{76}}$ (17)

If more precision is required, then evalf can be applied at each iteration using

 > p2:=rectoproc(rec,u(n),evalfun='evalf');
 ${\mathrm{p2}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{,}{\mathrm{loc0}}{,}{\mathrm{loc1}}{,}{\mathrm{loc2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{evalf}}{}\left({\mathrm{sin}}{}\left({1}\right)\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{evalf}}{}\left({\mathrm{cos}}{}\left({1}\right)\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{<}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{error}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{"index must be %1 or greater"}{,}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{-}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc2}}{≔}{\mathrm{evalf}}{}\left({−}\left({5}{+}\left({−}{3}{+}{\mathrm{i1}}\right){*}\left({\mathrm{i1}}{+}{1}\right)\right){*}{\mathrm{loc0}}{/}{\mathrm{i1}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{loc1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{loc2}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (18)
 > $\mathrm{Digits}≔30:$$\mathrm{p2}\left(100\right)$
 ${5.27739153869639769206242956896}{×}{{10}}^{{76}}$ (19)

It is also possible to give the precision as an argument:

 > p3 := rectoproc(rec,u(n),evalfun='evalf',params=[d],postargs=[d]);
 ${\mathrm{p3}}{≔}{\mathbf{proc}}\left({n}{,}{\mathrm{b1}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{,}{\mathrm{loc0}}{,}{\mathrm{loc1}}{,}{\mathrm{loc2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{evalf}}{}\left({\mathrm{sin}}{}\left({1}\right){,}{\mathrm{b1}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{evalf}}{}\left({\mathrm{cos}}{}\left({1}\right){,}{\mathrm{b1}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{<}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{error}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{"index must be %1 or greater"}{,}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{-}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc2}}{≔}{\mathrm{evalf}}{}\left({−}\left({5}{+}\left({−}{3}{+}{\mathrm{i1}}\right){*}\left({\mathrm{i1}}{+}{1}\right)\right){*}{\mathrm{loc0}}{/}{\mathrm{i1}}{,}{\mathrm{b1}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{loc1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{loc2}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (20)
 > $\mathrm{p3}\left(100,40\right)$
 ${5.277391538696397692062429568990170086821}{×}{{10}}^{{76}}$ (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 $n$ terms of the recurrence, use the list option.

 > $\mathrm{rec}≔\left\{u\left(n\right)n+u\left(n+2\right),u\left(0\right)=A,u\left(1\right)=B\right\}:$
 > rectoproc(rec,u(n),params=[p],extralocal=[A='f'(p),B='g'(A,p)]);
 ${\mathbf{proc}}\left({n}{,}{\mathrm{b1}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{,}{\mathrm{loc0}}{,}{\mathrm{loc1}}{,}{\mathrm{loc2}}{,}{\mathrm{xloc1}}{,}{\mathrm{xloc2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{xloc1}}{≔}{f}{}\left({\mathrm{b1}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{xloc2}}{≔}{g}{}\left({\mathrm{xloc1}}{,}{\mathrm{b1}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{xloc1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{xloc2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{<}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{error}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{"index must be %1 or greater"}{,}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{-}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc2}}{≔}{−}\left({\mathrm{i1}}{-}{1}\right){*}{\mathrm{loc0}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc0}}{≔}{\mathrm{loc1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}{≔}{\mathrm{loc2}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{loc1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (22)