Using "PDL::NiceSlice" slicing ndarrays becomes so much easier since, first of all, you don't need to
make explicit method calls. No
$pdl->slice(....);
calls, etc. Instead, "PDL::NiceSlice" introduces two ways in which to slice ndarrays without too much
typing:
• using parentheses directly following a scalar variable name, for example
$c = $y(0:-3:4,(0));
• using the so called defaultmethod invocation in which the ndarray object is treated as if it were a
reference to a subroutine (see also perlref). Take this example that slices an ndarray that is part of
a perl list @b:
$c = $b[0]->(0:-3:4,(0));
The format of the argument list is the same for both types of invocation and will be explained in more
detail below.
Parenthesesfollowingascalarvariablename
An arglist in parentheses following directly after a scalar variable name that is not preceded by "&"
will be resolved as a slicing command, e.g.
$x(1:4) .= 2; # only use this syntax on ndarrays
$sum += $x(,(1));
However, if the variable name is immediately preceded by a "&", for example
&$x(4,5);
it will not be interpreted as a slicing expression. Rather, to avoid interfering with the current subref
syntax, it will be treated as an invocation of the code reference $x with argumentlist "(4,5)".
The $x(ARGS) syntax collides in a minor way with the perl syntax. In particular, ``foreach $var(LIST)''
appears like a PDL slicing call. NiceSlice avoids translating the ``for $var(LIST)'' and ``foreach
$var(LIST)'' constructs for this reason. Since you can't use just any old lvalue expression in the
'foreach' 'for' constructs -- only a real perl scalar will do -- there's no functionality lost. If later
versions of perl accept ``foreach <lvalue-expr> (LIST)'', then you can use the code ref syntax, below, to
get what you want.
Thedefaultmethodsyntax
The second syntax that will be recognized is what I called the defaultmethod syntax. It is the method
arrow "->" directly followed by an open parenthesis, e.g.
$x->transpose->(($pos)) .= 0;
Note that this conflicts with the use of normal code references, since you can write in plain Perl
$sub = sub { print join ',', @_ };
$sub->(1,'a');
NOTE: Once "use PDL::NiceSlice" is in effect (you can always switch it off with a line "no
PDL::NiceSlice;" anywhere in the script) the source filter will incorrectly replace the above call to
$sub with an invocation of the slicing method. This is one of the pitfalls of using a source filter that
doesn't know anything about the runtime type of a variable (cf. the Implementation section).
This shouldn't be a major problem in practice; a simple workaround is to use the "&"-way of calling
subrefs, e.g.:
$sub = sub { print join ',', @_ };
&$sub(1,'a');
Whentousewhichsyntax?
Why are there two different ways to invoke slicing? The first syntax $x(args) doesn't work with chained
method calls. E.g.
$x->xchg(0,1)(0);
won't work. It can only be used directly following a valid perl variable name. Instead, use the defaultmethod syntax in such cases:
$x->transpose->(0);
Similarly, if you have a list of ndarrays @pdls:
$y = $pdls[5]->(0:-1);
Theargumentlist
The argument list is a comma separated list. Each argument specifies how the corresponding dimension in
the ndarray is sliced. In contrast to usage of the "slice" in PDL::Slices method the arguments should not
be quoted. Rather freely mix literals (1,3,etc), perl variables and function invocations, e.g.
$x($pos-1:$end,myfunc(1,3)) .= 5;
There can even be other slicing commands in the arglist:
$x(0:-1:$pdl($step)) *= 2;
NOTE: If you use function calls in the arglist make sure that you use parentheses around their argument
lists. Otherwise the source filter will get confused since it splits the argument list on commas that are
not protected by parentheses. Take the following example:
sub myfunc { return 5*$_[0]+$_[1] }
$x = sequence 10;
$sl = $x(0:myfunc 1, 2);
print $sl;
PDL barfed: Error in slice:Too many dims in slice
Caught at file /usr/local/bin/perldl, line 232, pkg main
The simple fix is
$sl = $x(0:myfunc(1, 2));
print $sl;
[0 1 2 3 4 5 6 7]
Note that using prototypes in the definition of myfunc does not help. At this stage the source filter is
simply not intelligent enough to make use of this information. So beware of this subtlety.
Another pitfall to be aware of: currently, you can't use the conditional operator in slice expressions
(i.e., "?:", since the parser confuses them with ranges). For example, the following will cause an error:
$x = sequence 10;
$y = rand > 0.5 ? 0 : 1; # this one is ok
print $x($y ? 1 : 2); # error !
syntax error at (eval 59) line 3, near "1,
For the moment, just try to stay clear of the conditional operator in slice expressions (or provide us
with a patch to the parser to resolve this issue ;).
Modifiers
Following a suggestion originally put forward by Karl Glazebrook the latest versions of "PDL::NiceSlice"
implement modifiers in slice expressions. Modifiers are convenient shorthands for common variations on
PDL slicing. The general syntax is
$pdl(<slice>;<modifier>)
Four modifiers are currently implemented:
• "_" : flatten the ndarray before applying the slice expression. Here is an example
$y = sequence 3, 3;
print $y(0:-2;_); # same as $y->flat->(0:-2)
[0 1 2 3 4 5 6 7]
which is quite different from the same slice expression without the modifier
print $y(0:-2);
[
[0 1]
[3 4]
[6 7]
]
• "|" : sever the link to the ndarray, e.g.
$x = sequence 10;
$y = $x(0:2;|)++; # same as $x(0:2)->sever++
print $y;
[1 2 3]
print $x; # check if $x has been modified
[0 1 2 3 4 5 6 7 8 9]
• "?" : short hand to indicate that this is really a where expression
As expressions like
$x->where($x>5)
are used very often you can write that shorter as
$x($x>5;?)
With the "?"-modifier the expression preceding the modifier is not really a slice expression (e.g.
ranges are not allowed) but rather an expression as required by the where method. For example, the
following code will raise an error:
$x = sequence 10;
print $x(0:3;?);
syntax error at (eval 70) line 3, near "0:"
That's about all there is to know about this one.
• "-" : squeeze out any singleton dimensions. In less technical terms: reduce the number of dimensions
(potentially) by deleting all dims of size 1. It is equivalent to doing a reshape(-1). That can be
very handy if you want to simplify the results of slicing operations:
$x = ones 3, 4, 5;
$y = $x(1,0;-); # easier to type than $x((1),(0))
print $y->info;
PDL: Double D [5]
It also provides a unique opportunity to have smileys in your code! Yes, PDL gives new meaning to
smileys.
Combiningmodifiers
Several modifiers can be used in the same expression, e.g.
$c = $x(0;-|); # squeeze and sever
Other combinations are just as useful, e.g. ";_|" to flatten and sever. The sequence in which modifiers
are specified is not important.
A notable exception is the "where" modifier ("?") which must not be combined with other flags (let me
know if you see a good reason to relax this rule).
Repeating any modifier will raise an error:
$c = $x(-1:1;|-|); # will cause error
NiceSlice error: modifier | used twice or more
Modifiers are still a new and experimental feature of "PDL::NiceSlice". I am not sure how many of you are
actively using them. Pleasedosoandexperimentwiththesyntax. I think modifiers are very useful and
make life a lot easier. Feedback is welcome as usual. The modifier syntax will likely be further tuned
in the future but we will attempt to ensure backwards compatibility whenever possible.
Argumentformats
In slice expressions you can use ranges and secondly, ndarrays as 1D index lists (although compare the
description of the "?"-modifier above for an exception).
• ranges
You can access ranges using the usual ":" separated format:
$x($start:$stop:$step) *= 4;
Note that you can omit the trailing step which then defaults to 1. Double colons ("::") are not
allowed to avoid clashes with Perl's namespace syntax. So if you want to use steps different from the
default you have to also at least specify the stop position. Examples:
$x(::2); # this won't work (in the way you probably intended)
$x(:-1:2); # this will select every 2nd element in the 1st dim
Just as with "slice" in PDL::Slices negative indices count from the end of the dimension backwards with
-1 being the last element. If the start index is larger than the stop index the resulting ndarray will
have the elements in reverse order between these limits:
print $x(-2:0:2);
[8 6 4 2 0]
A single index just selects the given index in the slice
print $x(5);
[5]
Note, however, that the corresponding dimension is not removed from the resulting ndarray but rather
reduced to size 1:
print $x(5)->info
PDL: Double D [1]
If you want to get completely rid of that dimension enclose the index in parentheses (again similar to
the "slice" in PDL::Slices syntax):
print $x((5));
5
In this particular example a 0D ndarray results. Note that this syntax is only allowed with a single
index. All these will be errors:
print $x((0,4)); # will work but not in the intended way
print $x((0:4)); # compile time error
An empty argument selects the whole dimension, in this example all of the first dimension:
print $x(,(0));
Alternative ways to select a whole dimension are
$x = sequence 5, 5;
print $x(:,(0));
print $x(0:-1,(0));
print $x(:-1,(0));
print $x(0:,(0));
Arguments for trailing dimensions can be omitted. In that case these dimensions will be fully kept in
the sliced ndarray:
$x = random 3,4,5;
print $x->info;
PDL: Double D [3,4,5]
print $x((0))->info;
PDL: Double D [4,5]
print $x((0),:,:)->info; # a more explicit way
PDL: Double D [4,5]
print $x((0),,)->info; # similar
PDL: Double D [4,5]
• dummy dimensions
As in "slice" in PDL::Slices, you can insert a dummy dimension by preceding a single index argument
with '*'. A lone '*' inserts a dummy dimension of order 1; a '*' followed by a number inserts a dummy
dimension of that order.
• ndarray index lists
The second way to select indices from a dimension is via 1D ndarrays of indices. A simple example:
$x = random 10;
$idx = long 3,4,7,0;
$y = $x($idx);
This way of selecting indices was previously only possible using "dice" in PDL::Slices
("PDL::NiceSlice" attempts to unify the "slice" and "dice" interfaces). Note that the indexing ndarrays
must be 1D or 0D. Higher dimensional ndarrays as indices will raise an error:
$x = sequence 5, 5;
$idx2 = ones 2,2;
$sum = $x($idx2)->sum;
ndarray must be <= 1D at /home/XXXX/.perldlrc line 93
Note that using index ndarrays is not as efficient as using ranges. If you can represent the indices
you want to select using a range use that rather than an equivalent index ndarray. In particular,
memory requirements are increased with index ndarrays (and execution time may be longer). That said, if
an index ndarray is the way to go use it!
As you might have expected ranges and index ndarrays can be freely mixed in slicing expressions:
$x = random 5, 5;
$y = $x(-1:2,pdl(3,0,1));
ndarraysasindicesinranges
You can use ndarrays to specify indices in ranges. No need to turn them into proper perl scalars with the
new slicing syntax. However, make sure they contain not more than one element! Otherwise a runtime error
will be triggered. First a couple of examples that illustrate proper usage:
$x = sequence 5, 5;
$rg = pdl(1,-1,3);
print $x($rg(0):$rg(1):$rg(2),2);
[
[11 14]
]
print $x($rg+1,:$rg(0));
[
[2 0 4]
[7 5 9]
]
The next one raises an error
print $x($rg+1,:$rg(0:1));
multielement ndarray where only one allowed at XXX/Core.pm line 1170.
The problem is caused by using the 2-element ndarray $rg(0:1) as the stop index in the second argument
":$rg(0:1)" that is interpreted as a range by "PDL::NiceSlice". You can use multielement ndarrays as
index ndarrays as described above but not in ranges. And "PDL::NiceSlice" treats any expression with
unprotected ":"'s as a range. Unprotected means as usual "notoccurringbetweenmatchedparentheses".