.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Core 3pm" .TH Core 3pm "2020-11-19" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" PDL::Core \- fundamental PDL functionality and vectorization/threading .SH "DESCRIPTION" .IX Header "DESCRIPTION" Methods and functions for type conversions, \s-1PDL\s0 creation, type conversion, threading etc. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use PDL::Core; # Normal routines \& use PDL::Core \*(Aq:Internal\*(Aq; # Hairy routines .Ve .SH "VECTORIZATION/THREADING: METHOD AND NOMENCLATURE" .IX Header "VECTORIZATION/THREADING: METHOD AND NOMENCLATURE" \&\s-1PDL\s0 provides vectorized operations via a built-in engine. Vectorization is called \*(L"threading\*(R" for historical reasons. The threading engine implements simple rules for each operation. .PP Each \s-1PDL\s0 object has a \*(L"shape\*(R" that is a generalized N\-dimensional rectangle defined by a \*(L"dim list\*(R" of sizes in an arbitrary set of dimensions. A \s-1PDL\s0 with shape 2x3 has 6 elements and is said to be two-dimensional, or may be referred to as a 2x3\-PDL. The dimensions are indexed numerically starting at 0, so a 2x3\-PDL has a dimension 0 (or \*(L"dim 0\*(R") with size 2 and a 1 dimension (or \*(L"dim 1\*(R") with size 3. .PP \&\s-1PDL\s0 generalizes *all* mathematical operations with the notion of \&\*(L"active dims\*(R": each operator has zero or more active dims that are used in carrying out the operation. Simple scalar operations like scalar multiplication ('*') have 0 active dims. More complicated operators can have more active dims. For example, matrix multiplication ('x') has 2 active dims. Additional dims are automatically vectorized across \*(-- e.g. multiplying a 2x5\-PDL with a 2x5\-PDL requires 10 simple multiplication operations, and yields a 2x5\-PDL result. .SS "Threading rules" .IX Subsection "Threading rules" In any \s-1PDL\s0 expression, the active dims appropriate for each operator are used starting at the 0 dim and working forward through the dim list of each object. All additional dims after the active dims are \&\*(L"thread dims\*(R". The thread dims do not have to agree exactly: they are coerced to agree according to simple rules: .IP "\(bu" 3 Null PDLs match any dim list (see below). .IP "\(bu" 3 Dims with sizes other than 1 must all agree in size. .IP "\(bu" 3 Dims of size 1 are expanded as necessary. .IP "\(bu" 3 Missing dims are expanded appropriately. .PP The \*(L"size 1\*(R" rule implements \*(L"generalized scalar\*(R" operation, by analogy to scalar multiplication. The \*(L"missing dims\*(R" rule acknowledges the ambiguity between a missing dim and a dim of size 1. .SS "Null PDLs" .IX Subsection "Null PDLs" PDLs on the left-hand side of assignment can have the special value \&\*(L"Null\*(R". A null \s-1PDL\s0 has no dim list and no set size; its shape is determined by the computed shape of the expression being assigned to it. Null PDLs contain no values and can only be assigned to. When assigned to (e.g. via the \f(CW\*(C`.=\*(C'\fR operator), they cease to be null PDLs. .PP To create a null \s-1PDL,\s0 use \f(CW\*(C`PDL\->null()\*(C'\fR. .SS "Empty PDLs" .IX Subsection "Empty PDLs" PDLs can represent the empty set using \*(L"structured Empty\*(R" variables. An empty \s-1PDL\s0 is not a null \s-1PDL.\s0 .PP Any dim of a \s-1PDL\s0 can be set explicitly to size 0. If so, the \s-1PDL\s0 contains zero values (because the total number of values is the product of all the sizes in the \s-1PDL\s0's shape or dimlist). .PP Scalar PDLs are zero-dimensional and have no entries in the dim list, so they cannot be empty. 1\-D and higher PDLs can be empty. Empty PDLs are useful for set operations, and are most commonly encountered in the output from selection operators such as which and whichND. Not all empty PDLs have the same threading properties \*(-- e.g. a 2x0\-PDL represents a collection of 2\-vectors that happens to contain no elements, while a simple 0\-PDL represents a collection of scalar values (that also happens to contain no elements). .PP Note that 0 dims are not adjustable via the threading rules \*(-- a dim with size 0 can only match a corresponding dim of size 0 or 1. .SS "Thread rules and assignments" .IX Subsection "Thread rules and assignments" Versions of \s-1PDL\s0 through 2.4.10 have some irregularity with threading and assignments. Currently the threading engine performs a full expansion of both sides of the computed assignment operator \f(CW\*(C`.=\*(C'\fR (which assigns values to a pre-existing \s-1PDL\s0). This leads to counter-intuitive behavior in some cases: .IP "\(bu" 3 Generalized scalars and computed assignment .Sp If the \s-1PDL\s0 on the left-hand side of \f(CW\*(C`.=\*(C'\fR has a dim of size 1, it can be treated as a generalized scalar, as in: .Sp .Vb 3 \& $x = sequence(2,3); \& $y = zeroes(1,3); \& $y .= $x; .Ve .Sp In this case, \f(CW$y\fR is automatically treated as a 2x3\-PDL during the threading operation, but half of the values from \f(CW$x\fR silently disappear. The output is, as Kernighan and Ritchie would say, \*(L"undefined\*(R". .Sp Further, if the value on the right of \f(CW\*(C`.=\*(C'\fR is empty, then \f(CW\*(C`.=\*(C'\fR becomes a silent no-op: .Sp .Vb 4 \& $x = zeroes(0); \& $y = zeroes(1); \& $y .= $x+1; \& print $y; .Ve .Sp will print \f(CW\*(C`[0]\*(C'\fR. In this case, \*(L"$x+1\*(R" is empty, and \*(L"$y\*(R" is a generalized scalar that is adjusted to be empty, so the assignment is carried out for zero elements (a no-op). .Sp Both of these behaviors are considered harmful and should not be relied upon: they may be patched away in a future version of \s-1PDL.\s0 .IP "\(bu" 3 Empty PDLs and generalized scalars .Sp Generalized scalars (PDLs with a dim of size 1) can match any size in the corresponding dim, including 0. Thus, .Sp .Vb 4 \& $x = ones(2,0); \& $y = sequence(2,1); \& $c = $x * $y; \& print $c; .Ve .Sp prints \f(CW\*(C`Empty[2,0]\*(C'\fR. .Sp This behavior is counterintuitive but desirable, and will be preserved in future versions of \s-1PDL.\s0 .SH "VARIABLES" .IX Header "VARIABLES" These are important variables of \fBglobal\fR scope and are placed in the \s-1PDL\s0 namespace. .PP \fI\f(CI$PDL::debug\fI\fR .IX Subsection "$PDL::debug" .Sp .RS 4 When true, \s-1PDL\s0 debugging information is printed. .RE .PP \fI\f(CI$PDL::verbose\fI\fR .IX Subsection "$PDL::verbose" .Sp .RS 4 When true, \s-1PDL\s0 functions provide chatty information. .RE .PP \fI\f(CI$PDL::use_commas\fI\fR .IX Subsection "$PDL::use_commas" .Sp .RS 4 Whether to insert commas when printing pdls .RE .PP \fI\f(CI$PDL::floatformat\fI, \f(CI$PDL::doubleformat\fI, \f(CI$PDL::indxformat\fI\fR .IX Subsection "$PDL::floatformat, $PDL::doubleformat, $PDL::indxformat" .Sp .RS 4 The default print format for floats, doubles, and indx values, respectively. The default default values are: .Sp .Vb 3 \& $PDL::floatformat = "%7g"; \& $PDL::doubleformat = "%10.8g"; \& $PDL::indxformat = "%12d"; .Ve .RE .PP \fI\f(CI$PDL::undefval\fI\fR .IX Subsection "$PDL::undefval" .Sp .RS 4 The value to use instead of \f(CW\*(C`undef\*(C'\fR when creating pdls. .RE .PP \fI\f(CI$PDL::toolongtoprint\fI\fR .IX Subsection "$PDL::toolongtoprint" .Sp .RS 4 The maximal size pdls to print (defaults to 10000 elements) .RE .SH "FUNCTIONS" .IX Header "FUNCTIONS" .SS "barf" .IX Subsection "barf" Standard error reporting routine for \s-1PDL.\s0 .PP \&\f(CW\*(C`barf()\*(C'\fR is the routine \s-1PDL\s0 modules should call to report errors. This is because \f(CW\*(C`barf()\*(C'\fR will report the error as coming from the correct line in the module user's script rather than in the \s-1PDL\s0 module. .PP For now, barf just calls \fBCarp::confess()\fR .PP Remember \f(CW\*(C`barf()\*(C'\fR is your friend. *Use* it! .PP At the perl level: .PP .Vb 1 \& barf("User has too low an IQ!"); .Ve .PP In C or \s-1XS\s0 code: .PP .Vb 1 \& barf("You have made %d errors", count); .Ve .PP Note: this is one of the few functions \s-1ALWAYS\s0 exported by PDL::Core .SS "pdl" .IX Subsection "pdl" \&\s-1PDL\s0 constructor \- creates new piddle from perl scalars/arrays, piddles, and strings .PP .Vb 2 \& $double_pdl = pdl(SCALAR|ARRAY REFERENCE|ARRAY|STRING); # default type \& $type_pdl = pdl(PDL::Type,SCALAR|ARRAY REFERENCE|ARRAY|STRING); .Ve .PP .Vb 10 \& $x = pdl [1..10]; # 1D array \& $x = pdl ([1..10]); # 1D array \& $x = pdl (1,2,3,4); # Ditto \& $y = pdl [[1,2,3],[4,5,6]]; # 2D 3x2 array \& $y = pdl "[[1,2,3],[4,5,6]]"; # Ditto (slower) \& $y = pdl "[1 2 3; 4 5 6]"; # Ditto \& $y = pdl q[1 2 3; 4 5 6]; # Ditto, using the q quote operator \& $y = pdl "1 2 3; 4 5 6"; # Ditto, less obvious, but still works \& $y = pdl 42 # 0\-dimensional scalar \& $c = pdl $x; # Make a new copy \& \& $u = pdl ushort(), 42 # 0\-dimensional ushort scalar \& $y = pdl(byte(),[[1,2,3],[4,5,6]]); # 2D byte piddle \& \& $n = pdl indx(), [1..5]; # 1D array of indx values \& $n = pdl indx, [1..5]; # ... can leave off parens \& $n = indx( [1..5] ); # ... still the same! \& \& $x = pdl([1,2,3],[4,5,6]); # 2D \& $x = pdl([1,2,3],[4,5,6]); # 2D .Ve .PP 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 \f(CW\*(C`[]\*(C'\fR .PP You can mix and match arrays, array refs, and PDLs in your argument list, and \f(CW\*(C`pdl\*(C'\fR will sort them out. You get back a \s-1PDL\s0 whose last (slowest running) dim runs across the top level of the list you hand in, and whose first (fastest running) dim runs across the deepest level that you supply. .PP At the moment, you cannot mix and match those arguments with string arguments, though we can't imagine a situation in which you would really want to do that. .PP The string version of pdl also allows you to use the strings \f(CW\*(C`bad\*(C'\fR, \f(CW\*(C`inf\*(C'\fR, and \f(CW\*(C`nan\*(C'\fR, and it will insert the values that you mean (and set the bad flag if you use \f(CW\*(C`bad\*(C'\fR). You can mix and match case, though you shouldn't. Here are some examples: .PP .Vb 4 \& $bad = pdl q[1 2 3 bad 5 6]; # Set fourth element to the bad value \& $bad = pdl q[1 2 3 BAD 5 6]; # ditto \& $bad = pdl q[1 2 inf bad 5]; # now third element is IEEE infinite value \& $bad = pdl q[nan 2 inf \-inf]; # first value is IEEE nan value .Ve .PP The default constructor uses \s-1IEEE\s0 double-precision floating point numbers. You can use other types, but you will get a warning if you try to use \f(CW\*(C`nan\*(C'\fR with integer types (it will be replaced with the \f(CW\*(C`bad\*(C'\fR value) and you will get a fatal error if you try to use \f(CW\*(C`inf\*(C'\fR. .PP Throwing a \s-1PDL\s0 into the mix has the same effect as throwing in a list ref: .PP .Vb 1 \& pdl(pdl(1,2),[3,4]) .Ve .PP is the same as .PP .Vb 1 \& pdl([1,2],[3,4]). .Ve .PP All of the dimensions in the list are \*(L"padded-out\*(R" with undefval to meet the widest dim in the list, so (e.g.) .PP .Vb 1 \& $x = pdl([[1,2,3],[2]]) .Ve .PP gives you the same answer as .PP .Vb 1 \& $x = pdl([[1,2,3],[2,undef,undef]]); .Ve .PP If your \s-1PDL\s0 module has bad values compiled into it (see PDL::Bad), you can pass \s-1BAD\s0 values into the constructor within pre-existing PDLs. The \s-1BAD\s0 values are automatically kept \s-1BAD\s0 and propagated correctly. .PP \&\f(CW\*(C`pdl()\*(C'\fR is a functional synonym for the 'new' constructor, e.g.: .PP .Vb 1 \& $x = new PDL [1..10]; .Ve .PP In order to control how undefs are handled in converting from perl lists to PDLs, one can set the variable \f(CW$PDL::undefval\fR. For example: .PP .Vb 8 \& $foo = [[1,2,undef],[undef,3,4]]; \& $PDL::undefval = \-999; \& $f = pdl $foo; \& print $f \& [ \& [ 1 2 \-999] \& [\-999 3 4] \& ] .Ve .PP \&\f(CW$PDL::undefval\fR defaults to zero. .PP As a final note, if you include an Empty \s-1PDL\s0 in the list of objects to construct into a \s-1PDL,\s0 it is kept as a placeholder pane \*(-- so if you feed in (say) 7 objects, you get a size of 7 in the 0th dim of the output \s-1PDL.\s0 The placeholder panes are completely padded out. But if you feed in only a single Empty \s-1PDL,\s0 you get back the Empty \s-1PDL\s0 (no padding). .SS "null" .IX Subsection "null" Returns a 'null' piddle. .PP .Vb 1 \& $x = null; .Ve .PP \&\f(CW\*(C`null()\*(C'\fR has a special meaning to \s-1PDL::PP\s0. 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). .PP .Vb 2 \& pdl> sumover sequence(10,10), $ans=null;p $ans \& [45 145 245 345 445 545 645 745 845 945] .Ve .SS "nullcreate" .IX Subsection "nullcreate" Returns a 'null' piddle. .PP .Vb 1 \& $x = PDL\->nullcreate($arg) .Ve .PP 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) \s-1PDL\s0 objects. .PP For the above usage: If \f(CW$arg\fR is a \s-1PDL,\s0 or a derived \s-1PDL,\s0 then \f(CW\*(C`$arg\->null\*(C'\fR is returned. If \f(CW$arg\fR is a scalar (i.e. a zero-dimensional \s-1PDL\s0) then \f(CW\*(C`PDL\->null\*(C'\fR is returned. .PP .Vb 4 \& PDL::Derived\->nullcreate(10) \& returns PDL::Derived\->null. \& PDL\->nullcreate($pdlderived) \& returns $pdlderived\->null. .Ve .SS "nelem" .IX Subsection "nelem" Return the number of elements in a piddle .PP .Vb 1 \& $n = nelem($piddle); $n = $piddle\->nelem; .Ve .PP .Vb 1 \& $mean = sum($data)/nelem($data); .Ve .SS "dims" .IX Subsection "dims" Return piddle dimensions as a perl list .PP .Vb 1 \& @dims = $piddle\->dims; @dims = dims($piddle); .Ve .PP .Vb 2 \& pdl> p @tmp = dims zeroes 10,3,22 \& 10 3 22 .Ve .PP See also shape which returns a piddle instead. .SS "shape" .IX Subsection "shape" Return piddle dimensions as a piddle .PP .Vb 1 \& $shape = $piddle\->shape; $shape = shape($piddle); .Ve .PP .Vb 2 \& pdl> p $shape = shape zeroes 10,3,22 \& [10 3 22] .Ve .PP See also dims which returns a perl list. .SS "ndims" .IX Subsection "ndims" Returns the number of dimensions in a piddle. Alias for getndims. .SS "getndims" .IX Subsection "getndims" Returns the number of dimensions in a piddle .PP .Vb 1 \& $ndims = $piddle\->getndims; .Ve .PP .Vb 2 \& pdl> p zeroes(10,3,22)\->getndims \& 3 .Ve .SS "dim" .IX Subsection "dim" Returns the size of the given dimension of a piddle. Alias for getdim. .SS "getdim" .IX Subsection "getdim" Returns the size of the given dimension. .PP .Vb 1 \& $dim0 = $piddle\->getdim(0); .Ve .PP .Vb 2 \& pdl> p zeroes(10,3,22)\->getdim(1) \& 3 .Ve .PP 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, .PP .Vb 1 \& print $x\->getdim(10000); .Ve .PP will print 1 for most practically encountered piddles. .SS "topdl" .IX Subsection "topdl" alternate piddle constructor \- ensures arg is a piddle .PP .Vb 1 \& $x = topdl(SCALAR|ARRAY REFERENCE|ARRAY); .Ve .PP The difference between \fBpdl()\fR and \f(CW\*(C`topdl()\*(C'\fR is that the latter will just 'fall through' if the argument is already a piddle. It will return a reference and \fI\s-1NOT\s0\fR a new copy. .PP This is particularly 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 \f(CW\*(C`topdl()\*(C'\fR will ensure nothing breaks if passed with '2'. .PP Note that \f(CW\*(C`topdl()\*(C'\fR is not exported by default (see example below for usage). .PP .Vb 2 \& use PDL::Core \*(Aq:Internal\*(Aq; # use the internal routines of \& # the Core module \& \& $x = topdl 43; # $x is piddle with value \*(Aq43\*(Aq \& $y = topdl $piddle; # fall through \& $x = topdl (1,2,3,4); # Convert 1D array .Ve .SS "get_datatype" .IX Subsection "get_datatype" Internal: Return the numeric value identifying the piddle datatype .PP .Vb 1 \& $x = $piddle\->get_datatype; .Ve .PP Mainly used for internal routines. .PP \&\s-1NOTE:\s0 get_datatype returns 'just a number' not any special type object, unlike type. .SS "howbig" .IX Subsection "howbig" Returns the sizeof a piddle datatype in bytes. .PP Note that \f(CW\*(C`howbig()\*(C'\fR is not exported by default (see example below for usage). .PP .Vb 2 \& use PDL::Core \*(Aq:Internal\*(Aq; # use the internal routines of \& # the Core module \& \& $size = howbig($piddle\->get_datatype); .Ve .PP Mainly used for internal routines. .PP \&\s-1NOTE: NOT\s0 a method! This is because get_datatype returns \&'just a number' not any special object. .PP .Vb 2 \& pdl> p howbig(ushort([1..10])\->get_datatype) \& 2 .Ve .SS "get_dataref" .IX Subsection "get_dataref" Return the internal data for a piddle, as a perl \s-1SCALAR\s0 ref. .PP Most piddles hold their internal data in a packed perl string, to take advantage of perl's memory management. This gives you direct access to the string, which is handy when you need to manipulate the binary data directly (e.g. for file I/O). If you modify the string, you'll need to call upd_data afterward, to make sure that the piddle points to the new location of the underlying perl variable. .PP Calling \f(CW\*(C`get_dataref\*(C'\fR automatically physicalizes your piddle (see make_physical). You definitely don't want to do anything to the \s-1SV\s0 to truncate or deallocate the string, unless you correspondingly call reshape to make the \&\s-1PDL\s0 match its new data dimension. .PP You definitely don't want to use get_dataref unless you know what you are doing (or are trying to find out): you can end up scrozzling memory if you shrink or eliminate the string representation of the variable. Here be dragons. .SS "upd_data" .IX Subsection "upd_data" Update the data pointer in a piddle to match its perl \s-1SV.\s0 .PP This is useful if you've been monkeying with the packed string representation of the \s-1PDL,\s0 which you probably shouldn't be doing anyway. (see get_dataref.) .SS "threadids" .IX Subsection "threadids" Returns the piddle thread IDs as a perl list .PP Note that \f(CW\*(C`threadids()\*(C'\fR is not exported by default (see example below for usage). .PP .Vb 2 \& use PDL::Core \*(Aq:Internal\*(Aq; # use the internal routines of \& # the Core module \& \& @ids = threadids $piddle; .Ve .SS "doflow" .IX Subsection "doflow" Turn on/off dataflow .PP .Vb 1 \& $x\->doflow; doflow($x); .Ve .SS "flows" .IX Subsection "flows" Whether or not a piddle is indulging in dataflow .PP .Vb 1 \& something if $x\->flows; $hmm = flows($x); .Ve .SS "new" .IX Subsection "new" new piddle constructor method .PP .Vb 1 \& $x = PDL\->new(SCALAR|ARRAY|ARRAY REF|STRING); .Ve .PP .Vb 7 \& $x = PDL\->new(42); # new from a Perl scalar \& $x = new PDL 42; # ditto \& $y = PDL\->new(@list_of_vals); # new from Perl list \& $y = new PDL @list_of_vals; # ditto \& $z = PDL\->new(\e@list_of_vals); # new from Perl list reference \& $w = PDL\->new("[1 2 3]"); # new from Perl string, using \& # Matlab constructor syntax .Ve .PP Constructs piddle from perl numbers and lists and strings with Matlab/Octave style constructor syntax. .PP The string input is fairly versatile though not performance optimized. The goal is to make it easy to copy and paste code from \s-1PDL\s0 output and to offer a familiar Matlab syntax for piddle construction. As of May, 2010, it is a new feature, so feel free to report bugs or suggest new features. See documentation for pdl for more examples of usage. .SS "copy" .IX Subsection "copy" Make a physical copy of a piddle .PP .Vb 1 \& $new = $old\->copy; .Ve .PP Since \f(CW\*(C`$new = $old\*(C'\fR just makes a new reference, the \&\f(CW\*(C`copy\*(C'\fR method is provided to allow real independent copies to be made. .SS "hdr_copy" .IX Subsection "hdr_copy" Return an explicit copy of the header of a \s-1PDL.\s0 .PP hdr_copy is just a wrapper for the internal routine _hdr_copy, which takes the hash ref itself. That is the routine which is used to make copies of the header during normal operations if the \fBhdrcpy()\fR flag of a \s-1PDL\s0 is set. .PP General-purpose deep copies are expensive in perl, so some simple optimization happens: .PP If the header is a tied array or a blessed hash ref with an associated method called \f(CW\*(C`copy\*(C'\fR, then that \->copy method is called. Otherwise, all elements of the hash are explicitly copied. References are recursively deep copied. .PP This routine seems to leak memory. .SS "unwind" .IX Subsection "unwind" Return a piddle which is the same as the argument except that all threadids have been removed. .PP .Vb 1 \& $y = $x\->unwind; .Ve .SS "make_physical" .IX Subsection "make_physical" Make sure the data portion of a piddle can be accessed from \s-1XS\s0 code. .PP .Vb 2 \& $x\->make_physical; \& $x\->call_my_xs_method; .Ve .PP 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 \fIvirtual\fR piddles that make use of the \fIvaffine\fR 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. .PP Note: this function should not be used unless absolutely necessary since otherwise memory requirements might be severely increased. Instead of writing your own \s-1XS\s0 code with the need to call \f(CW\*(C`make_physical\*(C'\fR you might want to consider using the \s-1PDL\s0 preprocessor (see \s-1PDL::PP\s0) which can be used to transparently access virtual piddles without the need to physicalise them (though there are exceptions). .SS "dummy" .IX Subsection "dummy" Insert a 'dummy dimension' of given length (defaults to 1) .PP No relation to the 'Dungeon Dimensions' in Discworld! .PP Negative positions specify relative to last dimension, i.e. \f(CW\*(C`dummy(\-1)\*(C'\fR appends one dimension at end, \&\f(CW\*(C`dummy(\-2)\*(C'\fR inserts a dummy dimension in front of the last dim, etc. .PP If you specify a dimension position larger than the existing dimension list of your \s-1PDL,\s0 the \s-1PDL\s0 gets automagically padded with extra dummy dimensions so that you get the dim you asked for, in the slot you asked for. This could cause you trouble if, for example, you ask for \f(CW$x\fR\->dummy(5000,1) because \f(CW$x\fR will get 5,000 dimensions, each of rank 1. .PP Because padding at the beginning of the dimension list moves existing dimensions from slot to slot, it's considered unsafe, so automagic padding doesn't work for large negative indices \*(-- only for large positive indices. .PP .Vb 1 \& $y = $x\->dummy($position[,$dimsize]); .Ve .PP .Vb 6 \& pdl> p sequence(3)\->dummy(0,3) \& [ \& [0 0 0] \& [1 1 1] \& [2 2 2] \& ] \& \& pdl> p sequence(3)\->dummy(3,2) \& [ \& [ \& [0 1 2] \& ] \& [ \& [0 1 2] \& ] \& ] \& \& pdl> p sequence(3)\->dummy(\-3,2) \& Runtime error: PDL: For safety, < \-(dims+1) forbidden in dummy. min=\-2, pos=\-3 .Ve .SS "clump" .IX Subsection "clump" \&\*(L"clumps\*(R" several dimensions into one large dimension .PP If called with one argument \f(CW$n\fR clumps the first \f(CW$n\fR dimensions into one. For example, if \f(CW$x\fR has dimensions \&\f(CW\*(C`(5,3,4)\*(C'\fR then after .PP .Vb 1 \& $y = $x\->clump(2); # Clump 2 first dimensions .Ve .PP the variable \f(CW$y\fR will have dimensions \f(CW\*(C`(15,4)\*(C'\fR and the element \f(CW\*(C`$y\->at(7,3)\*(C'\fR refers to the element \&\f(CW\*(C`$x\->at(1,2,3)\*(C'\fR. .PP Use \f(CW\*(C`clump(\-1)\*(C'\fR to flatten a piddle. The method flat is provided as a convenient alias. .PP Clumping with a negative dimension in general leaves that many dimensions behind \*(-- e.g. clump(\-2) clumps all of the first few dimensions into a single one, leaving a 2\-D piddle. .PP If \f(CW\*(C`clump\*(C'\fR is called with an index list with more than one element it is treated as a list of dimensions that should be clumped together into one. The resulting clumped dim is placed at the position of the lowest index in the list. This convention ensures that \f(CW\*(C`clump\*(C'\fR does the expected thing in the usual cases. The following example demonstrates typical usage: .PP .Vb 4 \& $x = sequence 2,3,3,3,5; # 5D piddle \& $c = $x\->clump(1..3); # clump all the dims 1 to 3 into one \& print $c\->info; # resulting 3D piddle has clumped dim at pos 1 \& PDL: Double D [2,27,5] .Ve .SS "thread_define" .IX Subsection "thread_define" define functions that support threading at the perl level .PP .Vb 3 \& thread_define \*(Aqtline(a(n);b(n))\*(Aq, over { \& line $_[0], $_[1]; # make line compliant with threading \& }; .Ve .PP \&\f(CW\*(C`thread_define\*(C'\fR 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 \s-1PDL::PP\s0 (see \s-1PDL::PP\s0); however, it is most useful to wrap existing perl functions so that the new routine supports \s-1PDL\s0 threading. .PP \&\f(CW\*(C`thread_define\*(C'\fR is used to define new \fIthreading aware\fR 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 \s-1PDL::PP\s0) 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 \&\s-1PDL\s0 threading, see PDL::Indexing). .PP The actual work is done by the \f(CW\*(C`signature\*(C'\fR class which parses the signature string, does runtime dimension checks and the routine \f(CW\*(C`threadover\*(C'\fR that generates the loop over all appropriate slices of pdl arguments and creates pdls as needed. .PP Similar to \f(CW\*(C`pp_def\*(C'\fR and its \f(CW\*(C`OtherPars\*(C'\fR 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 \f(CW\*(C`NOtherPars\*(C'\fR parameter in the signature. The number of \f(CW\*(C`NOtherPars\*(C'\fR specified will be passed unaltered into the subroutine given as the second argument of \&\f(CW\*(C`thread_define\*(C'\fR. Let's illustrate this with an example: .PP .Vb 4 \& PDL::thread_define \*(Aqtriangles(inda();indb();indc()), NOtherPars => 2\*(Aq, \& PDL::over { \& ${$_[3]} .= $_[4].join(\*(Aq,\*(Aq,map {$_\->at} @_[0..2]).",\-1,\en"; \& }; .Ve .PP This defines a function \f(CW\*(C`triangles\*(C'\fR 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 \f(CW$_[4]\fR. Here is typical usage: .PP .Vb 3 \& $txt = \*(Aq\*(Aq; \& triangles(pdl(1,2,3),pdl(1),pdl(0),\e$txt," "x10); \& print $txt; .Ve .PP resulting in the following output .PP .Vb 3 \& 1,1,0,\-1, \& 2,1,0,\-1, \& 3,1,0,\-1, .Ve .PP which is used in PDL::Graphics::TriD::VRML to generate \s-1VRML\s0 output. .PP Currently, this is probably not much more than a \s-1POP\s0 (proof of principle) but is hoped to be useful enough for some real life work. .PP Check \s-1PDL::PP\s0 for the format of the signature. Currently, the \&\f(CW\*(C`[t]\*(C'\fR qualifier and all type qualifiers are ignored. .SS "thread" .IX Subsection "thread" Use explicit threading over specified dimensions (see also PDL::Indexing) .PP .Vb 1 \& $y = $x\->thread($dim,[$dim1,...]) .Ve .PP .Vb 2 \& $x = zeroes 3,4,5; \& $y = $x\->thread(2,0); .Ve .PP Same as PDL::thread1, i.e. uses thread id 1. .SS "diagonal" .IX Subsection "diagonal" Returns the multidimensional diagonal over the specified dimensions. .PP .Vb 1 \& $d = $x\->diagonal(dim1, dim2,...) .Ve .PP .Vb 10 \& pdl> $x = zeroes(3,3,3); \& pdl> ($y = $x\->diagonal(0,1))++; \& pdl> p $x \& [ \& [ \& [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] \& ] \& ] .Ve .SS "thread1" .IX Subsection "thread1" Explicit threading over specified dims using thread id 1. .PP .Vb 1 \& $xx = $x\->thread1(3,1) .Ve .PP .Vb 1 \& Wibble .Ve .PP Convenience function interfacing to PDL::Slices::threadI. .SS "thread2" .IX Subsection "thread2" Explicit threading over specified dims using thread id 2. .PP .Vb 1 \& $xx = $x\->thread2(3,1) .Ve .PP .Vb 1 \& Wibble .Ve .PP Convenience function interfacing to PDL::Slices::threadI. .SS "thread3" .IX Subsection "thread3" Explicit threading over specified dims using thread id 3. .PP .Vb 1 \& $xx = $x\->thread3(3,1) .Ve .PP .Vb 1 \& Wibble .Ve .PP Convenience function interfacing to PDL::Slices::threadI. .SS "sever" .IX Subsection "sever" sever any links of this piddle to parent piddles .PP In \s-1PDL\s0 it is possible for a piddle to be just another view into another piddle's data. In that case we call this piddle a \fIvirtual piddle\fR and the original piddle owning the data its parent. In other languages these alternate views sometimes run by names such as \fIalias\fR or \fIsmart reference\fR. .PP Typical functions that return such piddles are \f(CW\*(C`slice\*(C'\fR, \f(CW\*(C`xchg\*(C'\fR, \&\f(CW\*(C`index\*(C'\fR, etc. Sometimes, however, you would like to separate the \&\fIvirtual piddle\fR from its parent's data and just give it a life of its own (so that manipulation of its data doesn't change the parent). This is simply achieved by using \f(CW\*(C`sever\*(C'\fR. For example, .PP .Vb 2 \& $x = $pdl\->index(pdl(0,3,7))\->sever; \& $x++; # important: $pdl is not modified! .Ve .PP In many (but not all) circumstances it acts therefore similar to copy. However, in general performance is better with \f(CW\*(C`sever\*(C'\fR and secondly, \&\f(CW\*(C`sever\*(C'\fR doesn't lead to futile copying when used on piddles that already have their own data. On the other hand, if you really want to make sure to work on a copy of a piddle use copy. .PP .Vb 2 \& $x = zeroes(20); \& $x\->sever; # NOOP since $x is already its own boss! .Ve .PP Again note: \f(CW\*(C`sever\*(C'\fR \fIis not\fR the same as copy! For example, .PP .Vb 5 \& $x = zeroes(1); # $x does not have a parent, i.e. it is not a slice etc \& $y = $x\->sever; # $y is now pointing to the same piddle as $x \& $y++; \& print $x; \& [1] .Ve .PP but .PP .Vb 5 \& $x = zeroes(1); \& $y = $x\->copy; # $y is now pointing to a new piddle \& $y++; \& print $x; \& [0] .Ve .SS "info" .IX Subsection "info" Return formatted information about a piddle. .PP .Vb 1 \& $x\->info($format_string); .Ve .PP .Vb 1 \& print $x\->info("Type: %T Dim: %\-15D State: %S"); .Ve .PP 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 \f(CW\*(C`%\*(C'\fR where the width is optional and the letter is one of .IP "T" 7 .IX Item "T" Type .IP "D" 7 .IX Item "D" Formatted Dimensions .IP "F" 7 .IX Item "F" Dataflow status .IP "S" 7 .IX Item "S" Some internal flags (P=physical,V=Vaffine,C=changed,B=may contain bad data) .IP "C" 7 .IX Item "C" Class of this piddle, i.e. \f(CW\*(C`ref $pdl\*(C'\fR .IP "A" 7 .IX Item "A" Address of the piddle struct as a unique identifier .IP "M" 7 .IX Item "M" Calculated memory consumption of this piddle's data area .SS "approx" .IX Subsection "approx" test for approximately equal values (relaxed \f(CW\*(C`==\*(C'\fR) .PP .Vb 3 \& # ok if all corresponding values in \& # piddles are within 1e\-8 of each other \& print "ok\en" if all approx $x, $y, 1e\-8; .Ve .PP \&\f(CW\*(C`approx\*(C'\fR is a relaxed form of the \f(CW\*(C`==\*(C'\fR operator and often more appropriate for floating point types (\f(CW\*(C`float\*(C'\fR and \f(CW\*(C`double\*(C'\fR). .PP Usage: .PP .Vb 1 \& $res = approx $x, $y [, $eps] .Ve .PP The optional parameter \f(CW$eps\fR is remembered across invocations and initially set to 1e\-6, e.g. .PP .Vb 3 \& approx $x, $y; # last $eps used (1e\-6 initially) \& approx $x, $y, 1e\-10; # 1e\-10 \& approx $x, $y; # also 1e\-10 .Ve .SS "mslice" .IX Subsection "mslice" Convenience interface to slice, allowing easier inclusion of dimensions in perl code. .PP .Vb 1 \& $w = $x\->mslice(...); .Ve .PP .Vb 2 \& # below is the same as $x\->slice("5:7,:,3:4:2") \& $w = $x\->mslice([5,7],X,[3,4,2]); .Ve .SS "nslice_if_pdl" .IX Subsection "nslice_if_pdl" If \f(CW$self\fR is a \s-1PDL,\s0 then calls \f(CW\*(C`slice\*(C'\fR with all but the last argument, otherwise \f(CW$self\fR\->($_[\-1]) is called where \f(CW$_\fR[\-1} is the original argument string found during PDL::NiceSlice filtering. .PP \&\s-1DEVELOPER\s0'S \s-1NOTE:\s0 this routine is found in Core.pm.PL but would be better placed in Slices/slices.pd. It is likely to be moved there and/or changed to \*(L"slice_if_pdl\*(R" for \s-1PDL 3.0.\s0 .PP .Vb 1 \& $w = $x\->nslice_if_pdl(...,\*(Aq(args)\*(Aq); .Ve .SS "nslice" .IX Subsection "nslice" \&\f(CW\*(C`nslice\*(C'\fR was an internally used interface for PDL::NiceSlice, but is now merely a springboard to PDL::Slice. It is deprecated and likely to disappear in \s-1PDL 3.0.\s0 .SS "inplace" .IX Subsection "inplace" Flag a piddle so that the next operation is done 'in place' .PP .Vb 1 \& somefunc($x\->inplace); somefunc(inplace $x); .Ve .PP In most cases one likes to use the syntax \f(CW\*(C`$y = f($x)\*(C'\fR, however in many case the operation \f(CW\*(C`f()\*(C'\fR 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(CW\*(C`f()\*(C'\fR in such a way that it operates in-place, and use \f(CW\*(C`inplace\*(C'\fR to hint that a new copy should be disabled. This also makes for clear syntax. .PP 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 \f(CW\*(C`log10\*(C'\fR). .PP .Vb 4 \& pdl> $x = xvals zeroes 10; \& pdl> log10(inplace $x) \& pdl> p $x \& [\-inf 0 0.30103 0.47712125 0.60205999 0.69897 0.77815125 0.84509804 0.90308999 0.95424251] .Ve .SS "is_inplace" .IX Subsection "is_inplace" Test the in-place flag on a piddle .PP .Vb 2 \& $out = ($in\->is_inplace) ? $in : zeroes($in); \& $in\->set_inplace(0) .Ve .PP Provides access to the inplace hint flag, within the perl millieu. That way functions you write can be inplace aware... If given an argument the inplace flag will be set or unset depending on the value at the same time. Can be used for shortcut tests that delete the inplace flag while testing: .PP .Vb 1 \& $out = ($in\->is_inplace(0)) ? $in : zeroes($in); # test & unset! .Ve .SS "set_inplace" .IX Subsection "set_inplace" Set the in-place flag on a piddle .PP .Vb 2 \& $out = ($in\->is_inplace) ? $in : zeroes($in); \& $in\->set_inplace(0); .Ve .PP Provides access to the inplace hint flag, within the perl millieu. Useful mainly for turning it \s-1OFF,\s0 as inplace turns it \s-1ON\s0 more conveniently. .SS "new_or_inplace" .IX Subsection "new_or_inplace" .Vb 2 \& $w = new_or_inplace(shift()); \& $w = new_or_inplace(shift(),$preferred_type); .Ve .PP Return back either the argument pdl or a copy of it depending on whether it be flagged in-place or no. Handy for building inplace-aware functions. .PP If you specify a preferred type (must be one of the usual \s-1PDL\s0 type strings, a list ref containing several of them, or a string containing several of them), then the copy is coerced into the first preferred type listed if it is not already one of the preferred types. .PP Note that if the inplace flag is set, no coersion happens even if you specify a preferred type. .SS "new_from_specification" .IX Subsection "new_from_specification" Internal method: create piddle by specification .PP This is the argument processing method called by zeroes and some other functions which constructs piddles from argument lists of the form: .PP .Vb 1 \& [type], $nx, $ny, $nz,... .Ve .PP For \f(CW$nx\fR, \f(CW$ny\fR, etc. 0 and 1D piddles are allowed. Giving those has the same effect as if saying \f(CW\*(C`$arg\->list\*(C'\fR, e.g. .PP .Vb 1 \& 1, pdl(5,2), 4 .Ve .PP is equivalent to .PP .Vb 1 \& 1, 5, 2, 4 .Ve .PP Note, however, that in all functions using \f(CW\*(C`new_from_specification\*(C'\fR calling \f(CW\*(C`func $piddle\*(C'\fR will probably not do what you want. So to play safe use (e.g. with zeroes) .PP .Vb 1 \& $pdl = zeroes $dimpdl\->list; .Ve .PP Calling .PP .Vb 1 \& $pdl = zeroes $dimpdl; .Ve .PP will rather be equivalent to .PP .Vb 1 \& $pdl = zeroes $dimpdl\->dims; .Ve .PP However, .PP .Vb 1 \& $pdl = zeroes ushort, $dimpdl; .Ve .PP will again do what you intended since it is interpreted as if you had said .PP .Vb 1 \& $pdl = zeroes ushort, $dimpdl\->list; .Ve .PP This is unfortunate and confusing but no good solution seems obvious that would not break existing scripts. .SS "isnull" .IX Subsection "isnull" Test whether a piddle is null .PP .Vb 2 \& croak("Input piddle mustn\*(Aqt be null!") \& if $input_piddle\->isnull; .Ve .PP This function returns 1 if the piddle is null, zero if it is not. The purpose of null piddles is to \*(L"tell\*(R" any \s-1PDL::PP\s0 methods to allocate new memory for an output piddle, but only when that \s-1PDL::PP\s0 method is called in full-arg form. Of course, there's no reason you couldn't commandeer the special value for your own purposes, for which this test function would prove most helpful. But in general, you shouldn't need to test for a piddle's nullness. .PP See \*(L"Null PDLs\*(R" for more information. .SS "isempty" .IX Subsection "isempty" Test whether a piddle is empty .PP .Vb 1 \& print "The piddle has zero dimension\en" if $pdl\->isempty; .Ve .PP 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. .PP .Vb 4 \& pdl> $w=sequence(10) \& pdl> $i=which($w < \-1) \& pdl> print "I found no matches!\en" if ($i\->isempty); \& I found no matches! .Ve .PP Note that having zero elements is rather different from the concept of being a null piddle, see the \s-1PDL::FAQ\s0 and PDL::Indexing manpages for discussions of this. .SS "zeroes" .IX Subsection "zeroes" construct a zero filled piddle from dimension list or template piddle. .PP Various forms of usage, .PP (i) by specification or (ii) by template piddle: .PP .Vb 9 \& # usage type (i): \& $w = zeroes([type], $nx, $ny, $nz,...); \& $w = PDL\->zeroes([type], $nx, $ny, $nz,...); \& $w = $pdl\->zeroes([type], $nx, $ny, $nz,...); \& # usage type (ii): \& $w = zeroes $y; \& $w = $y\->zeroes \& zeroes inplace $w; # Equivalent to $w .= 0; \& $w\->inplace\->zeroes; # "" .Ve .PP .Vb 9 \& pdl> $z = zeroes 4,3 \& pdl> p $z \& [ \& [0 0 0 0] \& [0 0 0 0] \& [0 0 0 0] \& ] \& pdl> $z = zeroes ushort, 3,2 # Create ushort array \& [ushort() etc. with no arg returns a PDL::Types token] .Ve .PP See also new_from_specification for details on using piddles in the dimensions list. .SS "zeros" .IX Subsection "zeros" construct a zero filled piddle (see zeroes for usage) .SS "ones" .IX Subsection "ones" construct a one filled piddle .PP .Vb 2 \& $w = ones([type], $nx, $ny, $nz,...); \& etc. (see \*(Aqzeroes\*(Aq) .Ve .PP .Vb 1 \& see zeroes() and add one .Ve .PP See also new_from_specification for details on using piddles in the dimensions list. .SS "reshape" .IX Subsection "reshape" Change the shape (i.e. dimensions) of a piddle, preserving contents. .PP .Vb 1 \& $x\->reshape(NEWDIMS); reshape($x, NEWDIMS); .Ve .PP 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. .PP ***Potential incompatibility with earlier versions of PDL**** If the list of \f(CW\*(C`NEWDIMS\*(C'\fR is empty \f(CW\*(C`reshape\*(C'\fR will just drop all dimensions of size 1 (preserving the number of elements): .PP .Vb 5 \& $w = sequence(3,4,5); \& $y = $w(1,3); \& $y\->reshape(); \& print $y\->info; \& PDL: Double D [5] .Ve .PP Dimensions of size 1 will also be dropped if \f(CW\*(C`reshape\*(C'\fR is invoked with the argument \-1: .PP .Vb 1 \& $y = $w\->reshape(\-1); .Ve .PP As opposed to \f(CW\*(C`reshape\*(C'\fR without arguments, \f(CW\*(C`reshape(\-1)\*(C'\fR preserves dataflow: .PP .Vb 12 \& $w = ones(2,1,2); \& $y = $w(0)\->reshape(\-1); \& $y++; \& print $w; \& [ \& [ \& [2 1] \& ] \& [ \& [2 1] \& ] \& ] .Ve .PP Important: Piddles are changed inplace! .PP Note: If \f(CW$x\fR is connected to any other \s-1PDL\s0 (e.g. if it is a slice) then the connection is first severed. .PP .Vb 10 \& pdl> $x = sequence(10) \& pdl> reshape $x,3,4; p $x \& [ \& [0 1 2] \& [3 4 5] \& [6 7 8] \& [9 0 0] \& ] \& pdl> reshape $x,5; p $x \& [0 1 2 3 4] .Ve .SS "squeeze" .IX Subsection "squeeze" eliminate all singleton dimensions (dims of size 1) .PP .Vb 1 \& $y = $w(0,0)\->squeeze; .Ve .PP Alias for \f(CW\*(C`reshape(\-1)\*(C'\fR. Removes all singleton dimensions and preserves dataflow. A more concise interface is provided by PDL::NiceSlice via modifiers: .PP .Vb 2 \& use PDL::NiceSlice; \& $y = $w(0,0;\-); # same as $w(0,0)\->squeeze .Ve .SS "flat" .IX Subsection "flat" flatten a piddle (alias for \f(CW\*(C`$pdl\->clump(\-1)\*(C'\fR) .PP .Vb 1 \& $srt = $pdl\->flat\->qsort; .Ve .PP Useful method to make a 1D piddle from an arbitrarily sized input piddle. Data flows back and forth as usual with slicing routines. Falls through if argument already <= 1D. .SS "convert" .IX Subsection "convert" Generic datatype conversion function .PP .Vb 1 \& $y = convert($x, $newtypenum); .Ve .PP .Vb 2 \& $y = convert $x, long \& $y = convert $x, ushort .Ve .PP \&\f(CW$newtype\fR is a type \fBnumber\fR, for convenience they are returned by \f(CW\*(C`long()\*(C'\fR etc when called without arguments. .SS "Datatype_conversions" .IX Subsection "Datatype_conversions" byte|short|ushort|long|indx|longlong|float|double (shorthands to convert datatypes) .PP .Vb 2 \& $y = double $x; $y = ushort [1..10]; \& # all of the above listed shorthands behave similarly .Ve .PP When called with a piddle argument, they convert to the specific datatype. .PP When called with a numeric, list, listref, or string argument they construct a new piddle. This is a convenience to avoid having to be long-winded and say \f(CW\*(C`$x = long(pdl(42))\*(C'\fR .PP Thus one can say: .PP .Vb 4 \& $w = float(1,2,3,4); # 1D \& $w = float q[1 2 3; 4 5 6]; # 2D \& $w = float([1,2,3],[4,5,6]); # 2D \& $w = float([[1,2,3],[4,5,6]]); # 2D .Ve .PP Note the last three give identical results, and the last two are exactly equivalent \- a list is automatically converted to a list reference for syntactic convenience. i.e. you can omit the outer \f(CW\*(C`[]\*(C'\fR .PP When called with no arguments, these functions return a special type token. This allows syntactical sugar like: .PP .Vb 1 \& $x = ones byte, 1000,1000; .Ve .PP This example creates a large piddle directly as byte datatype in order to save memory. .PP In order to control how undefs are handled in converting from perl lists to PDLs, one can set the variable \f(CW$PDL::undefval\fR; see the function \fBpdl()\fR for more details. .PP .Vb 4 \& pdl> p $x=sqrt float [1..10] \& [1 1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 3.16228] \& pdl> p byte $x \& [1 1 1 2 2 2 2 2 3 3] .Ve .SS "byte" .IX Subsection "byte" Convert to byte datatype .SS "short" .IX Subsection "short" Convert to short datatype .SS "ushort" .IX Subsection "ushort" Convert to ushort datatype .SS "long" .IX Subsection "long" Convert to long datatype .SS "indx" .IX Subsection "indx" Convert to indx datatype .SS "longlong" .IX Subsection "longlong" Convert to longlong datatype .SS "float" .IX Subsection "float" Convert to float datatype .SS "double" .IX Subsection "double" Convert to double datatype .SS "type" .IX Subsection "type" return the type of a piddle as a blessed type object .PP A convenience function for use with the piddle constructors, e.g. .PP .Vb 2 \& $y = PDL\->zeroes($x\->type,$x\->dims,3); \& die "must be float" unless $x\->type == float; .Ve .PP See also the discussion of the \f(CW\*(C`PDL::Type\*(C'\fR class in PDL::Types. Note that the \f(CW\*(C`PDL::Type\*(C'\fR objects have overloaded comparison and stringify operators so that you can compare and print types: .PP .Vb 2 \& $x = $x\->float if $x\->type < float; \& $t = $x\->type; print "Type is $t\e"; .Ve .SS "list" .IX Subsection "list" Convert piddle to perl list .PP .Vb 1 \& @tmp = list $x; .Ve .PP Obviously this is grossly inefficient for the large datasets \s-1PDL\s0 is designed to handle. This was provided as a get out while \s-1PDL\s0 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. .PP .Vb 3 \& for (list $x) { \& # Do something on each value... \& } .Ve .PP If you compile \s-1PDL\s0 with bad value support (the default), your machine's docs will also say this: .PP list converts any bad values into the string '\s-1BAD\s0'. .SS "unpdl" .IX Subsection "unpdl" Convert piddle to nested Perl array references .PP .Vb 1 \& $arrayref = unpdl $x; .Ve .PP This function returns a reference to a Perl list-of-lists structure equivalent to the input piddle (within the limitation that while values of elements should be preserved, the detailed datatypes will not as perl itself basically has \*(L"number\*(R" data rather than byte, short, int... E.g., \f(CW\*(C`sum($x \- pdl( $x\->unpdl ))\*(C'\fR should equal 0. .PP Obviously this is grossly inefficient in memory and processing for the large datasets \s-1PDL\s0 is designed to handle. Sometimes, however, you really want to move your data back to Perl, and with proper dimensionality, unlike \f(CW\*(C`list\*(C'\fR. .PP .Vb 2 \& use JSON; \& my $json = encode_json unpdl $pdl; .Ve .PP If you compile \s-1PDL\s0 with bad value support (the default), your machine's docs will also say this: .PP unpdl converts any bad values into the string '\s-1BAD\s0'. .SS "listindices" .IX Subsection "listindices" Convert piddle indices to perl list .PP .Vb 1 \& @tmp = listindices $x; .Ve .PP \&\f(CW@tmp\fR now contains the values \f(CW\*(C`0..nelem($x)\*(C'\fR. .PP Obviously this is grossly inefficient for the large datasets \s-1PDL\s0 is designed to handle. This was provided as a get out while \s-1PDL\s0 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. .PP .Vb 3 \& for $i (listindices $x) { \& # Do something on each value... \& } .Ve .SS "set" .IX Subsection "set" Set a single value inside a piddle .PP .Vb 1 \& set $piddle, @position, $value .Ve .PP \&\f(CW@position\fR 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 assignment operator \f(CW\*(C`.=\*(C'\fR. .PP .Vb 9 \& pdl> $x = sequence 3,4 \& pdl> set $x, 2,1,99 \& pdl> p $x \& [ \& [ 0 1 2] \& [ 3 4 99] \& [ 6 7 8] \& [ 9 10 11] \& ] .Ve .SS "at" .IX Subsection "at" Returns a single value inside a piddle as perl scalar. .PP .Vb 1 \& $z = at($piddle, @position); $z=$piddle\->at(@position); .Ve .PP \&\f(CW@position\fR 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 \s-1PDL\s0 internals. .PP .Vb 3 \& pdl> $x = sequence 3,4 \& pdl> p $x\->at(1,2) \& 7 .Ve .PP If you compile \s-1PDL\s0 with bad value support (the default), your machine's docs will also say this: .PP at converts any bad values into the string '\s-1BAD\s0'. .SS "sclr" .IX Subsection "sclr" return a single value from a piddle as a scalar .PP .Vb 2 \& $val = $x(10)\->sclr; \& $val = sclr inner($x,$y); .Ve .PP The \f(CW\*(C`sclr\*(C'\fR method is useful to turn a piddle into a normal Perl scalar. Its main advantage over using \f(CW\*(C`at\*(C'\fR for this purpose is the fact that you do not need to worry if the piddle is 0D, 1D or higher dimensional. Using \f(CW\*(C`at\*(C'\fR you have to supply the correct number of zeroes, e.g. .PP .Vb 4 \& $x = sequence(10); \& $y = $x\->slice(\*(Aq4\*(Aq); \& print $y\->sclr; # no problem \& print $y\->at(); # error: needs at least one zero .Ve .PP \&\f(CW\*(C`sclr\*(C'\fR is generally used when a Perl scalar is required instead of a one-element piddle. If the input is a multielement piddle the first value is returned as a Perl scalar. You can optionally switch on checks to ensure that the input piddle has only one element: .PP .Vb 2 \& PDL\->sclr({Check => \*(Aqwarn\*(Aq}); # carp if called with multi\-el pdls \& PDL\->sclr({Check => \*(Aqbarf\*(Aq}); # croak if called with multi\-el pdls .Ve .PP are the commands to switch on warnings or raise an error if a multielement piddle is passed as input. Note that these options can only be set when \f(CW\*(C`sclr\*(C'\fR is called as a class method (see example above). Use .PP .Vb 1 \& PDL\->sclr({Check=>0}); .Ve .PP to switch these checks off again (default setting); When called as a class method the resulting check mode is returned (0: no checking, 1: warn, 2: barf). .SS "cat" .IX Subsection "cat" concatenate piddles to N+1 dimensional piddle .PP Takes a list of N piddles of same shape as argument, returns a single piddle of dimension N+1. .PP .Vb 10 \& pdl> $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] \& ] \& ] .Ve .PP If you compile \s-1PDL\s0 with bad value support (the default), your machine's docs will also say this: .PP The output piddle is set bad if any input piddles have their bad flag set. .PP Similar functions include append, which appends only two piddles along their first dimension, and glue, which can append more than two piddles along an arbitrary dimension. .PP Also consider the generic constructor pdl, which can handle piddles of different sizes (with zero-padding), and will return a piddle of type 'double' by default, but may be considerably faster (up to 10x) than cat. .SS "dog" .IX Subsection "dog" Opposite of 'cat' :). Split N dim piddle to list of N\-1 dim piddles .PP 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.: .PP .Vb 10 \& pdl> $p = ones 3,3,3 \& pdl> ($x,$y,$c) = dog $p \& pdl> $y++; 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] \& ] \& ] .Ve .PP .Vb 1 \& Break => 1 Break dataflow connection (new copy) .Ve .PP If you compile \s-1PDL\s0 with bad value support (the default), your machine's docs will also say this: .PP The output piddles are set bad if the original piddle has its bad flag set. .SS "gethdr" .IX Subsection "gethdr" Retrieve header information from a piddle .PP .Vb 3 \& $pdl=rfits(\*(Aqfile.fits\*(Aq); \& $h=$pdl\->gethdr; \& print "Number of pixels in the X\-direction=$$h{NAXIS1}\en"; .Ve .PP The \f(CW\*(C`gethdr\*(C'\fR function retrieves whatever header information is contained within a piddle. The header can be set with sethdr and is always a hash reference or undef. .PP \&\f(CW\*(C`gethdr\*(C'\fR returns undef if the piddle has not yet had a header defined; compare with \f(CW\*(C`hdr\*(C'\fR and \f(CW\*(C`fhdr\*(C'\fR, which are guaranteed to return a defined value. .PP Note that \fBgethdr()\fR works by \fBreference\fR: you can modify the header in-place once it has been retrieved: .PP .Vb 3 \& $x = rfits($filename); \& $xh = $x\->gethdr(); \& $xh\->{FILENAME} = $filename; .Ve .PP It is also important to realise that in most cases the header is not automatically copied when you copy the piddle. See hdrcpy to enable automatic header copying. .PP Here's another example: 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) .PP .Vb 5 \& sub ext_rcols { \& my ($file, @columns)=@_; \& my $header={}; \& $$header{File}=$file; \& $$header{Columns}=\e@columns; \& \& @piddles=rcols $file, @columns; \& foreach (@piddles) { $_\->sethdr($header); } \& return @piddles; \& } .Ve .SS "hdr" .IX Subsection "hdr" Retrieve or set header information from a piddle .PP .Vb 1 \& $pdl\->hdr\->{CDELT1} = 1; .Ve .PP The \f(CW\*(C`hdr\*(C'\fR function allows convenient access to the header of a piddle. Unlike \f(CW\*(C`gethdr\*(C'\fR it is guaranteed to return a defined value, so you can use it in a hash dereference as in the example. If the header does not yet exist, it gets autogenerated as an empty hash. .PP Note that this is usually \*(-- but not always \*(-- What You Want. If you want to use a tied Astro::FITS::Header hash, for example, you should either construct it yourself and use \f(CW\*(C`sethdr\*(C'\fR to put it into the piddle, or use fhdr instead. (Note that you should be able to write out the \s-1FITS\s0 file successfully regardless of whether your \s-1PDL\s0 has a tied \s-1FITS\s0 header object or a vanilla hash). .SS "fhdr" .IX Subsection "fhdr" Retrieve or set \s-1FITS\s0 header information from a piddle .PP .Vb 1 \& $pdl\->fhdr\->{CDELT1} = 1; .Ve .PP The \f(CW\*(C`fhdr\*(C'\fR function allows convenient access to the header of a piddle. Unlike \f(CW\*(C`gethdr\*(C'\fR it is guaranteed to return a defined value, so you can use it in a hash dereference as in the example. If the header does not yet exist, it gets autogenerated as a tied Astro::FITS::Header hash. .PP Astro::FITS::Header tied hashes are better at matching the behavior of \&\s-1FITS\s0 headers than are regular hashes. In particular, the hash keys are CAsE INsEnSItiVE, unlike normal hash keys. See Astro::FITS::Header for details. .PP If you do not have Astro::FITS::Header installed, you get back a normal hash instead of a tied object. .SS "sethdr" .IX Subsection "sethdr" Set header information of a piddle .PP .Vb 5 \& $pdl = zeroes(100,100); \& $h = {NAXIS=>2, NAXIS1=>100, NAXIS=>100, COMMENT=>"Sample FITS\-style header"}; \& # add a FILENAME field to the header \& $$h{FILENAME} = \*(Aqfile.fits\*(Aq; \& $pdl\->sethdr( $h ); .Ve .PP The \f(CW\*(C`sethdr\*(C'\fR function sets the header information for a piddle. You must feed in a hash ref or undef, and the header field of the \s-1PDL\s0 is set to be a new ref to the same hash (or undefined). .PP The hash ref requirement is a speed bump put in place since the normal use of headers is to store fits header information and the like. Of course, if you want you can hang whatever ugly old data structure you want off of the header, but that makes life more complex. .PP Remember that the hash is not copied \*(-- the header is made into a ref that points to the same underlying data. To get a real copy without making any assumptions about the underlying data structure, you can use one of the following: .PP .Vb 2 \& use PDL::IO::Dumper; \& $pdl\->sethdr( deep_copy($h) ); .Ve .PP (which is slow but general), or .PP .Vb 1 \& $pdl\->sethdr( PDL::_hdr_copy($h) ) .Ve .PP (which uses the built-in sleazy deep copier), or (if you know that all the elements happen to be scalars): .PP .Vb 3 \& { my %a = %$h; \& $pdl\->sethdr(\e%a); \& } .Ve .PP which is considerably faster but just copies the top level. .PP The \f(CW\*(C`sethdr\*(C'\fR function must be given a hash reference or undef. For further information on the header, see gethdr, hdr, fhdr and hdrcpy. .SS "hdrcpy" .IX Subsection "hdrcpy" switch on/off/examine automatic header copying .PP .Vb 4 \& print "hdrs will be copied" if $x\->hdrcpy; \& $x\->hdrcpy(1); # switch on automatic header copying \& $y = $x\->sumover; # and $y will inherit $x\*(Aqs hdr \& $x\->hdrcpy(0); # and now make $x non\-infectious again .Ve .PP \&\f(CW\*(C`hdrcpy\*(C'\fR without an argument just returns the current setting of the flag. See also \*(L"hcpy\*(R" which returns its \s-1PDL\s0 argument (and so is useful in method-call pipelines). .PP Normally, the optional header of a piddle is not copied automatically in pdl operations. Switching on the hdrcpy flag using the \f(CW\*(C`hdrcpy\*(C'\fR method will enable automatic hdr copying. Note that an actual deep copy gets made, which is rather processor-inefficient \*(-- so avoid using header copying in tight loops! .PP Most PDLs have the \f(CW\*(C`hdrcpy\*(C'\fR flag cleared by default; however, some routines (notably rfits) set it by default where that makes more sense. .PP The \f(CW\*(C`hdrcpy\*(C'\fR flag is viral: if you set it for a \s-1PDL,\s0 then derived PDLs will get copies of the header and will also have their \f(CW\*(C`hdrcpy\*(C'\fR flags set. For example: .PP .Vb 8 \& $x = xvals(50,50); \& $x\->hdrcpy(1); \& $x\->hdr\->{FOO} = "bar"; \& $y = $x++; \& $c = $y++; \& print $y\->hdr\->{FOO}, " \- ", $c\->hdr\->{FOO}, "\en"; \& $y\->hdr\->{FOO} = "baz"; \& print $x\->hdr\->{FOO}, " \- ", $y\->hdr\->{FOO}, " \- ", $c\->hdr\->{FOO}, "\en"; .Ve .PP will print: .PP .Vb 2 \& bar \- bar \& bar \- baz \- bar .Ve .PP Performing an operation in which more than one \s-1PDL\s0 has its hdrcpy flag causes the resulting \s-1PDL\s0 to take the header of the first \s-1PDL:\s0 .PP .Vb 5 \& ($x,$y) = sequence(5,2)\->dog; \& $x\->hdrcpy(1); $y\->hdrcpy(1); \& $x\->hdr\->{foo} = \*(Aqa\*(Aq; \& $y\->hdr\->{foo} = \*(Aqb\*(Aq; \& print (($x+$y)\->hdr\->{foo} , ($y+$x)\->hdr\->{foo}); .Ve .PP will print: .PP .Vb 1 \& a b .Ve .SS "hcpy" .IX Subsection "hcpy" Switch on/off automatic header copying, with \s-1PDL\s0 pass-through .PP .Vb 2 \& $x = rfits(\*(Aqfoo.fits\*(Aq)\->hcpy(0); \& $x = rfits(\*(Aqfoo.fits\*(Aq)\->hcpy(1); .Ve .PP \&\f(CW\*(C`hcpy\*(C'\fR sets or clears the hdrcpy flag of a \s-1PDL,\s0 and returns the \s-1PDL\s0 itself. That makes it convenient for inline use in expressions. .SS "set_autopthread_targ" .IX Subsection "set_autopthread_targ" Set the target number of processor threads (pthreads) for multi-threaded processing. .PP .Vb 1 \& set_autopthread_targ($num_pthreads); .Ve .PP \&\f(CW$num_pthreads\fR is the target number of pthreads the auto-pthread process will try to achieve. .PP See PDL::ParallelCPU for an overview of the auto-pthread process. .PP .Vb 4 \& # Example turning on auto\-pthreading for a target of 2 pthreads and for functions involving \& # PDLs with greater than 1M elements \& set_autopthread_targ(2); \& set_autopthread_size(1); \& \& # Execute a pdl function, processing will split into two pthreads as long as \& # one of the pdl\-threaded dimensions is divisible by 2. \& $x = minimum($y); \& \& # Get the actual number of pthreads that were run. \& $actual_pthread = get_autopthread_actual(); .Ve .SS "get_autopthread_targ" .IX Subsection "get_autopthread_targ" Get the current target number of processor threads (pthreads) for multi-threaded processing. .PP .Vb 1 \& $num_pthreads = get_autopthread_targ(); .Ve .PP \&\f(CW$num_pthreads\fR is the target number of pthreads the auto-pthread process will try to achieve. .PP See PDL::ParallelCPU for an overview of the auto-pthread process. .SS "get_autopthread_actual" .IX Subsection "get_autopthread_actual" Get the actual number of pthreads executed for the last pdl processing function. .PP .Vb 1 \& $autopthread_actual = get_autopthread_actual(); .Ve .PP \&\f(CW$autopthread_actual\fR is the actual number of pthreads executed for the last pdl processing function. .PP See PDL::ParallelCPU for an overview of the auto-pthread process. .SS "set_autopthread_size" .IX Subsection "set_autopthread_size" Set the minimum size (in M\-elements or 2^20 elements) of the largest \s-1PDL\s0 involved in a function where auto-pthreading will be performed. For small PDLs, it probably isn't worth starting multiple pthreads, so this function is used to define a minimum threshold where auto-pthreading won't be attempted. .PP .Vb 1 \& set_autopthread_size($size); .Ve .PP \&\f(CW$size\fR is the mimumum size, in M\-elements or 2^20 elements (approx 1e6 elements) for the largest \s-1PDL\s0 involved in a function. .PP See PDL::ParallelCPU for an overview of the auto-pthread process. .PP .Vb 4 \& # Example turning on auto\-pthreading for a target of 2 pthreads and for functions involving \& # PDLs with greater than 1M elements \& set_autopthread_targ(2); \& set_autopthread_size(1); \& \& # Execute a pdl function, processing will split into two pthreads as long as \& # one of the pdl\-threaded dimensions is divisible by 2. \& $x = minimum($y); \& \& # Get the actual number of pthreads that were run. \& $actual_pthread = get_autopthread_actual(); .Ve .SS "get_autopthread_size" .IX Subsection "get_autopthread_size" Get the current autopthread_size setting. .PP .Vb 1 \& $autopthread_size = get_autopthread_size(); .Ve .PP \&\f(CW$autopthread_size\fR is the mimumum size limit for auto_pthreading to occur, in M\-elements or 2^20 elements (approx 1e6 elements) for the largest \s-1PDL\s0 involved in a function .PP See PDL::ParallelCPU for an overview of the auto-pthread process. .SH "AUTHOR" .IX Header "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. Modified, Craig DeForest (deforest@boulder.swri.edu) 2002. All rights reserved. There is no warranty. You are allowed to redistribute this software / documentation under certain conditions. For details, see the file \s-1COPYING\s0 in the \s-1PDL\s0 distribution. If this file is separated from the \s-1PDL\s0 distribution, the copyright notice should be included in the file.