
i1 : needsPackage "Truncations";

i2 : lineNumber = 0;

i1 : setRandomSeed();

i2 : symExt = (m,E) ->(
          ev := map(E,ring m,vars E);
          mt := transpose jacobian m;
          jn := gens kernel mt;
          q  := vars(ring m)**id_(target m);
          ans:= transpose ev(q*jn);
          --now correct the degrees:
          map(E^{(rank target ans):1}, E^{(rank source ans):0}, 
              ans));

i3 : S=ZZ/32003[x_0..x_2];

i4 : E=ZZ/32003[e_0..e_2,SkewCommutative=>true];

i5 : M=coker matrix{{x_0^2, x_1^2}};

i6 : m=presentation truncate(regularity M,M);

             4      8
o6 : Matrix S  <-- S

i7 : symExt(m,E)

o7 = {-1} | e_2 0   0   0   |
     {-1} | e_1 e_2 0   0   |
     {-1} | e_0 0   e_2 0   |
     {-1} | 0   e_0 e_1 e_2 |

             4      4
o7 : Matrix E  <-- E

i8 : bgg = (i,M,E) ->(
          S :=ring(M);
          numvarsE := rank source vars E;
          ev:=map(E,S,vars E);
          f0:=basis(i,M);
          f1:=basis(i+1,M);
          g :=((vars S)**f0)//f1;
          b:=(ev g)*((transpose vars E)**(ev source f0));
          --correct the degrees (which are otherwise
          --wrong in the transpose)
          map(E^{(rank target b):i+1},E^{(rank source b):i}, b));

i9 : M=cokernel matrix{{x_0^2, x_1^2, x_2^2}};

i10 : bgg(1,M,E)

o10 = {-2} | e_1 e_0 0   |
      {-2} | e_2 0   e_0 |
      {-2} | 0   e_2 e_1 |

              3      3
o10 : Matrix E  <-- E

i11 : tateResolution = (m,E,loDeg,hiDeg)->(
           M := coker m;
           reg := regularity M;
           bnd := max(reg+1,hiDeg-1);
           mt  := presentation truncate(bnd,M);
           o   := symExt(mt,E);
           --adjust degrees, since symExt forgets them
           ofixed   :=  map(E^{(rank target o):bnd+1},
                      E^{(rank source o):bnd},
                      o);
           res(coker ofixed, LengthLimit=>max(1,bnd-loDeg+1)));

i12 : m = matrix{{x_0,x_1}};

              1      2
o12 : Matrix S  <-- S

i13 : regularity coker m

o13 = 0

i14 : T = tateResolution(m,E,-2,4)

       1      1      1      1      1      1      1
o14 = E  <-- E  <-- E  <-- E  <-- E  <-- E  <-- E
                                                 
      0      1      2      3      4      5      6

o14 : ChainComplex

i15 : betti T

             0 1 2 3 4 5 6
o15 = total: 1 1 1 1 1 1 1
         -4: 1 1 1 1 1 1 1

o15 : BettiTally

i16 : T.dd_1

o16 = {-4} | e_2 |

              1      1
o16 : Matrix E  <-- E

i17 : sheafCohomology = (m,E,loDeg,hiDeg)->(
           T := tateResolution(m,E,loDeg,hiDeg);
           k := length T;
           d := k-hiDeg+loDeg;
           if d > 0 then 
              chainComplex apply(d+1 .. k, i->T.dd_(i))
           else T);

i18 : S=ZZ/32003[x_0..x_3];

i19 : E=ZZ/32003[e_0..e_3,SkewCommutative=>true];

i20 : m=koszul(3,vars S);

              6      4
o20 : Matrix S  <-- S

i21 : regularity coker m

o21 = 2

i22 : betti tateResolution(m,E,-6,2)

              0  1 2 3 4  5  6  7   8   9  10
o22 = total: 45 20 6 1 4 15 36 70 120 189 280
         -4: 45 20 6 . .  .  .  .   .   .   .
         -3:  .  . . 1 .  .  .  .   .   .   .
         -2:  .  . . . .  .  .  .   .   .   .
         -1:  .  . . . 4 15 36 70 120 189 280

o22 : BettiTally

i23 : betti sheafCohomology(m,E,-6,2)

             0 1 2  3  4  5   6   7   8
o23 = total: 6 1 4 15 36 70 120 189 280
         -2: 6 . .  .  .  .   .   .   .
         -1: . 1 .  .  .  .   .   .   .
          0: . . .  .  .  .   .   .   .
          1: . . 4 15 36 70 120 189 280

o23 : BettiTally

i24 : M=sheaf coker m;

i25 : HH^1(M(>=0))

o25 = cokernel | x_3 x_2 x_1 x_0 |

                             1
o25 : S-module, quotient of S

i26 : S = ZZ/32003[x_0..x_2];

i27 : U = coker koszul(3,vars S) ** S^{1};

i28 : k2 = koszul(2,vars S)

o28 = {1} | -x_1 -x_2 0    |
      {1} | x_0  0    -x_2 |
      {1} | 0    x_0  x_1  |

              3      3
o28 : Matrix S  <-- S

i29 : alpha = map(U ++ U, S^{-1}, transpose{{0,-1,0,1,0,0}});

o29 : Matrix

i30 : alphad = map(S^1, U ++ U, matrix{{0,1,0,0,0,1}} * (k2 ++ k2));

o30 : Matrix

i31 : F = prune homology(alphad, alpha);

i32 : betti  F

             0 1
o32 = total: 3 1
          1: 2 .
          2: 1 1

o32 : BettiTally

i33 : sortedBasis = (i,E) -> (
           m := basis(i,E);
           p := sortColumns(m,MonomialOrder=>Descending);
           m_p);

i34 : S=ZZ/32003[x_0..x_3];

i35 : E=ZZ/32003[e_0..e_3,SkewCommutative=>true];

i36 : koszul(2,vars S)

o36 = {1} | -x_1 -x_2 0    -x_3 0    0    |
      {1} | x_0  0    -x_2 0    -x_3 0    |
      {1} | 0    x_0  x_1  0    0    -x_3 |
      {1} | 0    0    0    x_0  x_1  x_2  |

              4      6
o36 : Matrix S  <-- S

i37 : sortedBasis(2,E)

o37 = | e_0e_1 e_0e_2 e_1e_2 e_0e_3 e_1e_3 e_2e_3 |

              1      6
o37 : Matrix E  <-- E

i38 : beilinson1=(e,dege,i,S)->(
           E := ring e;
           mi := if i < 0 or i >= numgens E then map(E^1, E^0, 0)
                 else if i === 0 then id_(E^1)
                 else sortedBasis(i+1,E);
           r := i - dege;
           mr := if r < 0 or r >= numgens E then map(E^1, E^0, 0)
                 else sortedBasis(r+1,E);
           s = numgens source mr;
           if i === 0 and r === 0 then
                substitute(map(E^1,E^1,{{e}}),S)
           else if i>0 and r === i then substitute(e*id_(E^s),S)
           else if i > 0 and r === 0 then
                (vars S) * substitute(contract(diff(e,mi),transpose mr),S)
           else substitute(contract(diff(e,mi), transpose mr),S));

i39 : beilinson1(e_1,1,3,S)

o39 = {-3} | 0  |
      {-3} | 0  |
      {-3} | -1 |
      {-3} | 0  |

              4      1
o39 : Matrix S  <-- S

i40 : beilinson1(e_1,1,2,S)

o40 = {-2} | 0 0 0 0  |
      {-2} | 1 0 0 0  |
      {-2} | 0 0 0 0  |
      {-2} | 0 1 0 0  |
      {-2} | 0 0 0 0  |
      {-2} | 0 0 0 -1 |

              6      4
o40 : Matrix S  <-- S

i41 : beilinson1(e_1,1,1,S)

o41 = | x_0 0 -x_2 0 -x_3 0 |

              1      6
o41 : Matrix S  <-- S

i42 : U = (i,S) -> (
           if i < 0 or i >= numgens S then S^0
           else if i === 0 then S^1
           else cokernel koszul(i+2,vars S) ** S^{i});

i43 : beilinson = (o,S) -> (
           coldegs := degrees source o;
           rowdegs := degrees target o;
           mats = table(numgens target o, numgens source o,
                    (r,c) -> (
                         rdeg = first rowdegs#r;
                         cdeg = first coldegs#c;
                         overS = beilinson1(o_(r,c),cdeg-rdeg,cdeg,S);
                         -- overS = substitute(overE,S);
                         map(U(rdeg,S),U(cdeg,S),overS)));
           if #mats === 0 then matrix(S,{{}})
           else matrix(mats));

i44 : S=ZZ/32003[x_0..x_2];

i45 : E = ZZ/32003[e_0..e_2,SkewCommutative=>true];

i46 : alphad = map(E^1,E^{-1,-1},{{e_1,e_2}})

o46 = | e_1 e_2 |

              1      2
o46 : Matrix E  <-- E

i47 : alpha = map(E^{-1,-1},E^{-2},{{e_1},{e_2}})

o47 = {1} | e_1 |
      {1} | e_2 |

              2      1
o47 : Matrix E  <-- E

i48 : alphad=beilinson(alphad,S);

o48 : Matrix

i49 : alpha=beilinson(alpha,S);

o49 : Matrix

i50 : F = prune homology(alphad,alpha);

i51 : betti  F

             0 1
o51 = total: 3 1
          1: 2 .
          2: 1 1

o51 : BettiTally

i52 : S = ZZ/32003[x_0..x_4];

i53 : E = ZZ/32003[e_0..e_4,SkewCommutative=>true];

i54 : beta=map(E^1,E^{-2,-1},{{e_0*e_2+e_1*e_3,-e_4}})

o54 = | e_0e_2+e_1e_3 -e_4 |

              1      2
o54 : Matrix E  <-- E

i55 : alpha=map(E^{-2,-1},E^{-3},{{e_4},{e_0*e_2+e_1*e_3}})

o55 = {2} | e_4           |
      {1} | e_0e_2+e_1e_3 |

              2      1
o55 : Matrix E  <-- E

i56 : beta=beilinson(beta,S);

o56 : Matrix

i57 : alpha=beilinson(alpha,S);

o57 : Matrix

i58 : G = prune homology(beta,alpha);

i59 : betti res G

              0 1 2 3
o59 = total: 10 9 5 1
          1: 10 4 1 .
          2:  . 5 4 1

o59 : BettiTally

i60 : foursect = random(S^4, S^10) * presentation G;

              4      9
o60 : Matrix S  <-- S

i61 : IX = trim minors(4,foursect);

o61 : Ideal of S

i62 : codim IX

o62 = 2

i63 : degree IX

o63 = 8

i64 : codim singularLocus IX

o64 = 5

i65 : alphad = matrix{{e_4*e_1, e_2*e_3},{e_0*e_2, e_3*e_4},
                      {e_1*e_3, e_4*e_0},{e_2*e_4, e_0*e_1},
                      {e_3*e_0, e_1*e_2}};

              5      2
o65 : Matrix E  <-- E

i66 : alphad=map(E^5,E^{-2,-2},alphad)

o66 = | -e_1e_4 e_2e_3  |
      | e_0e_2  e_3e_4  |
      | e_1e_3  -e_0e_4 |
      | e_2e_4  e_0e_1  |
      | -e_0e_3 e_1e_2  |

              5      2
o66 : Matrix E  <-- E

i67 : alpha=syz alphad

o67 = {2} | e_0e_1  e_2e_3 e_0e_4 e_1e_2 -e_3e_4 |
      {2} | -e_2e_4 e_1e_4 e_1e_3 e_0e_3 e_0e_2  |

              2      5
o67 : Matrix E  <-- E

i68 : alphad=beilinson(alphad,S);

o68 : Matrix

i69 : alpha=beilinson(alpha,S);

o69 : Matrix

i70 : FHM = prune homology(alphad,alpha);

i71 : betti res FHM

              0  1  2 3
o71 = total: 19 35 20 2
          3:  4  .  . .
          4: 15 35 20 .
          5:  .  .  . 2

o71 : BettiTally

i72 : regularity FHM

o72 = 5

i73 : betti sheafCohomology(presentation FHM,E,-6,6)

               0   1  2  3  4 5 6 7  8  9 10  11  12
o73 = total: 210 100 37 14 10 5 2 5 10 14 37 100 210
         -6: 210 100 35  4  . . . .  .  .  .   .   .
         -5:   .   .  2 10 10 5 . .  .  .  .   .   .
         -4:   .   .  .  .  . . 2 .  .  .  .   .   .
         -3:   .   .  .  .  . . . 5 10 10  2   .   .
         -2:   .   .  .  .  . . . .  .  4 35 100 210

o73 : BettiTally

i74 : -- presentation FHM has the basis elements in the reverse order now
      -- sect =  map(S^1,S^15,0) | random(S^1, S^4);
      sect =  random(S^1, S^4) | map(S^1,S^15,0)

o74 = | 5576 4983 5586 -2697 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |

              1      19
o74 : Matrix S  <-- S

i75 : mapcone = sect || transpose presentation FHM;

              36      19
o75 : Matrix S   <-- S

i76 : fmapcone = res coker mapcone;

i77 : IX =  trim ideal fmapcone.dd_2;

o77 : Ideal of S

i78 : codim IX

o78 = 2

i79 : degree IX

o79 = 10

i80 : codim singularLocus IX

o80 = 5

i81 : 
