updates/Maple17/Programming - Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : updates/Maple17/Programming

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.

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)

 

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:=1,3,5

(2.1)

 

This is not the case for Arrays.

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

a:=1NULL3NULL5

(2.2)

 

This paradigm was mirrored in selectremove.

 

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

2,3,5

(2.3)

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

NULL23NULL5NULL

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

235

(2.5)

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

table1&equals;1&comma;4&equals;4&comma;6&equals;6

(2.6)

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

 

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

M:=123456

(2.7)

select[flatten](isprime, M);

253

(2.8)

remove[flatten](isprime, M);

146

(2.9)

selectremove[flatten](isprime, M);

253&comma;146

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

numbers:=2&comma;5&comma;3&comma;1&comma;8

(3.1)

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

English:=two&comma;five&comma;three&comma;one&comma;eight

(3.2)

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

French:=deux&comma;cinq&comma;trois&comma;un&comma;huit

(3.3)

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&comma;1&comma;3&comma;2&comma;5

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

numbers_sorted:=1&comma;2&comma;3&comma;5&comma;8

(3.5)

English_sorted := English[p];

English_sorted:=one&comma;two&comma;three&comma;five&comma;eight

(3.6)

French_sorted := French[p];

French_sorted:=un&comma;deux&comma;trois&comma;cinq&comma;huit

(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 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&comma;numbers_sorted:=4&comma;1&comma;3&comma;2&comma;5&comma;1&comma;2&comma;3&comma;5&comma;8

(3.8)

English_sorted := English[p];

English_sorted:=one&comma;two&comma;three&comma;five&comma;eight

(3.9)

French_sorted := French[p];

French_sorted:=un&comma;deux&comma;trois&comma;cinq&comma;huit

(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 := <sin(y^2+x-2*z), y^3*z+x, -x^2+y>;

v:=siny2&plus;x2zy3z&plus;xx2&plus;y

(3.11)

w := diff~(v,x);

w:=cosy2&plus;x2z12x

(3.12)

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

v_sorted&comma;v_perm:=x2&plus;yy3z&plus;xsiny2&plus;x2z&comma;3&comma;2&comma;1

(3.13)

w_sorted := w[v_perm];

w_sorted:=2x1cosy2&plus;x2z

(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:= 1 .. 10000 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(3.15)

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

y:= 1 .. 10000 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(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:= 1 .. 7890 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(3.18)

y := y[inside];

y:= 1 .. 7890 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(3.19)

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

 

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

See Also

selectremove, sort


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam