I spent some time porting a Fortran application to C++. That was quite error-prone, so I had to compare intermediate values in the process, which were all large matrices of complex numbers. Comparing them is boring, takes long and is error-prone in itself, so I wrote a script which compares those matrices for me.

It's really a hack, but it works fine for me. Now I also use it to test my C++ application when I change internals that shouldn't affect the output.

#!/usr/bin/perl use strict; use warnings; use Math::Complex; use Data::Dumper; sub read_file { # we ignore that the structure is a matrix, and just read it as if + where a # list my $fn = shift; open my $file, '<', $fn or die "Can't open file '$fn' for reading: + $!"; my @list; my $contents = do { local $/; <$file> }; close $file or warn $!; my $number_re = qr{ \( \s* ([^\s(),]*) # real part \s*,\s* ([^\s(),]*) # imaginary part ,? \s* \) }x; while ($contents =~ m/$number_re/g){ push @list, $1 + $2 * i; } return @list; } my @a = read_file($ARGV[0] || 'grinv-cpp'); my @b = read_file($ARGV[1] || 'grinv-nano'); print scalar(@a), "\t", scalar(@b), "\n"; my $n = @a; for (0..$n - 1) { my $diff = abs($a[$_] - $b[$_]); if ($diff > 1e-4) { printf "Index %d (%d, %d): got %s, expected %s\n", $_, $_ % sqrt($n), int($_ / sqrt($n)), $a[$_], $b[$_]; } } # vim: ft=perl expandtab ts=4 sw=4

Actually I wrote quite many small scripts for similar tasks: converting matrices from one format to another one so it can be used as input by a different library, sanity check (check symmetries in my output matrices) etc, this is just the most recent example.

All of them saved much time, either my own or that of co-workers.


In reply to Re: How has Perl saved you time at your job this week|month|year|decade? by moritz
in thread How has Perl saved you time at your job this week|month|year|decade? by missingthepoint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.