Language and Programming - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : System : Information : Updates : Maple 2019 : updates/Maple2019/Language

Language and Programming

Maple 2019 includes the following enhancements to the Maple language and programming facilities.

 

Maple Language

New Commands

Kernel Options

Command Line Interface

Maple Language

Two Variable for-in Loops

Maple's for-in loop iterates over a container (list, Array, etc.), setting the loop's control variable to the value of each element in turn:

for i in [a,b,c] do
    print(i)
end do;

a

b

c

(1.1.1)

Usually, having just the element's value is sufficient, but sometimes its index into the container is also needed. The new two-variable form of the for-in loop makes this straightforward:

R:= LinearAlgebra:-RandomMatrix(3,4);

3227999274829314694467

(1.1.2)

for ind, val in R do
   if val < 0 then R[ind] := -val end if
end do:

R;

3227999274829314694467

(1.1.3)

Using Assignments, if- statements, try- statements, and Loops in Expressions

It is now possible to use statements within expressions. The supported statements are: assignments, if-statements, try-statements, and all forms of loop-statements.

An assignment can be written within an expression by enclosing it in parentheses. The value of an assignment expression is the value that was assigned (that is, the evaluated right-hand side of the assignment).

x:=2:

if x<>0 and (invx :=1/x) <1 then print(invx) end if:

12

(1.2.1)

i:=2:

do i until (i:=nextprime(i)) > 15;

2

3

5

7

11

13

(1.2.2)

The if- and try- expressions can be written anywhere within an expression:

x:=-3;

x−3

(1.2.3)

signedSqr := x* if x<0 then -x else x end if;

signedSqr−9

(1.2.4)

The value of such an expression is the value of the last expression computed in a statement sequence within it (i.e. the value that % would have if the expression were evaluated as a statement).

Loop expressions must generally be enclosed in parentheses, except in a few unambiguous cases: as the sole entry in a list or set, or the entire right-hand side of an assignment or return statement.

 

N :=5;

N5

(1.2.5)

[for i to N do i end do];

1&comma;2&comma;3&comma;4&comma;5

(1.2.6)

sqrSeq := for i to N do i^2 end do;

sqrSeq1,4,9,16,25

(1.2.7)

Unlike a loop-statement, the value of a loop expression is a sequence of the last values computed in each iteration of the loop.

Storage for the result of loop-expressions is allocated efficiently. In most cases, the amount of storage required can be determined in advance, so it is all pre-allocated. When it cannot be determined in advanced, it is grown in a way that is still O(N) in the number of iterations, instead of the O(N^2) cost when appending one element at a time.

Loop expressions make it easy to generate sequences whose length cannot be readily predetermined. For example, this generates a list of all primes less than some upper bound.

 

N:=100;

N100

(1.2.8)

[(i:=2), (while (i:= nextprime(i)) <N do i end do)]        

2&comma;3&comma;5&comma;7&comma;11&comma;13&comma;17&comma;19&comma;23&comma;29&comma;31&comma;37&comma;41&comma;43&comma;47&comma;53&comma;59&comma;61&comma;67&comma;71&comma;73&comma;79&comma;83&comma;89&comma;97

(1.2.9)

Note that input of these expression-statements is currently not supported by 2-D input in the Standard interface.

Increment, Decrement, and Assignment Operators

Maple now supports C-style increment, decrement, and assignment operators. Rather than having to write,

N := N+1;

N101

(1.3.1)

one can write,

N+=1;

N102

(1.3.2)

or:

++N;

N103

(1.3.3)

Besides being shorter to type, in cases where N is expensive to compute, this is more efficient. Consider,

A[expensiveOperation()] := A[expensiveOperation()] + 3;

 

compared to:

A[expensiveOperation()] += 3;

In the latter, expensiveOperation() is called only once.

The increment and decrement operators, ++ and --, can be applied either before or after the value to be incremented or decremented. If applied before, then the result of expression is the new value. If applied after, the result is the previous value:

N := 1;

N1

(1.3.4)

a := ++N;

N2

a2

(1.3.5)

b := N++;

N3

b2

(1.3.6)

N;

 

3

The assignment operators, like ordinary := assignments, can be used within an expression by enclosing the entire assignment in parentheses.

The complete set of assignment operators is:

 +=, -=, *=, /=, mod=, ^=, .=, and=, or=, xor=, implies=,
    union=, intersect=, minus=, and ,=

The last one, the `,=` operator, is special. In most cases, it constructs an expression sequence of the left and right-hand sides, assigning it to the left-hand side:

 

s := 1;

s1

(1.3.7)

s ,= 2, 3;

s1,2,3

(1.3.8)

s;

1,2,3

(1.3.9)

However, when it is applied to one-dimensional Arrays or any object implementing a `,=` method, the right-hand side is appended to the container in-place:

A := Array([a,b,c]);

22c

(1.3.10)

refA := A;

22c

(1.3.11)

A ,= d:

A, refA;

22cd,22cd

(1.3.12)

Note that input of these assignment operators is currently not supported by 2-D input in the Standard interface.

Efficient Mutable Strings and the String Function

Strings in Maple are immutable. This makes them very efficient to use as values, but less efficient to manipulate.

a := "Hello"; b:= "world";

aHello

bworld

(1.4.1)

a := cat(a," ",b);

aHello world

(1.4.2)

After the above has executed, `a` has the value "Hello world", but the string "Hello" still exists in memory until the garbage collector dispose of it. Thus, the following is inefficient:

s := "";

s

(1.4.3)

for c from "a" to "z" do s := cat(s,c) end do:

s;

abcdefghijklmnopqrstuvwxyz

(1.4.4)

This successively produces the strings "a", "ab", "abc", etc., leaving each previous result to be cleaned up later.

Maple 2019 allows mutable strings to be represented as one-dimensional Arrays of 1-byte integers. Such an Array can be constructed explicitly,

s := Array([], datatype=integer[1]);

s

(1.4.5)

or using the new notation:

s:= Array("");

s

(1.4.6)

The Array can also be constructed and initialized in a single call:

s := Array("Hello");

72101108108111

(1.4.7)

The `,=` assignment operator can be used to append to the Array, in-place:

s ,= " ";

7210110810811132

(1.4.8)

s ,= "world";

More than one item can be appended at once, including other such arrays:

7210110810811132119111114108100

(1.4.9)

question := Array("How are you?");

7211111932971141013212111111763

(1.4.10)

s ,= "! ", question, "\n";

The printf "%s" format understands these Arrays:

72101108108111321191111141081003332721111193297114101321211111176310

(1.4.11)

printf("The string: %s",s);

The string: Hello world! How are you?

The new String function can convert this back to a regular immutable string:

t := String(s);

tHello world! How are you?

(1.4.12)

The String function can also convert and concatenate multiple arguments into a string:

s := Array("Hello");

72101108108111

(1.4.13)

String(s, " world! The answer is ", 7!/5!);

Hello world! The answer is 42

(1.4.14)

Neutral Operators

The set of valid characters for symbolic neutral operators has been expanded. Previously, the allowable symbols after the initial "&" character were:

! " $ * + , - . / < = > ? @ \ ^ ~

Now, the first character after the "&" can also be one of these:

% & ' ( ) : ; [ ] ` { | }

The second and subsequent characters after the "&" can be from the original set, or these additional characters:

) ] | }

Neutral operators are usually left-associative:

a &x  b &y c;

Hello world&xworld&y{

(1.5.1)

If a neutral operator starts with "&&" and has at least one additional character, then it is right associative:

a &&x  b &&y c;

Hello world&&xworld&&y{

(1.5.2)

Note that input of the new neutral operators is currently not supported by 2-D input in the Standard interface.

Local Scoping

Maple 2019 removes the requirement that all procedure and module local and global variables, and module exported variables, be declared at the beginning of the procedure or module before any executable statement.

Declarations of exported variables can appear anywhere within the top-level statement sequence of a module, and global declarations can appear in any statement sequence within a procedure or module. Local variable declarations are even more flexible, and can appear as statements, within assignment expressions, and in the control variable specification of for-loops.

Variable declarations have a defined scope, and the same variable can be declared in more than one scope as long as the declarations are compatible, and one scope is not contained within the other.

For more information, see local_scopes.

This feature is not supported in 2-D input.

Miscellaneous

Operator procedures, defined using the arrow notation, may now have local variable declarations:

iota := n -> local i; (for i to n do i od):

iota(5);

1,2,3,4,5

(1.7.1)

Note that input of local variable declarations for operator procedures is currently not supported by 2-D input in the Standard interface.

The `in` membership test operator now works even if the member being tested evaluates to NULL:

n := 3;

n3

(1.7.2)

evalb(n in {1,2,3});

true

(1.7.3)

n := NULL;

n

(1.7.4)

evalb(n in {1,2,3});

false

(1.7.5)

Previously, cases where the candidate might be NULL had to be protected like this:

evalb(n <> NULL and n in {1,2,3});

false

(1.7.6)

The `lprint` command can now be invoked with an optional index argument, [2], which will cause it to line-break any procedures that it prints. Unlike the output produced by the pretty-printer, which will attempt to pack several statements onto one line if an entire statement sequence fits, `lprint[2]` will line-break and indent in a completely consistent manner, and will not center the result on the screen:

f := proc(x,y) local a; if x < y then y-x else a := x-y; a^2 end if end proc;

fprocx&comma;ylocala&semi;ifx<ytheny − xelsea ≔ x − y&semi;a&Hat;2end ifend proc

(1.7.7)

lprint[2](eval(f));

proc( x, y )
    local a;
    if x < y then
        y-x;
    else

        a := x-y;
        a^2;
    end if;
end proc

The "%a" and "%A" formats of the printf, sprintf, and fprintf functions can now be modified by the "Z" modifier, which instructs them to produce output equivalent to the `lprint[2]` output above. In conjunction with sprintf, this effectively allows lprinting to a string:

s := sprintf("%Za",eval(f));

sproc( x, y ) local a; if x < y then y-x; else a := x-y; a^2; end if; end proc

(1.7.8)

New Commands

ArrayTools

with(ArrayTools):

Partition

The Partition command partially sorts a data structure around a given order statistic.

 

v := [5, 2, 5, 2, 3, 4, 4, 5, 3, 1, 5, 2, 3, 2, 2, 4, 3, 3, 1, 2]:

Partition can be used to find the third-smallest entry of v.

w := Partition(v, 3)

w1&comma;1&comma;2&comma;2&comma;2&comma;2&comma;2&comma;2&comma;3&comma;3&comma;3&comma;3&comma;3&comma;4&comma;4&comma;4&comma;5&comma;5&comma;5&comma;5

(2.1.1.1)

w[3]

2

(2.1.1.2)

We see that w__3&equals;2, so the third-smallest entry of v is 2.

Remove

The Remove command removes entries and shrinks an Array.

 

A := Array([11, 22, 33, 44, 55]):

Remove A[3] and shrink:

Remove(A, 3)

11224455

(2.1.2.1)

Reverse

The Reverse command reverses the entries of a Matrix, Vector, or Array.

A := Array([1, 2, 3, 4]):

Reverse(A)

4321

(2.1.3.1)

convert/english

The convert/english command is not new, but it now allows for round-tripping of integer to english conversions.

en := convert(-1234567890, 'english');

enminus one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety

(2.2.1)

n := convert( en, 'english');

n−1234567890

(2.2.2)

Download

The Download command now makes it possible to download data from a URL to a local file.

tablereverse

The tablereverse function takes a table and returns a new table that is a reversal of the initial table.

T := table(["apple" = "fruit", "orange" = "fruit", "zucchini" = "vegetable", "milk" = "dairy"])

Ttablezucchini=vegetable&comma;milk=dairy&comma;orange=fruit&comma;apple=fruit

(2.4.1)

tablereverse(T)

tablefruit=apple&comma;orange&comma;vegetable=zucchini&comma;dairy=milk

(2.4.2)

Kernel Options

The kernelopts command has several new options:

• 

traceshowspecassign: boolean; If true, causes some assignments shown during tracing to appear in a distinctive way. The default is false.

• 

tracesplitmulassign: boolean; If true, causes multiple assignments shown during tracing to appear individually. The default is false.

Command Line Interface

New Interface Options

Several new options to the interface command have been introduced. These primarily affect the command-line interface:

The trailingsemicolon option, which is false by default, causes a semicolon to be printed after the last statement in each statement sequence when a procedure is printed (by lprint, print, or the "%a" printf format).

The compactdisplay option, which is true by default, tells the pretty-printer to pack multiple statements onto one line when they fit. If set to false, then multiple statements are usually printed on separate lines, the exception being when the entire single statement (e.g. an if-statement) containing the multiple statements fits on one line.

The existing errorbreak option, which controls what happens when Maple is reading a script of commands from a file, has a new allowed value of 3. This is equivalent to interface(errorbreak=2), meaning that reading should stop on any error, but has the additional effect of issuing a stack trace, as if the user had typed the tracelast command after the error occurred. This option can also be set by passing -e3 when invoking Maple from the system command line.

The echofile option tells Maple to echo all input and output of a session to a log file. The value of this option should be a string, specifying the name of the file. If the specified file name ends in ".html" or ".htm", the session is logged in HTML format, with syntax highlighting, otherwise it is logged as plain text. If an empty string or the string "none" is specified, echoing to a file is terminated. This option can be invoked from the very beginning of a session by using the --echofile=filename option when invoking Maple from the system command line.

Color Output

When used with a compatible terminal, Maple can now do color syntax highlighting for both output and user input. This new capability is controlled by the following interface options:

Option

Description

ansi=true or ansi=false

Controls whether any form of highlighting is used at all.

ansicolor=list, ansicolor=integer, ansicolor=[]

Controls the choice of colors used. If set to a list, the list specifies the colors for different categories of output. An integer in the range of 0 to 6 selects one of several predefined color palettes. An empty list produces output that uses bold, italic, and underline
attributes, but no colors.

ansilprint=true or ansilprint=false

Controls whether lprinted output is syntax highlighted.

ansiedit=true or ansiedit=false

Controls whether command-line input is syntax highlighted.

showcolors

Illustrates the current syntax highlight color palette.

showtermcolors

Shows what colors the terminal is capable of displaying.

 

Color support is enabled by default if the current terminal is known to support it. It can be manually controlled using the interface options described above, or these can be put in the user's Maple initialization file. See ansicolor for more information.

Color support has also been extended to the character plot driver, which can be selected using interface(plotdevice=colorchar). See plot/device.

Likewise, the ImageTools:-Preview command can now produce color character output (color "ASCII art"), which is much more representative of the actual image than a monochrome character preview was. See ImageTools,Preview for details.

Command History Manipulation

The command-line interface adds several new facilities for interactively manipulating, replaying, saving, and restoring the history of commands entered into Maple. See edit_history for details, or type "!!?" at the Maple prompt for a brief summary.

Debugger

The output of the showstat command has been improved with respect to the display of long lines, especially in-line procedures and statements that would previously display as one long unbroken line.

A new debugger command, edit, will open the source file containing the currently stopped procedure, at the line corresponding to the current statement. A corresponding Maple command, editsource(), will do the same for a specified procedure and statement number. Both require that the source code is available, and that kernelopts(keepdebuginfo) was set to true or Maple was started with the --keep-debug-info option on the system command line.

The help command in the debugger now displays a summarized version of the information on the debugger help page. An optional argument to the help command will display help for only those debugger commands for which the argument is a prefix (e.g. "help stop" will display help for stopat, stopwhen, etc.).

Mint

A new option for the stand-alone mint utility (normally used to find potential issues in Maple programs) now has an option to generate a "ctags" format file. This is a file that can be used by many text editors to find the implementation of a given procedure when a special key is pressed (or menu item invoked) on a call to that procedure. It was originally designed for the "C" programming language, but works equally well with other languages so long as there exists a tool to generate the tag file. The "-c" option to Mint, which should be followed by the desired name of the tag file, will do that for all the files it processes during one call. Typically, one would call mint on the top level module of a package, resulting in a tag file for the entire package.

In keeping with the theme of coloring command line output, mint can now also produce color output if the system environment variable MINT_COLORS is set appropriately. This is analogous to similar schemes used by Linux command line utilities such as grep (GREP_COLORS) or gcc (GCC_COLORS).

See mint for more details.