Programming Enhancements - Maple Help

Home : Support : Online Help : System : Information : Updates : Maple 17 : Programming Enhancements

 Language & Programming

Updates and changes in Language & Programming:

 • Reclaiming the Use of the Names 'I', 'D', 'gamma', and More
 • Selecting or removing items from an Array or Table
 • Sort with the output option

Designed for mathematical computation, the Maple language combines the best principles from procedural, functional, and object-oriented programming. Maple 17 adds many useful features to your Maple programming toolkit. Maple 17 has added several changes to how you interact with variables including improving subscript handling, reclaiming the use of variable names like 'I', 'D', and 'Gamma', and adding support for thread-local variables.  Maple 17 also features new options and functionalities for core commands such as sort and select / remove.

Reclaiming the Use of the Names 'I', 'D', 'gamma', and More

You can now reclaim variables such as 'D', 'I' or 'gamma' for use in your calculations.

 > local gamma := 1;
 > gamma + gamma;
 ${2}$ (1.1)

Selecting or removing items from an Array or Table

The select, remove, and selectremove commands are ideal for searching for data. The default behavior of these commands is to return a result object of exactly the same size and dimensions as the one you passed in, with NULL's in place of removed items.  Automatic flattening of NULLs makes the behavior different in lists compared to Arrays.



NULL is automatically removed from lists.



 > l := [1, NULL, 3, NULL, 5];
 ${l}{:=}\left[{1}{,}{3}{,}{5}\right]$ (2.1)

This is not the case for Arrays.



 > a := Array(1 .. 5, {1 = 1, 2 = NULL, 3 = 3, 4 = NULL, 5 = 5});
 ${a}{:=}\left[\begin{array}{ccccc}{1}& {\mathrm{NULL}}& {3}& {\mathrm{NULL}}& {5}\end{array}\right]$ (2.2)

This paradigm was mirrored in selectremove.

 > select(isprime, [1, 2, 3, 4, 5, 6]);
 $\left[{2}{,}{3}{,}{5}\right]$ (2.3)
 > select(isprime, <1, 2, 3, 4, 5, 6>);
 $\left[\begin{array}{c}{\mathrm{NULL}}\\ {2}\\ {3}\\ {\mathrm{NULL}}\\ {5}\\ {\mathrm{NULL}}\end{array}\right]$ (2.4)

Now, in Maple 17, you can use an option to force the removal of NULL from arrays and tables. The option, flatten, is noted in square brackets after the command name.

 > select[flatten](isprime, < 1, 2, 3, 4, 5, 6>);
 $\left[\begin{array}{r}{2}\\ {3}\\ {5}\end{array}\right]$ (2.5)
 > remove[flatten](isprime, table({1 = 1, 2 = 2, 3 = 3, 4 = 4, 5 = 5, 6 = 6}));
 ${\mathrm{table}}\left(\left[{1}{=}{1}{,}{4}{=}{4}{,}{6}{=}{6}\right]\right)$ (2.6)



Multi-dimensional data is flattened into a one-dimensional array.

 > M := <1, 2, 3; 4, 5, 6>;
 ${M}{:=}\left[\begin{array}{rrr}{1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$ (2.7)
 > select[flatten](isprime, M);
 $\left[\begin{array}{ccc}{2}& {5}& {3}\end{array}\right]$ (2.8)
 > remove[flatten](isprime, M);
 $\left[\begin{array}{ccc}{1}& {4}& {6}\end{array}\right]$ (2.9)
 > selectremove[flatten](isprime, M);
 $\left[\begin{array}{ccc}{2}& {5}& {3}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {4}& {6}\end{array}\right]$ (2.10)



Sort with the output option

A new feature in Maple 17 is the output option for the sort command. It can be used by programmers to find out what reordering, or permutation, is applied to a list or Vector in order to sort it, rather than just the sorted result. This is often useful if there are multiple lists of corresponding elements that all need to be sorted according to the values of one of these lists.

For example, suppose we have corresponding lists of English and French words for numbers:

 > numbers := [2, 5, 3, 1, 8];
 ${\mathrm{numbers}}{:=}\left[{2}{,}{5}{,}{3}{,}{1}{,}{8}\right]$ (3.1)
 > English := ["two", "five", "three", "one", "eight"];
 ${\mathrm{English}}{:=}\left[{"two"}{,}{"five"}{,}{"three"}{,}{"one"}{,}{"eight"}\right]$ (3.2)
 > French := ["deux", "cinq", "trois", "un", "huit"];
 ${\mathrm{French}}{:=}\left[{"deux"}{,}{"cinq"}{,}{"trois"}{,}{"un"}{,}{"huit"}\right]$ (3.3)

We would like all of these lists to be in numerical order. We can do this by sorting $\mathrm{numbers}$ with the 'output' = 'permutation' option:

 > p := sort(numbers, 'output' = 'permutation');
 ${p}{:=}\left[{4}{,}{1}{,}{3}{,}{2}{,}{5}\right]$ (3.4)



This tells us that the first element of the sorted list is the fourth element of the original list (because the first entry of $p$ is four); the second element of the sorted list is the first element of the original list, and so on. Thus, we can obtain the sorted lists as follows:

 > numbers_sorted := numbers[p];
 ${\mathrm{numbers_sorted}}{:=}\left[{1}{,}{2}{,}{3}{,}{5}{,}{8}\right]$ (3.5)
 > English_sorted := English[p];
 ${\mathrm{English_sorted}}{:=}\left[{"one"}{,}{"two"}{,}{"three"}{,}{"five"}{,}{"eight"}\right]$ (3.6)
 > French_sorted := French[p];
 ${\mathrm{French_sorted}}{:=}\left[{"un"}{,}{"deux"}{,}{"trois"}{,}{"cinq"}{,}{"huit"}\right]$ (3.7)

There is a slightly more efficient way of achieving this same result: In order to find the permutation $p$, Maple already created the sorted version of $\mathrm{numbers}$. In (3.5) above, we recreate that list as $\mathrm{numbers_sorted}$. We can do that in one step, using the option 'output' = ['permutation', 'sorted']:

 > p, numbers_sorted := sort(numbers, 'output' = ['permutation', 'sorted']);
 ${p}{,}{\mathrm{numbers_sorted}}{:=}\left[{4}{,}{1}{,}{3}{,}{2}{,}{5}\right]{,}\left[{1}{,}{2}{,}{3}{,}{5}{,}{8}\right]$ (3.8)
 > English_sorted := English[p];
 ${\mathrm{English_sorted}}{:=}\left[{"one"}{,}{"two"}{,}{"three"}{,}{"five"}{,}{"eight"}\right]$ (3.9)
 > French_sorted := French[p];
 ${\mathrm{French_sorted}}{:=}\left[{"un"}{,}{"deux"}{,}{"trois"}{,}{"cinq"}{,}{"huit"}\right]$ (3.10)

The output option is applicable when sorting a list, a Vector, or a one-dimensional Array. It can be used together with any predefined or custom sort order.

In this example, we sort a Vector and its element-wise derivative by length:

 > v := ;
 ${v}{:=}\left[\begin{array}{c}{\mathrm{sin}}{}\left({{y}}^{{2}}{+}{x}{-}{2}{}{z}\right)\\ {{y}}^{{3}}{}{z}{+}{x}\\ {-}{{x}}^{{2}}{+}{y}\end{array}\right]$ (3.11)
 > w := diff~(v,x);
 ${w}{:=}\left[\begin{array}{c}{\mathrm{cos}}{}\left({{y}}^{{2}}{+}{x}{-}{2}{}{z}\right)\\ {1}\\ {-}{2}{}{x}\end{array}\right]$ (3.12)
 > v_sorted, v_perm := sort(v, length, 'output' = ['sorted', 'permutation']);
 ${\mathrm{v_sorted}}{,}{\mathrm{v_perm}}{:=}\left[\begin{array}{c}{-}{{x}}^{{2}}{+}{y}\\ {{y}}^{{3}}{}{z}{+}{x}\\ {\mathrm{sin}}{}\left({{y}}^{{2}}{+}{x}{-}{2}{}{z}\right)\end{array}\right]{,}\left[{3}{,}{2}{,}{1}\right]$ (3.13)
 > w_sorted := w[v_perm];
 ${\mathrm{w_sorted}}{:=}\left[\begin{array}{c}{-}{2}{}{x}\\ {1}\\ {\mathrm{cos}}{}\left({{y}}^{{2}}{+}{x}{-}{2}{}{z}\right)\end{array}\right]$ (3.14)

Finally, in the following example, we take random points in the unit circle (using the rejection method). We then sort them by absolute value of the x-coordinate and color them in order.

 > n := 10^4:
 > x := Statistics:-Sample(Uniform(-1, 1), n);
 ${x}{:=}\left[\begin{array}{c}{\mathrm{1 .. 10000}}{{\mathrm{Vector}}}_{{\mathrm{row}}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (3.15)
 > y := Statistics:-Sample(Uniform(-1, 1), n);
 ${y}{:=}\left[\begin{array}{c}{\mathrm{1 .. 10000}}{{\mathrm{Vector}}}_{{\mathrm{row}}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (3.16)
 > inside := select( i -> x[i]^2+y[i]^2 <= 1, [seq(1 .. n)]):
 > k := numelems(inside);
 ${k}{:=}{7890}$ (3.17)
 > x := x[inside];
 ${x}{:=}\left[\begin{array}{c}{\mathrm{1 .. 7890}}{{\mathrm{Vector}}}_{{\mathrm{row}}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (3.18)
 > y := y[inside];
 ${y}{:=}\left[\begin{array}{c}{\mathrm{1 .. 7890}}{{\mathrm{Vector}}}_{{\mathrm{row}}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (3.19)
 > x, p := sort(x, (x1,x2)-> abs(x1) < abs(x2), 'output' = ['sorted', 'permutation']):
 > y := y[p]:
 > plots:-pointplot(, color = ColorTools:-HueSpread('red', k, 1/(2*k)));

 Thread Local Data Maple 17 has support for declaring variables that can store a different value for each thread that accesses the variable.  This allows algorithms to be implemented that need to maintain state, without needing to worrying about that state being modified by other threads.  This makes it easier to implement thread safe versions of those algorithms.     For more information, see: Thread Local Data