File: matrix.tex

package info (click to toggle)
gap 4r4p10-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 29,224 kB
  • ctags: 7,084
  • sloc: ansic: 98,591; sh: 3,284; perl: 2,263; makefile: 467; awk: 6
file content (1432 lines) | stat: -rw-r--r-- 47,257 bytes parent folder | download | duplicates (3)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
% This file was created automatically from matrix.msk.
% DO NOT EDIT!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%A  matrix.msk                  GAP documentation            Martin Schoenert
%A                                                           Alexander Hulpke
%%
%A  @(#)$Id: matrix.msk,v 1.40.2.5 2006/09/16 19:02:49 jjm Exp $
%%
%Y  (C) 1998 School Math and Comp. Sci., University of St.  Andrews, Scotland
%Y  Copyright (C) 2002 The GAP Group
%%
\Chapter{Matrices}

Matrices are represented in {\GAP} by lists of row vectors (see "Row Vectors").
The vectors must all have the same length, and their elements must lie in
a common ring. However, since checking rectangularness can be expensive
functions and methods of operations for matrices often will not give an error
message for non-rectangular lists of lists -- in such cases the result is
undefined.

Because matrices are just a special case of lists, all operations and
functions for lists are applicable to matrices also (see chapter
"Lists"). This especially includes accessing elements of a matrix (see
"List Elements"), changing elements of a matrix (see "List Assignment"),
and comparing matrices (see "Comparisons of Lists").

Note that, since a matrix is a list of lists, the behaviour of `ShallowCopy'
for matrices is just a special case of `ShallowCopy' for lists
(see~"Duplication of Lists");
called with an immutable matrix <mat>, `ShallowCopy' returns a mutable matrix
whose rows are identical to the rows of <mat>.
In particular the rows are still immutable.
To get a matrix whose rows are mutable,
one can use `List( <mat>, ShallowCopy )'.

\>`InfoMatrix' V

The info class for matrix operations is `InfoMatrix'.




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Categories of Matrices}

\>IsMatrix( <obj> ) C

A *matrix* is a list of lists of equal length whose entries lie in a
common ring.

Note that matrices may have different multiplications,
besides the usual matrix product there is for example the Lie product.
So there are categories such as `IsOrdinaryMatrix' and `IsLieMatrix'
(see~"IsOrdinaryMatrix", "IsLieMatrix")
that describe the matrix multiplication.
One can form the product of two matrices only if they support the same
multiplication.


\beginexample
gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
gap> IsMatrix(mat);
true
\endexample

Note also the filter `IsTable' (see section "IsTable")
which  may be more appropriate than `IsMatrix' for some purposes.

Note that the empty list '[ ]' and more complex ``empty'' structures
such as `[[ ]]' are *not* matrices, although special
methods allow them be used in place of matrices in some
situations. See "EmptyMatrix" below.

\beginexample
gap> [[0]]*[[]];
[ [  ] ]
gap> IsMatrix([[]]);
false
\endexample


\>IsOrdinaryMatrix( <obj> ) C

An *ordinary matrix* is a matrix whose multiplication is the ordinary
matrix multiplication.

Each matrix in internal representation is in the category
`IsOrdinaryMatrix',
and arithmetic operations with objects in `IsOrdinaryMatrix' produce
again matrices in `IsOrdinaryMatrix'.

Note that we want that Lie matrices shall be matrices that behave in the
same way as ordinary matrices, except that they have a different
multiplication.
So we must distinguish the different matrix multiplications,
in order to be able to describe the applicability of multiplication,
and also in order to form a matrix of the appropriate type as the
sum, difference etc.~of two matrices which have the same multiplication.


\>IsLieMatrix( <mat> ) C

A *Lie matrix* is a matrix whose multiplication is given by the
Lie bracket.
(Note that a matrix with ordinary matrix multiplication is in the
category `IsOrdinaryMatrix', see~"IsOrdinaryMatrix".)

Each matrix created by `LieObject' is in the category `IsLieMatrix',
and arithmetic operations with objects in `IsLieMatrix' produce
again matrices in `IsLieMatrix'.





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Operators for Matrices}

The rules for arithmetic operations involving matrices are in fact
special cases of those for the arithmetic of lists,
given in Section~"Arithmetic for Lists" and the following sections,
here we reiterate that definition, in the language of vectors and matrices.

Note that the additive behaviour sketched below is defined only for lists in
the category `IsGeneralizedRowVector',
and the multiplicative behaviour is defined only for lists in the category
`IsMultiplicativeGeneralizedRowVector'
(see~"Filters Controlling the Arithmetic Behaviour of Lists").


\>`<mat1> + <mat2>'{addition!matrices} O

returns the sum of the two matrices <mat1> and <mat2>,
Probably the most usual situation is that <mat1> and <mat2> have the same
dimensions and are defined over a common field;
in this case the sum is a new matrix over the same field where each entry
is the sum of the corresponding entries of the matrices.

In more general situations, the sum of two matrices need not be a matrix,
for example adding an integer matrix <mat1> and a matrix <mat2> over
a finite field yields the table of pointwise sums,
which will be a mixture of finite field elements and integers if <mat1> has
bigger dimensions than <mat2>.


\>`<scalar> + <mat>'{addition!scalar and matrix} O
\>`<mat> + <scalar>'{addition!matrix and scalar} O

returns the sum of the scalar <scalar> and the matrix <mat>.
Probably the most usual situation is that the entries of <mat> lie in a
common field with <scalar>;
in this case the sum is a new matrix over the same field where each entry
is the sum of the scalar and the corresponding entry of the matrix.

More general situations are for example the sum of an integer scalar and a
matrix over a finite field, or the sum of a finite field element and an
integer matrix.


\>`<mat1> - <mat2>'{subtraction!matrices}
\>`<scalar> - <mat>'{subtraction!scalar and matrix} O
\>`<mat> - <scalar>'{subtraction!matrix and scalar} O

Subtracting a matrix or scalar is defined as adding its additive inverse,
so the statements for the addition hold likewise.


\>`<scalar> * <mat>'{multiplication!scalar and matrix} O
\>`<mat> * <scalar>'{multiplication!matrix and scalar} O

returns the product of the scalar <scalar> and the matrix <mat>.
Probably the most usual situation is that the elements of <mat> lie in a
common field with <scalar>;
in this case the product is a new matrix over the same field where each
entry is the product of the scalar and the corresponding entry of the matrix.

More general situations are for example the product of an integer scalar and
a matrix over a finite field,
or the product of a finite field element and an integer matrix.


\>`<vec> * <mat>'{multiplication!vector and matrix} O

returns the product of the row vector <vec> and the matrix <mat>.
Probably the most usual situation is that <vec> and <mat> have the same
lengths and are defined over a common field,
and that all rows of <mat> have the same length $m$, say;
in this case the product is a new row vector of length $m$ over the same
field which is the sum of the scalar multiples of the rows of <mat> with the
corresponding entries of <vec>.

More general situations are for example the product of an integer vector and
a matrix over a finite field,
or the product of a vector over a finite field and an integer matrix.


\>`<mat> * <vec>'{multiplication!matrix and vector} O

returns the product of the matrix <mat> and the row vector <vec>.
(This is the standard product of a matrix with a *column* vector.)
Probably the most usual situation is that the length of <vec> and of all rows
of <mat> are equal, and that the elements of <mat> and <vec> lie in a common
field;
in this case the product is a new row vector of the same length as <mat> and
over the same field which is the sum of the scalar multiples of the columns
of <mat> with the corresponding entries of <vec>.

More general situations are for example the product of an integer matrix and
a vector over a finite field,
or the product of a matrix over a finite field and an integer vector.


\>`<mat1> * <mat2>'{multiplication!matrices} O

This form evaluates to the (Cauchy) product of the two matrices <mat1> and
<mat2>.
Probably the most usual situation is that the number of columns of <mat1>
equals the number of rows of <mat2>,
and that the elements of <mat> and <vec> lie in a common field;
if <mat1> is a matrix with $m$ rows and $n$ columns, say, and <mat2> is a
matrix with $n$ rows and $o$ columns, the result is a new matrix with $m$
rows and $o$ columns.
The element in row $i$ at position $j$ of the product is the sum of
$<mat1>[i][l] \* <mat2>[l][j]$, with $l$ running from $1$ to $n$.


\>`Inverse( <mat> )'{inverse!matrix} O

returns the inverse of the matrix <mat>,
which must be an invertible square matrix.
If <mat> is not invertible then `fail' is returned.


\>`<mat1> / <mat2>'{quotient!matrices} O
\>`<scalar> / <mat>'{quotient!scalar and matrix} O
\>`<mat> / <scalar>'{quotient!matrix and scalar} O
\>`<vec> / <mat>'{quotient!vector and matrix} O

In general, `<left> / <right>' is defined as `<left> * <right>^-1'.
Thus in the above forms the right operand must always be invertible.


\>`<mat> ^ <int>'{power!matrix} O
\>`<mat1> ^ <mat2>'{conjugate!matrix} O
\>`<vec> ^ <mat>'{image!vector under matrix} O

Powering a square matrix <mat> by an integer <int> yields the <int>-th power
of <mat>; if <int> is negative then <mat> must be invertible,
if <int> is `0' then the result is the identity matrix `One( <mat> )',
even if <mat> is not invertible.

Powering a square matrix <mat1> by an invertible square matrix <mat2> of the
same dimensions yields the conjugate of <mat1> by <mat2>, i.e.,
the matrix `<mat2>^-1 * <mat1> * <mat2>'.

Powering a row vector <vec> by a matrix <mat> is in every respect equivalent
to `<vec> * <mat>'.
This operations reflects the fact that matrices act naturally on row vectors
by multiplication from the right, and that the powering operator is {\GAP}'s
standard for group actions.


\>`Comm( <mat1>, <mat2> )'{matrices!commutator} O

returns the commutator of the square invertible matrices <mat1> and <mat2>
of the same dimensions and over a common field,
which is the matrix `<mat1>^-1 * <mat2>^-1 * <mat1> * <mat2>'.


The following cases are still special cases of the general list arithmetic
defined in~"Arithmetic for Lists".


\>`<scalar> + <matlist>'{addition!scalar and matrix list} O
\>`<matlist> + <scalar>'{addition!scalar and matrix list} O
\>`<scalar> - <matlist>'{subtraction!scalar and matrix list} O
\>`<matlist> - <scalar>'{subtraction!scalar and matrix list} O
\>`<scalar> * <matlist>'{multiplication!scalar and matrix list} O
\>`<matlist> * <scalar>'{multiplication!scalar and matrix list} O
\>`<matlist> / <scalar>'{quotient!scalar and matrix list} O

A scalar <scalar> may also be added, subtracted, multiplied with, or
divided into a list <matlist> of matrices. The result is a new list
of matrices where each matrix is the result of performing the operation
with the corresponding matrix in <matlist>.


\>`<mat> * <matlist>'{multiplication!matrix and matrix list} O
\>`<matlist> * <mat>'{multiplication!matrix and matrix list} O

A matrix <mat> may also be multiplied with a list <matlist> of matrices.
The result is a new list of matrices, where each entry is the product of
<mat> and the corresponding entry in <matlist>.


\>`<matlist> / <mat>'{quotient!matrix and matrix list} O

Dividing a list <matlist> of matrices by an invertible matrix <mat>
evaluates to `<matlist> * <mat>^-1'.


\>`<vec> * <matlist>'{multiplication!vector and matrix list} O

returns the product of the vector <vec> and the list of matrices <mat>.
The lengths <l> of <vec> and <matlist> must be equal.
All matrices in <matlist> must have the same dimensions. The elements of
<vec> and the elements of the matrices in <matlist> must lie in a common
ring. The product is the sum over `<vec>[<i>] * <matlist>[<i>]' with
<i> running from 1 to <l>.


For the mutability of results of arithmetic operations,
see~"Mutability and Copyability".


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Properties and Attributes of Matrices}

\>DimensionsMat( <mat> ) A

is a list of length 2, the first being the number of rows, the second
being the number of columns of the matrix <mat>.


\beginexample
gap> DimensionsMat([[1,2,3],[4,5,6]]);
[ 2, 3 ]
\endexample
\>DefaultFieldOfMatrix( <mat> ) A

For a matrix <mat>, `DefaultFieldOfMatrix' returns either a field
(not necessarily the smallest one) containing all entries of <mat>,
or `fail'.

If <mat> is a matrix of finite field elements or a matrix of cyclotomics,
`DefaultFieldOfMatrix' returns the default field generated by the matrix
entries (see~"Creating Finite Fields" and "Operations for Cyclotomics").


\beginexample
gap> DefaultFieldOfMatrix([[Z(4),Z(8)]]);
GF(2^6)
\endexample

\indextt{Trace!of a matrix}
\>TraceMat( <mat> ) F
\>Trace( <mat> ) F

The trace of a square matrix is the sum of its diagonal entries.


\beginexample
gap> TraceMat([[1,2,3],[4,5,6],[7,8,9]]);
15
\endexample
\>DeterminantMat( <mat> ) A
\>Determinant( <mat> ) F

returns the determinant of the square matrix <mat>.

These methods assume implicitly that <mat> is defined over an
integral domain whose quotient field is implemented in {\GAP}. For
matrices defined over an arbitrary commutative ring with one 
see~"DeterminantMatDivFree".


\>DeterminantMatDestructive( <mat> ) O

Does the same as `DeterminantMat', with the difference that it may
destroy its argument. The matrix <mat> must be mutable.


\beginexample
gap> DeterminantMat([[1,2],[2,1]]);
-3
gap> mm:= [[1,2],[2,1]];;
gap> DeterminantMatDestructive( mm );
-3
gap> mm;
[ [ 1, 2 ], [ 0, -3 ] ]
\endexample

\>DeterminantMatDivFree( <mat> ) O

returns the determinant of a square matrix <mat> over an arbitrary 
commutative ring with one using the division free method of 
Mahajan and Vinay \cite{MV97}.



\>IsMonomialMatrix( <mat> ) P

A matrix is monomial if  and only if it  has exactly one nonzero entry in
every row and every column.


\beginexample
gap> IsMonomialMatrix([[0,1],[1,0]]);
true
\endexample

\>IsDiagonalMat( <mat> ) O

returns true if mat has only zero entries off the main diagonal, false
otherwise.


\>IsUpperTriangularMat( <mat> ) O

returns true if mat has only zero entries below the main diagonal, false
otherwise.


\>IsLowerTriangularMat( <mat> ) O

returns true if mat has only zero entries below the main diagonal, false
otherwise.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Matrix Constructions}

\>IdentityMat( <m> [, <F>] ) F

returns a (mutable) <m>$\times$<m> identity matrix over the field given
by <F> (i.e. the smallest field containing the element <F> or <F> itself
if it is a field).


\>NullMat( <m>, <n> [, <F>] ) F

returns a (mutable) <m>$\times$<n> null matrix over the field given by
<F>.


\beginexample
gap> IdentityMat(3,1);
[ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]
gap> NullMat(3,2,Z(3));
[ [ 0*Z(3), 0*Z(3) ], [ 0*Z(3), 0*Z(3) ], [ 0*Z(3), 0*Z(3) ] ]
\endexample
\>EmptyMatrix( <char> ) F

is an empty (ordinary) matrix in characteristic <char> that can be added
to or multiplied with empty lists (representing zero-dimensional row
vectors). It also acts (via `^') on empty lists.



\beginexample
gap> EmptyMatrix(5);
EmptyMatrix( 5 )
gap> AsList(last);
[  ]
\endexample

\>DiagonalMat( <vector> ) F

returns a diagonal matrix <mat> with the diagonal entries given by
<vector>.


\beginexample
gap> DiagonalMat([1,2,3]);
[ [ 1, 0, 0 ], [ 0, 2, 0 ], [ 0, 0, 3 ] ]
\endexample
\>PermutationMat( <perm>, <dim> [, <F> ] ) F

returns a matrix in dimension <dim> over the field given by <F> (i.e.
the smallest field containing the element <F> or <F> itself if it is a
field)  that
represents the permutation <perm> acting by permuting the basis vectors
as it permutes points.


\beginexample
gap> PermutationMat((1,2,3),4,1);
[ [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 1, 0, 0, 0 ], [ 0, 0, 0, 1 ] ]
\endexample

\>TransposedMatImmutable( <mat> ) A
\>TransposedMatAttr( <mat> ) AM
\>TransposedMat( <mat> ) AM
\>TransposedMatMutable( <mat> ) O
\>TransposedMatOp( <mat> ) O

These functions all return the transposed of the matrix <mat>, i.e.,
a matrix <trans> such that `<trans>[<i>][<k>] = <mat>[<k>][<i>]' holds.

They differ only w.r.t. the mutability of the result.

`TransposedMat' is an attribute and hence returns an immutable result.
`TransposedMatMutable' is guaranteed to return a new *mutable* matrix.

`TransposedMatImmutable' and `TransposedMatAttr' are synonyms of
`TransposedMat',
and `TransposedMatOp' is a synonym of `TransposedMatMutable',
in analogy to operations such as `Zero' (see~"Zero").


\>TransposedMatDestructive( <mat> ) O

If <mat> is a mutable matrix, then the transposed
is computed by swapping the entries in <mat>. In this way <mat> gets
changed. In all other cases the transposed is computed by `TransposedMat'.


\beginexample
gap> TransposedMat([[1,2,3],[4,5,6],[7,8,9]]);
[ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
gap> mm:= [[1,2,3],[4,5,6],[7,8,9]];;
gap> TransposedMatDestructive( mm );
[ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
gap> mm;
[ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
\endexample

\>KroneckerProduct( <mat1>, <mat2> ) O

The Kronecker product of two matrices is the matrix obtained when
replacing each entry <a> of <mat1> by the product `<a>*<mat2>' in one
matrix.


\beginexample
gap> KroneckerProduct([[1,2]],[[5,7],[9,2]]);
[ [ 5, 7, 10, 14 ], [ 9, 2, 18, 4 ] ]
\endexample


\>ReflectionMat( <coeffs> ) F
\>ReflectionMat( <coeffs>, <root> ) F
\>ReflectionMat( <coeffs>, <conj> ) F
\>ReflectionMat( <coeffs>, <conj>, <root> ) F

Let <coeffs> be a row vector.
`ReflectionMat' returns the matrix of the reflection in this vector.

More precisely, if <coeffs> is the coefficients of a vector $v$ w.r.t. a
basis $B$ (see~"Basis"), say, then the returned matrix describes the
reflection in $v$ w.r.t. $B$ as a map on a row space, with action from
the right.

The optional argument <root> is a root of unity that determines the order
of the reflection.  The default is a reflection of order 2.
For triflections one should choose a third root of unity etc.
(see~"ref:E").

<conj> is a function of one argument that conjugates a ring element.
The default is `ComplexConjugate'.

The matrix of the reflection in $v$ is defined as
$$
M = I_n + \overline{v^{tr}} . \frac{w-1}{v \overline{v^{tr}}} . v
$$
where `$w$ = root',
$n$ is the length of the coefficient list,
and `$\overline{\vphantom{x}}$' denotes the conjugation.



\>PrintArray( <array> ) F

pretty-prints the array <array>.



\>MutableIdentityMat( <m> [, <F>] ) F

returns a (mutable) <m>$\times$<m> identity matrix over the field given
by <F>.
This is identical to `IdentityMat' and is present in {\GAP}~4.1
only for the sake of compatibility with beta-releases.
It should *not* be used in new code.



\>MutableNullMat( <m>, <n>  [, <F>] ) F

returns a (mutable) <m>$\times$<n> null matrix over the field given
by <F>.
This is identical to `NullMat' and is present in {\GAP}~4.1
only for the sake of compatibility with beta-releases.
It should *not* be used in new code.



\>MutableCopyMat( <mat> ) O

`MutableCopyMat'  returns a fully mutable copy  of  the  matrix <mat>.

The default method does `List(<mat>,ShallowCopy)' and thus may also
be called for the empty list, returning a new empty list.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Random Matrices}

\>RandomMat( <m>, <n> [, <R>] ) F

`RandomMat' returns a new mutable random matrix with <m> rows and
<n> columns with elements taken from the ring <R>, which defaults
to `Integers'.


\>RandomInvertibleMat( <m> [, <R>] ) F

`RandomInvertibleMat' returns a new mutable invertible random
matrix with <m> rows and columns with elements taken from the ring
<R>, which defaults to `Integers'.


\>RandomUnimodularMat( <m> ) F

returns a new random mutable <m>$\times$<m> matrix with integer
entries that is invertible over the integers.


\beginexample
gap> RandomMat(2,3,GF(3));
[ [ Z(3), Z(3), 0*Z(3) ], [ Z(3), Z(3)^0, Z(3) ] ]
gap> RandomInvertibleMat(4);
[ [ 1, -2, -1, 0 ], [ 1, 0, 1, -1 ], [ 0, 2, 0, 4 ], [ -1, -3, 1, -4 ] ]
\endexample

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Matrices Representing Linear Equations and the Gaussian Algorithm}

\atindex{Gaussian algorithm}{@Gaussian algorithm}
\>RankMat( <mat> ) A

If <mat> is a matrix whose rows span a free module over the ring
generated by the matrix entries and their inverses
then `RankMat' returns the dimension of this free module.
Otherwise `fail' is returned.

Note that `RankMat' may perform a Gaussian elimination.
For large rational matrices this may take very long,
because the entries may become very large.


\beginexample
gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> RankMat(mat);
2
\endexample
\>TriangulizeMat( <mat> ) O

applies the Gaussian Algorithm to the mutable matrix <mat> and changes
<mat> such that it is in upper triangular
normal form (sometimes called ``Hermite normal form'').


\beginexample
gap> m:=TransposedMatMutable(mat);
[ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
gap> TriangulizeMat(m);m;
[ [ 1, 0, -1 ], [ 0, 1, 2 ], [ 0, 0, 0 ] ]
\endexample
\>NullspaceMat( <mat> ) A
\>TriangulizedNullspaceMat( <mat> ) A

returns a list of row vectors that form a basis of the vector space of
solutions to the equation `<vec>*<mat>=0'. The result is an immutable
matrix. This basis is not guaranteed to be in any specific form.

The variant `TriangulizedNullspaceMat' returns a basis of the nullspace
in triangulized form as is often needed for algorithms.


\>NullspaceMatDestructive( <mat> ) O
\>TriangulizedNullspaceMatDestructive( <mat> ) O

This function does the same as `NullspaceMat'. However, the latter function
makes a copy of <mat> to avoid having to change it. This function
does not do that; it returns the null space and may destroy <mat>;
this saves a lot of memory in case <mat> is big. The matrix <mat>
must be mutable.

The variant `TriangulizedNullspaceMatDestructive' returns a basis of the
nullspace in triangulized form. It may destroy the matrix <mat>.


\beginexample
gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> NullspaceMat(mat);
[ [ 1, -2, 1 ] ]
gap> mm:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> NullspaceMatDestructive( mm );
[ [ 1, -2, 1 ] ]
gap> mm;
[ [ 1, 2, 3 ], [ 0, -3, -6 ], [ 0, 0, 0 ] ]
\endexample
\>SolutionMat( <mat>, <vec> ) O

returns a row vector <x> that is a solution of the equation `<x> * <mat>
= <vec>'. It returns `fail' if no such vector exists.


\>SolutionMatDestructive( <mat>, <vec> ) O

Does the same as `SolutionMat( <mat>, <vec> )' except that it may
destroy the matrix <mat>. The matrix <mat> must be mutable.


\beginexample
gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> SolutionMat(mat,[3,5,7]);
[ 5/3, 1/3, 0 ]
gap> mm:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> SolutionMatDestructive( mm, [3,5,7] );
[ 5/3, 1/3, 0 ]
gap> mm;
[ [ 1, 2, 3 ], [ 0, -3, -6 ], [ 0, 0, 0 ] ]
\endexample
\>BaseFixedSpace( <mats> ) F

`BaseFixedSpace' returns a list of row vectors that form a base of the
vector space $V$ such that $v M = v$ for all $v$ in $V$ and all matrices
$M$ in the list <mats>.  (This is the common eigenspace of all matrices
in <mats> for the eigenvalue 1.)


\beginexample
gap> BaseFixedSpace([[[1,2],[0,1]]]);
[ [ 0, 1 ] ]
\endexample

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Eigenvectors and eigenvalues}

\>GeneralisedEigenvalues( <F>, <A> ) O
\>GeneralizedEigenvalues( <F>, <A> ) O

The generalised eigenvalues of the matrix <A> over the field <F>.


\>GeneralisedEigenspaces( <F>, <A> ) O
\>GeneralizedEigenspaces( <F>, <A> ) O

The generalised eigenspaces of the matrix <A> over the field <F>.


\>Eigenvalues( <F>, <A> ) O

The eigenvalues of the matrix <A> over the field <F>.


\>Eigenspaces( <F>, <A> ) O

The eigenspaces of the matrix <A> over the field <F>.


\>Eigenvectors( <F>, <A> ) O

The eigenspaces of the matrix <A> over the field <F>.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Elementary Divisors}

See also chapter "Integral Matrices and Lattices".

\>ElementaryDivisorsMat( [<ring>, ] <mat> ) O
\>ElementaryDivisorsMatDestructive( <ring>, <mat> ) F

`ElementaryDivisors' returns a list of the elementary divisors, i.e., the
unique <d> with `<d>[<i>]' divides  `<d>[<i>+1]' and <mat> is  equivalent
to a diagonal matrix with the elements `<d>[<i>]' on the diagonal.
The operations are performed over the ring <ring>, which must contain
all matrix entries. For compatibility reasons it can be omitted and
defaults to `Integers'.

The function `ElementaryDivisorsMatDestructive' produces the same result
but in the process destroys the contents of <mat>.


\beginexample
gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> ElementaryDivisorsMat(mat);
[ 1, 3, 0 ]
gap> x:=X(Rationals,"x");;
gap> mat:=mat*One(x)-x*mat^0;       
[ [ -x+1, 2, 3 ], [ 4, -x+5, 6 ], [ 7, 8, -x+9 ] ]
gap> ElementaryDivisorsMat(PolynomialRing(Rationals,1),mat);
[ 1, 1, x^3-15*x^2-18*x ]
gap> mat:=KroneckerProduct(CompanionMat((x-1)^2),CompanionMat((x^3-1)*(x-1)));;
gap> mat:=mat*One(x)-x*mat^0;                                                 
[ [ -x, 0, 0, 0, 0, 0, 0, 1 ], [ 0, -x, 0, 0, -1, 0, 0, -1 ], 
  [ 0, 0, -x, 0, 0, -1, 0, 0 ], [ 0, 0, 0, -x, 0, 0, -1, -1 ], 
  [ 0, 0, 0, -1, -x, 0, 0, -2 ], [ 1, 0, 0, 1, 2, -x, 0, 2 ], 
  [ 0, 1, 0, 0, 0, 2, -x, 0 ], [ 0, 0, 1, 1, 0, 0, 2, -x+2 ] ]
gap> ElementaryDivisorsMat(PolynomialRing(Rationals,1),mat);
[ 1, 1, 1, 1, 1, 1, x-1, x^7-x^6-2*x^4+2*x^3+x-1 ]
\endexample
\>DiagonalizeMat( <ring>, <mat> ) O

brings the mutable matrix <mat>, considered as a matrix over <ring>,
into diagonal form by elementary row and column operations.


\beginexample
gap> m:=[[1,2],[2,1]];;
gap> DiagonalizeMat(Integers,m);m;
[ [ 1, 0 ], [ 0, 3 ] ]
\endexample

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Echelonized Matrices}

\>SemiEchelonMat( <mat> ) A

A matrix over a field $F$ is in semi-echelon form if the first nonzero
element in each row is the identity of $F$,
and all values exactly below these pivots are the zero of $F$.

`SemiEchelonMat' returns a record that contains information about
a semi-echelonized form of the matrix <mat>.

The components of this record are

\beginitems
`vectors'&
      list of row vectors, each with pivot element the identity of $F$,

`heads'&
      list that contains at position <i>, if nonzero, the number of the
      row for that the pivot element is in column <i>.
\enditems


\>SemiEchelonMatDestructive( <mat> ) O

This does the same as `SemiEchelonMat( <mat> )', except that it may
(and probably will) destroy the matrix <mat>.


\beginexample
gap> mm:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> SemiEchelonMatDestructive( mm );
rec( heads := [ 1, 2, 0 ], vectors := [ [ 1, 2, 3 ], [ 0, 1, 2 ] ] )
gap> mm;
[ [ 1, 2, 3 ], [ 0, 1, 2 ], [ 0, 0, 0 ] ]
\endexample

\>SemiEchelonMatTransformation( <mat> ) A

does the same as `SemiEchelonMat' but additionally stores the linear
transformation $T$ performed on the matrix.
The additional components of the result are

\beginitems
`coeffs'&
      a list of coefficients vectors of the `vectors' component,
      with respect to the rows of <mat>, that is, `coeffs * mat'
      is the `vectors' component.

`relations'&
      a list of basis vectors for the (left) null space of <mat>.
\enditems


\beginexample
gap> SemiEchelonMatTransformation([[1,2,3],[0,0,1]]);
rec( heads := [ 1, 0, 2 ], vectors := [ [ 1, 2, 3 ], [ 0, 0, 1 ] ], 
  coeffs := [ [ 1, 0 ], [ 0, 1 ] ], relations := [  ] )
\endexample
\>SemiEchelonMats( <mats> ) O

A list of matrices over a field $F$ is in semi-echelon form if the
list of row vectors obtained on concatenating the rows of each matrix
is a semi-echelonized matrix (see "SemiEchelonMat").

`SemiEchelonMats' returns a record that contains information about
a semi-echelonized form of the list <mats> of matrices.

The components of this record are

\beginitems
`vectors'&
      list of matrices, each with pivot element the identity of $F$,

`heads'&
      matrix that contains at position [<i>,<j>], if nonzero,
      the number of the matrix that has the pivot element in
      this position
\enditems


\>SemiEchelonMatsDestructive( <mats> ) O

Does the same as `SemiEchelonmats', except that it may destroy
its argument. Therefore the argument must be a list of matrices
that re mutable.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Matrices as Basis of a Row Space}

\>BaseMat( <mat> ) A

returns a basis for the row space generated by the rows of <mat> in the
form of an immutable matrix.


\>BaseMatDestructive( <mat> ) O

Does the same as `BaseMat', with the difference that it may destroy
the matrix <mat>. The matrix <mat> must be mutable.


\beginexample
gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> BaseMat(mat);
[ [ 1, 2, 3 ], [ 0, 1, 2 ] ]
gap> mm:= [[1,2,3],[4,5,6],[5,7,9]];;
gap> BaseMatDestructive( mm );
[ [ 1, 2, 3 ], [ 0, 1, 2 ] ]
gap> mm;
[ [ 1, 2, 3 ], [ 0, 1, 2 ], [ 0, 0, 0 ] ]
\endexample
\>BaseOrthogonalSpaceMat( <mat> ) A

Let $V$ be the row space generated  by the rows of  <mat> (over any field
that contains all  entries of <mat>).  `BaseOrthogonalSpaceMat( <mat>  )'
computes a base of the orthogonal space of $V$.

The rows of <mat> need not be linearly independent.




\>SumIntersectionMat( <M1>, <M2> ) O

performs  Zassenhaus'  algorithm to compute  bases  for  the sum  and the
intersection of spaces generated by the rows of the matrices <M1>, <M2>.

returns a list  of length 2,   at first position   a base of the sum,  at
second  position a  base   of the   intersection.   Both  bases  are   in
semi-echelon form (see~"Echelonized matrices").


\beginexample
gap> SumIntersectionMat(mat,[[2,7,6],[5,9,4]]);
[ [ [ 1, 2, 3 ], [ 0, 1, 2 ], [ 0, 0, 1 ] ], [ [ 1, -3/4, -5/2 ] ] ]
\endexample

\>BaseSteinitzVectors( <bas>, <mat> ) F

find vectors extending mat to a basis spanning the span of <bas>.
Both <bas> and <mat> must be matrices of full (row) rank. It returns a
record with the following components:
\beginitems
`subspace'&is a basis of the space spanned by <mat> in upper triangular
form with leading ones at all echelon steps and zeroes above these ones.

`factorspace'& is a list of extending vectors in upper triangular form.

`factorzero'& is a zero vector.

`heads'& is a list of integers which can be used to decompose vectors in
the basis vectors. The <i>th entry indicating the vector
that gives an echelon step at position <i>.
A negative number indicates an echelon step in the subspace, a positive
number an echelon step in the complement, the absolute value gives the
position of the vector in the lists `subspace' and `factorspace'.
\enditems


\beginexample
gap> BaseSteinitzVectors(IdentityMat(3,1),[[11,13,15]]);
rec( factorspace := [ [ 0, 1, 15/13 ], [ 0, 0, 1 ] ], 
  factorzero := [ 0, 0, 0 ], subspace := [ [ 1, 13/11, 15/11 ] ], 
  heads := [ -1, 1, 2 ] )
\endexample

See also chapter "Integral Matrices and Lattices"


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Triangular Matrices}

\>DiagonalOfMat( <mat> ) O

returns the diagonal of <mat> as a list.


\beginexample
gap> DiagonalOfMat([[1,2],[3,4]]);
[ 1, 4 ]
\endexample
\>UpperSubdiagonal( <mat>, <pos> ) O

returns a mutable list containing the entries of the <pos>th upper
subdiagonal of <mat>.


\beginexample
gap> UpperSubdiagonal(mat,1);
[ 2, 6 ]
\endexample
\>DepthOfUpperTriangularMatrix( <mat> ) A

If <mat> is an upper triangular matrix this attribute returns the
index of the first nonzero diagonal.


\beginexample
gap> DepthOfUpperTriangularMatrix([[0,1,2],[0,0,1],[0,0,0]]);
1
gap> DepthOfUpperTriangularMatrix([[0,0,2],[0,0,0],[0,0,0]]);
2
\endexample

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Matrices as Linear Mappings}

\>CharacteristicPolynomial( <mat> ) A
\>CharacteristicPolynomial( [[<F>, <E>, ] <mat> [, <ind>] ) O

For a square matrix <mat>, `CharacteristicPolynomial' returns the
*characteristic polynomial* of <mat>, that is, the `StandardAssociate'
of the determinant of the
matrix $<mat> - X \cdot I$, where $X$ is an indeterminate and $I$ is the
appropriate identity matrix.

If fields <F> and <E> are given, then <F> must be a subfield of <E>, and 
<mat> must have entries in <E>. Then `CharacteristicPolynomial' returns
the characteristic polynomial of the <F>-linear mapping induced by <mat> 
on the underlying <E>-vector space of <mat>. In this case, 
the characteristic polynomial is computed using `BlownUpMat' (see~"BlownUpMat") 
for the field extension of $E/F$ generated by the default field.
Thus, if $F = E$, the result is the same as for the one argument version.

The returned polynomials are expressed in the indeterminate number <ind>.
If <ind> is not given, it defaults to $1$.

`CharacteristicPolynomial(<F>, <E>, <mat>)' is a multiple of the 
minimal polynomial `MinimalPolynomial(<F>, <mat>)'
(see~"MinimalPolynomial").

Note that, up to {\GAP} version 4.4.6, `CharacteristicPolynomial' only 
allowed to specify one field (corresponding to <F>) as an argument.
That usage has been disabled because its definition turned out to be 
ambiguous and may have lead to unexpected results. (To ensure
backward compatibility, it is still possible to use the old form 
if <F> contains the default field of the matrix, see~"DefaultFieldOfMatrix",
but this feature will disappear in future versions of {\GAP}.)


\beginexample
gap> CharacteristicPolynomial( [ [ 1, 1 ], [ 0, 1 ] ] );
x^2-2*x+1
gap> mat := [[0,1],[E(4)-1,E(4)]];;
gap> CharacteristicPolynomial( mat );
x^2+(-E(4))*x+(1-E(4))
gap> CharacteristicPolynomial( Rationals, CF(4), mat );
x^4+3*x^2+2*x+2
gap> mat:= [ [ E(4), 1 ], [ 0, -E(4) ] ];;
gap> CharacteristicPolynomial( mat );
x^2+1
gap> CharacteristicPolynomial( Rationals, CF(4), mat );
x^4+2*x^2+1
\endexample

\>JordanDecomposition( <mat> ) A

`JordanDecomposition( <mat > )' returns a list `[S,N]' such that
`S' is a semisimple matrix and `N' is nilpotent. Furthermore, `S'
and `N' commute and `<mat>=S+N'.


\beginexample
gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
gap> JordanDecomposition(mat);
[ [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ], 
  [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ] ]
\endexample

\>BlownUpMat( <B>, <mat> ) F

Let <B> be a basis of a field extension $F / K$,
and <mat> a matrix whose entries are all in $F$.
(This is not checked.)
`BlownUpMat' returns a matrix over $K$ that is obtained by replacing each
entry of <mat> by its regular representation w.r.t.~<B>.

More precisely,
regard <mat> as the matrix of a linear transformation on the row space
$F^n$ w.r.t.~the $F$-basis with vectors $(v_1, ldots, v_n)$, say,
and suppose that the basis <B> consists of the vectors
$(b_1,  \ldots, b_m)$;
then the returned matrix is the matrix of the linear transformation
on the row space $K^{mn}$ w.r.t.~the $K$-basis whose vectors are
$(b_1 v_1, \ldots b_m v_1, \ldots, b_m v_n)$.

Note that the linear transformations act on *row* vectors, i.e.,
each row of the matrix is a concatenation of vectors of <B>-coefficients.


\>BlownUpVector( <B>, <vector> ) F

Let <B> be a basis of a field extension $F / K$,
and <vector> a row vector whose entries are all in $F$.
`BlownUpVector' returns a row vector over $K$ that is obtained by
replacing each entry of <vector> by its coefficients w.r.t.~<B>.

So `BlownUpVector' and `BlownUpMat' (see~"BlownUpMat") are compatible
in the sense that for a matrix <mat> over $F$,
`BlownUpVector( <B>, <mat> \* <vector> )'
is equal to
`BlownUpMat( <B>, <mat> ) \* BlownUpVector( <B>, <vector> )'.


\beginexample
gap> B:= Basis( CF(4), [ 1, E(4) ] );;
gap> mat:= [ [ 1, E(4) ], [ 0, 1 ] ];;  vec:= [ 1, E(4) ];;
gap> bmat:= BlownUpMat( B, mat );;  bvec:= BlownUpVector( B, vec );;
gap> Display( bmat );  bvec;
[ [   1,   0,   0,   1 ],
  [   0,   1,  -1,   0 ],
  [   0,   0,   1,   0 ],
  [   0,   0,   0,   1 ] ]
[ 1, 0, 0, 1 ]
gap> bvec * bmat = BlownUpVector( B, vec * mat );
true
\endexample

\>CompanionMat( <poly> ) F

computes a companion matrix of the polynomial <poly>. This matrix has
<poly> as its minimal polynomial.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Matrices over Finite Fields}

Just as for row vectors, (see section "Row Vectors over Finite
Fields"), {\GAP} has a special representation for matrices over small
finite fields.

To be eligible to be represented in this way, each row of a matrix
must be able to be represented as a compact row vector of the same
length  over *the same* finite field.

\beginexample
gap> v := Z(2)*[1,0,0,1,1];
[ Z(2)^0, 0*Z(2), 0*Z(2), Z(2)^0, Z(2)^0 ]
gap> ConvertToVectorRep(v,2);
2
gap> v;
<a GF2 vector of length 5>
gap> m := [v];; ConvertToMatrixRep(m,GF(2));; m;
<a 1x5 matrix over GF2>
gap> m := [v,v];; ConvertToMatrixRep(m,GF(2));; m;
<a 2x5 matrix over GF2>
gap> m := [v,v,v];; ConvertToMatrixRep(m,GF(2));; m;
<a 3x5 matrix over GF2>
gap> v := Z(3)*[1..8];
[ Z(3), Z(3)^0, 0*Z(3), Z(3), Z(3)^0, 0*Z(3), Z(3), Z(3)^0 ]
gap> ConvertToVectorRep(v);
3
gap> m := [v];; ConvertToMatrixRep(m,GF(3));; m;
[ [ Z(3), Z(3)^0, 0*Z(3), Z(3), Z(3)^0, 0*Z(3), Z(3), Z(3)^0 ] ]
gap> RepresentationsOfObject(m);
[ "IsPositionalObjectRep", "Is8BitMatrixRep" ]
gap> m := [v,v,v,v];; ConvertToMatrixRep(m,GF(3));; m;
< mutable compressed matrix 4x8 over GF(3) >
\endexample

All compressed matrices over GF(2) are viewed as `\<a <n>x<m> matrix
over GF2>', while over fields GF(q) for q between 3 and 256, matrices
with 25 or more entries are viewed in this way, and smaller ones as
lists of lists.

Matrices can be converted to this special representation via
the following functions.

\>ImmutableMatrix( <field>, <matrix>, [<change>] ) F

returns an immutable matrix equal to <matrix> which is in the most
compact representation possible over <field>.
The input matrix <matrix> or
its rows might change the representation,
however the result of `ImmutableMatrix' is not necessarily
*identical* to <matrix> if a conversion is not possible.
If <change> is `true', the rows of `matrix' (or `matrix' itself) may be
changed to become immutable (otherwise they are copied first).


\>ConvertToMatrixRep( <list> ) F
\>ConvertToMatrixRep( <list>, <field> ) F
\>ConvertToMatrixRep( <list>, <fieldsize> ) F
\>ConvertToMatrixRepNC( <list> ) F
\>ConvertToMatrixRepNC( <list>, <field> ) F
\>ConvertToMatrixRepNC( <list>, <fieldsize> ) F

`ConvertToMatrixRep( <list> )' converts <list> to an internal
matrix representation if possible.  `ConvertToMatrixRep( <list> ,
<field> )' converts <list> to an internal matrix representation
appropriate for a matrix over <field>.

It is forbidden to call
this function unless all elements of <list> are vectors with
entries in  <field>.
Violation of this condition can lead to
unpredictable behaviour or a system crash. (Setting the assertion level
to at least 2 might catch some violations before a crash,
see~"SetAssertionLevel".)

Instead of a <field> also its size <fieldsize> may be given.

<list> may already be a compressed matrix. In this case, if no
<field> or <fieldsize> is given, then nothing happens.

<list> itself may be mutable, but its entries must be immutable.

The return value is the size of the field over which the matrix
ends up written, if it is written in a compressed representation.

In general, it is better to call `ImmutableMatrix'
(see~"ImmutableMatrix") instead since this function can also deal with
mutable rows or rows locked in a wrong representation.




Note that the main advantage of this special representation of
matrices is in low dimensions, where various overheads can be
reduced. In higher dimensions, a list of compressed vectors will be
almost as fast. Note also that list access and assignment will be
somewhat slower for compressed matrices than for plain lists.

In order to form a row of a compressed matrix a vector must accept
certain restrictions. Specifically, it cannot change its length or
change the field over which it is compressed. The main consequences of
this are: that only elements of the appropriate field can be assigned
to entries of the vector, and only to positions between 1 and the
original length; that the vector cannot be shared between two matrices
compressed over different fields.

This is enforced by the filter `IsLockedRepresentationVector'.  When a
vector becomes part of a compressed matrix, this filter is set for it.
Assignment, `Unbind',  `ConvertToVectorRep' and `ConvertToMatrixRep'
are all prevented from altering a vector with this filter.

\beginexample
gap> v := [Z(2),Z(2)];; ConvertToVectorRep(v,GF(2));; v;
<a GF2 vector of length 2>
gap> m := [v,v]; 
[ <a GF2 vector of length 2>, <a GF2 vector of length 2> ]
gap> ConvertToMatrixRep(m,GF(2)); 
2
gap> m2 := [m[1], [Z(4),Z(4)]]; # now try and mix in some GF(4)
[ <a GF2 vector of length 2>, [ Z(2^2), Z(2^2) ] ]
gap> ConvertToMatrixRep(m2); # but m2[1] is locked
#I  ConvertToVectorRep: locked vector not converted to different field
fail
gap> m2 := [ShallowCopy(m[1]), [Z(4),Z(4)]]; # a fresh copy of row 1
[ <a GF2 vector of length 2>, [ Z(2^2), Z(2^2) ] ]
gap> ConvertToMatrixRep(m2); # now it works
4
gap> m2;
[ [ Z(2)^0, Z(2)^0 ], [ Z(2^2), Z(2^2) ] ]
gap> RepresentationsOfObject(m2);
[ "IsPositionalObjectRep", "Is8BitMatrixRep" ]
\endexample

Arithmetic operations (see~"Arithmetic for Lists" and the following
sections) preserve the compression status of matrices in the sense that
if all arguments are compressed matrices written over the same field and
the result is a matrix then also the result is a compressed matrix
written over this field.

There are also two operations that are only available for matrices
written over finite fields.

\>ProjectiveOrder( <mat> ) A

Returns an integer n and a finite field element e such that <A>^n = eI.
<mat> must be a matrix defined over a finite field.


\beginexample
gap> ProjectiveOrder([[1,4],[5,2]]*Z(11)^0);
[ 5, Z(11)^5 ]
\endexample

\>SimultaneousEigenvalues( <matlist>, <expo> ) F

The matrices in  <matlist>  must  be matrices over GF(<q>) for some
prime <q>.  Together, they must  generate an  abelian p-group  of
exponent <expo>.
Then the eigenvalues of <mat>  in the splitting field `GF(<q>^<r>)' for
some <r> are powers of an element $\xi$ in the splitting field, which is
of order  <expo>.  `SimultaneousEigenvalues' returns a matrix of
integers  mod <expo>, say $(a_{i,j})$,  such that the power
$\xi^{a_{i,j}}$ is an eigenvalue of the <i>-th matrix in  <matlist> and
the eigenspaces of the different matrices to the eigenvalues
$\xi^{a_{i,j}}$ for fixed <j> are equal.





Finally, there are two operations that deal with matrices over a ring,
but only care about the residues of their entries modulo some ring element.
In the case of the integers and a prime number $p$, say,
this is effectively computation in a matrix over the prime field
in characteristic $p$.

\>InverseMatMod( <mat>, <obj> ) O

For a square matrix <mat>, `InverseMatMod' returns a matrix <inv>
such that `<inv> * <mat>' is congruent to the identity matrix modulo
<obj>, if such a matrix exists, and `fail' otherwise.


\beginexample
gap> mat:= [ [ 1, 2 ], [ 3, 4 ] ];;  inv:= InverseMatMod( mat, 5 );
[ [ 3, 1 ], [ 4, 2 ] ]
gap> mat * inv;
[ [ 11, 5 ], [ 25, 11 ] ]
\endexample

\>NullspaceModQ( <E>, <q> ) F

<E> must be a matrix of integers and <q> a prime power.
Then `NullspaceModQ' returns the set of all vectors of integers modulo
<q>, which solve the homogeneous equation system given by <E> modulo <q>.


\beginexample
gap> mat:= [ [ 1, 3 ], [ 1, 2 ], [ 1, 1 ] ];;  NullspaceModQ( mat, 5 );
[ [ 0, 0, 0 ], [ 1, 3, 1 ], [ 2, 1, 2 ], [ 4, 2, 4 ], [ 3, 4, 3 ] ]
\endexample

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Special Multiplication Algorithms for Matrices over GF(2)}

When multiplying two compressed matrices $M$ and $N$ over GF(2) of dimensions
$a\times b$ and $b\times c$, say, where $a$, $b$ and $c$ are all
greater than or equal to 128, \GAP\ by default uses a more
sophisticated matrix multiplication algorithm, in which linear
combinations of groups of 8 rows of $M$ are remembered and re-used in
constructing various rows of the product. This is called level 8
grease. To optimise memory access patterns, these combinations are
stored for $(b+255)/256$ sets of 8 rows at once. This number is called
the blocking level.

These levels of grease and blocking are found experimentally to give
good performance across a range of processors and matrix sizes, but
other levels may do even better in some cases. You can control the
levels exactly using the functions below:

\>PROD_GF2MAT_GF2MAT_SIMPLE( <m1>, <m2> ) F

This function performs the standard unblocked and ungreased matrix
multiplication for matrices of any size.

\>PROD_GF2MAT_GF2MAT_ADVANCED( <m1>, <m2>, <g>, <b> ) F

This function computes the product of <m1> and <m2>, which must be
compressed matrices over GF(2) of compatible dimensions, using level <g>
grease and level <b> blocking.

We plan to include greased blocked matrix multiplication for other
finite fields, and greased blocked algorithms for inversion and other
matrix operations in a future release.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Block Matrices}

Block matrices are a special representation of matrices which can save a
lot of memory if large matrices have a block structure with lots of zero
blocks. {\GAP} uses the representation `IsBlockMatrixRep' to store block
matrices.
\indextt{IsBlockMatrixRep}


\>AsBlockMatrix( <m>, <nrb>, <ncb> ) F

returns a block matrix with <nrb> row blocks and <ncb> column blocks
which is equal to the ordinary matrix <m>.


\>BlockMatrix( <blocks>, <nrb>, <ncb> ) F
\>BlockMatrix( <blocks>, <nrb>, <ncb>, <rpb>, <cpb>, <zero> ) F

`BlockMatrix' returns an immutable matrix in the sparse representation
`IsBlockMatrixRep'.
The nonzero blocks are described by the list <blocks> of triples,
the matrix has <nrb> row blocks and <ncb> column blocks.

If <blocks> is empty (i.e., if the matrix is a zero matrix) then
the dimensions of the blocks must be entered as <rpb> and <cpb>,
and the zero element as <zero>.

Note that all blocks must be ordinary matrices (see~"IsOrdinaryMatrix"),
and also the block matrix is an ordinary matrix.


\beginexample
gap> M := BlockMatrix([[1,1,[[1, 2],[ 3, 4]]],
>                      [1,2,[[9,10],[11,12]]],
>                      [2,2,[[5, 6],[ 7, 8]]]],2,2);
<block matrix of dimensions (2*2)x(2*2)>
gap> Display(M);
[ [   1,   2,   9,  10 ],
  [   3,   4,  11,  12 ],
  [   0,   0,   5,   6 ],
  [   0,   0,   7,   8 ] ]
\endexample

\>MatrixByBlockMatrix( <blockmat> ) A

returns a plain ordinary matrix that is equal to the block matrix
<blockmat>.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%E