-------------------------------------
-- Macaulay 2 sample input
-- Fields (July 24, 2006)
-------------------------------------

R = QQ[x_1..x_5]
I = monomialIdeal(x_1*x_2,x_2*x_3*x_4)

J1= monomialIdeal(x_1*x_2,x_2*x_3*x_4,x_2)
J2 =ideal(x_1*x_2,x_2*x_3*x_4,x_2)
 
-- Simplicial Complexes 

loadPackage "SimplicialComplexes";
R = ZZ[x_1..x_5] -- introduce polynomial ring 
L = {x_1*x_2*x_3,x_1*x_2*x_4,x_1*x_3*x_4,x_2*x_3*x_4,x_2*x_5,x_3*x_5}  
-- write each facet as a monomial
Delta = simplicialComplex L
dim Delta
fVector Delta
facets Delta -- all facets of Delta
faces(0,Delta) -- all 0-dim faces
faces(1,Delta) -- all 1-dim faces
faces(4,Delta) -- all 4-dim faces (there are none!)
isPure(Delta)



aUpperd = (a,d)-> (
     k = {};
     for i from 0 to (d-1) do (
	  ki = 0;
	  while (a >= binomial(ki,d-i)) do ki = ki+1;
	  k = append(k,ki-1);
	  a = a - binomial(ki-1,d-i);
	  );
     l = sum toList apply(0..(d-1),j->binomial(k_j,d+1-j));
     return(l)
     );

aUpperd(23,3)


validfVector=f->(
     for j from 0 to #f-2 do (
	  if (f_(j+1) > aUpperd(f_j,j+1)) then return (false);
     	  );
     return(true);
     );

validfVector(5,8,4)
validfVector(6,16,18)

monomialIdeal Delta

R = QQ[x_1..x_6]
I = monomialIdeal (x_1*x_2,x_2*x_3,x_3*x_4,x_4*x_5,x_5*x_6,x_6*x_1)
D = simplicialComplex I
fVector D


S = R/I
dim S -- dimension of ring
dim D -- dimension of simplicial complex

T = ZZ[x_1..x_6]  -- incorrect example
I = monomialIdeal (x_1*x_2,x_2*x_3,x_3*x_4,x_4*x_5,x_5*x_6,x_6*x_1)
D = simplicialComplex I
B = T/I
dim B 
dim D  -- dim B =/= dim D + 1

R = QQ[x,y,z] -- coordinate ring of P^2
IX = 
intersect(ideal(y,z),ideal(x,z),ideal(x,y),ideal(y-x,z-x),ideal(19*y-64*x,19*z-42*x))
G = res IX
G.dd
betti res IX


BettiIJ = (i,j,I)->(
     G = res I;
     B = G_i;
     B = flatten degrees B;
     t = tally B;
     if (t #? j) then return (t#j)
     else return(0);
     );


BettiIJ(2,3,IX)
BettiIJ(2,4,IX)

B = coker gens IX
regularity(B)
pdim(B)

S = R/IX
regularity(S)
pdim(S)

R = QQ[x,y,z,w]
I = (ideal(x,y,z,w))^4;
betti res I



verifyLinearRes=I->(
     D = min flatten degrees I;
     B = coker gens I;
     R = regularity(B);
     if D-1 =!= R then return (false);
     return(true);
     );
verifyLinearRes(I)
verifyLinearRes(IX)

--gin code from a tutorial by
--Mark Green and Mike Stillman
gin = method();

gin Ideal := I -> (
    S:=ring I;
    StoS:=map(S,S,random(S^{0},S^{numgens S:-1}));
    monomialIdeal StoS I);

gin MonomialIdeal := I -> gin ideal I;
gin IX

apply(1..100, j-> print gin IX)