To test a matrix algorithm, I would like to be able to calculate with variables instead of numbers only but not change the algorithm itself.
The direction in which I assume there is a solution (but probably there are others equally welcome) would be to use polymorphism within PDL or replace the PDL library with a symbolic library with the same API as PDL has.
To illustrate my point, the following is a simple algorithm implemented using PDL:
use utf8;
use strict;
use warnings;
use PDL;
sub algorithm ($$) {
my $alpha = shift;
my $beta = shift;
my $A = pdl(
[ cos $alpha, -sin $alpha ],
[ sin $alpha, cos $alpha ],
);
my $B = pdl(
[ cos $beta, -sin $beta ],
[ sin $beta, cos $beta ],
);
print $A x $B;
}
Now to test the code, instead of many calls like
algorithm 0.1, 0.1;
algorithm 0.2, 0.1;
algorithm 0.1, 0.2;
…
use a single call similar to
algorithm 'α', 'β';
or – equally acceptable – similar to
algorithm pdl('α'), pdl('β');
which would eventually output a matrix of terms in the named variables α
and β
(of course, Latin variable names a
and b
should be equally possible).
Ideally, nothing in the algorithm would have to change for this; in the end, I want to test the algorithm as it is, not a different algorithm. Adding a use PDL::Symbolic qw( pdl );
or use SPDL qw( pdl );
statement in the header as extension or replacement of use PDL;
to me seems an acceptably small exception to the rule of not changing anything.
The only solution which comes to my mind is basically to re-implement the PDL API, at least the functions used in my algorithm and probably with less consideration for efficiency, but using a symbolic object instead of each piddle cell, and probably expanded with an indexing-naming feature for the cells within larger piddles for improved usability.
Is there a better way than programming this library myself from scratch?