import
Custom importer that recognizes configuration defaults specified at use time, as in
use Test::PDL -require_equal_types => 0;
is_pdl
Run a test comparing an ndarray to an expected ndarray, and fail with detailed diagnostics if they don't
compare equal.
is_pdl( $got, $expected );
is_pdl( $got, $expected, $test_name );
is_pdl( $got, $expected, { test_name => $test_name } );
is_pdl( $got, $expected, { atol => $absolute_tolerance, ... } );
Yields ok if the first two arguments are ndarrays that compare equal, not ok if the ndarrays are
different, or if at least one is not an ndarray. Prints a diagnostic when the comparison fails, with the
reason and a brief printout of both arguments. See the documentation of eq_pdl() for the comparison
criteria. $test_name is optional.
Named after is() from Test::More.
eq_pdl
Return true if two ndarrays compare equal, false otherwise. In list context, additionally returns a
diagnostic string.
my $equal = eq_pdl( $got, $expected );
my $equal = eq_pdl( $got, $expected, { atol => $absolute_tolerance, ... } );
my( $equal, $diag ) = eq_pdl( $got, $expected );
my( $equal, $diag ) = eq_pdl( $got, $expected, { atol => $absolute_tolerance, ... } );
eq_pdl() contains just the comparison part of is_pdl(), without the infrastructure required to write
tests with Test::More. It could be used as part of a larger test in which the equality of two ndarrays
must be verified. By itself, eq_pdl() does not generate any output, so it should be safe to use outside
test suites.
In list context, eq_pdl() returns a list with three elements, the first one being a boolean whether the
ndarrays compared equal, the second being a diagnostic string explaining why the comparison failed (or
the empty string, if it didn't fail). The third is either the mask of not-equal if the values didn't
match, or "undef". This is useful in combination with Test::Deep, but might also be useful on its own.
eq_pd() does not need Test::Builder, so you can use it as part of something else, without side effects
(like generating output).
The criteria for equality are the following:
• Both arguments must be ndarrays for the comparison to succeed. Currently, there is no implicit
conversion from scalar to ndarray.
• The type of both ndarrays must be equal if (and only if) require_equal_types is true.
• The number of dimensions must be equal. That is, a two-dimensional ndarray only compares equal with
another two-dimensional ndarray.
• The extent of the dimensions are compared one by one and must match. That is, a ndarray with
dimensions (5,4) cannot compare equal with an ndarray of dimensions (5,3). Note that degenerate
dimensions are not treated specially, and thus a ndarray with dimensions (5,4,1) is considered
different from an ndarray with dimensions (5,4).
• For ndarrays that conform in type and shape, the bad value pattern is examined. If the two ndarrays
have bad values in different positions, the ndarrays are considered different. Note that two ndarrays
may compare equal even though their bad flag is different, if there are no bad values.
• And last but not least, the values themselves are examined one by one. As of 0.21, both integer and
floating-point types are compared approximately. The approximate comparison is implemented using a
combination of relative and absolute tolerances, which can be set by supplying an argument to "use
Test::PDL", or by supplying an optional hash to this function. By default, the absolute and relative
tolerances are both equal to 1e-6. The user can specify a pure relative tolerance by specifying "atol
=> 0", and a pure absolute tolerance by specifying "rtol => 0". If both tolerances are specified,
values compare equal if either their difference is lower than or equal to the absolute tolerance or
their relative difference (with respect to the expected value) is lower than or equal to the relative
tolerance. For expected values equal to zero, relative differences (with respect to the expected
value) make no sense, and the use of combined absolute and relative tolerances is recommended.
test_pdl
Special comparison to be used in conjunction with Test::Deep to test ndarrays inside data structures.
my $expected = { ..., some_field => test_pdl( 1,2,-7 ), ... };
my $expected = [ ..., test_short( 1,2,-7 ), ... ];
Suppose you want to compare data structures that happen to contain ndarrays. You use is_deeply() (from
Test::More) or cmp_deeply() (from Test::Deep) to compare the structures element by element.
Unfortunately, you cannot just write
my $got = my_sub( ... );
my $expected = {
...,
some_field => pdl( ... ),
...
};
is_deeply $got, $expected;
Neither does cmp_deeply() work in the same situation. is_deeply() tries to compare the ndarrays using the
(overloaded) "==" comparison operator, which doesn't work. It simply dies with an error message saying
that multidimensional ndarrays cannot be compared, whereas cmp_deeply() performs only a shallow
comparison of the references.
What you need is a special comparison, which is provided by this function, to be used with cmp_deeply().
You need to rewrite $expected as follows
my $expected = {
...,
some_field => test_pdl( ... ),
...
};
cmp_deeply $got, $expected;
Note that you need to write test_pdl() instead of pdl(). You could achieve the same thing with
my $expected = {
...,
some_field => code( sub { eq_pdl( shift, pdl( ... ) ) } ),
...
};
but the diagnostics provided by test_pdl() are better, and it's easier to use. test_pdl() accepts the
same arguments as the PDL constructor pdl() does. If you need to compare an ndarray with a type different
from the default type, use one of the provided test_byte(), test_short(), test_long(), etc.:
my $expected = { data => test_short( -4,-9,13 ) };
If you need to manipulate the expected value, you should keep in mind that the return value of test_pdl()
and the like are not ndarrays. Therefore, in-place modification of the expected value won't work:
my $expected = { data => test_short( -99,-9,13 )->inplace->setvaltobad( -99 ) }; # won't work!
You should rather do
my $expected = { data => test_pdl( short(-99,-9,13)->inplace->setvaltobad(-99) ) };
test_pdl() will correctly set the type of the expected value to short in the above example.