 updatesR4/language - Help

New Language Features That Have Been Added to Maple V for Release 4

Maple Debugger:

 • The Maple debugger provides a full set of debugging facilities including watchpoints, breakpoints, and single-step, trace-back (immediately) after an error condition.
 > f:=proc(x) g(x+1) end: g:=proc(x) 1/x end: f(-1);

 Error, (in g) division by zero

 > tracelast;
 f called with arguments: -1  #(f,1): g(x+1)
 g called with arguments: 0  #(g,1): 1/x

 f called with arguments: -1 #(f,1): g(x+1) g called with arguments: 0 #(g,1): 1/x Error, (in g) division by zero

I/O library:

 • An extensive I/O library with a complete suite of functions including

 • A new function writedata() serves as a complement to readdata. The function readdata() can take a file descriptor.

setattribute/attributes:

 • The routine setattribute allows users to assign an attribute to an expression. Attributes can be queried using attributes.
 > setattribute(Rose, pink); attributes(Rose);
 ${\mathrm{Rose}}$
 ${\mathrm{pink}}$ (1)

 Rose pink

 > L:=setattribute( [ Rose, Candy], pink ); attributes(L);
 ${L}{≔}\left[{\mathrm{Rose}}{,}{\mathrm{Candy}}\right]$
 ${\mathrm{pink}}$ (2)

 L := [Rose, Candy] pink

Type Matching:

 • A new type matching facility, typematch, simplifies Maple coding.
 > typematch( x^3,  c::(a::name ^ b::integer) );
 ${\mathrm{true}}$ (3)
 > printf(a=%a, b=%a, c=%a \n,a, b, c );
 a=x, b=3, c=x^3
 > a := 'a': b := 'b': c := 'c':
 A name as the third argument gets a replacement list (instead of doing the assignments).
 > typematch( x^3, c::(a::name ^ b::integer), 's'); s;
 ${\mathrm{true}}$
 $\left[{a}{=}{x}{,}{b}{=}{3}{,}{c}{=}{{x}}^{{3}}\right]$ (4)

 true 3 [a = x, b = 3, c = x ]

Special Parameter Type Declarations:

 • evaln: evaluate the argument to a name.
 > f:=proc(x::evaln) print(x) end:  y:=3; f(y), f( y[y] );
 ${y}{≔}{3}$
 ${y}$
 ${{y}}_{{3}}$ (5)

 y := 3 y y

 • uneval: do not evaluate the argument.
 > g:=proc(x::uneval) print(x) end: g(y), g(y[y]), g(diff(x^25,x));
 ${y}$
 ${{y}}_{{y}}$
 $\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}\left({{x}}^{{25}}\right)$ (6)

 y y[y] d   25 -- x dx

 Compare this behavior with the normal evaluation:
 > h:=proc(x) print(x) end: h(y), h(y[y]), h(diff(x^25,x));
 ${3}$
 ${{3}}_{{3}}$
 ${25}{}{{x}}^{{24}}$ (7)

 3 3 24 25 x

 > y:='y':
 • New types:
 > type(x^4+y, quartic(x));
 ${\mathrm{true}}$ (8)

The algsubs Substitution Facility:

 • There is a new substitution facility algsubs  which is more general than the present subs command.
 > algsubs( x+y=5, x+y+z );
 ${5}{+}{z}$ (9)

C and Fortran:

 • Complete Fortran and C subroutines from Maple procedures. For C, the ansi form can be requested.
 > a :=proc(x) local y; y:=x+5; y:=sin(y)+3 end:
 > C(a);
 ${C}{}\left({a}\right)$ (10)

 double a(x) double x; { double y; y = x+5; y = sin(y)+3; }

 > fortran(a);
 ${\mathrm{fortran}}{}\left({a}\right)$ (11)

 real function a(x) real x real y y = x+5 y = sin(y)+3 return end

Assertions:

 • The function ASSERT is used to guarantee pre- and post-conditions while a Maple procedure is executing.

Examples

 > $\mathrm{kernelopts}\left(\mathrm{ASSERT}=\mathrm{true}\right)$
 ${\mathrm{false}}$ (12)
 > $\mathrm{ASSERT}\left(2+2=5,\mathrm{wrong addition}\right)$

 > $\mathrm{kernelopts}\left(\mathrm{ASSERT}=\mathrm{false}\right)$
 ${\mathrm{true}}$ (13)
 > $\mathrm{ASSERT}\left(2+2=5,\mathrm{wrong addition}\right)$

 • The add function is used to add up an explicit sequence of values. The mul function computes a product of an explicit sequence of values. Although one can use the for-loop statement to obtain the same effect, add/mul are generally more efficient than the for-loop versions because the for-loop versions construct many intermediate sums and products.
 > L := [seq(i, i=1..5)]:
 > mul( x-i, i=L );
 $\left({x}{-}{1}\right){}\left({x}{-}{2}\right){}\left({x}{-}{3}\right){}\left({x}{-}{4}\right){}\left({x}{-}{5}\right)$ (14)
 ${{a}}_{{5}}{}{{x}}^{{5}}{+}{{a}}_{{4}}{}{{x}}^{{4}}{+}{{a}}_{{3}}{}{{x}}^{{3}}{+}{{a}}_{{2}}{}{{x}}^{{2}}{+}{{a}}_{{1}}{}{x}{+}{{a}}_{{0}}$ (15)

 2         3         4         5 a + a x + a x  + a x  + a x  + a x

The map2 Function:

 • map2 is a map() function that expands on the second argument.
 > map2(op,1, [ x^3, y=z] );
 $\left[{x}{,}{y}\right]$ (16)

The dismantle function:

 • The routine dismantle displays internal representation of data structures.
 > readlib(dismantle)( [ 1, x+3, y^x ] );
 LIST(2)    EXPSEQ(4)       INTPOS(2): 1       SUM(5)          NAME(4): x          INTPOS(2): 1          INTPOS(2): 3          INTPOS(2): 1       POWER(3)          NAME(4): y          NAME(4): x

 LIST(2) EXPSEQ(4) INTPOS(2): 1 SUM(5) NAME(4): x INTPOS(2): 1 INTPOS(2): 3 INTPOS(2): 1 POWER(3) NAME(4): y NAME(4): x

Recording and Analysis of Run-time Information:

 • Various routines to support the recording and analysis of runtime information are provided: profile, showprofile, exprofile, unprofile, excallgraph.
 > fib:=proc(n) option remember; if n<2 then n else fib(n-1)+fib(n-2) fi;end:
 > profile(fib);
 > fib(5);
 ${5}$ (17)
 > showprofile(fib);
 function           depth    calls     time    time%         bytes   bytes% --------------------------------------------------------------------------- fib                    5        9    0.001   100.00          6480   100.00 --------------------------------------------------------------------------- total:                 5        9    0.001   100.00          6480   100.00

 function           depth    calls     time    time%         bytes   bytes% --------------------------------------------------------------------------- fib                    5        9    0.000     0.00          9648   100.00 --------------------------------------------------------------------------- total:                 5        9    0.000     0.00          9648   100.00

Maplemint:

 • maplemint generates semantic information for a procedure and also checks for sections of code which can never be executed.
 > a:=proc()    local b; global c;    if (b=5) then    b:=6;    RETURN(true);    lprint(test);    fi; end:
 > maplemint(a);
 Procedure a()   These names were used as global names but were not declared:  test   These global variables were declared, but never used:  c   These local variables were used before they were assigned a value:  b   There is unreachable code following a RETURN or return statement at       statement 4: lprint(test)

 This code is unreachable: lprint(test) These global variables were declared, but never used: c These local variables were used before they were assigned a value: b

Miscellaneous Functions:

 • hasfun tests an expression for a specified function:
 > e := sin(x)+exp(y)+1;
 ${e}{≔}{\mathrm{sin}}{}\left({x}\right){+}{{ⅇ}}^{{y}}{+}{1}$ (18)
 > hasfun(e,exp);
 ${\mathrm{true}}$ (19)
 > hasfun(e,{sin,cos},x);
 ${\mathrm{true}}$ (20)
 • hasoption selects an option from a list or set of options:
 > Options := [title=sin(w*x), numpoints=99, style=POINT];
 ${\mathrm{Options}}{≔}\left[{\mathrm{title}}{=}{\mathrm{sin}}{}\left({w}{}{x}\right){,}{\mathrm{numpoints}}{=}{99}{,}{\mathrm{style}}{=}{\mathrm{POINT}}\right]$ (21)

 Options := [title = sin(w x), numpoints = 99, style = POINT]

 > hasoption( Options, style, 's', 'Options' );
 ${\mathrm{true}}$ (22)
 > s; Options;
 ${\mathrm{POINT}}$
 $\left[{\mathrm{title}}{=}{\mathrm{sin}}{}\left({w}{}{x}\right){,}{\mathrm{numpoints}}{=}{99}\right]$ (23)

 POINT [title = sin(w x), numpoints = 99]

 • applyop applies a function to specified operand(s) of an expression:
 > e := (z+1)*ln(z*(z^2-2));
 ${e}{≔}\left({z}{+}{1}\right){}{\mathrm{ln}}{}\left({z}{}\left({{z}}^{{2}}{-}{2}\right)\right)$ (24)

 2 e := (z + 1) ln(z (z  - 2))

 > applyop(expand,2,e);
 $\left({z}{+}{1}\right){}{\mathrm{ln}}{}\left({z}{}\left({{z}}^{{2}}{-}{2}\right)\right)$ (25)

 2 (z + 1) ln(z (z  - 2))

 > applyop(expand,[2,1],e);
 $\left({z}{+}{1}\right){}{\mathrm{ln}}{}\left({{z}}^{{3}}{-}{2}{}{z}\right)$ (26)

 3 (z + 1) ln(z  - 2 z)

 • depends checks for mathematical dependence:
 > restart;
 > depends(sin(x)+cos(z),{x,y});
 ${\mathrm{true}}$ (27)
 > depends(int(f(x),x=a..b),x);
 ${\mathrm{false}}$ (28)
 • remove:
 The command remove does the opposite of select. It removes items from a list, set, sum, product, or function according to a boolean value.
 > integers := [\$10..20]:
 > select(isprime, integers);
 $\left[{11}{,}{13}{,}{17}{,}{19}\right]$ (29)
 > remove(isprime, integers);
 $\left[{10}{,}{12}{,}{14}{,}{15}{,}{16}{,}{18}{,}{20}\right]$ (30)
 • ispoly is a predicate function for testing and picking apart polynomials:
 > ispoly( 3*x^2-11*x+5, quadratic,x, 't0','t1','t2'); t0,t1,t2;
 ${\mathrm{true}}$
 ${5}{,}{-11}{,}{3}$ (31)

 true 5, -11, 3

 • coeff:
 coeff(a,x,i); where a is a polynomial in x works without the need of using collect.
 > coeff( (1+x+x^2)^100000, x, 5 );
 ${83341666458332500020000}$ (32)

Remember Tables:

 • Remember tables of functions can be printed together with the functions:
 > interface(verboseproc=3):
 > fib:=proc(n::nonnegint) option remember;    if n=1 or n=0 then 1 else fib(n-1)+fib(n-2) fi end:
 > fib(5);
 ${8}$ (33)
 > print(fib);
 ${\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}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{=}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{or}}\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}}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{fib}}{}\left({n}{-}{1}\right){+}{\mathrm{fib}}{}\left({n}{-}{2}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{\text{#(0) = 1}}{\text{#(1) = 1}}{\text{#(2) = 2}}{\text{#(3) = 3}}{\text{#(4) = 5}}{\text{#(5) = 8}}$ (34)

 proc(n::nonnegint) option remember; if n = 1 or n = 0 then 1 else fib(n - 1) + fib(n - 2) end if end proc # fib(0) = 1 # fib(1) = 1 # fib(2) = 2 # fib(3) = 3 # fib(4) = 5 # fib(5) = 8

Language changes:

 • :: (new) binding operator, which binds variables to the matched components in type tests, and in parameter declaration.
 • The <> operator is now user definable; <|> and <||> are now removed.
 • <> gets transformed upon entry to a call to the anglebracket() function.
 > restart;
 > anglebracket:=proc() f([ args ]) end: ;
 $\left[\begin{array}{c}x\\ y\\ z\\ t\end{array}\right]$ (35)
 • Selection operation l[i..j] where l is a list (set) now returns a list (set).
 > l := [1,3,5,7,9];
 ${l}{≔}\left[{1}{,}{3}{,}{5}{,}{7}{,}{9}\right]$ (36)
 > l[1..3];
 $\left[{1}{,}{3}{,}{5}\right]$ (37)
 • Negative selectors can now be used. The -1th element is the last, the -2th the second last, and so on:
 > l[-2];
 ${7}$ (38)
 • Extraction of operands from an expression (op):  if the first argument to op is a list, the elements of the list refer to a sub-operand of the input at the increasing levels of nesting:
 > w := f(g(a,b),h(c,d));
 ${w}{≔}{f}{}\left({g}{}\left({a}{,}{b}\right){,}{h}{}\left({c}{,}{d}\right)\right)$ (39)
 > op([2,1],w) = op(1, op(2, w) );
 ${c}{=}{c}$ (40)
 > op([-1,-1],w);
 ${d}$ (41)
 • Similarly for subsop:
 > p := f(x,g(x,y,z),x);
 ${p}{≔}{f}{}\left({x}{,}{g}{}\left({x}{,}{y}{,}{z}\right){,}{x}\right)$ (42)
 > subsop( [2,3]=w, p );
 ${f}{}\left({x}{,}{g}{}\left({x}{,}{y}{,}{f}{}\left({g}{}\left({a}{,}{b}\right){,}{h}{}\left({c}{,}{d}\right)\right)\right){,}{x}\right)$ (43)
 > subsop( [2,0]=h, [2,3]=w, 3=a, p );
 ${f}{}\left({x}{,}{h}{}\left({x}{,}{y}{,}{f}{}\left({g}{}\left({a}{,}{b}\right){,}{h}{}\left({c}{,}{d}\right)\right)\right){,}{a}\right)$ (44)
 >