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
Thread Local Data
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.
See Also
You can now reclaim variables such as 'D', 'I' or 'gamma' for use in your calculations.
local gamma := 1;
gamma + gamma;
2
For more information, see: Local Names
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:=1,3,5
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]);
2,3,5
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}));
table1=1,4=4,6=6
Multi-dimensional data is flattened into a one-dimensional array.
M := <1, 2, 3; 4, 5, 6>;
select[flatten](isprime, M);
remove[flatten](isprime, M);
selectremove[flatten](isprime, M);
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];
numbers:=2,5,3,1,8
English := ["two", "five", "three", "one", "eight"];
English:=two,five,three,one,eight
French := ["deux", "cinq", "trois", "un", "huit"];
French:=deux,cinq,trois,un,huit
We would like all of these lists to be in numerical order. We can do this by sorting numbers with the 'output' = 'permutation' option:
p := sort(numbers, 'output' = 'permutation');
p:=4,1,3,2,5
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];
numbers_sorted:=1,2,3,5,8
English_sorted := English[p];
English_sorted:=one,two,three,five,eight
French_sorted := French[p];
French_sorted:=un,deux,trois,cinq,huit
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 numbers. In (3.5) above, we recreate that list as numbers_sorted. We can do that in one step, using the option 'output' = ['permutation', 'sorted']:
p, numbers_sorted := sort(numbers, 'output' = ['permutation', 'sorted']);
p,numbers_sorted:=4,1,3,2,5,1,2,3,5,8
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 := <sin(y^2+x-2*z), y^3*z+x, -x^2+y>;
w := diff~(v,x);
v_sorted, v_perm := sort(v, length, 'output' = ['sorted', 'permutation']);
w_sorted := w[v_perm];
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);
y := Statistics:-Sample(Uniform(-1, 1), n);
inside := select( i -> x[i]^2+y[i]^2 <= 1, [seq(1 .. n)]):
k := numelems(inside);
k:=7890
x := x[inside];
y := y[inside];
x, p := sort(x, (x1,x2)-> abs(x1) < abs(x2), 'output' = ['sorted', 'permutation']):
y := y[p]:
plots:-pointplot(<x,y>, color = ColorTools:-HueSpread('red', k, 1/(2*k)));
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
selectremove, sort
Download Help Document