PDL::Core - fundamental PDL functionality
Methods and functions for type conversions, PDL creation, type conversion, threading etc.
use PDL::Core; # Normal routines use PDL::Core ':Internal'; # Hairy routines
piddle constructor - creates new piddle from perl scalars/arrays
$a = pdl(SCALAR|ARRAY REFERENCE|ARRAY);
$a = pdl [1..10]; # 1D 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 $a = pdl([1,2,3],[4,5,6]); # 2D $a = pdl([[1,2,3],[4,5,6]]); # 2D
Note the last two are equivalent - a list is automatically converted to a
list reference for syntactic convenience. i.e. you can omit the outer []
pdl()
is a functional synonym for the 'new' constructor, e.g.:
$x = new PDL [1..10];
In order to control how undefs are handled in converting from perl lists to
PDLs, one can set the variable $PDL::undefval
. For example:
$foo = [[1,2,undef],[undef,3,4]]; $PDL::undefval = -999; $f = pdl $foo; print $f [ [ 1 2 -999] [-999 3 4] ]
$PDL::undefval
defaults to zero.
Returns a 'null' piddle.
$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]
Returns a 'null' piddle.
$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.
Return the number of elements in a piddle
$n = nelem($piddle); $n = $piddle->nelem;
$mean = sum($data)/nelem($data);
Return piddle dimensions as a perl list
@dims = $piddle->dims; @dims = dims($piddle);
perldl> p @tmp = dims zeroes 10,3,22 10 3 22
Returns the number of dimensions in a piddle
$ndims = $piddle->getndims;
perldl> p zeroes(10,3,22)->getndims 3
Returns the size of the given dimension.
$dim0 = $piddle->getdim(0);
perldl> p zeroes(10,3,22)->getdim(1) 3
Negative indices count from the end of the dims array. Indices beyond the end will return a size of 1. This reflects the idea that any pdl is equivalent to an infinitely dimensional array in which only a finite number of dimensions have a size different from one. For example, in that sense a 3D piddle of shape [3,5,2] is equivalent to a [3,5,2,1,1,1,1,1,....] piddle. Accordingly,
print $a->getdim(10000);
will print 1 for most practically encountered piddles.
alternate piddle constructor - ensures arg is a piddle
$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
Internal: Return the numeric value identifying the piddle datatype
$x = $piddle->get_datatype;
Mainly used for internal routines.
NOTE: get_datatype returns 'just a number' not any special type object, unlike type.
Returns the size of a piddle datatype in bytes.
$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
Returns the piddle thread IDs as a perl list
@ids = threadids $piddle;
Turn on/off dataflow
$x->doflow; doflow($x);
Whether or not a piddle is indulging in dataflow
something if $x->flows; $hmm = flows($x);
new piddle constructor method
$x = PDL->new(SCALAR|ARRAY|ARRAY REF);
$x = PDL->new(42); $y = new PDL [1..10];
Constructs piddle from perl numbers and lists.
Make a physical copy of a piddle
$new = $old->copy;
Since $new = $old
just makes a new reference, the
copy method is provided to allow real independent copies to be made.
Return a piddle which is the same as the argument except that all threadids have been removed.
$y = $x->unwind;
Make sure the data portion of a piddle can be accessed from XS code.
$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 PDL::Indexing). 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 PDL::PP) which can be used to transparently access virtual piddles without the
need to physicalise them (though there are exceptions).
Insert a 'dummy dimension' of given length (defaults to 1)
No relation to the 'Dungeon Dimensions' in Discworld! Negative positions specify relative to last dimension, i.e. dummy(-1) appends one dimension at end, dummy(-2) inserts a dummy dimension in front of the last dim, etc.
$y = $x->dummy($position[,$dimsize]);
perldl> p sequence(3)->dummy(0,3) [ [0 0 0] [1 1 1] [2 2 2] ]
define functions that support threading at the perl level
thread_define 'tline(a(n);b(n))', over { line $_[0], $_[1]; # make line compliant with threading };
thread_define
provides some support for threading (see
PDL::Indexing) at the perl level. It allows you to do things for which you normally
would have resorted to PDL::PP (see PDL::PP); 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 PDL::Indexing and PDL::PP) 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 PDL::Indexing).
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::Graphics::TriD::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 PDL::PP for the format of the signature. Currently, the
[t]
qualifier and all type qualifiers are ignored.
Use explicit threading over specified dimensions (see also PDL::Indexing)
$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.
Returns the multidimensional diagonal over the specified dimensions.
$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] ] ]
Explicit threading over specified dims using thread id 1.
$xx = $x->thread1(3,1)
Wibble
Convenience function interfacing to threadI.
Explicit threading over specified dims using thread id 2.
$xx = $x->thread2(3,1)
Wibble
Convenience function interfacing to threadI.
Explicit threading over specified dims using thread id 3.
$xx = $x->thread3(3,1)
Wibble
Convenience function interfacing to threadI.
sever any links of this piddle to parent piddles
In PDL it is possible for a piddle to be just another view into another piddle's data. In that case we call this piddle a virtual piddle and the original piddle owning the data its parent. In other languages these alternate views sometimes run by names such as alias or smart reference.
Typical functions that return such piddles are slice
, xchg,
index
, etc. Sometimes, however, you would like to separate the
virtual piddle from its parent's data and just give it a life of its own. This is simply
achieved by using sever
. For example,
$a = $pdl->index(pdl(0,3,7))->sever; $a++; # important: $pdl is not modified!
In this regard it acts similar to copy. However, in general performance is better with sever
and secondly, sever
doesn't lead to futile copying when used on piddles that already have their
own data.
$a = zeroes(20); $a->sever; # NOOP since $a is already its own boss!
Return formatted information about a piddle.
$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
Type
Formatted Dimensions
Dataflow status
Some internal flags (P=physical,V=Vaffine,C=changed)
Class of this piddle, i.e. ref $pdl
Address of the piddle struct as a unique identifier
Calculated memory consumption of this piddle's data area
test for approximately equal values (relaxed ==
)
# ok if all corresponding values in # piddles are within 1e-8 of each other print "ok\n" if all approx $a, $b, 1e-8;
approx
is a relaxed form of the ==
operator and often more appropriate for floating point types (float
and double
).
Usage:
$res = approx $a, $b [, $eps]
The optional parameter $eps
is remembered across invocations and initially set to 1e-6, e.g.
approx $a, $b; # last $eps used (1e-6 initially) approx $a, $b, 1e-10; # 1e-10 approx $a, $b; # also 1e-10
Convenience interface to slice, allowing easier inclusion of dimensions in perl code.
$a = $x->mslice(...);
# below is the same as $x->slice("5:7,:,3:4:2") $a = $x->mslice([5,7],X,[3,4,2]);
Flag a piddle so that the next operation is done 'in place'
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]
Internal method: create piddle by specification
This is the argument processing method called by zeroes and some other functions which constructs piddles from argument lists of the form:
[type], $nx, $ny, $nz,...
For $nx
, $ny
, etc. 0 and 1D piddles are allowed. Giving those has the same effect as if
saying $arg->list
, e.g.
1, pdl(5,2), 4
is equivalent to
1, 5, 2, 4
Note, however, that in all functions using new_from_specification
calling func $piddle
will probably not do what you want. So to play safe use (e.g. with zeroes)
$pdl = zeroes $dimpdl->list;
Calling
$pdl = zeroes $dimpdl;
will rather be equivalent to
$pdl = zeroes $dimpdl->dims;
However,
$pdl = zeroes ushort, $dimpdl;
will again do what you intended since it is interpreted as if you had said
$pdl = zeroes ushort, $dimpdl->list;
This is unfortunate and confusing but no good solution seems obvious that would not break existing scripts.
Test whether a piddle is empty
print "The piddle has zero dimension\n" if $pdl->isempty;
This function returns 1 if the piddle has zero elements. This is useful in particular when using the indexing function which. In the case of no match to a specified criterion, the returned piddle has zero dimension.
perldl> $a=sequence(10) perldl> $i=which($a < -1) perldl> print "I found no matches!\n" if ($a->isempty);
Note that having zero elements is rather different from the concept of being a null piddle, see the PDL::FAQ and PDL::Indexing manpages for discussions of this.
construct a zero filled piddle from dimension list or template piddle.
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]
See also new_from_specification for details on using piddles in the dimensions list.
construct a one filled piddle
$a = ones([type], $nx, $ny, $nz,...); etc. (see 'zeroes')
see zeroes() and add one
See also new_from_specification for details on using piddles in the dimensions list.
Change the shape (i.e. dimensions) of a piddle, preserving contents.
$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]
flatten a piddle (alias for $pdl-
clump(-1)>)
$srt = $pdl->flat->qsort;
Useful method to make a 1D piddle from an arbitrarily sized input piddle. Data flows back and forth as usual with slicing routines.
Generic datatype conversion function
$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.
byte|short|ushort|long|float|double convert shorthands
$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 / listref argument they construct a new
piddle. This is a convenience to avoid having to be long-winded and say $x = long(pdl(42))
Thus one can say:
$a = float(1,2,3,4); # 1D $a = float([1,2,3],[4,5,6]); # 2D $a = float([[1,2,3],[4,5,6]]); # 2D
Note the last two are equivalent - a list is automatically converted to a
list reference for syntactic convenience. i.e. you can omit the outer []
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.
In order to control how undefs are handled in converting from perl lists to
PDLs, one can set the variable $PDL::undefval
; see the function pdl() for more details.
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]
Convert to byte datatype - see 'Datatype_conversions'
Convert to short datatype - see 'Datatype_conversions'
Convert to ushort datatype - see 'Datatype_conversions'
Convert to long datatype - see 'Datatype_conversions'
Convert to float datatype - see 'Datatype_conversions'
Convert to double datatype - see 'Datatype_conversions'
return the type of a piddle as a blessed type object
A convenience function for use with the piddle constructors, e.g.
$b = PDL->zeroes($a->type,$a->dims,3);
See the discussion of the PDL::Type
object in PDL::Types.
Convert piddle to perl list
@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... }
Convert piddle indices to perl list
@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 a single value inside a piddle
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] ]
Returns a single value inside a piddle as perl scalar.
$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
concatentate piddles to N+1 dimensional piddle
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] ] ]
Opposite of 'cat' :). Split N dim piddle to list of N-1 dim piddles
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)
Standard error reporting routine for PDL.
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
Retrieve header information from a piddle
$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. See hdrcpy to enable automatic header copying.
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; }
Set header information of a piddle
$pdl=rfits('file.fits'); $h=$pdl->gethdr; # add a FILENAME field to the header $$h{FILENAME} = 'file.fits'; $pdl->sethdr( $h );
The sethdr
function sets the header information for a piddle. Normally you would get
the current header information with
gethdr, add/change/remove fields, then apply those changes with
sethdr
.
The sethdr
function must be given a hash reference. For further information on the
header, see gethdr and
hdrcpy.
switch on/off/examine automatic header copying
print "hdrs will be copied" if $a->hdrcpy; $a->hdrcpy(1); # switch on hdr copying $b = $a->sumover; # and $b will inherit $a's hdr $a->hdrcpy(0); # and now make $a non-infectious again
Normally, the optional header of a piddle is not copied automatically in
pdl operations. Switching on the hdrcpy flag using the hdrcpy
method will enable automatic hdr copying. Note that copying is by reference for efficiency reasons. hdrcpy
without an argument just returns the current setting of the flag.
Copyright (C) Karl Glazebrook (kgb@aaoepp.aao.gov.au), Tuomas J. Lukka, (lukka@husc.harvard.edu) and Christian Soeller (c.soeller@auckland.ac.nz) 1997. All rights reserved. There is no warranty. You are allowed to redistribute this software / documentation under certain conditions. For details, see the file COPYING in the PDL distribution. If this file is separated from the PDL distribution, the copyright notice should be included in the file.