updatesR4/language - Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : updatesR4/language

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

 

Maple Debugger:

I/O library:

setattribute/attributes:

Type Matching:

Special Parameter Type Declarations:

The algsubs Substitution Facility:

C and Fortran:

Assertions:

Examples

The add and mul Functions:

The map2 Function:

The dismantle function:

Recording and Analysis of Run-time Information:

Maplemint:

Miscellaneous Functions:

Remember Tables:

Language changes:

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) numeric exception: division by zero

  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

Error, (in g) numeric exception: division by zero

  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

open

close

read

fclose

feof

fflush

filepos

fopen

fprintf

fremove

fscanf

iostatus

open

readbytes

readline

sprintf

sscanf

writebytes

writeline

 

• 

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);

Rose

pink

(1)

 Rose

 pink

L:=setattribute( [ Rose, Candy], pink ); attributes(L);

LRose,Candy

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) );

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;

true

a=x,b=3,c=x3

(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] );

y3

y

y3

(5)

 y := 3

   y

  y[3]

• 

uneval: do not evaluate the argument.

g:=proc(x::uneval) print(x) end: g(y), g(y[y]), g(diff(x^25,x));

y

yy

ⅆⅆxx25

(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

33

25x24

(7)

   3

  3[3]

     24

 25 x

y:='y':

• 

New types:

  

type[linear], type[quadratic], type[cubic], type[quartic].

type(x^4+y, quartic(x));

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.

readlib(C):

Error, could not find `C` in the library

a :=proc(x) local y; y:=x+5; y:=sin(y)+3 end:

C(a);

Ca

(10)

 double a(x)

 double x;

 {

   double y;

   y = x+5;

   y = sin(y)+3;

 }

fortran(a);

fortrana

(11)

 real function a(x)

 real x

 real y

   y = x+5

   y = sin(y)+3

 return

 end

  

See fortran[procedure] and C[procedure].

Assertions:

• 

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

Examples

kerneloptsASSERT=true

false

(12)

ASSERT2+2=5,`wrong addition`

Error, assertion failed, `wrong addition`

 Error, assertion failed, wrong addition

kerneloptsASSERT=false

true

(13)

ASSERT2+2=5,`wrong addition`

The add and mul Functions:

• 

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 );

x1x2x3x4x5

(14)

add( a[i]*x^i, i=0..5 );

a5x5+a4x4+a3x3+a2x2+a1x+a0

(15)

                       2         3         4         5

 a[0] + a[1] x + a[2] x  + a[3] x  + a[4] x  + a[5] x

The map2 Function:

• 

map2 is a map() function that expands on the second argument.

map2(op,1, [ x^3, y=z] );

x,y

(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.

readlib(profile):

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.

readlib(maplemint):

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;

esinx+&ExponentialE;y+1

(18)

hasfun(e,exp);

true

(19)

hasfun(e,{sin,cos},x);

true

(20)
• 

hasoption selects an option from a list or set of options:

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

Optionstitle=sinwx&comma;numpoints=99&comma;style=POINT

(21)

 Options :=

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

hasoption( Options, style, 's', 'Options' );

true

(22)

s; Options;

POINT

title=sinwx&comma;numpoints=99

(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));

ez+1lnzz22

(24)

                     2

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

applyop(expand,2,e);

z+1lnzz22

(25)

                2

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

applyop(expand,[2,1],e);

z+1lnz32z

(26)

             3

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

• 

depends checks for mathematical dependence:

restart;

depends(sin(x)+cos(z),{x,y});

true

(27)

depends(int(f(x),x=a..b),x);

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);

11&comma;13&comma;17&comma;19

(29)

remove(isprime, integers);

10&comma;12&comma;14&comma;15&comma;16&comma;18&comma;20

(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;

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);

procn::nonnegintoptionremember&semi;ifn&equals;1orn&equals;0then1elsefibn1&plus;fibn2end ifend proc#(0) = 1#(1) = 1#(2) = 2#(3) = 3#(4) = 5#(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: <x,y,z,t>;

xyzt

(35)
• 

Selection operation l[i..j] where l is a list (set) now returns a list (set).

l := [1,3,5,7,9];

l1&comma;3&comma;5&comma;7&comma;9

(36)

l[1..3];

1&comma;3&comma;5

(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));

wfga&comma;b&comma;hc&comma;d

(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);

pfx&comma;gx&comma;y&comma;z&comma;x

(42)

subsop( [2,3]=w, p );

fx&comma;gx&comma;y&comma;fga&comma;b&comma;hc&comma;d&comma;x

(43)

subsop( [2,0]=h, [2,3]=w, 3=a, p );

fx&comma;hx&comma;y&comma;fga&comma;b&comma;hc&comma;d&comma;a

(44)