LREtools - Maple Programming Help

Home : Support : Online Help : Mathematics : Factorization and Solving Equations : LREtools : LREtools/REtoproc

LREtools

 REtoproc
 convert a recurrence into a procedure

 Calling Sequence REtoproc(problem, options)

Parameters

 problem - problem options - optional arguments described below

Description

 • This procedure returns a Maple procedure that, given a non-negative integer n as input, gives the nth term of the linear recurrence.
 • If the argument 'remember' or 'remember'=true is specified, then the procedure will compute the sequence via a recursive procedure using option remember. By default, 'remember'=false and the sequence is computed iteratively.
 • The argument 'remember' should be given when one needs a large number of values of the sequence.
 • When 'remember'=false and the coefficients of the recurrence are nonconstant polynomials, the returned procedure will run in a straight loop, using a linear number of arithmetic operations and without wasting extra space.
 • When 'remember'=false and the coefficients are constant, the procedure will employ a binary-based optimization strategy (for large index values only), and runs with a logarithmic number of arithmetic operations. This optimization can be disabled by specifying constantoptimize=false.
 • When 'remember'=false, by default the initial terms of the recurrence are embedded in the code. This can be changed to put the initial terms of the recurrence in the remember table by specifying initialremember=true.
 • When the first terms of the recurrence are not explicitly supplied, they are represented symbolically.
 • The command with(LREtools,REtoproc) allows the use of the abbreviated form of this command.

Examples

 > $\mathrm{with}\left(\mathrm{LREtools}\right):$$\mathrm{interface}\left(\mathrm{verboseproc}=3\right):$
 > fib := REtoproc(f(i)=f(i-1)+f(i-2),f(i),{f(0)=0,f(1)=1});
 ${\mathrm{fib}}{≔}{\mathbf{proc}}\left({n}{::}{\mathrm{nonnegint}}{,}{}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{,}{\mathrm{u0}}{,}{\mathrm{u1}}{,}{\mathrm{u2}}{,}{a}{,}{\mathrm{res}}{,}{l}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{u0}}{≔}{0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{u1}}{≔}{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}}{\mathrm{u0}}\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}}{\mathrm{u1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\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}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\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{u2}}{≔}{\mathrm{u0}}{+}{\mathrm{u1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{u0}}{≔}{\mathrm{u1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{u1}}{≔}{\mathrm{u2}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{u0}}{+}{\mathrm{u1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{a}{≔}{\mathrm{array}}{}\left({1}{..}{2}{,}{1}{..}{2}{,}\left[\left[{1}{,}{1}\right]{,}\left[{1}{,}{0}\right]\right]\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{res}}{≔}{\mathrm{array}}{}\left({1}{..}{2}{,}\left[{\mathrm{u1}}{,}{\mathrm{u0}}\right]\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{l}{≔}{\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}}{l}{[}{1}{]}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{res}}{≔}{\mathrm{array}}{}\left({1}{..}{2}{,}\left[{1}{=}{a}{[}{1}{,}{1}{]}{*}{\mathrm{res}}{[}{1}{]}{+}{a}{[}{1}{,}{2}{]}{*}{\mathrm{res}}{[}{2}{]}{,}{2}{=}{a}{[}{2}{,}{1}{]}{*}{\mathrm{res}}{[}{1}{]}{+}{a}{[}{2}{,}{2}{]}{*}{\mathrm{res}}{[}{2}{]}\right]\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{l}{=}\left[{1}\right]\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{res}}{[}{1}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{in}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{l}{[}{2}{..}{−}{2}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{a}{≔}{\mathrm{array}}{}\left({1}{..}{2}{,}{1}{..}{2}{,}\left[\left({1}{,}{2}\right){=}{a}{[}{1}{,}{1}{]}{*}{a}{[}{1}{,}{2}{]}{+}{a}{[}{1}{,}{2}{]}{*}{a}{[}{2}{,}{2}{]}{,}\left({2}{,}{2}\right){=}{a}{[}{1}{,}{2}{]}{*}{a}{[}{2}{,}{1}{]}{+}{a}{[}{2}{,}{2}{]}{^}{2}{,}\left({2}{,}{1}\right){=}{a}{[}{1}{,}{1}{]}{*}{a}{[}{2}{,}{1}{]}{+}{a}{[}{2}{,}{1}{]}{*}{a}{[}{2}{,}{2}{]}{,}\left({1}{,}{1}\right){=}{a}{[}{1}{,}{1}{]}{^}{2}{+}{a}{[}{1}{,}{2}{]}{*}{a}{[}{2}{,}{1}{]}\right]\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{res}}{≔}{\mathrm{array}}{}\left({1}{..}{2}{,}\left[{1}{=}{a}{[}{1}{,}{1}{]}{*}{\mathrm{res}}{[}{1}{]}{+}{a}{[}{1}{,}{2}{]}{*}{\mathrm{res}}{[}{2}{]}{,}{2}{=}{a}{[}{2}{,}{1}{]}{*}{\mathrm{res}}{[}{1}{]}{+}{a}{[}{2}{,}{2}{]}{*}{\mathrm{res}}{[}{2}{]}\right]\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}}\left({a}{[}{1}{,}{1}{]}{^}{2}{+}{a}{[}{1}{,}{2}{]}{*}{a}{[}{2}{,}{1}{]}\right){*}{\mathrm{res}}{[}{1}{]}{+}\left({a}{[}{1}{,}{1}{]}{*}{a}{[}{1}{,}{2}{]}{+}{a}{[}{1}{,}{2}{]}{*}{a}{[}{2}{,}{2}{]}\right){*}{\mathrm{res}}{[}{2}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (1)
 > $\mathrm{fib}\left(100\right)$
 ${354224848179261915075}$ (2)
 > fib2 := REtoproc(f(i)=f(i-1)+f(i-2),f(i),{f(0)=0,f(1)=1},'remember');
 ${\mathrm{fib2}}{≔}{\mathbf{proc}}\left({n}{::}{\mathrm{nonnegint}}{,}{}\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({n}{-}{2}\right){+}{\mathrm{procname}}{}\left({n}{-}{1}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{\text{#(0) = 0}}{\text{#(1) = 1}}$ (3)
 > $\mathrm{fib2}\left(100\right)$
 ${354224848179261915075}$ (4)
 > rec := REtoproc(f(i)=i^2*f(i-1)+f(i-2),f(i),{},'remember');
 ${\mathrm{rec}}{≔}{\mathbf{proc}}\left({n}{::}{\mathrm{nonnegint}}{,}{}\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}}{n}{^}{2}{*}{\mathrm{procname}}{}\left({n}{-}{1}\right){+}{\mathrm{procname}}{}\left({n}{-}{2}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{\text{#(0) = f(0)}}{\text{#(1) = f(1)}}$ (5)
 > $\mathrm{rec}\left(100\right)$
 ${9058477158744039108901484875567483833514619274297483550856232216805415823966784458605109749449017462079827918537250156488348819998559536412999537100386489240766568099262887636990141071242906533135806432242520067765428965147272705161620549611736618446171403767978982293333281882312513474668524116583587067499136325876}{}{f}{}\left({1}\right){+}{2203823124569416698291075403023306063089022127683403942005208386556556750073849332403233124551522569729148712230224505771905205727903820151912839068030542989769962446264480000034834468134656141631555555405058769399965103438352643881658179110007643158335872008434125847040069584050780045011709372240155319761920733361}{}{f}{}\left({0}\right)$ (6)
 > 

 Please add your Comment (Optional) E-mail Address (Optional) What is ? This question helps us to combat spam