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

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]

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

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

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]

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

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.

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

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

AUTHOR

Copyright (C) Karl Glazebrook (kgb@aaoepp.aao.gov.au), Tuomas J. Lukka, (lukka@husc.harvard.edu) and Christian Soeller (csoelle@sghms.ac.uk) 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.