// test Witt's theorem

/*
F := GF(7);
J := Matrix(F,5,5,[0,0,0,1,0, 0,0,1,0,0, 0,-1,0,0,0, -1,0,0,0,0, 0,0,0,0,0]);
V := RSpace(F,5,J);
U := sub< V | V.1, V.2 >;
f := hom< U-> V | [V.3,V.4]>;
U1 := sub< V | V.1, V.3, V.4 >;
*/

WittTest := procedure(U,V,f)
  flag :=  IsIsometry(U,V,f);
  print "f is an isometry:",flag;
  if flag then
    g := ExtendIsometry(V,U,f);
    assert IsIsometry(V,V,g);
    print "g is an isometry";
    assert forall{ u : u in Basis(U) | f(u) eq g(u) };
    print "g extends f";
  end if;
end procedure;

print "Symplectic spaces\n=================";
Flds := [*GF(7),Rationals()*];

for F in Flds do

print "\n",F;
print "Not an isometry";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,-1,0,0, -1,0,0,0]);
V := RSpace(F,4,J);
U := sub< V | V.1,V.2 >;
f := hom< U-> V | V.1,V.4 >;
print "f is an isometry:",IsIsometry(U,V,f);
try 
  g := ExtendIsometry(V,U,f);
catch e
  print e`Position;
  print "expected failure\n";
end try; 

print "Subset of the radical";
J := Matrix(F,4,4,[0,0,0,1, 0,0,0,0, 0,0,0,0, -1,0,0,0]);
V := RSpace(F,4,J);
U := sub< V | V.2 >;
f := hom< U -> V | V.2+3*V.3 >;
WittTest(U,V,f);

print "\nHyperplane containing the radical";
J := Matrix(F,5,5,[0,1,0,0,0, -1,0,0,0,0, 0,0,0,1,0, 0,0,-1,0,0, 0,0,0,0,0]);
V := RSpace(F,5,J);
U := sub< V | V.1,V.3,V.2+V.4,V.5 >;
f := hom< U -> V | [V.1->V.3,V.3->V.1,V.2+V.4->V.2+V.4,V.5->V.5] >;
WittTest(U,V,f);

print "\nNot subset of the radical";
J := Matrix(F,4,4,[0,0,0,1, 0,0,0,0, 0,0,0,0, -1,0,0,0]);
V := RSpace(F,4,J);
U := sub< V | V.1 >;
f := hom< U -> V | V.2+3*V.4 >;
WittTest(U,V,f);

print "\nNo radical, subspace of dimension 1, test(1)";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,-1,0,0, -1,0,0,0]);
V := RSpace(F,4,J);
U := sub< V | V.1 >;
f := hom< U-> V | V.4 >;
WittTest(U,V,f);

print "\nNo radical, subspace of dimension 1, test (2)";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,-1,0,0, -1,0,0,0]);
V := RSpace(F,4,J);
U := sub< V | V.1 >;
f := hom< U-> V | V.1+V.2 >;
WittTest(U,V,f);

print "\nNo radical, subspace of dimension 2";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,-1,0,0, -1,0,0,0]);
V := RSpace(F,4,J);
U := sub< V | V.1,V.2 >;
f := hom< U-> V | V.1+V.2, V.1+3*V.2 >;
WittTest(U,V,f);

print "\nNo radical, hyperplanes";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,-1,0,0, -1,0,0,0]);
V := RSpace(F,4,J);
U := sub< V | V.1, V.2, V.3 >;
f := hom< U-> V | [V.3,V.4,-V.1]>;
WittTest(U,V,f);
end for;

print "\nQuadratic spaces\n==============";
print "\nCharacteristic 2";
F<t> := GF(16);
B := Matrix(6,6,[F| 
    0,1, 0,0, 0,0,
    0,0, 0,0, 0,0,
    0,0, 0,1, 0,0,
    0,0, 0,0, 0,0,
    0,0, 0,0, 0,1,
    0,0, 0,0, 0,0]);
V := QuadraticSpace(B);
e1 := V![t^13, t^5,  t^4,  t^6,  t^14, t^10];
e2 := V![t^5,  t^12, t^12, t^14, t^7,  t^4];
e3 := V![t^12, t^8,  t^4,  t^14, t^12,   1];
U := sub< V | e1,e2,e3 >;
f1 := V![t^8, 1, 1, 1, 1, 1];
f2 := V![t^7, t^8, t^2, 1, 1, 1];
f3 := V![t^11, t^13, t^8, t, 1, 1];
f := hom< U -> V | e1->f1, e2->f2, e3->f3 >;
WittTest(U,V,f);

print "\nCharacteristic 7";
F<t> := GF(49);
B := Matrix(6,6,[F| 
    0,1, 0,0, 0,0,
    0,0, 0,0, 0,0,
    0,0, 0,1, 0,0,
    0,0, 0,0, 0,0,
    0,0, 0,0, 0,1,
    0,0, 0,0, 0,0]);
V := QuadraticSpace(B);
U := sub< V | V.1,V.2 >;
f := hom< U -> V | [V.4,V.3] >;
WittTest(U,V,f);

print "Unitary spaces\n=================";
F1<xi> := GF(7^2);
sigma1 := hom< F1 -> F1 | x :-> x^7 >;

F2<i> := QuadraticField(-1);
sigma2 := hom< F2 -> F2 | x :-> ComplexConjugate(x) >;

Flds := [* <F1,sigma1>, <F2,sigma2> *];

for FF in Flds do
F<xi> := FF[1];
sigma := FF[2];

print "\n",F;
print "Not an isometry";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,1,0,0, 1,0,0,0]);
V := UnitarySpace(J,sigma);
U := sub< V | V.1,V.2 >;
f := hom< U-> V | V.1,V.4 >;
print "f is an isometry:",IsIsometry(U,V,f);
try 
  g := ExtendIsometry(V,U,f);
catch e
  printf e`Position;
  print "expected failure\n";
end try; 

print "Subset of the radical";
J := Matrix(F,4,4,[0,0,0,1, 0,0,0,0, 0,0,0,0, 1,0,0,0]);
V := UnitarySpace(J,sigma);
U := sub< V | V.2 >;
f := hom< U -> V | V.2+3*V.3 >;
WittTest(U,V,f);

print "\nHyperplane containing the radical";
J := Matrix(F,5,5,[0,1,0,0,0, 1,0,0,0,0, 0,0,0,1,0, 0,0,1,0,0, 0,0,0,0,0]);
V := UnitarySpace(J,sigma);
U := sub< V | V.1,V.3,V.2+V.4,V.5 >;
f := hom< U -> V | [V.1->V.3,V.3->V.1,V.2+V.4->V.2+V.4,V.5->V.5] >;
WittTest(U,V,f);

print "\nNot subset of the radical";
J := Matrix(F,4,4,[0,0,0,1, 0,0,0,0, 0,0,0,0, 1,0,0,0]);
V := UnitarySpace(J,sigma);
U := sub< V | V.1 >;
f := hom< U -> V | V.2+3*V.4 >;
WittTest(U,V,f);

print "\nNo radical, subspace of dimension 1, test(1)";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,1,0,0, 1,0,0,0]);
V := UnitarySpace(J,sigma);
U := sub< V | V.1 >;
f := hom< U-> V | V.4 >;
WittTest(U,V,f);

print "\nNo radical, subspace of dimension 1, test (2)";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,1,0,0, 1,0,0,0]);
V := UnitarySpace(J,sigma);
U := sub< V | V.1 >;
f := hom< U-> V | V.1+V.2 >;
WittTest(U,V,f);

print "\nNo radical, subspace of dimension 2";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,1,0,0, 1,0,0,0]);
V := UnitarySpace(J,sigma);
IsUnitarySpace(V);
U := sub< V | V.1,V.2 >;
f := hom< U-> V | V.1+V.2, V.1+3*V.2 >;
WittTest(U,V,f);

print "\nNo radical, hyperplanes";
J := Matrix(F,4,4,[0,0,0,1, 0,0,1,0, 0,1,0,0, 1,0,0,0]);
V := UnitarySpace(J,sigma);
IsUnitarySpace(V);
U := sub< V | V.1, V.2, V.3 >;
f := hom< U-> V | [V.3,V.4,V.1]>;
WittTest(U,V,f);

end for;

