Data-NDArray-Shared

 view release on metacpan or  search on metacpan

t/02-oracle.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use List::Util qw(sum0 min max);
use Data::NDArray::Shared;

# Deterministic checks. No RNG, no sleep: build a 3D f64 array, populate every
# element from a fixed arithmetic formula, and verify get() / reductions /
# reshape / element-wise ops against a pure-Perl reference computed over the
# same formula.

my ($D0, $D1, $D2) = (5, 6, 7);
my $N = $D0 * $D1 * $D2;     # 210
is $N, 210, 'test array has 210 elements';

my $a = Data::NDArray::Shared->new(undef, "f64", $D0, $D1, $D2);
is $a->size, $N, '3D array size == 210';
is $a->itemsize, 8, '3D array itemsize == 8';
is_deeply [ $a->strides ], [ $D1 * $D2, $D2, 1 ], 'row-major 3D strides';

# value formula: [i][j][k] = i*100 + j*10 + k
my @ref;     # flat row-major reference
for my $i (0 .. $D0 - 1) {
    for my $j (0 .. $D1 - 1) {
        for my $k (0 .. $D2 - 1) {
            my $v = $i * 100 + $j * 10 + $k;
            $a->set($i, $j, $k, $v);
            push @ref, $v;     # row-major append matches flat order
        }
    }
}

t/02-oracle.t  view on Meta::CPAN

{
    my $bad = 0;
    for my $i (0 .. $D0 - 1) {
        for my $j (0 .. $D1 - 1) {
            for my $k (0 .. $D2 - 1) {
                my $want = $i * 100 + $j * 10 + $k;
                $bad++ if $a->get($i, $j, $k) != $want;
            }
        }
    }
    is $bad, 0, 'get(i,j,k) matches the formula for all 210 elements';
}

# get_flat matches the row-major reference sequence
{
    my $bad = 0;
    $bad++ for grep { $a->get_flat($_) != $ref[$_] } 0 .. $N - 1;
    is $bad, 0, 'get_flat matches the row-major reference sequence';
}

# (b) reductions match the pure-Perl computation



( run in 0.846 second using v1.01-cache-2.11-cpan-9581c071862 )