The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

PDL::Core - fundamental PDL functionality

DESCRIPTION

Methods and functions for type conversions, PDL creation, type conversion, threading etc.

SYNOPSIS

use PDL::Core; # Normal routines use PDL::Core ':Internal'; # Hairy routines

FUNCTIONS

pdl

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::undef defaults to zero.

null

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]

nullcreate

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.

nelem

Return the number of elements in a piddle

 $n = nelem($piddle); $n = $piddle->nelem;
 $mean = sum($data)/nelem($data);

dims

Return piddle dimensions a a perl list

 @dims = $piddle->dims;  @dims = dims($piddle);
 perldl> p @tmp = dims zeroes 10,3,22
 10 3 22

PDL::getndims

Returns the number of dimensions in a piddle

   $ndims = $piddle->getndims;
  perldl> p zeroes(10,3,22)->getndims
  3

topdl

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

PDL::get_datatype

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.

howbig

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

dims

Returns the dimensions of a piddle as a perl list

  ($m,$n) = dims $piddle;

threadids

Returns the piddle thread IDs as a perl list

  @ids = threadids $piddle;

doflow

Turn on/off dataflow

   $x->doflow;  doflow($x);

flows

Whether or not a piddle is indulging in dataflow

   something if $x->flows; $hmm = flows($x);

PDL::new

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.        

PDL::copy

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.

PDL::unwind

Return a piddle which is the same as the argument except that all threadids have been removed.

 $y = $x->unwind;

PDL::make_physical

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

dummy

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

thread_define

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

PDL::thread

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.

diagonal

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

PDL::thread1

Explicit threading over specified dims using thread id 1.

   $xx = $x->thread1(3,1)
  Wibble

Convenience function interfacing to threadI (see PDL::Slices).

PDL::thread2

Explicit threading over specified dims using thread id 2.

   $xx = $x->thread2(3,1)
  Wibble

Convenience function interfacing to threadI (see PDL::Slices).

PDL::thread3

Explicit threading over specified dims using thread id 3.

   $xx = $x->thread3(3,1)
  Wibble

Convenience function interfacing to threadI (see PDL::Slices).

PDL::info

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

T

Type

D

Formatted Dimensions

F

Dataflow status

S

Some internal flags (P=physical,V=Vaffine,C=changed)

C

Class of this piddle, i.e. ref $pdl

A

Address of the piddle struct as a unique identifier

M

Calculated memory consumption of this piddle's data area

mslice

Convenience interface to 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")

inplace

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]

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.

PDL::new_from_specification

Internal method: create piddle by specification

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,...

isempty

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 the PDL::Indexing and PDL::Primitive::isnull manpages for discussions on this.

zeroes

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]

ones

construct a one filled piddle

   $a = ones([type], $nx, $ny, $nz,...);
   etc. (see 'zeroes')
 see zeroes() and add one

reshape

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]

convert

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.

Datatype_conversions

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. For example:

  $foo = [[1,2,undef],[undef,3,4]];
  $PDL::undefval = -999;
  $f = float $foo;
  print $f

  [
   [   1    2 -999]
   [-999    3    4]
  ]

$PDL::undef defaults to zero.

 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]

byte

Convert to byte datatype - see 'Datatype_conversions'

short

Convert to short datatype - see 'Datatype_conversions'

ushort

Convert to ushort datatype - see 'Datatype_conversions'

long

Convert to long datatype - see 'Datatype_conversions'

float

Convert to float datatype - see 'Datatype_conversions'

double

Convert to double datatype - see 'Datatype_conversions'

type

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

list

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...
  }

listindices

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

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

at

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

cat

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

dog

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)

barf

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

gethdr

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

sethdr

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.

AUTHOR

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.