Language and Programming - Maple Programming Help

 Language and Programming

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

Coding in Maple

Code Edit Regions

There are numerous upgrades and enhancements for Code Edit Region embedded components.

 • Visual updates include the code edit region's width automatically fits the entire window.

 • Code edit regions automatically display command completion lists.
 • It is now possible to cursor into and out of code edit regions. Previously the only way to enter a code edit region was using the mouse.

Code Editor

The Code Editor (Startup code and Embedded Component code) has also been updated with several new features and several usability improvements. To access the Startup code editor, click on the gears icon ( ) on the Maple toolbar.

 • A new console window at the bottom of the code editor makes it possible to test the startup code from directly inside the code editor.
 • Using maplemint, the code editor gives a greater level of information about programming errors.
 • The code editor automatically displays command completion lists.
 • Errors are now shown inline and the code editor displays a notification strip for errors in the scroll bar on the right side of the window.
 • Automatic text wrapping improves readability.
 • Menu items were added for cut, copy, paste, undo, and redo.

mint and maplemint

mint and maplemint generate static analysis information for procedures, giving parameter naming conflicts, unreachable code, unused parameters or variables, and more.  The mint.exe tool is available for use with text files that contain Maple code, and maplemint() is a command that can be given Maple procedures to analyze.

 > a:=proc() local b,i;    b:=6;    while (b>0) do       for i from 1 to 6 do          break;          printf(%d,i);       end do;       for i in [1,3,5] do          lprint(i);          break;          lprint(test);       end do;       b:=b-1;    end do; end proc:
 > maplemint(a);
 Procedure a()   These names were used as global names but were not declared:  %d, test   There is unreachable code following a break statement at statement 5:       printf(%d,i)   There is unreachable code following a break statement at statement 9:       lprint(test)

In Maple 2018 the implementation of the external mint.exe tool was merged together with the maplemint() command so they could both analyze code in the same way.  As a result, the maplemint() command now works on modules and reports a wider class of warnings, and the external tool now supports the following new warnings:

 > m := module()     export p1 := proc( x )        global G;        x^2;     end proc:     export p2 := proc( y )        local a, b;        a := b*y;        b := 42;        a + b;     end proc:     export p3 := proc( z )        with(LinearAlgebra);        Determinant(z);     end proc: end module:
 > maplemint(m);
 Nested Procedure p1( x ) on lines 1 to 2   These global variables were declared, but never used:  G Nested Procedure p2( y ) on lines 3 to 4   These local variables were used before they were assigned a value:  b Nested Procedure p3( z ) on lines 5 to 6   Ineffectual call to with() inside a proc at statement 1: with(LinearAlgebra)

Inline Assignments

Assignments can now be used as, or embedded within, expressions. The assignment must be enclosed in parentheses. The result of the assignment is the evaluated right-hand side.

 > g := x -> (x-1,x+1);
 ${x}{↦}\left({x}{-}{1}{,}{x}{+}{1}\right)$ (2.1)
 > g((z := 2));
 ${1}{,}{3}$ (2.2)
 > z
 ${2}$ (2.3)

See assignment.

Until Clause in For Loops

An optional until clause has been added to Maple's loop control structure. When used, this clause is written in place of end do, and consists of the keyword until, followed by a Boolean expression. When the expression evaluates to true after the loop's body has executed, the loop terminates.

Since the until clause is checked at the end of the loop, a loop with an until clause and no other terminating condition (a to or while part) will execute its body at least once.

For example, find the next prime after a given number using the isprime function.

 > n := 37; do n := n+1 until     isprime(n); n;
 ${n}{≔}{37}$
 ${n}{≔}{38}$
 ${n}{≔}{39}$
 ${n}{≔}{40}$
 ${n}{≔}{41}$
 ${41}$ (3.1)

See do.

StringTools

DecodeEntities and EncodeEntities encode and decode strings with HTML entities.

 > with(StringTools):
 > r := DecodeEntities("エンコード");
 ${r}{≔}{"ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰"}$ (4.1)
 > r := EncodeEntities( r ): lprint(r);
 "エンコード"

Compare the Value and Structure of Expressions

SameStructure accepts two expressions, inert or otherwise, and compares their structure.  If they are the same in value and structure then this command returns true, otherwise false.

 > with(InertForm):
 > SameStructure(y = 1-x, y = 1-x)
 ${\mathrm{true}}$ (5.1)
 > SameStructure(1-x = y, y = 1-x)
 ${\mathrm{false}}$ (5.2)

select, remove, and selectremove

The select, remove, and selectremove commands can now perform substitutions in-place in Arrays, Matrices, Vectors, tables, and objects. This functionality is specified by appending inplace as an index to the command name.

When selectremove[inplace] is called, the first result is computed in-place, and the removed elements are returned in a new container that is returned as the second result.

 > selectremove[inplace](type, [1,2,3,4,5,6,7,8,9], odd);
 $\left[{1}{,}{3}{,}{5}{,}{7}{,}{9}\right]{,}\left[{2}{,}{4}{,}{6}{,}{8}\right]$ (6.1)

For details, see select.

 Additional Updates Object overloading of the select, remove, and selectremove functions, as well as andmap and ormap, is now supported.  For details, see object/builtins. The RandomTools:-Generate command offers a new flavor called variable, and adds an exclude option to RandomTools:-Generate(integer(...)). The new RandomTools:-RandomExpand command produces a new expression in expanded form that is equivalent to the given expression.