Here's a fun way to test your matrix multiplication program.

Choose a natural number $N, the size of your matrices. Here, I'll use

our $N = 6;
for brevity, but I recommend $N = 10; for your tests.

Create the triangular matrices C and I with the following recursive constructions.

our $C; for my $k (0 .. $N - 1) { for my $l (0 .. $N - 1) { my $e; if (0 == $k) { if (0 == $l) { $e = 1; } else { $e = 0; } } else { $e = 0; if (0 < $l) { $e += $$C[$k-1][$l-1]; } if ($l < $N - 1) { $e += $$C[$k-1][$l+1]; } } $$C[$k][$l] = $e; } } our $U; for my $k (0 .. $N - 1) { for my $l (0 .. $N - 1) { my $e; if (0 == $k) { if (0 == $l) { $e = 1; } else { $e = 0; } } elsif (1 == $k) { if (1 == $l) { $e = 2; } else { $e = 0; } } else { $e = -$$U[$k-2][$l]; if (0 < $l) { $e += 2 * $$U[$k-1][$l-1]; } } $$U[$k][$l] = $e; } }

This will look something like this.

=begin output C = ( 1 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 2 0 1 0 0 2 0 3 0 1 0 0 5 0 4 0 1 ) U = ( 1 0 0 0 0 0 0 2 0 0 0 0 -1 0 4 0 0 0 0 -4 0 8 0 0 1 0 -12 0 16 0 0 6 0 -32 0 32 ) =end output

Now compute the matrix product C·CT, where CT is the transpose of C. For this, you'll of course need to write a matrix transposition function, but that should be easier than the matrix multiplication function.

You should get a matrix like this:

C*C^T = ( 1 0 1 0 2 0 0 1 0 2 0 5 1 0 2 0 5 0 0 2 0 5 0 14 2 0 5 0 14 0 0 5 0 14 0 42 )
Note that any antidiagonal of this matrix contains the same number in all positions. This should be true for larger $N as well. If it's not, then either you have made a mistake in one of your functions, or I have made a mistake in this description.

Further, compute the matrix C·U. You should get a diagonal matrix.


In reply to Re: matrix multiplication by ambrus
in thread matrix multiplication by etheral

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.