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