
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 objectoriented 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 threadlocal 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.
For more information, see: Local Names


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

This paradigm was mirrored in selectremove.
>

select(isprime, [1, 2, 3, 4, 5, 6]);

$\left[{2}{\,}{3}{\,}{5}\right]$
 (2.2) 
>

select(isprime, <1, 2, 3, 4, 5, 6>);

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

>

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.3) 
${}$
Multidimensional data is flattened into a onedimensional array.
>

M := <1, 2, 3; 4, 5, 6>;

>

select[flatten](isprime, M);

>

remove[flatten](isprime, M);

>

selectremove[flatten](isprime, M);

${}$${}$


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 onedimensional Array. It can be used together with any predefined or custom sort order.
In this example, we sort a Vector and its elementwise derivative by length:
>

v := <sin(y^2+x2*z), y^3*z+x, x^2+y>;

>

v_sorted, v_perm := sort(v, length, 'output' = ['sorted', 'permutation']);

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 xcoordinate and color them in order.
>

x := Statistics:Sample(Uniform(1, 1), n);

>

y := Statistics:Sample(Uniform(1, 1), n);

>

inside := select( i > x[i]^2+y[i]^2 <= 1, [seq(1 .. n)]):

>

x, p := sort(x, (x1,x2)> abs(x1) < abs(x2), 'output' = ['sorted', 'permutation']):

>

plots:pointplot(<x,y>, 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

${}$
