3

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?

  • Interesting question. Maybe you can find some inspiration from [Math::Symbolic](https://metacpan.org/pod/Math::Symbolic). Or you could try to wrap (with Perl XS) the [symengine](https://github.com/symengine/symengine) C++ library used by [SymPy](https://www.sympy.org/en/index.html). – Håkon Hægland May 06 '20 at 12:56
  • I have started on implementing Perl bindings for the symengine C++ library. You can test it [here](https://github.com/hakonhagland/perl-math-symengine) if you like. Let me know what more you would like to have implemented in the module. – Håkon Hægland May 10 '20 at 12:15

1 Answers1

2

Very short answer: No.

Slightly more helpful answer: PDL's whole purpose is to operate on binary data (as C or Fortran would), in the computer's memory (using the "shared memory" model), at great speed. You are describing an entirely different piece of software.

Ed.
  • 1,992
  • 1
  • 13
  • 30