1#!perl
2
3use strict;
4use warnings;
5
6use Math::Matrix;
7use Test::More tests => 36;
8
9################################################################
10
11note('prod() on a 4-by-5 matrix');
12
13{
14    my $x = Math::Matrix -> new([[ 3, -1,  5,  2,  8 ],
15                                 [ 4,  0,  2, -3,  1 ],
16                                 [ 2,  6, -5,  1, -2 ],
17                                 [ 0, -3,  4,  2,  3 ]]);
18    my $y = $x -> prod();
19
20    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
21    is_deeply([ @$y ], [[    0,    0, -200,  -12,  -48 ]], '$y has the right values');
22
23    # Verify that modifying $y does not modify $x.
24
25    my ($nrowy, $ncoly) = $y -> size();
26    for my $i (0 .. $nrowy - 1) {
27        for my $j (0 .. $ncoly - 1) {
28            $y -> [$i][$j] += 100;
29        }
30    }
31
32    is_deeply([ @$x ], [[ 3, -1,  5,  2,  8 ],
33                        [ 4,  0,  2, -3,  1 ],
34                        [ 2,  6, -5,  1, -2 ],
35                        [ 0, -3,  4,  2,  3 ]], '$x is unmodified');
36}
37
38note('prod(1) on a 4-by-5 matrix');
39
40{
41    my $x = Math::Matrix -> new([[ 3, -1,  5,  2,  8 ],
42                                 [ 4,  0,  2, -3,  1 ],
43                                 [ 2,  6, -5,  1, -2 ],
44                                 [ 0, -3,  4,  2,  3 ]]);
45    my $y = $x -> prod(1);
46
47    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
48    is_deeply([ @$y ], [[    0,    0, -200,  -12,  -48 ]], '$y has the right values');
49
50    # Verify that modifying $y does not modify $x.
51
52    my ($nrowy, $ncoly) = $y -> size();
53    for my $i (0 .. $nrowy - 1) {
54        for my $j (0 .. $ncoly - 1) {
55            $y -> [$i][$j] += 100;
56        }
57    }
58
59    is_deeply([ @$x ], [[ 3, -1,  5,  2,  8 ],
60                        [ 4,  0,  2, -3,  1 ],
61                        [ 2,  6, -5,  1, -2 ],
62                        [ 0, -3,  4,  2,  3 ]], '$x is unmodified');
63}
64
65note('prod(2) on a 4-by-5 matrix');
66
67{
68    my $x = Math::Matrix -> new([[ 3, -1,  5,  2,  8 ],
69                                 [ 4,  0,  2, -3,  1 ],
70                                 [ 2,  6, -5,  1, -2 ],
71                                 [ 0, -3,  4,  2,  3 ]]);
72    my $y = $x -> prod(2);
73
74    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
75    is_deeply([ @$y ], [[ -240 ],
76                        [    0 ],
77                        [  120 ],
78                        [    0 ]], '$y has the right values');
79
80    # Verify that modifying $y does not modify $x.
81
82    my ($nrowy, $ncoly) = $y -> size();
83    for my $i (0 .. $nrowy - 1) {
84        for my $j (0 .. $ncoly - 1) {
85            $y -> [$i][$j] += 100;
86        }
87    }
88
89    is_deeply([ @$x ], [[ 3, -1,  5,  2,  8 ],
90                        [ 4,  0,  2, -3,  1 ],
91                        [ 2,  6, -5,  1, -2 ],
92                        [ 0, -3,  4,  2,  3 ]], '$x is unmodified');
93}
94
95################################################################
96
97note('prod() on a 4-by-1 matrix');
98
99{
100    my $x = Math::Matrix -> new([[ 3 ],
101                                 [ 4 ],
102                                 [ 2 ],
103                                 [ 0 ]]);
104    my $y = $x -> prod();
105
106    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
107    is_deeply([ @$y ], [[ 0 ]], '$y has the right values');
108
109    # Verify that modifying $y does not modify $x.
110
111    my ($nrowy, $ncoly) = $y -> size();
112    for my $i (0 .. $nrowy - 1) {
113        for my $j (0 .. $ncoly - 1) {
114            $y -> [$i][$j] += 100;
115        }
116    }
117
118    is_deeply([ @$x ], [[ 3 ],
119                        [ 4 ],
120                        [ 2 ],
121                        [ 0 ]], '$x is unmodified');
122}
123
124note('prod(1) on a 4-by-1 matrix');
125
126{
127    my $x = Math::Matrix -> new([[ 3 ],
128                                 [ 4 ],
129                                 [ 2 ],
130                                 [ 0 ]]);
131    my $y = $x -> prod(1);
132
133    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
134    is_deeply([ @$y ], [[ 0 ]], '$y has the right values');
135
136    # Verify that modifying $y does not modify $x.
137
138    my ($nrowy, $ncoly) = $y -> size();
139    for my $i (0 .. $nrowy - 1) {
140        for my $j (0 .. $ncoly - 1) {
141            $y -> [$i][$j] += 100;
142        }
143    }
144
145    is_deeply([ @$x ], [[ 3 ],
146                        [ 4 ],
147                        [ 2 ],
148                        [ 0 ]], '$x is unmodified');
149}
150
151note('prod(2) on a 4-by-1 matrix');
152
153{
154    my $x = Math::Matrix -> new([[ 3 ],
155                                 [ 4 ],
156                                 [ 2 ],
157                                 [ 0 ]]);
158    my $y = $x -> prod(2);
159
160    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
161    is_deeply([ @$y ], [[ 3 ],
162                        [ 4 ],
163                        [ 2 ],
164                        [ 0 ]], '$y has the right values');
165
166    # Verify that modifying $y does not modify $x.
167
168    my ($nrowy, $ncoly) = $y -> size();
169    for my $i (0 .. $nrowy - 1) {
170        for my $j (0 .. $ncoly - 1) {
171            $y -> [$i][$j] += 100;
172        }
173    }
174
175    is_deeply([ @$x ], [[ 3 ],
176                        [ 4 ],
177                        [ 2 ],
178                        [ 0 ]], '$x is unmodified');
179}
180
181################################################################
182
183note('prod() on a 1-by-5 matrix');
184
185{
186    my $x = Math::Matrix -> new([[ 3, -1,  5,  2,  8 ]]);
187    my $y = $x -> prod();
188
189    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
190    is_deeply([ @$y ], [[ -240 ]], '$y has the right values');
191
192    # Verify that modifying $y does not modify $x.
193
194    my ($nrowy, $ncoly) = $y -> size();
195    for my $i (0 .. $nrowy - 1) {
196        for my $j (0 .. $ncoly - 1) {
197            $y -> [$i][$j] += 100;
198        }
199    }
200
201    is_deeply([ @$x ], [[ 3, -1,  5,  2,  8 ]], '$x is unmodified');
202}
203
204note('prod(1) on a 1-by-5 matrix');
205
206{
207    my $x = Math::Matrix -> new([[ 3, -1,  5,  2,  8 ]]);
208    my $y = $x -> prod(1);
209
210    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
211    is_deeply([ @$y ], [[ 3, -1,  5,  2,  8 ]], '$y has the right values');
212
213    # Verify that modifying $y does not modify $x.
214
215    my ($nrowy, $ncoly) = $y -> size();
216    for my $i (0 .. $nrowy - 1) {
217        for my $j (0 .. $ncoly - 1) {
218            $y -> [$i][$j] += 100;
219        }
220    }
221
222    is_deeply([ @$x ], [[ 3, -1,  5,  2,  8 ]], '$x is unmodified');
223}
224
225note('prod(2) on a 1-by-5 matrix');
226
227{
228    my $x = Math::Matrix -> new([[ 3, -1,  5,  2,  8 ]]);
229    my $y = $x -> prod(2);
230
231    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
232    is_deeply([ @$y ], [[ -240 ]], '$y has the right values');
233
234    # Verify that modifying $y does not modify $x.
235
236    my ($nrowy, $ncoly) = $y -> size();
237    for my $i (0 .. $nrowy - 1) {
238        for my $j (0 .. $ncoly - 1) {
239            $y -> [$i][$j] += 100;
240        }
241    }
242
243    is_deeply([ @$x ], [[ 3, -1,  5,  2,  8 ]], '$x is unmodified');
244}
245
246################################################################
247
248note('prod() on an empty matrix');
249
250{
251    my $x = Math::Matrix -> new([]);
252    my $y = $x -> prod();
253
254    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
255    is_deeply([ @$y ], [], '$y has the right values');
256    is_deeply([ @$x ], [], '$x is unmodified');
257}
258
259note('prod(1) on an empty matrix');
260
261{
262    my $x = Math::Matrix -> new([]);
263    my $y = $x -> prod(1);
264
265    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
266    is_deeply([ @$y ], [], '$y has the right values');
267    is_deeply([ @$x ], [], '$x is unmodified');
268}
269
270note('prod(2) on an empty matrix');
271
272{
273    my $x = Math::Matrix -> new([]);
274    my $y = $x -> prod(2);
275
276    is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
277    is_deeply([ @$y ], [], '$y has the right values');
278    is_deeply([ @$x ], [], '$x is unmodified');
279}
280