1#!perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8# Ensure a recent version of Math::Complex. Math Complex didn't support any way
9# of cloning/copying Math::Complex objects before version 1.57.
10
11my $min_math_complex_ver = 1.57;
12eval "use Math::Complex $min_math_complex_ver";
13plan skip_all => "Math::Complex $min_math_complex_ver required" if $@;
14
15use lib 't/lib';
16use Math::Matrix::Complex;
17
18plan tests => 24;
19
20note('from 2-by-3 to 1-by-6');
21
22{
23    my $x = Math::Matrix::Complex -> new([[1, 3, 5],
24                                 [2, 4, 6]]);
25    my $y = $x -> reshape(1, 6);
26
27    is(ref($y), 'Math::Matrix::Complex', '$y is a Math::Matrix::Complex');
28    is_deeply([ @$y ], [[1, 2, 3, 4, 5, 6]],
29              '$y has the right values');
30
31    # Verify that modifying $y does not modify $x.
32
33    my ($nrowy, $ncoly) = $y -> size();
34    for my $i (0 .. $nrowy - 1) {
35        for my $j (0 .. $ncoly - 1) {
36            $y -> [$i][$j] += 100;
37        }
38    }
39
40    is_deeply([ @$x ], [[1, 3, 5],
41                        [2, 4, 6]], '$x is unmodified');
42}
43
44note('from 2-by-3 to 3-by-2');
45
46{
47    my $x = Math::Matrix::Complex -> new([[1, 3, 5],
48                                 [2, 4, 6]]);
49    my $y = $x -> reshape(3, 2);
50
51    is(ref($y), 'Math::Matrix::Complex', '$y is a Math::Matrix::Complex');
52    is_deeply([ @$y ], [[1, 4], [2, 5], [3, 6]],
53              '$y has the right values');
54
55    # Verify that modifying $y does not modify $x.
56
57    my ($nrowy, $ncoly) = $y -> size();
58    for my $i (0 .. $nrowy - 1) {
59        for my $j (0 .. $ncoly - 1) {
60            $y -> [$i][$j] += 100;
61        }
62    }
63
64    is_deeply([ @$x ], [[1, 3, 5],
65                        [2, 4, 6]], '$x is unmodified');
66}
67
68note('from 2-by-3 to 6-by-1');
69
70{
71    my $x = Math::Matrix::Complex -> new([[1, 3, 5],
72                                 [2, 4, 6]]);
73    my $y = $x -> reshape(6, 1);
74
75    is(ref($y), 'Math::Matrix::Complex', '$y is a Math::Matrix::Complex');
76    is_deeply([ @$y ], [[1], [2], [3], [4], [5], [6]],
77              '$y has the right values');
78
79    # Verify that modifying $y does not modify $x.
80
81    my ($nrowy, $ncoly) = $y -> size();
82    for my $i (0 .. $nrowy - 1) {
83        for my $j (0 .. $ncoly - 1) {
84            $y -> [$i][$j] += 100;
85        }
86    }
87
88    is_deeply([ @$x ], [[1, 3, 5],
89                        [2, 4, 6]], '$x is unmodified');
90}
91
92note('from 1-by-6 to 2-by-3');
93
94{
95    my $x = Math::Matrix::Complex -> new([[1, 2, 3, 4, 5, 6]]);
96    my $y = $x -> reshape(2, 3);
97
98    is(ref($y), 'Math::Matrix::Complex', '$y is a Math::Matrix::Complex');
99    is_deeply([ @$y ], [[1, 3, 5],
100                        [2, 4, 6]],
101              '$y has the right values');
102
103    # Verify that modifying $y does not modify $x.
104
105    my ($nrowy, $ncoly) = $y -> size();
106    for my $i (0 .. $nrowy - 1) {
107        for my $j (0 .. $ncoly - 1) {
108            $y -> [$i][$j] += 100;
109        }
110    }
111
112    is_deeply([ @$x ], [[1, 2, 3, 4, 5, 6]], '$x is unmodified');
113}
114
115note('from 1-by-6 to 6-by-1');
116
117{
118    my $x = Math::Matrix::Complex -> new([[1, 2, 3, 4, 5, 6]]);
119    my $y = $x -> reshape(6, 1);
120
121    is(ref($y), 'Math::Matrix::Complex', '$y is a Math::Matrix::Complex');
122    is_deeply([ @$y ], [[1], [2], [3], [4], [5], [6]],
123              '$y has the right values');
124
125    # Verify that modifying $y does not modify $x.
126
127    my ($nrowy, $ncoly) = $y -> size();
128    for my $i (0 .. $nrowy - 1) {
129        for my $j (0 .. $ncoly - 1) {
130            $y -> [$i][$j] += 100;
131        }
132    }
133
134    is_deeply([ @$x ], [[1, 2, 3, 4, 5, 6]], '$x is unmodified');
135}
136
137note('from 6-by-1 to 2-by-3');
138
139{
140    my $x = Math::Matrix::Complex -> new([[1], [2], [3], [4], [5], [6]]);
141    my $y = $x -> reshape(2, 3);
142
143    is(ref($y), 'Math::Matrix::Complex', '$y is a Math::Matrix::Complex');
144    is_deeply([ @$y ], [[1, 3, 5],
145                        [2, 4, 6]],
146              '$y has the right values');
147
148    # Verify that modifying $y 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    is_deeply([ @$x ], [[1], [2], [3], [4], [5], [6]], '$x is unmodified');
158}
159
160note('from 6-by-1 to 1-by-6');
161
162{
163    my $x = Math::Matrix::Complex -> new([[1], [2], [3], [4], [5], [6]]);
164    my $y = $x -> reshape(1, 6);
165
166    is(ref($y), 'Math::Matrix::Complex', '$y is a Math::Matrix::Complex');
167    is_deeply([ @$y ], [[1, 2, 3, 4, 5, 6]],
168              '$y has the right values');
169
170    # Verify that modifying $y does not modify $x.
171
172    my ($nrowy, $ncoly) = $y -> size();
173    for my $i (0 .. $nrowy - 1) {
174        for my $j (0 .. $ncoly - 1) {
175            $y -> [$i][$j] += 100;
176        }
177    }
178
179    is_deeply([ @$x ], [[1], [2], [3], [4], [5], [6]], '$x is unmodified');
180}
181
182note('from 0-by-0 to 0-by-0');
183
184{
185    my $x = Math::Matrix::Complex -> new([]);
186    my $y = $x -> reshape(0, 0);
187
188    is(ref($y), 'Math::Matrix::Complex', '$y is a Math::Matrix::Complex');
189    is_deeply([ @$y ], [], '$y has the right values');
190    is_deeply([ @$x ], [], '$x is unmodified');
191}
192