$a = pdl(SCALAR|ARRAY REFERENCE|ARRAY);
$a = pdl [1..10]; # 1D array $a = pdl (1,2,3,4); # Ditto $b = pdl [[1,2,3],[4,5,6]]; # 2D 3x2 array $b = pdl 42 # 0-dimensional scalar $c = pdl $a; # Make a new copy
pdl()
is a functional synonym for the 'new' constructor, e.g.:
$x = new PDL [1..10];
$x = null;
null()
has a special meaning to PDL::PP
. It is used to flag a special kind of empty piddle, which can grow to
appropriate dimensions to store a result. (As opposed to storing a result
in an existing piddle).
perldl> sumover sequence(10,10), $ans=null;p $ans [45 145 245 345 445 545 645 745 845 945]
$x = PDL->nullcreate($arg) This is an routine used by many of the threading primitives (i.e. sumover, minimum, etc.) to generate a null piddle for the function's output that will behave properly for derived (or subclassed) PDL objects.
For the above usage: If $arg
is a PDL, or a derived PDL, then
$arg->null is returned. If $arg
is a scalar (i.e. a
zero-dimensional PDL) then $PDL->null is returned.
PDL::Derived->nullcreate(10) returns PDL::Derived->null.
PDL->nullcreate($pdlderived) returns $pdlderived->null.
$n = nelem($piddle); $n = $piddle->nelem;
$mean = sum($data)/nelem($data);
@dims = $piddle->dims; @dims = dims($piddle);
perldl> p @tmp = dims zeroes 10,3,22 10 3 22
$ndims = $piddle->getndims;
perldl> p zeroes(10,3,22)->getndims 3
$a = topdl(SCALAR|ARRAY REFERENCE|ARRAY);
The difference between pdl()
and topdl()
is that
the latter will just 'fall through' if the argument is already a piddle. It
will return a reference and NOT a new copy.
This is particulary useful if you are writing a function which is doing
some fiddling with internals and assumes a piddle argument (e.g. for method
calls). Using topdl()
will ensure nothing breaks if passed
with '2'.
$a = topdl 43; # $a is piddle with value '43' $b = topdl $piddle; # fall through $a = topdl (1,2,3,4); # Convert 1D array
$x = $piddle->get_datatype;
Mainly used for internal routines.
NOTE: get_datatype returns 'just a number' not any special type object.
$size = howbig($piddle->get_datatype);
Mainly used for internal routines.
NOTE: NOT a method! This is because get_datatype returns 'just a number' not any special object.
perldl> p howbig(ushort([1..10])->get_datatype) 2
($m,$n) = dims $piddle;
@ids = threadids $piddle;
$x->doflow; doflow($x);
something if $x->flows; $hmm = flows($x);
$x = PDL->new(SCALAR|ARRAY|ARRAY REF);
$x = PDL->new(42); $y = new PDL [1..10];
Constructs piddle from perl numbers and lists.
$new = $old->copy;
Since $new = $old
just makes a new reference, the
copy
method is provided to allow real independent copies to be made.
$y = $x->unwind;
$a->make_physical; $a->call_my_xs_method;
Ensures that a piddle gets its own allocated copy of data. This obviously implies that there are certain piddles which do not have their own data. These are so called virtual piddles that make use of the vaffine optimisation (see the PDL/Indexing manpage). They do not have their own copy of data but instead store only access information to some (or all) of another piddle's data.
Note: this function should not be used unless absolutely neccessary since otherwise memory requirements might be severly increased. Instead of writing your own XS code with the need to call make_physical you might want to consider using the PDL preprocessor (see the PDL/PP manpage) which can be used to transparently access virtual piddles without the need to physicalise them (though there are exceptions).
No relation to the 'Dungeon Dimensions' in Discworld!
$y = $x->dummy($position[,$dimsize]);
perldl> p sequence(3)->dummy(0,3) [ [0 0 0] [1 1 1] [2 2 2] ]
thread_define 'tline(a(n);b(n))', over { line $_[0], $_[1]; # make line compliant with threading };
thread_define
provides some support for threading (see
the PDL/Indexing manpage) at the perl level. It allows you to do things for which you normally
would have resorted to PDL::PP (see the PDL/PP manpage); however, it is most useful to wrap existing perl functions so that the
new routine supports PDL threading.
thread_define
is used to define new threading aware
functions. Its first argument is a symbolic repesentation of the new
function to be defined. The string is composed of the name of the new
function followed by its signature (see the PDL/Indexing manpage and the PDL/PP manpage) in parentheses. The second argument is a subroutine that will be called
with the slices of the actual runtime arguments as specified by its
signature. Correct dimension sizes and minimal number of dimensions for all
arguments will be checked (assuming the rules of PDL threading, see the PDL/Indexing manpage).
The actual work is done by the signature
class which parses the signature string, does runtime dimension checks and
the routine threadover
that generates the loop over all appropriate slices of pdl arguments and
creates pdls as needed.
Similar to pp_def
and its OtherPars
option it is possible to define the new function so that it accepts normal
perl args as well as piddles. You do this by using the NOtherPars
parameter in the signature. The number of NOtherPars
specified will be passed unaltered into the subroutine given as the second
argument of
thread_define
. Let's illustrate this with an example:
PDL::thread_define 'triangles(inda();indb();indc()), NOtherPars => 2', PDL::over { ${$_[3]} .= $_[4].join(',',map {$_->at} @_[0..2]).",-1,\n"; };
This defines a function triangles
that takes 3 piddles as input plus 2 arguments which are passed into the
routine unaltered. This routine is used to collect lists of indices into a
perl scalar that is passed by reference. Each line is preceded by a prefix
passed as $_[4]
. Here is typical usage:
$txt = ''; triangles(pdl(1,2,3),pdl(1),pdl(0),\$txt," "x10); print $txt;
resulting in the following output
1,1,0,-1, 2,1,0,-1, 3,1,0,-1,
which is used in PDL::VRML to generate VRML output.
Currently, this is probably not much more than a POP (proof of principle) but is hoped to be useful enough for some real life work.
Check the PDL/PP manpage for the format of the signature. Currently, the
[t]
qualifier and all type qualifiers are ignored.
$b = $a->thread($dim,[$dim1,...])
$a = zeroes 3,4,5; $b = $a->thread(2,0);
Same as PDL::thread1
, i.e. uses thread id 1.
$d = $x->diagonal(dim1, dim2,...)
perldl> $a = zeroes(3,3,3); perldl> ($b = $a->diagonal(0,1))++; perldl> p $a [ [ [1 0 0] [0 1 0] [0 0 1] ] [ [1 0 0] [0 1 0] [0 0 1] ] [ [1 0 0] [0 1 0] [0 0 1] ] ]
$xx = $x->thread1(3,1)
Wibble
Convenience function interfacing to threadI (see the PDL/Slices manpage).
$xx = $x->thread2(3,1)
Wibble
Convenience function interfacing to threadI (see the PDL/Slices manpage).
$xx = $x->thread3(3,1)
Wibble
Convenience function interfacing to threadI (see the PDL/Slices manpage).
$x->info($format_string);
print $x->info("Type: %T Dim: %-15D State: %S");
Returns a string with info about a piddle. Takes an optional argument to
specify the format of information a la sprintf. Format specifiers are in
the form %<width><letter>
where the width is optional and the letter is one of
ref $pdl
slice
, allowing easier inclusion of dimensions in perl code.
$a = $x->mslice(...);
$a = $x->mslice([5,7],X,[3,4,2]); # eq: $x->slice("5:7,:,3:4:2")
somefunc($x->inplace); somefunc(inplace $x);
In most cases one likes to use the syntax $y = f($x)
, however in many case the operation f()
can be done correctly 'in place', i.e. without making a new copy of the
data for output. To make it easy to use this, we write f()
in such a way that it operates in-place, and use inplace
to hint that a new copy should be disabled. This also makes for clear
syntax.
Obviously this will not work for all functions, and if in doubt see the
function's documentation. However one can assume this is true for all
elemental functions (i.e. those which just operate array element by array
element like log10
).
perldl> $x = xvals zeroes 10; perldl> log10(inplace $x) perldl> p $x [ -Inf 0 0.30103 0.47712125 0.60205999 0.69897 0.77815125 0.84509804 0.90308999 0.95424251]
This is the argument processing method called by zeroes
(q.v.) and some other functions which constructs piddles from argument
listss of the form:
[type], $nx, $ny, $nz,...
Various forms of usage,
(i) by specification or (ii) by template piddle:
# usage type (i): $a = zeroes([type], $nx, $ny, $nz,...); $a = PDL->zeroes([type], $nx, $ny, $nz,...); $a = $pdl->zeroes([type], $nx, $ny, $nz,...); # usage type (ii): $a = zeroes $b; $a = $b->zeroes zeroes inplace $a; # Equivalent to $a .= 0; $a->inplace->zeroes; # ""
perldl> $z = zeroes 4,3 perldl> p $z [ [0 0 0 0] [0 0 0 0] [0 0 0 0] ] perldl> $z = zeroes ushort, 3,2 # Create ushort array
[ushort() etc. with no arg returns a PDL::Types token]
$a = ones([type], $nx, $ny, $nz,...); etc. (see 'zeroes')
see zeroes() and add one
$x->reshape(NEWDIMS); reshape($x, NEWDIMS);
The data elements are preserved, obviously they will wrap differently and get truncated if the new array is shorter. If the new array is longer it will be zero-padded.
Note: an explicit copy is forced - this is the only way (for now) of
stopping a crash if $x
is a slice.
perldl> $x = sequence(10) perldl> reshape $x,3,4; p $x [ [0 1 2] [3 4 5] [6 7 8] [9 0 0] ] perldl> reshape $x,5; p $x [0 1 2 3 4]
$y = convert($x, $newtype);
$y = convert $x, long $y = convert $x, ushort
$newtype
is a type number, for convenience they are returned by long()
etc when called without arguments.
$y = double $x; $y = ushort [1..10];
(all of byte|short|ushort|long|float|double behave similarly)
When called with a piddle argument, they convert to the specific datatype.
When called with a numeric or list ref argument they construct a new
piddle. This is a convenience to avoid having to be long-winded and say
<$x = long(pdl(42))>
When called with no arguments return a special type token. This allows syntactical sugar like:
$x = ones byte, 1000,1000;
This example creates a large piddle directly as byte datatype in order to save memory.
perldl> p $x=sqrt float [1..10] [1 1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 3.16228] perldl> p byte $x [1 1 1 2 2 2 2 2 3 3]
A convenience function for use with the piddle constructors, e.g.
$b = PDL->zeroes($a->type,$a->dims,3);
@tmp = list $x;
Obviously this is grossly inefficient for the large datasets PDL is designed to handle. This was provided as a get out while PDL matured. It should now be mostly superseded by superior constructs, such as PP/threading. However it is still occasionally useful and is provied for backwards compatibility.
for (list $x) { # Do something on each value... }
@tmp = listindices $x;
@tmp
now contains the values 0..nelem($x).
Obviously this is grossly inefficient for the large datasets PDL is designed to handle. This was provided as a get out while PDL matured. It should now be mostly superseded by superior constructs, such as PP/threading. However it is still occasionally useful and is provied for backwards compatibility.
for $i (listindices $x) { # Do something on each value... }
set $piddle, @position, $value
@position
is a coordinate list, of size equal to the number of dimensions in the
piddle. Occasionally useful, mainly provided for backwards compatibility as
superseded by use of slice
and assigment operator .=
.
perldl> $x = sequence 3,4 perldl> set $x, 2,1,99 perldl> p $x [ [ 0 1 2] [ 3 4 99] [ 6 7 8] [ 9 10 11] ]
$z = at($piddle, @position); $z=$piddle->at(@position);
@position
is a coordinate list, of size equal to the number of dimensions in the
piddle. Occasionally useful in a general context, quite useful too inside
PDL internals.
perldl> $x = sequence 3,4 perldl> p $x->at(1,2) 7
Takes a list of N piddles of same shape as argument, returns a single piddle of dimension N+1
perldl> $x = cat ones(3,3),zeroes(3,3),rvals(3,3); p $x
[ [ [1 1 1] [1 1 1] [1 1 1] ] [ [0 0 0] [0 0 0] [0 0 0] ] [ [1 1 1] [1 0 1] [1 1 1] ] ]
Takes a single N-dimensional piddle and splits it into a list of N-1 dimensional piddles. The breakup is done along the last dimension. Note the dataflown connection is still preserved by default, e.g.:
perldl> $p = ones 3,3,3 perldl> ($a,$b,$c) = dog $p perldl> $b++; p $p
[ [ [1 1 1] [1 1 1] [1 1 1] ] [ [2 2 2] [2 2 2] [2 2 2] ] [ [1 1 1] [1 1 1] [1 1 1] ] ]
Break => 1 Break dataflow connection (new copy)
barf()
is the routine PDL modules should call to report
errors. This is because barf()
will report the error as coming
from the correct line in the module user's script rather than in the PDL
module.
It does this magic by unwinding the stack frames until it reaches a package
NOT beginning with ``PDL::''. If you DO want it to report errors in some
module PDL::Foo (e.g. when debugging PDL::Foo) then set the variable $PDL::Foo::Debugging=1
.
Additionally if you set the variable $PDL::Debugging=1
you will get a COMPLETE stack trace back up to the top level package.
Finally barf()
will try and report usage information from the
PDL documentation database if the error message is of the form 'Usage:
func'.
Remember barf()
is your friend. *Use* it!
At the perl level:
barf("User has too low an IQ!");
In C or XS code:
barf("You have made %d errors", count);
Note: this is one of the few functions ALWAYS exported by PDL::Core
$pdl=rfits('file.fits'); $h=$pdl->gethdr; print "Number of pixels in the X-direction=$$h{NAXIS1}\n";
The gethdr function retrieves whatever header information is contained within a piddle. The header can be set with sethdr and is always a hash reference and has to be dereferenced for access to the value.
It is important to realise that you are free to insert whatever hash reference you want in the header, so you can use it to record important information about your piddle, and that it is not automatically copied when you copy the piddle.
For instance a wrapper around rcols that allows your piddle to remember the file it was read from and the columns could be easily written (here assuming that no regexp is needed, extensions are left as an exercise for the reader)
sub ext_rcols { my ($file, @columns)=@_; my $header={}; $$header{File}=$file; $$header{Columns}=\@columns;
@piddles=rcols $file, @columns; foreach (@piddles) { $_->sethdr($header); } return @piddles; }