Slicing is a basic, extremely common operation, and PDL's slice method would be cumbersome to use in many cases. PDL::NiceSlice rectifies that by incorporating new slicing syntax directly into the language via a perl source filter (see the perlfilter man page). NiceSlice adds no new functionality, only convenient syntax.

NiceSlice is loaded automatically in the perldl or pdl2 shell, but (to avoid conflicts with other modules) must be loaded explicitly in standalone perl/PDL scripts (see below). If you prefer not to use a prefilter on your standalone scripts, you can use the slice method in those scripts, rather than the more compact NiceSlice constructs.

The new slicing syntax can be switched on and off in scripts and perl modules by using or unloading PDL::NiceSlice.

But now back to scripts and modules. Everything after use PDL::NiceSlice will be translated and you can use the new slicing syntax. Source filtering will continue until the end of the file is encountered. You can stop sourcefiltering before the end of the file by issuing a no PDL::NiceSlice statement.

Here is an example:

use PDL::NiceSlice;
# this code will be translated
# and you can use the new slicing syntax
no PDL::NiceSlice;
# this code won't
# and the new slicing syntax will raise errors!

See also Filter::Simple and example in this distribution for further examples.

NOTE: Unlike "normal" modules you need to include a use PDL::NiceSlice call in each and every file that contains code that uses the new slicing syntax. Imagine the following situation: a file test0.pl

Following conventional perl wisdom everything should be alright since we used PDL and PDL::NiceSlice already from within test0.pl and by the time test1.pl is required things should be defined and imported, etc. A quick test run will, however, produce something like the following:

Note that we don't need to issue use PDL again. PDL::NiceSlice is a somewhat funny module in that respect. It is a consequence of the way source filtering works in Perl (see also the IMPLEMENTATION section below).

Using PDL::NiceSlice slicing piddles 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 piddles without too much typing:

using parentheses directly following a scalar variable name, for example

$c = $b(0:-3:4,(0));

using the so called default method invocation in which the piddle object is treated as if it were a reference to a subroutine (see also perlref). Take this example that slices a piddle 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.

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.

$a(1:4) .= 2; # only use this syntax on piddles
$sum += $a(,(1));

However, if the variable name is immediately preceded by a &, for example

&$a(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 $a with argumentlist (4,5).

The $a(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.

The second syntax that will be recognized is what I called the default method syntax. It is the method arrow -> directly followed by an open parenthesis, e.g.

$a->xchg(0,1)->(($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.:

The argument list is a comma separated list. Each argument specifies how the corresponding dimension in the piddle is sliced. In contrast to usage of the slice method the arguments should not be quoted. Rather freely mix literals (1,3,etc), perl variables and function invocations, e.g.

$a($pos-1:$end,myfunc(1,3)) .= 5;

There can even be other slicing commands in the arglist:

$a(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:

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:

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 piddle before applying the slice expression. Here is an example

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:

- : 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:

Modifiers are still a new and experimental feature of PDL::NiceSlice. I am not sure how many of you are actively using them. Please do so and experiment with the syntax. 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.

In slice expressions you can use ranges and secondly, piddles 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:

$a($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:

$a(::2); # this won't work (in the way you probably intended)
$a(:-1:2); # this will select every 2nd element in the 1st dim

Just as with slice 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 piddle will have the elements in reverse order between these limits:

print $a(-2:0:2);
[8 6 4 2 0]

A single index just selects the given index in the slice

print $a(5);
[5]

Note, however, that the corresponding dimension is not removed from the resulting piddle but rather reduced to size 1:

print $a(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 syntax):

print $a((5));
5

In this particular example a 0D piddle results. Note that this syntax is only allowed with a single index. All these will be errors:

print $a((0,4)); # will work but not in the intended way
print $a((0:4)); # compile time error

An empty argument selects the whole dimension, in this example all of the first dimension:

As in slice, 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.

piddle index lists

The second way to select indices from a dimension is via 1D piddles of indices. A simple example:

$a = random 10;
$idx = long 3,4,7,0;
$b = $a($idx);

This way of selecting indices was previously only possible using dice (PDL::NiceSlice attempts to unify the slice and dice interfaces). Note that the indexing piddles must be 1D or 0D. Higher dimensional piddles as indices will raise an error:

Note that using index piddles 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 piddle. In particular, memory requirements are increased with index piddles (and execution time may be longer). That said, if an index piddle is the way to go use it!

As you might have expected ranges and index piddles can be freely mixed in slicing expressions:

You can use piddles 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:

print $a($rg+1,:$rg(0:1));
multielement piddle where only one allowed at XXX/Core.pm line 1170.

The problem is caused by using the 2-element piddle $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 piddles as index piddles as described above but not in ranges. And PDL::NiceSlice treats any expression with unprotected :'s as a range. Unprotected means as usual "not occurring between matched parentheses".

PDL::NiceSlice exploits the ability of Perl to use source filtering (see also perlfilter). A source filter basically filters (or rewrites) your perl code before it is seen by the compiler. PDL::NiceSlice searches through your Perl source code and when it finds the new slicing syntax it rewrites the argument list appropriately and splices a call to the slice method using the modified arg list into your perl code. You can see how this works in the perldl or pdl2 shells by switching on reporting (see above how to do that).

Note: To avoid clobbering the DATA filehandle PDL::NiceSlice switches itself off when encountering the __END__ or __DATA__ tokens. This should not be a problem for you unless you use SelfLoader to load PDL code including the new slicing from that section. It is even desirable when working with Inline::Pdlpp, see below.

There is currently an undesired interaction between PDL::NiceSlice and the new Inline::Pdlpp module (currently only in PDL CVS). Since PP code generally contains expressions of the type $var() (to access piddles, etc) PDL::NiceSlice recognizes those incorrectly as slice expressions and does its substitutions. This is not a problem if you use the DATA section for your Pdlpp code -- the recommended place for Inline code anyway. In that case PDL::NiceSlice will have switched itself off before encountering any Pdlpp code (see above):

The cleaner solution is to always stick with the DATA way of including your Inline code as in the first example. That way you keep your nice Perl code at the top and all the ugly Pdlpp stuff etc at the bottom.

Copyright (c) 2001, 2002 Christian Soeller. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as PDL itself (see http://pdl.perl.org).