/////////////////////////////////////////////////////////////////////////
// projective
// $Revision: 55006 $
// $Date: 2017-06-27 09:55:37 +1000 (Tue, 27 Jun 2017) $
// $LastChangedBy: allan $
/////////////////////////////////////////////////////////////////////////

//////////////////////////////////////
// Danilov: a non-projective complete
// toric variety
//////////////////////////////////////
"------\nDanilov's Example\n";

rays := [ [ 0, 0, 1],
          [ 4, 0, 1],
          [ 0, 4, 1],
          [ 1, 1, 1],
          [ 2, 1, 1],
          [ 1, 2, 1],
          [-1,-1,-1] ];
cones := [ [1,3,6],
           [1,4,6],
           [1,2,4],
           [2,4,5],
           [2,3,5],
           [3,5,6],
           [4,5,6],
           [1,3,7],
           [1,2,7],
           [2,3,7] ];
F := Fan(rays,cones);
assert IsComplete(F);
assert IsQFactorial(F);
assert IsCanonical(F);
X := ToricVariety(Rationals(),F);
assert IsComplete(X);
assert IsQFactorial(X);
assert IsCanonical(X);
time assert not IsProjective(X);    // approx 0.4s
time Y:=Resolution(X);              // approx 2.3s
time assert not IsProjective(Y);    // approx 12s

////////////////////////////////////////////////////////////////////////////////////////////////////
// we reproduce the example of Fujino from Cox, Little, Schenck book (Example 6.2.23, currently).
// here X is complete and not projective, however NefCone is of full dimension in Pic(X). 
////////////////////////////////////////////////////////////////////////////////////////////////////
"------\nFujino's Example\n";

L:=ToricLattice(3);                                         
R:=[L|[1,0,1],[0,1,1],[-1,-1,1],[1,0,-1],[0,1,-1],[-1,-1,-1]];
S:=[[1,2,3],[1,2,4],[2,4,5],[1,3,4,6],[2,3,5,6],[4,5,6]];     
X:=ToricVariety(Rationals(), Fan(R,S));                       
assert IsComplete(X);
assert Dimension(PicardLattice(X)) eq 1;
assert IsStrictlyConvex(NefCone(X));
assert IsMaximumDimensional(NefCone(X));
assert not IsProjective(X);
D:=Divisor(X,[3,0,0,3,0,0]);
assert IsCartier(D);
assert PicardClass(D) in NefCone(X);
P:=Polyhedron(D);                                                
Y:=Proj(D);                                                   
assert Fan(X) ne Fan(Y);                                  
psi:=ToricVarietyMap(X,Y);
assert GoodDescription(psi) eq GoodDescription(IdentityMap(X));
i:=Index(IntersectionForms(X),0);
assert not IsZero(i);
C:=Cones(Fan(X),2)[i]; 
Z:=Scheme(X,C);                                               
ZZ,psi2:=Stratum(Z);                                          
ZZ:=Ambient(ZZ);
assert Dimension(ZZ) eq 1;
GoodDescription(psi2);
assert #RiemannRochBasis(D) eq 12;
assert IsPrincipal(Pullback(psi2,D));
PP:=ToricVariety(Rationals(), RiemannRochDimension(D)-1);
psi3:=ToricVarietyMap(X, PP, RiemannRochBasis(D));
assert [RationalFunction(s) : s in GoodDescription(psi3)] eq RiemannRochBasis(D);
assert {RationalFunction(s) : s in GoodDescription(psi2*psi3)} eq {0,1};

/////////////////////////////////////////////////////////////////////////
// Construct Toric Variety from a Polytope.
// Thanks to Janko Boehm for providing this example.
// We start from a polytope, which is a Minkowski sum of two triangles
// in three-space and construct the toric variety from the dual fan. 
// The goal is to identify the Picard-Cox ring inside the Cox homogeneous
// coordinate ring.
/////////////////////////////////////////////////////////////////////////
"------\nConstruct Toric Variety from a Polytope\n";

T1:=[[1,0,0],[0,0,0],[0,1,0]];
T2:=[[0,0,1],[0,0,0],[-1,-1,-1]];
P1:=Polytope(T1);
P2:=Polytope(T2);
P:=P1+P2;
assert NumberOfVertices(P) eq 8;
assert NumberOfFacets(P) eq 8;
F:=DualFan(P);
time F2:=Resolution(F);     // approx 0.5s

assert #Rays(F) eq 8;
X<x1,x2,x3,x4,x5,x6,x7,x8>:=ToricVariety(Rationals(),F);
assert Dimension(PicardLattice(X)) eq 2;
assert IsFano(X);
S:=[ Representative(X,b) : b in Basis(PicardLattice(X))] cat [ Divisor(X,b) : b in Basis(MonomialLattice(X))];
assert #S eq 5;
SS:=[Weil(s) : s in S];
C:=Cone(LinearConeGenerators(SS)) meet PositiveQuadrant(Dual(RayLattice(X)));

assert Dimension(C) eq 5;
assert #ZGenerators(C) eq 6;
assert #MinimalRGenerators(C) eq 6;

pic_cox_gens:=[DefiningMonomial(Divisor(X,v)) :  v in ZGenerators(C)];

// Therefore Q-Picard-CoxRing is generated by 6 polynomials:
//    [ x5*x7, x3*x5*x6*x8, x3*x4, x2*x8, x1*x6, x1*x2*x4*x7 ]

Kernel(Matrix(ZGenerators(C)));
// with relation: 2nd one * 6th one is product of the rest.

R:=PolynomialRing(BaseRing(X), #ZGenerators(C));
I:=IrrelevantComponents(X);
A:=Spec(CoordinateRing(X));
P:=Spec(R);
irrel:=[Scheme(A,i) : i in I];;
phi:=map<A->P| pic_cox_gens>;
irrel_in_pic_cox:=Setseq({phi(irr) : irr in irrel});
back_irrel:=[irr@@phi : irr in irrel_in_pic_cox];
prime_irrels:=Setseq(Seqset(&cat[PrimeComponents(irr) : irr in back_irrel]));
assert irrel subset prime_irrels;
assert &and [&or[irr subset irr2 : irr2 in irrel] : irr in  prime_irrels];


// very non-Q-Factorial example
Rs:=[[1,1,1],[1,1,-1],[1,-1,1],[2,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]
];
cones:=[[1,2,3,4],[5,6,7,8],[1,2,5,6],[3,4,7,8],[1,3,5,7],[2,4,6,8]];
F:=Fan(Rs,cones);
X<x1,x2,x3,x4,x5,x6,x7,x8>:=ToricVariety(Rationals(),F);
assert IsComplete(F);
assert not IsProjective(X);
assert Dimension(PicardLattice(X)) eq 0;
time F2:=Resolution(F);         // approx 3.6s
S:=Scheme(X, [x1, x7, x3^2* x4^3-x5^2*x6^2]);
assert Dimension(S) eq 0;
S:=Scheme(X, [x1,x7]);
assert Dimension(S) eq 0;
assert Dimension(Ideal(S))-NumberOfGradings(S) eq 1;


// We verify that for surfaces, the resolution of sings is minimal
X<x0,x1,x2>:=WPS(GF(17),[Random([1..15]) : i in [0..2]]);
X;
// MinimalRes will have new ray for each ZGenerator of a cone in fan of X
zgens:=Seqset(&cat[ZGenerators(C) : C in Cones(Fan(X))]);
XX:=Resolution(X);
assert Seqset(Rays(XX)) eq zgens;


// Another example for that:
P:=RandomPolytope(2,10,10);
time F:=DualFan(P);
F;
time zgens:=Seqset(&cat[ZGenerators(C) : C in Cones(F)]);  // approx 0.04s


FF:=Resolution(F);
assert Seqset(Rays(FF)) eq zgens;




C:=Cone([                        
    [ 0, 2, 1, 2, 0 ],
    [ 0, 3, 0, 2, 0 ],
    [ 0, 3, 2, 3, 3 ],
    [ 1, 2, 3, 2, 2 ],
    [ 1, 3, 2, 1, 3 ],
    [ 2, 0, 0, 3, 0 ],
    [ 3, 1, 2, 1, 0 ],
    [ 3, 2, 1, 0, 2 ]
]);

F:=Fan(C);
X<[x]>:=ToricVariety(Rationals(),F);
S:=NonQFactorialLocus(X);
assert  Dimension(S) eq 1;




SetSeed(1);
dim:=4;
num:=6;
repeat
    P:=RandomPolytope(dim, num, 2);
until not IsSimplicial(P);
F:=DualFan(P);
//THINK: need to improve LatticeBasisInCone to get rid of
//       positive:=false;
X<[x]>:=ToricVariety(Rationals(),F:positive:=false);
S:=NonQFactorialLocus(X);
d:=Max([Dimension(Ambient(P)) - Dimension(c) 
            :  c in NonSimplicialCones(F)]);
//THINK: this is too slow for dim=4, num=7!
time assert d eq Dimension(S);


/////////////////////////////////////////////////////////////////////////
// Finally, clear the caches
/////////////////////////////////////////////////////////////////////////
"------\nClearing caches\n";

CacheClearToricVariety();
CacheClearToricLattice();
