-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathmatrix.t
More file actions
423 lines (354 loc) · 17.5 KB
/
matrix.t
File metadata and controls
423 lines (354 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env perl
=head1 MathObjects - Matrix
Tests creation and manipulation of Matrix math objects.
=cut
use Test2::V0 '!E', { E => 'EXISTS' };
die "PG_ROOT not found in environment.\n" unless $ENV{PG_ROOT};
do "$ENV{PG_ROOT}/t/build_PG_envir.pl";
loadMacros('MathObjects.pl');
Context('Matrix');
subtest 'Creating a degree 1 Matrix (row vector)' => sub {
ok my $M1 = Matrix(1, 2, 3), 'Create a row vector';
is $M1->class, 'Matrix', 'M1 is a Matrix';
my $M2 = Compute('[1,2,3]');
is $M2->class, 'Matrix', 'Creation using Compute results in a Matrix';
is [ $M1->value ], [ 1, 2, 3 ], 'M1 is the row matrix [1,2,3]';
is [ $M2->value ], [ 1, 2, 3 ], 'M2 is the row matrix [1,2,3]';
is $M1->degree, 1, 'M1 is a degree 1 matrix';
is $M2->degree, 1, 'M2 is a degree 1 matrix';
};
subtest 'Creating a degree 2 Matrix' => sub {
my $values = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ];
my $A = Matrix($values);
is $A->class, 'Matrix', 'Input as array ref is a Matrix';
is [ $A->value ], $values, 'The entry values is correct';
my $B = Matrix('[[1,2,3,4],[5,6,7,8], [9,10,11,12]]');
is $B->class, 'Matrix', 'Input as a string is a Matrix';
my $C = Compute('[[1,2,3,4],[5,6,7,8], [9,10,11,12]]');
is $C->class, 'Matrix', 'Input using Compute is a Matrix';
is $A->degree, 2, 'A is a degree 2 matrix';
is $C->degree, 2, 'C is a degree 2 matrix';
};
subtest 'Creating a degree 3 Matrix (tensor)' => sub {
my $values = [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ];
ok my $M3 = Matrix([ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ]), 'Creation of a tensor';
is $M3->class, 'Matrix', 'Checking the result is a Matrix';
is $M3->degree, 3, 'M3 is a degree 3 matrix';
};
subtest 'Get dimensions' => sub {
my $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
my $B = Matrix([ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]);
my $C = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
my $row = Matrix([ 1, 2, 3, 4 ]);
my @dimsA = $A->dimensions;
my @dimsB = $B->dimensions;
my @dimsC = $C->dimensions;
my @dimsRow = $row->dimensions;
is \@dimsA, [ 3, 4 ], 'The dimensions of A are correct';
is \@dimsB, [ 3, 3 ], 'The dimensions of B are correct';
is \@dimsC, [ 2, 2, 2 ], 'The dimensions of C are correct';
is \@dimsRow, [4], 'The dimensions of a row vector are correct';
};
subtest 'Use isSquare, isOne, and isRow methods' => sub {
my $A1 = Matrix([ 1, 2, 3, 4 ]);
my $B1 = Matrix([1]);
my $C1 = Matrix([2]);
ok !$A1->isSquare, 'The matrix A1 is not square';
ok $B1->isSquare, 'The matrix B1 is square';
ok $C1->isSquare, 'The matrix C1 is square';
ok !$A1->isOne, 'The matrix A1 is not an identity';
ok $B1->isOne, 'The matrix B1 is an identity';
ok !$C1->isOne, 'The matrix C1 is not an identity';
ok $A1->isRow, 'The matrix A1 is a row';
ok $B1->isRow, 'The matrix B1 is a row';
ok $C1->isRow, 'The matrix C1 is a row';
my $A2 = Matrix([ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ]);
my $B2 = Matrix([ 1, 0 ], [ 0, 1 ]);
my $C2 = Matrix([ 2, 0 ], [ 1, 2 ]);
ok !$A2->isSquare, 'The matrix A2 is not square';
ok $B2->isSquare, 'The matrix B2 is square';
ok $C2->isSquare, 'The matrix C2 is square';
ok !$A2->isOne, 'The matrix A2 is not an identity';
ok $B2->isOne, 'The matrix B2 is an identity';
ok !$C2->isOne, 'The matrix C2 is not an identity';
ok !$A2->isRow, 'The matrix A2 is not a row';
ok !$B2->isRow, 'The matrix B2 is not a row';
ok !$C2->isRow, 'The matrix C2 is not a row';
my $A3 = Matrix([ [ 1, 2, 3 ], [ 4, 5, 6 ] ], [ [ 7, 8, 9 ], [ 10, 11, 12 ] ]);
my $B3 = Matrix([ [ 1, 0 ], [ 0, 1 ] ], [ [ 1, 0 ], [ 0, 1 ] ]);
my $C3 = Matrix([ [ 2, 0 ], [ 0, 1 ] ], [ [ 1, 0 ], [ 0, 1 ] ]);
ok !$A3->isSquare, 'The matrix A3 is not square';
ok $B3->isSquare, 'The matrix B3 is square';
ok $C3->isSquare, 'The matrix C3 is square';
ok !$A3->isOne, 'The matrix A3 is not an identity';
ok $B3->isOne, 'The matrix B3 is an identity';
ok !$C3->isOne, 'The matrix C3 is not an identity';
ok !$A3->isRow, 'The matrix A3 is not a row';
ok !$B3->isRow, 'The matrix B3 is not a row';
ok !$C3->isRow, 'The matrix C3 is not a row';
};
subtest 'Use tests for triangular matrices' => sub {
my $A1 = Matrix([ [ 1, 2, 3, 4 ], [ 0, 6, 7, 8 ], [ 0, 0, 11, 12 ], [ 0, 0, 0, 16 ] ]);
my $A2 = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ]);
my $A3 = Matrix($A1, $A1);
my $A4 = Matrix($A2, $A1);
ok $A1->isUpperTriangular, 'Test for upper triangular matrix';
ok !$A2->isUpperTriangular, 'Not an upper triangular matrix';
ok $A3->isUpperTriangular, 'Test for upper triangular degree 3 matrix';
ok !$A4->isUpperTriangular, 'Not an upper triangular degree 3 matrix';
my $B1 = Matrix([ [ 1, 0, 0, 0 ], [ 5, 6, 0, 0 ], [ 9, 10, 11, 0 ], [ 13, 14, 15, 16 ] ]);
my $B2 = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
my $B3 = Matrix($B1, $B1);
my $B4 = Matrix($B2, $B2);
ok $B1->isLowerTriangular, 'Test for lower triangular matrix';
ok !$B2->isLowerTriangular, 'Not a lower triangular matrix';
ok $B3->isLowerTriangular, 'Test for lower triangular degree 3 matrix';
ok !$B4->isLowerTriangular, 'Not a lower triangular degree 3 matrix';
};
subtest 'Test if a Matrix is symmetric' => sub {
my $A = Matrix(5);
ok $A->isSymmetric, 'Test a degree 1 Matrix of length 1 is symmetric';
my $B = Matrix([ 1, 2 ], [ 2, 3 ]);
my $C = Matrix([ 1, 2 ], [ 3, 4 ]);
ok $B->isSymmetric, 'Test a degree 2 symmetric Matrix';
ok !$C->isSymmetric, 'Test a degree 2 nonsymmetric Matrix';
my $D = Matrix($B, $B);
my $E = Matrix($B, $C);
ok $D->isSymmetric, 'Test a degree 3 symmetric Matrix';
ok !$E->isSymmetric, 'Test a degree 3 nonsymmetric Matrix';
};
subtest 'Test if a Matrix is orthogonal' => sub {
my $A = Matrix(-1);
my $B = Matrix( 2);
ok $A->isOrthogonal, 'Test a degree 1 orthogonal Matrix';
ok !$B->isOrthogonal, 'Test a degree 1 nonorthogonal Matrix';
my $C = Matrix([ 3 / 5, 4 / 5 ], [ -4 / 5, 3 / 5 ]);
my $D = Matrix([ 1, 2 ], [ 3, 4 ]);
ok $C->isOrthogonal, 'Test a degree 2 orthogonal Matrix';
ok !$D->isOrthogonal, 'Test a degree 2 nonorthogonal Matrix';
my $E = Matrix($C, [ [ 0, 1 ], [ -1, 0 ] ]);
my $F = Matrix($D, $C);
ok $E->isOrthogonal, 'Test a degree 3 orthogonal Matrix';
ok !$F->isOrthogonal, 'Test a degree 3 nonorthogonal Matrix';
};
subtest 'Test if Matrix is in (R)REF' => sub {
my $A1 = Matrix(0);
my $A2 = Matrix(1);
my $A3 = Matrix(2);
my $A4 = Matrix(1, 3, 4);
my $A5 = Matrix(2, 3, 4);
my $A6 = Matrix(0, 3, 4);
my $A7 = Matrix(0, 1, 4);
ok $A1->isRREF, "$A1 is in RREF";
ok $A2->isRREF, "$A2 is in RREF";
ok !$A3->isRREF, "$A3 is not in RREF";
ok $A4->isRREF, "$A4 is in RREF";
ok !$A5->isRREF, "$A5 is not in RREF";
ok !$A6->isRREF, "$A6 is not in RREF";
ok $A7->isRREF, "$A7 is in RREF";
my $B1 = Matrix([ 1, 2, 3 ], [ 0, 4, 5 ]);
my $B2 = Matrix([ 1, 2, 3 ], [ 0, 1, 5 ]);
my $B3 = Matrix([ 1, 0, 3 ], [ 0, 1, 5 ]);
my $B4 = Matrix([ 0, 1, 3 ], [ 2, 1, 5 ]);
ok $B1->isREF, "$B1 is in REF";
ok !$B1->isRREF, "$B1 is not in RREF";
ok $B2->isREF, "$B2 is in REF";
ok !$B2->isRREF, "$B2 is not in RREF";
ok $B3->isREF, "$B3 is in REF";
ok $B3->isRREF, "$B3 is in RREF";
ok !$B4->isREF, "$B4 is not in REF";
ok !$B4->isRREF, "$B4 is not in RREF";
};
subtest 'Transpose a Matrix' => sub {
my $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
my $B = Matrix([ [ 1, 5, 9 ], [ 2, 6, 10 ], [ 3, 7, 11 ], [ 4, 8, 12 ] ]);
is $A->transpose->TeX, $B->TeX, 'Test the transpose of a matrix';
my $row = Matrix([ 1, 2, 3, 4 ]);
my $row_trans = Matrix([ [1], [2], [3], [4] ]);
is $row->transpose->TeX, $row_trans->TeX, 'Transpose of a degree 1 Matrix';
my $C = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
my $D = Matrix([ [ [ 1, 3 ], [ 2, 4 ] ], [ [ 5, 7 ], [ 6, 8 ] ] ]);
is $C->transpose->TeX, $D->TeX, 'Test the transpose of a degree 3 tensor';
};
subtest 'Extract an element' => sub {
my $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
my $B = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
my $row = Matrix([ 1, 2, 3, 4 ]);
is $A->element(1, 1), 1, 'Extract an element from a degree 2 matrix';
is $A->element(3, 2), 10, 'Extract an element from a degree 2 matrix';
is $B->element(1, 2, 1), 3, 'Extract an element from a degree 3 matrix';
is $row->element(2), 2, 'Extract an element from a degree 1 matrix';
};
subtest 'Extract a column' => sub {
my $A1 = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
my $col = Matrix([ [2], [6], [10] ]);
is $A1->column(2)->TeX, $col->TeX, 'Extract a column from a matrix';
like dies {
$A1->column(-1);
}, qr/Column must be a positive integer/, 'Test that an error is thrown for passing a non-positive integer';
};
subtest 'Construct an identity matrix' => sub {
my $I = Value::Matrix->I(3);
my $B = Matrix([ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]);
my $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
is $I->TeX, $B->TeX, 'Create a 3 x 3 identity matrix';
is $A->I->TeX, $B->TeX, 'Create a 3 x 3 identity matrix by using an existing matrix';
};
subtest 'Construct a permutation matrix' => sub {
my $P1 = Value::Matrix->P(3, [ 1, 2, 3 ]);
is $P1->TeX, Matrix([ [ 0, 0, 1 ], [ 1, 0, 0 ], [ 0, 1, 0 ] ])->TeX, 'Create permuation matrix on cycle (123)';
my $P2 = Value::Matrix->P(6, [ 1, 3 ], [ 2, 4, 6 ]);
is $P2->TeX,
Matrix([
[ 0, 0, 1, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0 ],
[ 0, 1, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0 ],
[ 0, 0, 0, 1, 0, 0 ]
])->TeX, 'Create a permutation matrix on cycle product (13)(246)';
my $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ]);
my $P3 = $A->P([ 1, 4 ]);
is $P3->TeX,
Matrix([ [ 0, 0, 0, 1 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 1, 0, 0, 0 ] ])->TeX,
'Create a permutation matrix based on an existing matrix';
};
subtest 'Construct a zero matrix' => sub {
my $Z1 = Matrix([ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]);
my $Z2 = Matrix([ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]);
is Value::Matrix->Zero(3, 4)->TeX, $Z1->TeX, 'Create a 3 by 4 zero matrix';
is Value::Matrix->Zero(4)->TeX, $Z2->TeX, 'Create a 4 by 4 zero matrix';
my $A1 = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
is $A1->Zero->TeX, $Z1->TeX, 'Create a zero matrix with same size as the given one';
like dies {
Value::Matrix->Zero(4, 0);
}, qr/Dimension must be a positive integer/, 'Test that an error is thrown for passing a non-positive integer';
};
subtest 'Add matrices' => sub {
my $row1 = Matrix(1, 2, 3);
my $row2 = Matrix(4, 5, 6);
my $sum1 = Matrix(5, 7, 9);
ok $row1+ $row2 == $sum1, 'Checking the sum of two row matrices';
my $A = Matrix([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]);
my $B = Matrix([ [ 0, 1, 0 ], [ -1, 2, -3 ], [ -2, -1, 0 ] ]);
my $sum2 = Matrix([ [ 1, 3, 3 ], [ 3, 7, 3 ], [ 5, 7, 9 ] ]);
ok $A+ $B == $sum2, 'Checking the sum of two 3 by 3 matrices';
#tensors
my $M1 = Matrix([ [ [ 1, 0 ], [ 0, 1 ] ], [ [ 1, 0 ], [ 0, 1 ] ] ]);
my $M2 = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
my $M3 = Matrix([ [ [ 2, 2 ], [ 3, 5 ] ], [ [ 6, 6 ], [ 7, 9 ] ] ]);
ok $M1 + $M2 == $M3, 'Checking the sum of two tensors';
my $row3 = Matrix([ 1, 2, 3, 4 ]);
like dies { $row1 + $row3 }, qr/Can't add Matrices with different dimensions/,
'Test that adding row matrices of different dimsensions throws an error';
my $C = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ]);
like dies { $A + $C }, qr/Can't add Matrices with different dimensions/,
'Test that adding matrices of different dimsensions throws an error';
my $M4 = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ] ]);
like dies { $M3 + $M4 }, qr/Can't add Matrices with different dimensions/,
'Test that adding tensors of different dimsensions throws an error';
};
subtest 'Subtract matrices' => sub {
my $row1 = Matrix( 1, 2, 3);
my $row2 = Matrix( 4, 5, 6);
my $diff1 = Matrix(-3, -3, -3);
ok $row1 - $row2 == $diff1, 'Checking the difference of two row matrices';
my $A = Matrix([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]);
my $B = Matrix([ [ 0, 1, 0 ], [ -1, 2, -3 ], [ -2, -1, 0 ] ]);
my $diff2 = Matrix([ [ 1, 1, 3 ], [ 5, 3, 9 ], [ 9, 9, 9 ] ]);
ok $A - $B == $diff2, 'Checking the difference of two 3 by 3 matrices';
#tensors
my $M1 = Matrix([ [ [ 1, 0 ], [ 0, 1 ] ], [ [ 1, 0 ], [ 0, 1 ] ] ]);
my $M2 = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
my $M3 = Matrix([ [ [ 0, -2 ], [ -3, -3 ] ], [ [ -4, -6 ], [ -7, -7 ] ] ]);
ok $M1 - $M2 == $M3, 'Checking the difference of two tensors';
my $row3 = Matrix([ 1, 2, 3, 4 ]);
like dies { $row1 - $row3 }, qr/Can't subtract Matrices with different dimensions/,
'Test that subtracting row matrices of different dimsensions throws an error';
my $C = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ]);
like dies { $A - $C }, qr/Can't subtract Matrices with different dimensions/,
'Test that subtracting matrices of different dimsensions throws an error';
my $M4 = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ] ]);
like dies { $M3 - $M4 }, qr/Can't subtract Matrices with different dimensions/,
'Test that subtracting tensors of different dimsensions throws an error';
};
subtest 'Multiply matrices' => sub {
my $A = Matrix([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]);
my $B = Matrix([ [ 0, 1, 0 ], [ -1, 2, -3 ], [ -2, -1, 0 ] ]);
my $prod1 = Matrix([ [ -8, 2, -6 ], [ -17, 8, -15 ], [ -26, 14, -24 ] ]);
my $C = Matrix([ [ 1, -5, -2, -5 ], [ 0, -5, 5, -4 ], [ 4, 1, -1, 1 ] ]);
my $prod2 = Matrix([ [ 13, -12, 5, -10 ], [ 28, -39, 11, -34 ], [ 43, -66, 17, -58 ] ]);
ok $A*$B == $prod1, 'Checking the product of two 3 by 3 matrices';
ok $A*$C == $prod2, 'Checking the product of a 3 by 3 and 3 by 4 matrix';
like dies { $C * $A }, qr/Matrices of dimensions \d+x\d+ and \d+x\d+ can't be multiplied/,
'Test that multiplying row matrices of incompatible dimsensions throws an error';
# multiply degree 2 and 1 matrices.
my $row = Matrix(1, 2, 3);
my $prod3 = Matrix(14, 32, 50);
ok $A*$row == $prod3, 'Multiply a 3 by 3 matrix and a row matrix of length 3 (the row is promoted to a matrix)';
my $col = Matrix([ [1], [2], [3] ]);
my $prod4 = Matrix([ [14], [32], [50] ]);
ok $A*$col == $prod4, 'Multiply a 3 by 3 matrix and a column matrix of length 3';
my $v = Vector(1, 2, 3);
my $prod5 = Vector(14, 32, 50);
ok $A*$v == $prod5, 'Multiply a 3 by 3 matrix and a vector of length 3';
};
subtest 'Construct an elementary matrix' => sub {
my $E1 = Value::Matrix->E(3, [ 1, 3 ]);
is $E1->TeX, Matrix([ [ 0, 0, 1 ], [ 0, 1, 0 ], [ 1, 0, 0 ] ])->TeX, 'Elementary Matrix with a row swap';
my $E2 = Value::Matrix->E(4, [2], 3);
is $E2->TeX, Matrix([ [ 1, 0, 0, 0 ], [ 0, 3, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX,
'Elementary Matrix with row multiple';
my $E3 = Value::Matrix->E(4, [ 3, 2 ], -3);
is $E3->TeX, Matrix([ [ 1, 0, 0, 0 ], [ 0, 1, -3, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX,
'Elementary Matrix with row multiple and add';
my $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ]);
is $A->E([ 1, 4 ])->TeX,
Matrix([ [ 0, 0, 0, 1 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 1, 0, 0, 0 ] ])->TeX,
'Elementary Matrix from syntax $A->E an existing matrix with a row swap';
is $A->E([2], 5)->TeX,
Matrix([ [ 1, 0, 0, 0 ], [ 0, 5, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX,
'Elementary Matrix from syntax $A->E an existing matrix with a row multiple';
is $A->E([ 2, 4 ], -4)->TeX,
Matrix([ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, -4, 0, 1 ] ])->TeX,
'Elementary Matrix from syntax $A->E an existing matrix with a row multiple and add';
};
subtest 'Extract a slice from a Matrix' => sub {
my $A = Matrix(1, 2, 3);
my $a = Real(3);
ok $a == $A->slice(1, 3), 'Get a slice from a degree 1 Matrix';
my $B = Matrix([ 2, 3, 4 ], [ 1, 2, 3 ]);
my $b = Matrix(3, 2);
ok $A == $B->slice(1, 2), 'Get a slice from a degree 2 Matrix';
ok $b == $B->slice(2, 2), 'Get a slice from a degree 2 Matrix';
my $C = Matrix([ [ 2, 3, 4 ], [ 1, 2, 3 ] ], [ [ 2, 3, 4 ], [ 1, 2, 3 ] ], [ [ 1, 1, 1 ], [ 5, 6, 7 ] ]);
my $c = Matrix([ 2, 3, 4 ], [ 2, 3, 4 ], [ 1, 1, 1 ]);
my $d = Matrix([ 3, 2 ], [ 3, 2 ], [ 1, 6 ]);
ok $B == $C->slice(1, 1), 'Get a slice from a degree 3 Matrix';
ok $c == $C->slice(2, 1), 'Get a slice from a degree 3 Matrix';
ok $d == $C->slice(3, 2), 'Get a slice from a degree 3 Matrix';
};
subtest 'Transpose a Matrix' => sub {
my $A = Matrix(1, 2);
my $At = Matrix([1], [2]);
ok $A->transpose == $At, 'Transpose a degree 1 matrix';
my $B = Matrix([ 1, 2, 3 ], [ 4, 5, 6 ]);
my $Bt = Matrix([ 1, 4 ], [ 2, 5 ], [ 3, 6 ]);
ok $B->transpose == $Bt, 'Transpose a degree 2 matrix';
my $C = Matrix($B, $B);
my $Ct = Matrix($Bt, $Bt);
ok $C->transpose == $Ct, 'Transpose a degree 3 matrix';
my $D = Matrix([ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ]);
my $Dt = Matrix([ [ 1, 3 ], [ 5, 7 ] ], [ [ 2, 4 ], [ 6, 8 ] ]);
ok $D->transpose([ 1, 2, 3 ]) == $Dt, 'Transpose a degree 3 matrix using a cycle';
my $E = Matrix(
[ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ],
[ [ [ 9, 10 ], [ 11, 12 ] ], [ [ 13, 14 ], [ 15, 0 ] ] ]
);
my $Et = Matrix(
[ [ [ 1, 3 ], [ 2, 4 ] ], [ [ 9, 11 ], [ 10, 12 ] ] ],
[ [ [ 5, 7 ], [ 6, 8 ] ], [ [ 13, 15 ], [ 14, 0 ] ] ]
);
ok $E->transpose([ [ 1, 2 ], [ 3, 4 ] ]) == $Et, 'Transpose a degree 4 matrix using two cycles';
};
done_testing;