sequence#

sequence(@dims) – create an ndarray filled with 0, 1, 2, ..., N-1.

The returned PDL has shape @dims and, stored in row-major order, contains the arithmetic progression 0 .. prod(@dims)-1. Element type defaults to double; an optional leading PDL::Type blessed argument requests another type (e.g. long, byte).

Also supported as an instance method: $pdl->sequence returns a new PDL with the same dims and element type as $pdl, filled with the progression 0..nelem-1.

Signature#

$w = sequence(@dims);              # double, shape @dims
$w = sequence($type, @dims);       # typed form
$w = sequence($template_pdl);      # shape and type borrowed
$w = $template_pdl->sequence;      # instance-method form

Broadcasting#

Not applicable: sequence is a constructor. The fill pattern always depends solely on the flat element index.

Bad values#

The result is freshly allocated with BADFLAG clear; it contains no bad cells. If a template PDL is passed, only its dimensions and element type are inherited – the badflag is not copied.

Examples#

use PDL;
print sequence(5);             # [0 1 2 3 4]
print sequence(3, 2);          # [[0 1 2] [3 4 5]]

## Typed form

my $b = sequence(byte, 4);     # 1-byte elements, values [0 1 2 3]
print $b->type;                # "byte"

## Instance-method form: reshape/retype from existing PDL

my $tmpl = zeroes(long, 2, 3);
my $seq  = $tmpl->sequence;    # same dims (2,3), type long, values 0..5

## Empty-dim edge case

print sequence(0)->nelem;      # 0 (empty PDL, but valid)

Errors#

Croaks with "PDL::sequence: allocation failed" if the backing pdl_from_f64 allocation returns null (e.g. out-of-memory, or an overflowing dim product).

Performance notes#

The progression is built in a Vec<f64> before being handed to pdl_from_f64. For very large PDLs this temporarily doubles the memory footprint versus an in-place fill; an in-place axisvals2($pdl->flat->inplace, 0, ...) path (as in the pure-Perl upstream) is a future optimisation.

Upstream#

Mirrors PDL::sequence in lib/PDL/Basic.pm, which is itself pure Perl wrapping _construct + axisvals2.