Yes, PDL does distinguish between scalars (0 dimensions) and 1-element, 
1-dimensional PDLs.  So do pretty much all languages that keep track of 
multidimensional structured arrays.  PDL goes to great lengths to do "what you 
meant" in most cases -- e.g. if you treat an <n>-dimensional PDL as 
<n+1>-dimensional, it is often assumed to have the extra dimension, of size 
one.  It is possible to go a long time without ever having to deal with that 
distinction directly.

The main place I've had to deal with that lately is that there's no such thing 
as a 0-dimensional empty PDL.  The way you make a structured Empty is to 
generate a PDL with size 0 in one of its dimensions - but a scalar PDL doesn't 
have any dimensions to set to 0. (That makes sense mathematically too -- 
scalars aren't structured collections of things, they're atoms - so you can't 
have zero atoms in a scalar) 




On Sep 26, 2012, at 10:09 AM, David Mertens wrote:

> Hello everybody -
> 
> I ran into this most interesting issue today as I was working on
> degenerate histogram plotting, which is plotting a histogram with a
> single column. Fire up the pdl repl and behold the following:
> 
> pdl> $first = pdl(5)
> pdl> p $first
> 5
> pdl> p $first->nelem
> 1
> pdl> p join(', ', $first->dims)
> 
> pdl> p $first->ndims
> 0
> 
> pdl> $second = zeroes(1) + 5
> pdl> p $second
> [5]
> pdl> p $second->nelem
> 1
> pdl> p join(', ', $second->dims)
> 1
> pdl> p $second->ndims
> 1
> 
> Notice that the first piddle has NO DIMENSIONS, even though it is a
> bona fide piddle and it has nelem == 1. How is it possible that nelem
> != product(@dims)?
> 
> I can handle this in my library with code like "my @dims = $xs->dims
> or (1);" However, it seems odd to me that PDL even supports the notion
> of "scalar" piddles that are distinct from one-dimensional piddles
> with single elements. Can anybody shed light on this conundrum? Is
> there any method call that is otherwise unknown to me that returns (1)
> for a "scalar" piddle's dims? Should I update the docs for "dims" to
> mention this potential pitfall?
> 
> Thoughts?
> David
> 
> -- 
> "Debugging is twice as hard as writing the code in the first place.
>  Therefore, if you write the code as cleverly as possible, you are,
>  by definition, not smart enough to debug it." -- Brian Kernighan
> 
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
> 


_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to