1#!perl 2 3use strict; 4use warnings; 5 6use Math::Matrix; 7use Test::More tests => 20; 8 9note('2-by-3 x 3-by-2 -> 6-by-6'); 10 11{ 12 my $x = Math::Matrix -> new([[4, 5, 6], 13 [7, 8, 9]]); 14 my $y = Math::Matrix -> new([[3, 2]]); 15 my $z = $x -> repmat($y); 16 17 is(ref($z), 'Math::Matrix', '$z is a Math::Matrix'); 18 is_deeply([ @$z ], [[4, 5, 6, 4, 5, 6], 19 [7, 8, 9, 7, 8, 9], 20 [4, 5, 6, 4, 5, 6], 21 [7, 8, 9, 7, 8, 9], 22 [4, 5, 6, 4, 5, 6], 23 [7, 8, 9, 7, 8, 9]], 24 '$z has the right values'); 25 26 # Verify that modifying $z does not modify $x or $y. 27 28 my ($nrowy, $ncoly) = $z -> size(); 29 for my $i (0 .. $nrowy - 1) { 30 for my $j (0 .. $ncoly - 1) { 31 $z -> [$i][$j] += 100; 32 } 33 } 34 35 is_deeply([ @$x ], [[4, 5, 6], 36 [7, 8, 9]], '$x is unmodified'); 37 is_deeply([ @$y ], [[3, 2]], '$y is unmodified'); 38} 39 40note('empty x 3-by-2 -> empty'); 41 42{ 43 my $x = Math::Matrix -> new([]); 44 my $y = Math::Matrix -> new([[3, 2]]); 45 my $z = $x -> repmat($y); 46 47 is(ref($z), 'Math::Matrix', '$z is a Math::Matrix'); 48 is_deeply([ @$z ], [], '$z has the right values'); 49 50 is_deeply([ @$x ], [], '$x is unmodified'); 51 is_deeply([ @$y ], [[3, 2]], '$y is unmodified'); 52} 53 54note('2-by-3 x 0-by-2 -> empty'); 55 56{ 57 my $x = Math::Matrix -> new([[4, 5, 6], 58 [7, 8, 9]]); 59 my $y = Math::Matrix -> new([[0, 2]]); 60 my $z = $x -> repmat($y); 61 62 is(ref($z), 'Math::Matrix', '$z is a Math::Matrix'); 63 is_deeply([ @$z ], [], '$z has the right values'); 64 65 is_deeply([ @$x ], [[4, 5, 6], 66 [7, 8, 9]], '$x is unmodified'); 67 is_deeply([ @$y ], [[0, 2]], '$y is unmodified'); 68} 69 70note('2-by-3 x 2-by-0 -> empty'); 71 72{ 73 my $x = Math::Matrix -> new([[4, 5, 6], 74 [7, 8, 9]]); 75 my $y = Math::Matrix -> new([[2, 0]]); 76 my $z = $x -> repmat($y); 77 78 is(ref($z), 'Math::Matrix', '$z is a Math::Matrix'); 79 is_deeply([ @$z ], [], '$z has the right values'); 80 81 is_deeply([ @$x ], [[4, 5, 6], 82 [7, 8, 9]], '$x is unmodified'); 83 is_deeply([ @$y ], [[2, 0]], '$y is unmodified'); 84} 85 86note('2-by-3 x 0-by-0 -> empty'); 87 88{ 89 my $x = Math::Matrix -> new([[4, 5, 6], 90 [7, 8, 9]]); 91 my $y = Math::Matrix -> new([[0, 0]]); 92 my $z = $x -> repmat($y); 93 94 is(ref($z), 'Math::Matrix', '$z is a Math::Matrix'); 95 is_deeply([ @$z ], [], '$z has the right values'); 96 97 is_deeply([ @$x ], [[4, 5, 6], 98 [7, 8, 9]], '$x is unmodified'); 99 is_deeply([ @$y ], [[0, 0]], '$y is unmodified'); 100} 101