
Language & Programming


 
Updates and changes in Language & Programming:

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

 (2.1) 
This is not the case for Arrays.
>

a := Array(1 .. 5, {1 = 1, 2 = NULL, 3 = 3, 4 = NULL, 5 = 5});

 (2.2) 
This paradigm was mirrored in selectremove.
>

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

 (2.3) 
>

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

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

 (2.5) 
>

remove[flatten](isprime, table({1 = 1, 2 = 2, 3 = 3, 4 = 4, 5 = 5, 6 = 6}));

 (2.6) 
Multidimensional data is flattened into a onedimensional array.
>

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

 (2.7) 
>

select[flatten](isprime, M);

 (2.8) 
>

remove[flatten](isprime, M);

 (2.9) 
>

selectremove[flatten](isprime, M);

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

 (3.1) 
>

English := ["two", "five", "three", "one", "eight"];

 (3.2) 
>

French := ["deux", "cinq", "trois", "un", "huit"];

 (3.3) 
We would like all of these lists to be in numerical order. We can do this by sorting with the 'output' = 'permutation' option:
>

p := sort(numbers, 'output' = 'permutation');

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

 (3.5) 
>

English_sorted := English[p];

 (3.6) 
>

French_sorted := French[p];

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

p, numbers_sorted := sort(numbers, 'output' = ['permutation', 'sorted']);

 (3.8) 
>

English_sorted := English[p];

 (3.9) 
>

French_sorted := French[p];

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

 (3.11) 
 (3.12) 
>

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

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

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

 (3.15) 
>

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

 (3.16) 
>

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

 (3.17) 
 (3.18) 
 (3.19) 
>

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

