This file is indexed.

/usr/share/gap/lib/meatauto.gi is in gap-libs 4r6p5-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
#############################################################################
##
#W  meatauto.gi                  GAP Library                    Michael Smith
#W                                                           Alexander Hulpke
##
##
#Y  Copyright 1996 -- School of Mathematical Sciences, ANU   
#Y  Copyright (C)  2004,  The GAP Group
##
##  This file contains meataxe type routines to compute module homomorphisms
##  for modules that are not necessarily irreducible. They are mainly a
##  conversion of the module routines in the GAP3 package `autag' by the
##  first author.
##

InstallGlobalFunction(TestModulesFitTogether,function(m1,m2);
  if m1.field<>m2.field then
    Error("different fields");
  fi;
  if Length(m1.generators)<>Length(m2.generators) then
    Error("generators are different lengths");
  fi;
end);

# basis for homomorphism space, efficient only for small dimensions
BindGlobal("SmalldimHomomorphismsModules",function(m1,m2)
local f, d1, d2, e, z, g1, g2, r, b, n, a, gp, i, j, k;
  f:=m1.field;
  d1:=m1.dimension;
  d2:=m2.dimension;
  e:=[];
  z:=ListWithIdenticalEntries(d1*d2,Zero(f));
  ConvertToVectorRep(z,f);
  for gp in [1..Length(m1.generators)] do
    g1:=m1.generators[gp];
    g2:=m2.generators[gp];
    for i in [1..d1] do
      for j in [1..d2] do
	# calculate equation for i-th row, j-th column
        r:=ShallowCopy(z);
	# the entry in g*hom is the i-th row of g with the variables in the
	# j-th column
	for k in [1..d1] do
	  b:=(k-1)*d2+j;
	  r[b]:=r[b]+g1[i][k];
        od;
	# the entry in hom*g is the variables in the i-th row of hom with the
	# j-th column of g
	for k in [1..d2] do
	  b:=(i-1)*d2+k;
	  r[b]:=r[b]-g2[k][j];
	od;
	Add(e,r);
      od;
    od;
  od;
  n:=NullspaceMat(TransposedMat(e));
  b:=[];
  for i in n do
    # convert back to d1 x d2 matrix
    a:=[];
    for j in [1..d1] do
      Add(a,i{[(j-1)*d2+1..(j-1)*d2+d2]});
    od;
    a:=ImmutableMatrix(f,a);
    Add(b,a);
  od;
  return b;
end);


# the following code is essentially due to Michael Smith

# These routines are designed to accumulate a system of linear equations
#
#    M_1 X = V_1,  M_2 X = V_2 ...  M_t X = V_t
#
# Where each M_i is an m_i*n matrix, X is the unknown length n vector, and
# each V is an length m_i vector.  The equations can be added as each batch
# is calculated. Here is some pseudo-code to demonstrate:
#
#   eqns := newEqns (n, field);
#   i := 1;
#   repeat
#     <calculate M_i and V_i>
#     addEqns(M_i, V_i)
#     increment i;
#   until  i > t  or  eqns.failed;
#   if not eqns.failed then
#     S := solveEqns(eqns);
#   fi;
#
# As demonstrated by the example, an early notification of failure is
# available by checking ".failed".  All new equations are sifted with respect
# to the current set, and only added if they are independent of the current
# set. If a new equation reduces to the zero row and a nonzero vector
# entry, then there is no solution and this is immediately returned by
# setting eqns.failed to true.  The function solveEqns has an already
# triangulised system of equations, so it simply reduces above the pivots
# and returns the solution vector.


BindGlobal("SMTX_AddEqns",function ( eqns, newmat, newvec)
local n, zero, weights, mat, vec, NextPositionProperty, ReduceRow, t,
      newweight, newrow, newrhs, i, l, k;

# Add a bunch of equations to the system of equations in <eqns>.  Each
# row of <newmat> is the left-hand side of a new equation, and the
# corresponding row of <newvec> the right-hand side. Each equation in
# filtered against the current echelonised system stored in <eqns> and
# then added if it is independent of the system.  As soon as a
# left-hand side reduces to 0 with a non-zero right-hand side, the flag
# <eqns.failed> is set.

  Info(InfoMtxHom,6,"addEqns: entering" );

  n := eqns.dim;
  zero := Zero(eqns.field);
  weights := eqns.weights;
  mat := eqns.mat;
  vec := eqns.vec;

  NextPositionProperty := function (list, func, start )
  local   i;
    for i  in [ start .. Length( list ) ]  do
      if func( list[ i ] )  then
	return i;
      fi;
    od;
    return fail;
  end;

  # reduce the (lhs,rhs) against the semi-echelonised current matrix,
  # and return either: (1) the reduced rhs if the lhs reduces to zero,
  # or (2) a list containing the new echelon weight, the new row and
  # the new rhs for the system, and the row number that this
  # equation should placed.
  ReduceRow := function (lhs, rhs)
  local lead, i, z;
    lead := PositionProperty(lhs, i->i<>zero);
    if lead = fail then
      return rhs;
    fi;
    for i in [1..Length(weights)] do
      if weights[i] = lead then
	z := lhs[lead];
	lhs := lhs - z * mat[i]; rhs := rhs - z * vec[i];
	lead := NextPositionProperty(lhs, i->i<>zero, lead);
	if lead = fail then
	  return rhs;
	fi;
      elif weights[i] > lead then
	return [lead, lhs, rhs, i];
      fi;
    od;
    return [lead, lhs, rhs, Length(weights)+1];
  end;

  for k in [1..Length(newmat)] do
    t := ReduceRow(newmat[k], newvec[k]);

    if IsList(t) then
      # new equation
      newweight := t[1];
      newrow := t[2];
      newrhs := t[3];
      i := t[4]; # position for new row

      # normalise so that leading entry is 1
      newrhs := newrhs / newrow[newweight]; 
      newrow := newrow / newrow[newweight]; # NB: in this order

      if i = Length(mat)+1 then
	# add new equation to end of list
	Add(mat, newrow);
	Add(vec, newrhs);
	Add(weights, newweight);
      else
	l := Length(mat);
	# move down other rows to make space for this new one...
	mat{[i+1..l+1]} := mat{[i..l]};  
	vec{[i+1..l+1]} := vec{[i..l]};
	# and then slot it in
	mat[i] := newrow;
	vec[i] := newrhs;
	weights{[i+1..l+1]} := weights{[i..l]};
	weights[i] := newweight;
      fi;

    else
      # no new equation, check whether inconsistent due to
      # nonzero rhs reduction
      
      if not IsZero(t) then
	Info(InfoMtxHom,6,"addEqns: FAIL!" );
	eqns.failed := true;
	return eqns; # return immediately
      fi;
    fi;
  od;
end);

BindGlobal("SMTX_NewEqns",function (arg)
local X, n, F, V, eqns;

  if Length(arg) <2 then
    Error("NewEqns(dim, field) or NewEqns(X, V)");
  fi;

  if IsInt(arg[1]) then
    X := false;
    n := arg[1];
    F := arg[2];
  else
    X := arg[1];
    V := arg[2];
    n := Length(X[1]);
    F := Field(X[1][1]); # Note: prime field only
  fi;

  eqns := rec();
  eqns.dim := n;              # number of variables
  eqns.field := F;            # field over which the equation hold
  eqns.mat := [];             # left-hand sides of system
  eqns.weights := [];         # echelon weights for lhs matrix
  eqns.vec := [];             # right-hand sides of system
  eqns.failed := false;         # flag to indicate inconsistent system
  eqns.index := [];           # index for row ordering

  if IsMatrix(X) then
    SMTX_AddEqns(eqns, X, V);
  fi;

  return eqns;
end);

BindGlobal("SMTX_KillAbovePivotsEqns",function (eqns)
# Eliminate entries above pivots. Note that the pivot entries are
# all 1 courtesy of SMTX_AddEqns.

local m, n, zero, i, c, j, factor;

  Info(InfoMtxHom,6,"killAbovePivotsEqns: entering" );
  m := Length(eqns.mat);
  n := eqns.dim;
  if m > 0 then
    zero := Zero(eqns.field);
    for i in [1..m] do
      c := eqns.weights[i];
      for j in [1..i-1] do
	if eqns.mat[j][c] <> zero then
	  Info(InfoMtxHom,6,"solveEqns: kill mat[",j,",",c,"]");
	  factor := eqns.mat[j][c];
	  eqns.mat[j] := eqns.mat[j] - factor*eqns.mat[i];
	  eqns.vec[j] := eqns.vec[j] - factor*eqns.vec[i];
	fi;
      od;
    od;
  fi;
  Info(InfoMtxHom,6,"killAbovePivotsEqns: leaving" );
end);

BindGlobal("SMTX_NullspaceEqns",function(e)

# Take the matrix stored in equation record <e> and compute a basis
# for its nullspace, ie  x  such that  mat * x = 0.  Note that the
# vector is on the other side of the matrix from GAP's NullspaceMat.
# This means we get to skip the Transposing that occurs at the top
# of that function (a bonus!).
#
# This function is a modified version NullspaceMat in matrix.g

local mat, m, n, zero, one, empty, i, k, nullspace, row,mi;

  SMTX_KillAbovePivotsEqns(e);
  mat := e.mat;

  m := Length(mat);
  n := e.dim;
  zero := Zero(e.field);
  one  := One(e.field);

  # insert empty rows to bring the leading term of each row on the diagonal
  empty := ListWithIdenticalEntries(n,zero);
  ConvertToVectorRep(empty,e.field);
  i := 1;
  while i <= Length(mat)  do
    if i < n  and mat[i][i] = zero  then
      mi:=Minimum(Length(mat),n-1);
      for k in [mi,mi-1..i]  do
	mat[k+1] := mat[k];
      od;
      mat[i] := empty;
    fi;
    i := i+1;
  od;
  for i  in [ Length(mat)+1 .. n ]  do
    mat[i] := empty;
  od;

  # The following comment from NullspaceMat:
  # 'mat' now  looks  like  [ [1,2,0,2], [0,0,0,0], [0,0,1,3], [0,0,0,0] ],
  # and the solutions can be read in those columns with a 0 on the diagonal
  # by replacing this 0 by a -1, in  this  example  [2,-1,0,0], [2,0,3,-1].
  nullspace := [];
  for k in [1..n] do
    if mat[k][k] = zero  then
      row := [];
      for i  in [1..k-1]  do row[i] := -mat[i][k];  od;
      row[k] := one;
      for i  in [k+1..n]  do row[i] := zero;  od;
      Add( nullspace, row );
    fi;
  od;

  return nullspace;
end);


BindGlobal("EchResidueCoeffs",function (base, ech, v,mode)
local n, coeffs, x, zero, z, i;
# 
# Take a semi-ech basis <base>, with ech weights <ech>, and a vector
# <v> in the subspace spanned by <base>. Returns:
# if mode>2:
# a record containing the
# residue after removing projection of <v> onto subspace spanned by
# <base>, as well as the coefficients of the linear combination of
# <base> elements used to obtain the projection. Also return the
# projection.
# if mode =1 returns only the coefficients
# if mode=2 returns only the residue

# Note that the pivots of <base> must be set to 1.

  n:=Length(base);

  if n = 0 then
    coeffs:=[];
    x:=v;
  else
    x:=v;
    zero:=x[1]*0;
    coeffs:=ListWithIdenticalEntries(n, zero);
    for i in [1..n] do
      z:=x[ech[i]];
      if z <> zero then
	x:=x - z * base[i];
	coeffs[i]:=z;
      fi;
    od;
  fi;

  if mode=1 then
    return coeffs;
  elif mode=2 then
    return x;
  else
    return rec(coeffs:=coeffs,
	      residue:=x,
	      projection:=v - x
	      );
  fi;
end);

BindGlobal("SpinSpaceVector",function (V, U, ech, v,zero)
local gens, pos, settled, oldlen, i, j;

# Take <U> a semi-ech basis for a submodule of <V>, with ech-weights
# <ech>, and a vector <v> in <V>. Return a semi-ech basis for the
# submodule generated by <U> and <v>.
  
  U:=ShallowCopy(U);
  ech:=ShallowCopy(ech);
  gens:=V.generators;

  v:=EchResidueCoeffs(U, ech, v,2);
  pos:=PositionProperty(v, i->i<>zero);
  if pos = fail then
    return U;
  fi;
  Add(U, v/v[pos]); Add(ech, pos);

  settled:=Maximum(Length(U),1); # <U> is a submodule
  repeat
    oldlen:=Length(U);
    for i in [settled+1..Length(U)] do
      for j in [1..Length(gens)] do
	v:=EchResidueCoeffs(U, ech, (U[i] * gens[j]),2);
	pos:=PositionProperty(v, i->i<>zero);
	if pos <> fail then
	  Add(U, v/v[pos]); Add(ech, pos);
	fi;
      od;
    od;
    settled:=oldlen;
  until oldlen = Length(U);
  return U;
end);

BindGlobal("SpinHomFindVector",function (r)
local V, nv, W, nw, U, echu, F, matsV, matsW, k, g1, g2, max_stack_len, _t,
      newstack, v0, extradim, N, count, look_lim, done, grpalg, i, M, pos, A,
      j,zero;

# <r> contains information about modules <V> and <W>, and a submodule
# <U> of <V> with semi-ech information <echu>. The routine selects
# an element of <V> lying outside of <U> that will be used to spin
# up to a new submodule U'.
#
# It returns a list [<v0>, <M>] where <v0> is the element of <V>
# and <M> is a basis for a submodule of <W> which <v0> must map into
# under any hom.

  V:=r.V;    nv:=V.dimension;
  W:=r.W;    nw:=W.dimension;
  U:=r.U;    echu:=r.echu;
  F:=V.field;
  zero:=Zero(F);
  
  if not IsBound(r.mats) then
    matsV:=V.generators;
    matsW:=W.generators;
    k:=Length(matsV);
    r.mats:=List([1..k], i -> [matsV[i], matsW[i]]);

    # do preprocessing to make random matrices list in parallel

    for i in [1..10] do
      g1:=Random([1..k]);
      g2:=g1;
      while g2 = g1 and Length(r.mats)>1 do
	g2:=Random([1..k]);
      od;
      Add(r.mats,[r.mats[g1][1]*r.mats[g2][1], 
		  r.mats[g1][2]*r.mats[g2][2]]);
      k:=k + 1;
    od;

    r.zero:=[ ImmutableMatrix(F,NullMat(nv,nv,F)), 
	      ImmutableMatrix(F,NullMat(nw,nw,F)) ];

    # we build a stack of good grpalg elements to use for choosing
    # elements <v0> --- an element <A> in <stack> is of the form:
    #   A[1] = v0
    #   A[2] = grpalg element whose nullspace contains v0
    #   A[3] = Dim(<U,v0>^G)-Dim(U) i.e. increase in dim by adding 
    #          <v0> to <U>
    r.stack:=[];

  else 
    k:=Length(r.mats);
  fi;

  max_stack_len:=10;

  # adjust the elements of the stack to account for the larger
  # submodule <U> we now have
  _t:=Runtime();
  newstack:=[];
  for A in r.stack do
    v0:=A[1];
    extradim:=Length(SpinSpaceVector(V, U, echu, v0,zero))
		- Length(U);
    if extradim > 0 then
      Add(newstack, [v0, A[2], extradim]);
    fi;
  od;
  r.stack:=newstack;
  Info(InfoMtxHom,2,"stack reduced to length ", Length(r.stack), " (",
	  Runtime()-_t, ")");

  # <N> contains the nullspace in <V> of a group algebra element ---
  # initialise it to the empty list for the following repeat loop
  N:=[];

  count:=0;
  look_lim:=5;  # give up after this many random grpalg elements

  _t:=Runtime();

  if Length(r.stack) > 0 then
    # if we have something left, don't bother generating any new
    # grpalg elements (?)
    count:=look_lim + 1;
  fi;

  done:=false; 
  while count < look_lim and Length(r.stack) < max_stack_len and not done do

    # we look for a while and take the best element found

    # We are looking for an element <v0> of a nullspace that lies
    # outside of <U>

    repeat
      # Take a work record <r> containing the information about the two
      # modules <V> and <W>, and return a random group algebra element
      # record containing its action on each of the modules.

      # first take two elements of the list and multiply them
      # together
      g1:=Random([1..k]);
      repeat
	g2:=Random([1..k]);
      until g2 <> g1 or Length(r.mats)=1;
      Add(r.mats,[r.mats[g1][1]*r.mats[g2][1], 
		  r.mats[g1][2]*r.mats[g2][2]]);
      k:=k + 1;

      # Now take a random linear sum of the existing generators as new
      # generator.  Record the sum in coefflist

      grpalg:=ShallowCopy(r.zero);

      for g1 in [1..k] do
	g2:=Random(F);
	if not IsZero(g2) then
	  grpalg[1]:=grpalg[1] + g2*r.mats[g1][1];
	  grpalg[2]:=grpalg[2] + g2*r.mats[g1][2];
	fi;
      od;
      N:=TriangulizedNullspaceMat(grpalg[1]);
      count:=count + 1;
    until Length(N) > 0 or count >= look_lim;

    if Length(N) > 0 then

      # now find best element of <N> for adding to <stack>
      extradim:=List(N, y ->
			Length(SpinSpaceVector(V, U, echu, y,zero)) 
			- Length(U));
      i:=1;
      for j in [2..Length(extradim)] do
	if extradim[j] > extradim[i] then
	  i:=j;
	fi;
      od;
      if extradim[i] > 0 then
	# exit early if we have found an element that gets use all
	# of <V> after spinning
	done:=extradim[i] = nv - Length(U);
	if done then
	  r.stack:=[[N[i], grpalg, extradim[i]]];
	else
	  Add(r.stack, [N[i], grpalg, extradim[i]]);
	fi;
      fi;
    fi;

  od;
  Info(InfoMtxHom,2,"stack loop done, stack now length ", Length(r.stack), " (",
	  Runtime()-_t, ")");

  if Length(r.stack) > 0 then
    #
    # find best element in r.stack and use it
    i:=1;
    for j in [2..Length(r.stack)] do
      if r.stack[j][3] > r.stack[i][3] then
	i:=j;
      fi;
    od;
    v0:=r.stack[i][1];
    M:=TriangulizedNullspaceMat(r.stack[i][2][2]);

  else

    # we haven't found a good grpalg element, so just choose
    # something outside of <U> and use it

    Info(InfoMtxHom,1,"too many random grpalg elements...");
    M:=IdentityMat(nw,F);
    pos:=Difference([1..nv], echu)[1];
    v0:=ListWithIdenticalEntries(nv,zero);
    ConvertToVectorRep(v0,F);
    v0[pos]:=One(F);
  fi;
  return [v0, M];

end);

# compute a semi-echelonised basis for a matrix algebra
# If a linearly dependent set of elements is supplied, this
# routine will trim it down to a basis.
BindGlobal("SMTX_EcheloniseMats",function (gens, F)
local n, m, zero, ech, k, i, j, found, l;
  
  if Length(gens) = 0 then
    return [ [], [] ];
  fi;
  # copy the list to avoid destroying the original list
  gens:=List(gens,i->List(i,ShallowCopy));

  n:=Length(gens[1]);
  m:=Length(gens[1][1]);
  zero:=Zero(F);

  ech:=[];
  k:=1;

  while k <= Length(gens) do
    i:=1; j:=1;
    found:=false;
    while not found and i <= n do
      if (gens[k][i][j] <> zero) then
	found:=true;
      else
	j:=j + 1;
	if (j > m) then
	  j:=1; i:=i + 1;
	fi;
      fi;
    od;

    if found then

      # Now basis element k will have echelonisation index [i,j]
      Add(ech, [i,j]);

      # First normalise the [i,j] position to 1
      gens[k]:=gens[k] / gens[k][i][j];

      # Now zero position [i,j] in all further generators
      for l in [k+1..Length(gens)] do
	if (gens[l][i][j] <> zero) then
	  gens[l]:=gens[l] - gens[k] * gens[l][i][j];
	fi;
      od;
      k:=k + 1;
    else
      # no non-zero element found, delete from list
      gens{[k..Length(gens)-1]}:=gens{[k+1..Length(gens)]};
      Unbind(gens[Length(gens)]);
      # WAS: gens:=gens{ Cat([1..k-1], [k+1..Length(gens)])};
    fi;
  od;
  return [List(gens,i->ImmutableMatrix(F,i)), ech];
end);

# The SpinHom routine in this file was written during August 1996. The
# basic idea comes from a discussion I had with Charles Leedham-Green early
# in 1995. He gave me a rough sketch of the algorithm that he and John
# Cannon developed for Magma. Some details were missing, and this is my
# attempt at filling in some of them. 
#
# Many improvements were made on my earlier version, in large part due to a
# discussion I had with Alice Niemeyer in early 1996. She relayed to me
# some comments of Klaus Lux on my earlier version. This is a combination
# of the suggestions of Klaus and Alice and my own ideas.
#
# Note: This provides an enormous speed-up on the the default GAP routine,
# and on my own naive intertwining routine, especially when the module is
# large enough and/or it is irreducible. However, this routine is nowhere
# near as good as the Magma algorithm, and I do not know how to improve it.
#
# The code is heavily commented, and I appreciate suggestions on how to
# improve it (particularly bits of code).
BindGlobal("SpinHom",function (V, W)
local nv, nw, F, zero, zeroW, gV, gW, k, U, echu, r, homs, s, work, ans, v0,
      M, x, pos, z, echm, t, v, echv, a, u, e, start, oldlen, ag, m, uu, ret,
      c, s1, X, mat, uuc, uic, newhoms, hom, Uhom, imv0, imv0c, image, i, j, l;

# Compute Hom(V,W) for G-modules <V> and <W>. The algorithm starts with
# the trivial submodule <U> of <V> for which Hom(U,V) is trivial.  It
# then computes Hom(U',W) for U' a submodule generated by <U> and a
# single element <v0> in <V>. This U' becomes the next <U> as the process
# is iterated, ending when <U'> = <V>. The element <v0> is chosen in a
# nullspace of a group algebra element in order to restrict it possible
# images in <W>.

  nv:=V.dimension;
  nw:=W.dimension;
  
  F:=V.field;
  if F<>W.field then
    Error("different fields");
  fi;
  zero:=Zero(F);

  zeroW:=ListWithIdenticalEntries(nw,zero);
  ConvertToVectorRep(zeroW,F);

  # group generating sets acting on each module
  gV:=V.generators;
  gW:=W.generators;

  # <k> is the number of generators of the acting group
  k:=Length(gV);
  if k<>Length(gW) then
    Error("generator lengths");
  fi;

  # <U> is the semi-ech basis for the currently known submodule, of
  # dimension <r>
  U:=[];
  echu:=[];
  r:=0;

  # <homs> contains a basis for Hom(U,W), of dimension <s>
  homs:=[];
  s:=0;

  # define a record which stores information about the modules <V>, <W>
  # and <U> for passing into a routine that selects a new vector <v0>
  # for spinning up to a larger submodule U'.
  work:=rec(V:=V, W:=W, U:=U, echu:=echu);

  repeat

    # we loop until <U> is the whole of <V>

    ans:=SpinHomFindVector(work);
    v0:=ans[1];
    M:=ans[2];

    # find residue of <v0> modulo current submodule <U>
    x:=EchResidueCoeffs(U, echu, v0,2);

    # normalise <x> (ie get a 1 in leading position)
    pos:=PositionProperty(x, i->i<>zero);
    z:=x[pos];
    x:=x / z; 
    v0:=v0 / z;

    # we know that <v0> has to map into the subspace <M> of <W>. 
    echm:=List(M, y -> PositionProperty(y, i->i<>zero));
    t:=Length(M);

    # now we start building extension of semi-echelonised basis for
    # the submodule U' generated by <U> and <v0>
    #
    # new elements of semi-ech basis will be stored in <v>, with
    # echelon weights stored in <echv>

    v:=[ x ];
    echv:=[ pos ];

    # we need to keep track of how each new element of the semi-ech
    # basis was obtained from <v0> --- new basis element <v[i]> will
    # satisfy:
    #
    #     v[i]  =  v0*a[i] + u[i]
    #
    # where <a[i]> is an element of the group algebra FG, and <u[i]> is
    # the element of <U> that was subtracted during semi-ech reduction

    a:=[ M ];
    u:=[ x - v0 ];

    # we will accumulate the homogeneous linear system in <e> 
    #
    # the first <s> variables are the coefficients of basis elements of
    # Hom(U,W), which describes how a hom of U' acts on submodule <U>
    #
    # the other <t> variables are the coefficients of basis elements of
    # <M>, which describes the image of <v0> under a hom
    #
    e:=SMTX_NewEqns(s + t, F);

    # we will close the submodule by spinning <v0> --- the variable
    # <start> will trim off the elements of <v> that we have already
    # used
    start:=1;

    repeat

      # take an element <v[i]> of <v> and a group generator <g[j]>
      # and check whether <v[i]^g[j]> is a new basis element.
      #
      # if it is, add it to the basis, with its definition.
      #
      # if it isn't, we get an equation which an element of Hom(U',W)
      # must satisfy

      oldlen:=Length(v);

      for i in [start..oldlen] do     ### loop on vectors in <v>
	for j in [1..k] do          ### loop on generators of G

	  if Length(a[i])=0 then
	    #T: special treatment 0-dimensional
	    ag:=[];
	  else
	    ag:=a[i] * gW[j];
	  fi;

	  # create new element <x>, with its definition as the
	  # difference between <v0^m> and <uu> in <U>.
	  x:=v[i] * gV[j];
	  m:=ag;
	  uu:=u[i] * gV[j];

	  ret:=EchResidueCoeffs(U, echu, x,3);
	  x:=ret.residue;
	  uu:=uu - ret.projection;

	  # reduce modulo the new semi-ech basis elements in <v>,
	  # storing the coefficients in <c>
	  #
	  c:=ListWithIdenticalEntries(Length(v),zero);
	  ConvertToVectorRep(c,F);
	  for l in [1..Length(v)] do
	    z:=x[echv[l]];
	    if z <> zero then
	      x:=x - z * v[l];
	      if Length(m) > 0 then
		  m:=m - z * a[l];
	      fi;
	      c[l]:=c[l] + z;
	      uu:=uu - z * u[l];
	    fi;
	  od;

	  # Note: at this point, <x> has been reduced modulo the
	  # semi-ech basis <U> union <v>, and that
	  # 
	  #     x = v0 * a[i] + uu

	  pos:=PositionProperty(x, i->i<>zero);
	  if pos <> fail then

	    # new semi-ech basis element <x>

	    z:=x[pos];
	    Add(v, x/z);
	    Add(echv, pos);
	    Add(a, m/z);
	    Add(u, uu/z);

	  else

	    # we get some equations !

	    s1:=Sum([1..Length(v)], y -> c[y] * v[y]);
	    uu:=v[i] * gV[j] - s1;

	    X:=NullMat(t, nw, F);
	    for l in [1..Length(v)] do
	      if c[l] <> zero then
		if Length(X) > 0 then
		  X:=X + c[l] * a[l];
		fi;
		uu:=uu + c[l] * u[l];
	      fi;
	    od;

	    if Length(X) > 0 then
	      X:=X - ag;
	    fi;

	    mat:=[];
	    uuc:=EchResidueCoeffs(U, echu, uu,1);
	    uic:=EchResidueCoeffs(U, echu, u[i],1);
	    for l in [1..s] do
	      Add(mat, uuc * homs[l] - uic * homs[l] * gW[j]);
	    od;
	    Append(mat, X);
	    SMTX_AddEqns(e, TransposedMat(mat), zeroW);
	  fi;
	od;
      od;

      start:=oldlen+1;

      # exit when no new elements were added --- i.e. the subspace
      # is closed under action of G and is therefore a submodule

    until oldlen = Length(v); 

    # we have the system of equations, so find its solution space

    ans:=SMTX_NullspaceEqns(e);

    # Now build the homomorphisms

    newhoms:=[];
    for i in [1..Length(ans)] do

      # Each row of ans is of the form:
      #
      #     [ b_1, b_2, ..., b_s, c_1, c_2, ..., c_t ]
      #
      # where the action of this hom on <U> is as \Sum{b_l homs[l]}
      # and the hom sends <v0> to Sum{c_l M[l]}

      hom:=[];
      if r > 0 then
	Uhom:=NullMat(r, nw, F);
	for l in [1..s] do
	  if ans[i][l] <> zero then
	    Uhom:=Uhom + ans[i][l] * homs[l];
	  fi;
	od;
	for l in [1..r] do
	  Add(hom, Uhom[l]);
	od;
      fi;

      imv0:=zeroW * zero;
      for l in [1..t] do
	if ans[i][s+l] <> zero then
	  imv0:=imv0 + ans[i][s+l] * M[l];
	fi;
      od;
      imv0c:=EchResidueCoeffs(M, echm, imv0,1);
      for l in [1..Length(v)] do
	image:=imv0c * a[l];
	if r > 0 then
	  image:=image + EchResidueCoeffs(U, echu, u[l],1) * Uhom;
	fi;
	Add(hom, image);
      od;
      hom:=ImmutableMatrix(F,hom);
      Assert(1,hom<>0*hom);
      Add(newhoms, hom);
    od;

    # now update <U> to be the now larger submodule

    Append(U,v);
    Append(echu, echv);
    homs:=newhoms;
    r:=Length(U);
    s:=Length(homs);

    Info(InfoMtxHom,1,"U is now dimension ", r, " and dim(Hom(U,W)) = ", s);

  until r = nv; # i.e. <U> = <V>

  if Length(homs)=0 then
    return homs;
  fi;

  # We must change basis on <V> from <U> to the usual one before returning

  U:=ImmutableMatrix(F,U);
  return U^-1 * homs;

end);


# module isomorphism and decomposition routines
#
# These are functions for computing with modules, including:
#
#   (1) computing a direct sum decomposition of a module into
#   indecomposable summands.
#
#   (2) deciding module isomorphism using the decomposition.
#
# The algorithm for deciding indecomposability is based on the algorithm
# described by G. Schneider in the Journal of Symbolic Computation, 
# Volume 9, Numbers 5 & 6, 1990


# Take a Fitting element and use it to split M into a direct sum
# of submodules. Return the submodules.
# r is the rank of a (which migth be known before
BindGlobal("FittingSplitModule",function (a,r,F)
local n, ro;

  # do we have a fitting matrix?
  # a matrix is a fitting matrix if it is singular but not nilpotent.
  # case
  n:=Length(a);
  if r=n or r=0 then
    # not singular or zero.
    return fail;
  fi;
  # now square repeatedly until the rank stays the same and >0
  repeat
    ro:=r;
    a:=a^2;
    r:=RankMat(a);
  until ro=r or r=0;
  if r=0 then
    return fail;
  fi;
  # otherwise a is a power of a fitting matrix, the space will split in
  # Kern(a) \oplus Image(a)

  Info(InfoMeatAxe,2,"Decomposition ",r,":",n-r," found");

  return [ImmutableMatrix(F,BaseMat(a)),NullspaceMat(a)];
end);

# Take a module and break it into two pieces if possible.
# The function searches for a decomposition of the module M while
# attempting to prove indecomposability at the same time.  Of course,
# only one of these will succeed.
BindGlobal("ProperModuleDecomp",function (M)
local proveIndecomposability, addnilpotent, n, F, zero, basis, enddim, 
      echelon, nildim, p, maxorder, maxa, nilbase, nilech, cnt, remain,
      used, coeffs, a, rk, order, fit, pos, newa, lastdim, i;
  
  # Check whether we have found the indecomposability proof. That is,
  # see whether our regular element generates a subalgebra which
  # complements the current nilpotent ideal (the approximation to
  # radical)
  proveIndecomposability:=function ()
  local maxaord;
  # NB: <maxa> is not local

    if enddim - nildim = LogInt(maxorder + 1,p) then
      # Yes, found the residue field root and proved indecomposability!
      maxaord:=Order(maxa);
      while maxaord > maxorder do
	maxa:=maxa^p;
	maxaord:=maxaord / p;
      od;
      SMTX.SetEndAlgResidue(M, [maxa, maxaord]);
      Info(InfoMtxHom,3,"proved ",Length(nilbase));
      SMTX.SetBasisEndomorphismsRadical(M, nilbase);
      return true;
    fi;
    return false;
  end;

  # take a new nilpotent element and sift against current nilpotent
  # ideal basis. If it does not lie in the space spanned so far,
  # add it to nilbasis 
  addnilpotent:=function (a)
  local i, r, c, k, done, l;
  # NB: <remain> and <nildim> and <cnt> are not local

    for i in [1..nildim] do
      r:=echelon[nilech[i]][1]; c:=echelon[nilech[i]][2];
      if a[r][c] <> zero then
	a:=a - a[r][c] * nilbase[i] / nilbase[i][r][c];
      fi;
    od;

    # find which echelon index to remove due to this new element
    k:=1; done:=false;
    while not done and k <= Length(remain) do
      l:=remain[k];
      r:=echelon[l][1]; c:=echelon[l][2];
      if a[r][c] <> zero then
	done:=true;
      else
	k:=k + 1;
      fi;
    od;

    if k > Length(remain) then
      # in nilpotent ideal already, return
      return false;
    fi;
    
    # We now know this nilpotent element is a new one
    Add(nilbase, a);

    # the k-th basis element was used to make the new element a. So
    # remove it from future random element calculations
    #
    Add(nilech, remain[k]);
    remain:=Difference(remain, [remain[k]]);
    nildim:=nildim + 1;
    cnt:=1;
    return true;
  end;

  n:=M.dimension;
  F:=M.field;
  zero:=Zero(F);
  Info(InfoMtxHom,2,"ProperModuleDecomp for module of dimension ", n);

  if n = 1 then
    # A 1-dimensional module is always indecomposable
    Info(InfoMtxHom,3,"1dimensional");
    SMTX.SetEndAlgResidue(M, [[[ PrimitiveElement(F) ]], Size(F) - 1]);
    SMTX.SetBasisEndomorphismsRadical(M, []);
    return fail;
  fi;

  basis:=SMTX.BasisModuleEndomorphisms(M);
  if Length(basis) = 1 then
    # if endomorphism algebra has dimension 1 then indecomposable
    #SMTX.SetEndAlgResidueFlag(M, F.root * GModOps.EndAlgBasisFlag(M)[1], F.size - 1);
    SMTX.SetEndAlgResidue(M, [PrimitiveElement(F)*basis[1], Size(F) - 1]);
    Info(InfoMtxHom,3,"basislength 1");
    SMTX.SetBasisEndomorphismsRadical(M, []);
    return fail;
  fi;

  enddim:=Length(basis);            # dim of endo algebra
  echelon:=SMTX_EcheloniseMats(basis,F)[2]; # echelon indices for endalg basis
  nildim:=0;                        # dim of current approx to radical
  p:=Size(F);
  maxorder:=1;                      # order of largest order regular elmt
				      #   found so far
  maxa:=IdentityMat(n,F);           # the regular elmt with order maxorder
  nilbase:=[];                      # basis for approx to radical
  nilech:=[];
  
  cnt:=1;

  # We will "quotient" out the nilpotent subspace as we go. The elements
  # of remain tell us which (echelonised) basis elements of the
  # endomorphism algebra we will take use in our random linear
  # combination.
  #
  remain:=[1..enddim];
  used:=[];

  repeat
    # we will loop until too many passes without an improvement in knowledge
    repeat
      # randomly sample endomorphism algebra
      repeat
	coeffs:=List([1..enddim], x -> Random(F));
      until ForAny(remain,x->not IsZero(coeffs[x]));

      a:=LinearCombination(basis,coeffs);

      rk:=RankMat(a);
      if rk=n then
	# a regular element, check to see whether its order is
	# larger than previously known, and if so whether it
	# generates the residue field modulo current nilpotent ideal
	order:=Order(a);

	while (order mod p = 0) do
	    order:=order / p;
	od;
	if order > maxorder then
	  maxorder:=order;
	  maxa:=a;
	  if proveIndecomposability() then
	    return fail;
	  fi;
	  cnt:=1;
	else
	  cnt:=cnt + 1;
	fi;
      else 
	fit:=FittingSplitModule(a,rk,F);
	if fit<>fail then
	  return fit;
	elif addnilpotent(a) then
	  # new nilpotent element, added to nilbasis. Now close nilbasis to
	  # basis for an ideal.

	  # keep a pointer to the first new element added to nilbase
	  pos:=nildim; # a was just added

	  # first add powers of a
	  newa:=a^2;
	  repeat
	    lastdim:=nildim;
	    addnilpotent(newa);
	    newa:=newa * a;
	  until lastdim = nildim or IsZero(newa);

	  # now close nilbase to make ideal basis
	  repeat
	    for i in [1..enddim] do
	      a:=nilbase[pos] * basis[i];
	      fit:=FittingSplitModule(a,RankMat(a),F);
	      if fit <> fail then
		return fit;
	      fi;
	      addnilpotent(a);
	    od;
	    pos:=pos + 1;
	  until pos = nildim + 1;
	fi;
      fi;

      if proveIndecomposability() then
	return fail;
      fi;
    until (cnt >= 2000);
    Error("Unable to ascertain module decomposition within time limits.\n",
	  "Call `return;' to try again.");
  until false;
end);


BindGlobal("SMTX_Indecomposition",function(m)
local n, F, stack, i, d, d2, md, b, endo, sel, e1, e2;
  if not IsBound(m.indecomposition) then
    n:=m.dimension;
    F:=m.field;
    stack:=[[IdentityMat(n,F),m]];
    i:=1;
    while i<=Length(stack) do
      d:=ProperModuleDecomp(stack[i][2]);
      if d<>fail then
	if Length(stack[i][1])<n then
	  d2:=List(d,j->j*stack[i][1]);
	else
	  d2:=d;
	fi;
	md:=List(d2,i->SMTX.InducedActionSubmodule(m,i));
	Assert(1,ForAll(md,i->i<>fail));
	# Translate endomorphism rings
	b:=Concatenation(d[1],d[2]); # local new basis
	# basechange
	endo:=List(stack[i][2].basisModuleEndomorphisms,
	           i->b*i/b);
	sel:=[1..Length(d[1])];
	e1:=List(endo,i->i{sel}{sel});
	e1:=SMTX_EcheloniseMats(e1,F)[1];
	Assert(1,ForAll(md[1].generators,i->ForAll(e1,j->i*j=j*i)));
	md[1].basisModuleEndomorphisms:=e1;
	sel:=[Length(d[1])+1..stack[i][2].dimension];
	e2:=List(endo,i->i{sel}{sel});
	e2:=SMTX_EcheloniseMats(e2,F)[1];
	Assert(1,ForAll(md[2].generators,i->ForAll(e2,j->i*j=j*i)));
	md[2].basisModuleEndomorphisms:=e2;
	stack[i]:=[d2[1],md[1]];
	Add(stack,[d2[2],md[2]]);
      else
	SMTX.SetIsIndecomposable(stack[i][2],true);
	i:=i+1;
      fi;
    od;
    m.indecomposition:=stack;
  fi;
  return m.indecomposition;
end);

SMTX.Indecomposition:=SMTX_Indecomposition;


# Check isomorphism of indecomposable modules.
#
# If they are isomorphic then the homomorphism space between them is a
# disguised copy of the endomorphism algebra. This is a local algebra,
# and hence all singular elements are nilpotent. Certainly it cannot
# have a basis consisting entirely of nilpotent elements (a theorem of
# Wedderburn), so at least one basis element for Hom(M1,M2) must be an
# isomorphism if they are isomorphic.
BindGlobal("IsomIndecModules",function (M1, M2)
local base, i,n;

  if not (SMTX.IsIndecomposable(M1) and SMTX.IsIndecomposable(M2)) then
    Error("IsomIndecModules: requires indecomposable modules");
  fi;

  n:=M1.dimension;
  # module dimensions certainly must match
  if n<>M2.dimension or

    # their endomorphism algebras must have same dimension
     Length(SMTX.BasisModuleEndomorphisms(M1)) <> 
     Length(SMTX.BasisModuleEndomorphisms(M2)) or

    (SMTX.BasisEndomorphismsRadical(M1)<>fail  and
     SMTX.BasisEndomorphismsRadical(M2)<>fail  and
     Length(SMTX.BasisEndomorphismsRadical(M1))<>
     Length(SMTX.BasisEndomorphismsRadical(M2)) ) then
    return fail;
  fi;
  # the easy options have run out

  # Last case, both modules are idecomposable but not necessarily irreducible.
  # In this case, compute Hom and look for isom in the basis.

  base:=SMTX.BasisModuleHomomorphisms(M1, M2);
  
  for i in base do
    if RankMat(i) = n then
      return i;
    fi;
  od;
  
  return fail;

end);

BindGlobal("SMTX_HomogeneousComponents",function(m)
local d, h, found, i, m1, idx, imgs, hom, j;
  d:=SMTX.Indecomposition(m);
  h:=[];
  found:=[];
  i:=1;
  while Length(found)<Length(d) do
    if not i in found then
      m1:=d[i][2];
      idx:=[i];
      AddSet(found,i);
      imgs:=[];
      for j in [i+1..Length(d)] do
	if not j in found and m1.dimension=d[j][2].dimension then
	  hom:=IsomIndecModules(d[j][2],m1);
	  if hom<>fail then
	    Add(idx,j);
	    AddSet(found,j);
	    Add(imgs,rec(component:=d[j],isomorphism:=hom^-1));
	  fi;
	fi;
      od;
      Add(h,rec(component:=d[i],images:=imgs,indices:=idx));
    fi;
    i:=i+1;
  od;
  return h;
end);

SMTX.HomogeneousComponents:=SMTX_HomogeneousComponents;


# Test for isomorphism of modules. Will return one of:
#
# (1) the isomorphism as an F-matrix between M1 and M2
# (2) fail if the two modules are definitely not isomorphic
#
# Note that the isomorphism X is such that conjugating each generator
# acting on M1 by X gives the corresponding action on M2. Therefore
# X^-1 is a matrix whose rows correspond to a new basis of M1 that
# duplicates the action of M2 on M1.
#
# If necessary, uses the decomposition into indecomposable summands.  A
# homogeneous component is a direct sum of multiple copies of a single
# indecomposable summand. The homogeneous components must match between
# each module, with their multiplicities.
BindGlobal("SMTX_IsomorphismModules",function (M1, M2)
local F, n, hc1, hc2, nc, b1, b2, map, remain, j, found, hom, i, k;

  TestModulesFitTogether(M1,M2);
  F:=M1.field;
  n:=M1.dimension;
  
  if n <> M2.dimension then
    # Modules have different dimensions
    return fail;
  elif (SMTX.BasisEndomorphismsRadical(M1)<>fail  and
    SMTX.BasisEndomorphismsRadical(M2)<>fail  and
    Length(SMTX.BasisEndomorphismsRadical(M1))<>
    Length(SMTX.BasisEndomorphismsRadical(M2)) ) then
    # different endomorphism algebra dimensions
    return fail;
  fi;

  hc1:=SMTX.HomogeneousComponents(M1); 
  hc2:=SMTX.HomogeneousComponents(M2); 

  nc:=Length(hc1);
  if nc <> Length(hc2) then
    return fail;
  fi;
  
  # build bases that must be mapped to each other iteratively
  b1:=[];
  b2:=[];
  map:=[];

  remain:=[1..nc];
  for i in [1..nc] do
    j:=1;found:=false;
    while j<=nc and not found do
      if j in remain and Length(hc1[i].indices)=Length(hc2[j].indices) then
        # test: i isomorphic j?
	hom:=IsomIndecModules(hc1[i].component[2],hc2[j].component[2]);
	if hom<>fail then
	  # the homogeneous components are isomorphic
	  found:=true;
	  Append(b1,hc1[i].component[1]);
	  Append(b2,hc2[j].component[1]);
	  Add(map,hom);
	  for k in [1..Length(hc1[i].images)] do
	    Append(b1,hc1[i].images[k].component[1]);
	    Append(b2,hc2[j].images[k].component[1]);
	    Add(map,hc1[i].images[k].isomorphism^-1*hom*
		    hc2[j].images[k].isomorphism);
	  od;
	fi;
      fi;
      j:=j+1;
    od;
    if found=false then
      # one homogeneous component has no image -- the modules cannot be
      # isomorphic
      return fail;
    fi;
  od;
  b1:=ImmutableMatrix(M1.field,b1);
  b2:=ImmutableMatrix(M1.field,b2);
  return b1^-1*ImmutableMatrix(M1.field,DirectSumMat(map))*b2;
end);

SMTX.IsomorphismModules:=SMTX_IsomorphismModules;

# Note: matalg is a basis for a nilpotent matrix algebra whose elements
# are all in lower diagonal form (zeros on the main diagonal).
#
# Echelonisation indices are chosen as the earliest non-zero entries
# running down diagonals below the main diagonal: 
#   [2,1], [3,2], [4,3], ..., [3,1], [4,2], ..., [n-1,1], [n, 2], [n,1]
BindGlobal("SMTX_EcheloniseNilpotentMatAlg",function (matalg, F)
local zero, n, flags, base, ech, k, diff, i, j, found, l;
  
  zero:=Zero(F);
  n := Length(matalg[1][1]);
  flags := NullMat(n,n);

  base := matalg;
  ech := [];
  k := 1;

  while k <= Length(base) do
    diff := 1;
    i := 2; j := i - diff;
    found := false;
    while not found and diff < n do
      if (base[k][i][j] <> zero) and
	(flags[i][j] = 0) then
	found := true;
      else
	i := i + 1; 
	j := i - diff;
	if (i > n) then
	  diff := diff + 1;
	  i := diff + 1; 
	  j := i - diff;
	fi;
      fi;
    od;

    if found then

      # Now basis element k will have echelonisation index [i,j]
      Add(ech, [i,j]);

      # First normalise the [i,j] position to 1
      base[k] := base[k] / base[k][i][j];

      # Now zero position [i,j] in all other basis elements
      for l in [1..Length(base)] do
	if (l <> k) and (base[l][i][j] <> zero) then
	  base[l] := base[l] - base[k] * base[l][i][j];
	fi;
      od;
      k := k + 1;

    else
      # no non-zero element found, delete from list
      base := base{ Concatenation([1..k-1], [k+1..Length(base)])};
    fi;
  od;
  return [base, ech];
end);

# compute a change of basis that exhibits the matrix algebra
# defined by the basis 'matalg' in triangular form.
BindGlobal("SMTX_NilpotentBasis",function (matalg)
local decompose, field, Y, mats, newbase;

  decompose := function ( m, b )
  local n, subs, vs, vsi,rep, newm,j,ran;

    if Length(m) = 0 then
      # all action is now zero, so append current full basis and
      # finish up
      Append(Y, b);
    else
      
      n := Length(m[1][1]);

      # find the intersection of the nullspaces
      subs:=NullspaceMat(m[1]);
      for j in [2..Length(m)] do
	subs:=SumIntersectionMat(subs,NullspaceMat(m[j]))[2];
      od;
      

      # Use matrix group routine to compute action of nilpotent
      # matrices on the quotient vectorspace
      vs := BaseSteinitzVectors(IdentityMat(n,field),subs);
      vs:=Concatenation(vs.subspace,vs.factorspace);
      vs:=ImmutableMatrix(field,vs);
      vsi:=vs^-1;
      ran:=[Length(subs)+1..n];
      rep:=List(m,i->vs*i*vsi);
      rep:=List(rep,i->i{ran}{ran});

      # Take a copy of the non-zero matrices acting on the quotient space
      #
      newm := Filtered(rep,x->not IsZero(x));
      
      Append(Y, subs * b);
      decompose( newm, vs{ran} * b );

    fi;
  end;

  # return empty list if empty matrix list
  if Length(matalg) = 0 then return []; fi;

  field := DefaultField(matalg[1][1]);

  Y   := [];

  decompose( matalg, IdentityMat(Length(matalg[1][1]), field));
  #
  # Y is the change of basis matrix

  if Length(matalg) > 0 then
      mats := Y * matalg / Y;
  fi;
  #
  # mats is now a list of matrices in lower triangular form
  
  # echelonise them along lower diagonals
  #
  newbase := SMTX_EcheloniseNilpotentMatAlg(mats, field)[1];
  
  return [newbase, Y];
  
end);


# module automorphism group
BindGlobal("SMTX_ModuleAutomorphisms",function(m)
  local f, h, hb, hbi, bas, auts, autorder, dim, nb, nbi, r, q, w, Fqr, gl, a, subm, nilbase, homs, sub, endos, au, aubas, it, coeffs, cnt, ol, ind, i, j, g, k;
  f:=m.field;
  h:=MTX.HomogeneousComponents(m);
  # construct basis for each homogeneous component
  hb:=[];
  for i in h do
    # basis of component
    bas:=ShallowCopy(i.component[1]);
    for j in i.images do
      #Append(bas,LeftQuotient(j.isomorphism,j.component[1]));
      Append(bas,j.isomorphism*j.component[1]);
    od;
    #bas:=MTX.NormedBasisAndBaseChange(bas)[1];
    Add(hb,bas);
  od;

  # each homogeneous component separately
  auts:=[];
  autorder:=1;
  for i in [1..Length(h)] do
    # basis of component
    bas:=hb[i];
    dim:=h[i].component[2].dimension;
    nb:=Concatenation(bas,Concatenation(hb{Difference([1..Length(h)],[i])}));
    nb:=ImmutableMatrix(f,nb);
    nbi:=nb^-1;

    # start by building those automorphisms that fix the homogeneous
    # components - ie, do not involve maps from M_i to M_j unless
    # M_i is the same isomorphism type as M_j
    r:=Length(h[i].indices);
    # first the subgroup GL(multiplicity, residue field)
    q:=SMTX.EndAlgResidue(h[i].component[2]);
    w:=q[1];
    q:=q[2]+1;
    Fqr:=PrimitiveElement(GF(q));
    gl:=GL(r,q);
    autorder:=autorder*Size(gl);
    Info(InfoMtxHom,3,"increase by gl",Size(gl)," ",autorder);
    for g in GeneratorsOfGroup(gl) do
      a:=IdentityMat(m.dimension,f);
      for j in [1..r] do
	for k in [1..r] do
	  if IsZero(g[j][k]) then
	    subm:=w*0;
	  else
	    subm:=w^LogFFE(g[j][k],Fqr);
	  fi;
	  a{[(j-1)*dim+1..j*dim]}{[(k-1)*dim+1..k*dim]}:=subm;
	od;
      od;
      a:=nbi*a*nb;
      Assert(1,ForAll(m.generators,i->i*a=a*i));
      Add(auts,a);
    od;

    # now the subgroup { I + Y | Y in S } where S generates the radical
    # of the endomorphism algebra as a circle group
    nilbase:=SMTX.BasisEndomorphismsRadical(h[i].component[2]);
    if Length(nilbase)>0 then
      nilbase:=SMTX_NilpotentBasis(nilbase);
      nilbase:=nilbase[2]^-1*nilbase[1]*nilbase[2];
    fi;
    a:=(Size(f)^Length(nilbase))^(r^2);
    autorder := autorder * a;
    Info(InfoMtxHom,3,"increase by radical",a," ",autorder);

    for j in nilbase do;
      a:=IdentityMat(m.dimension,f);
      subm:=IdentityMat(dim,f)+j;
      a{[1..dim]}{[1..dim]}:=subm;
      a:=nbi*a*nb;
      Assert(1,ForAll(m.generators,i->i*a=a*i));
      Add(auts,a);
    od;

    # Now the automorphisms that act trivially when restricted to
    # each homogeneous component, but which include action between
    # homogeneous components via elements of Hom(M_i, M_j)
    for j in [1..Length(h)] do
      if i <> j then
	homs:=SMTX.BasisModuleHomomorphisms(h[i].component[2],
	                                    h[j].component[2]);
	if Length(homs) > 0 then
	  hbi:=0;
	  for k in [1..j-1] do
	    hbi:=hbi+Length(hb[k]);
	  od;
	  if i>j then 
	    hbi:=hbi+Length(hb[i]);
	  fi;
	  hbi:=hbi+[1..h[j].component[2].dimension];

	  a:=(Size(f)^Length(homs))^(r*Length(h[j].indices));
	  autorder:=autorder*a;
	  Info(InfoMtxHom,3,"increase by mixing ",j,":",a," ",autorder);
	  for k in homs do
	    a:=IdentityMat(m.dimension,f);
	    a{[1..dim]}{hbi}:=k;
	    a:=nbi*a*nb;
	    Assert(1,ForAll(m.generators,i->i*a=a*i));
	    Add(auts,a);
	  od;
	fi;
      fi;
    od;
  od;

  if Length(auts)=0 then
    return Group(auts,IdentityMat(m.dimension,f));
  else
    a:=Group(auts);
    Assert(1,Size(a)=autorder);
    SetSize(a,autorder);
    return a;
  fi;

end);

SMTX.ModuleAutomorphisms:=SMTX_ModuleAutomorphisms;

SMTX.SetIsIndecomposable:=function(m,b)
  m.isIndecomposable:=b;
end;

SMTX.HasIsIndecomposable:=function(m)
  return IsBound(m.isIndecomposable);
end;

SMTX.IsIndecomposable:=function(m)
  if not SMTX.HasIsIndecomposable(m) then
    m.isIndecomposable:=Length(SMTX.Indecomposition(m))=1;
  fi;
  return m.isIndecomposable;
end;


SMTX_BasisModuleHomomorphisms:=function(m1,m2)
local b;
  TestModulesFitTogether(m1,m2);
  if m1.dimension>5 then
    b:= SpinHom(m1,m2);
    Assert(1,Length(b)=Length(SmalldimHomomorphismsModules(m1,m2)));
  else
    b:= SmalldimHomomorphismsModules(m1,m2);
  fi;
  Assert(1,ForAll([1..Length(m1.generators)],
           i->ForAll(b,j->m1.generators[i]*j=j*m2.generators[i])));
  return b;
end;

SMTX.BasisModuleHomomorphisms:=SMTX_BasisModuleHomomorphisms;

SMTX_BasisModuleEndomorphisms:=function(m)
  if not IsBound(m.basisModuleEndomorphisms) then
    m.basisModuleEndomorphisms:=Immutable(SMTX.BasisModuleHomomorphisms(m,m));
  fi;
  return m.basisModuleEndomorphisms;
end;

SMTX.BasisModuleEndomorphisms:=SMTX_BasisModuleEndomorphisms;

SMTX.SetBasisEndomorphismsRadical:=SMTX.Setter("basisEndoRad");
SMTX.BasisEndomorphismsRadical:=SMTX.Getter("basisEndoRad");

SMTX.SetEndAlgResidue:=SMTX.Setter("endAlgResidue");
SMTX.EndAlgResidue:=SMTX.Getter("endAlgResidue");