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
for brevity, but I recommend $N = 10; for your tests.our $N = 6;
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:
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.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 )
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |