(*
(* generalpoints.nb *)
*)
enumerateSimplices :=

(

(*
simplex[i] = list of the indices of its vertices
*)
ClearAll[simplex,simplexVolume,NumberOfSimplices];
Module[{i=1,
		det,
		c=1/(Length[vertex[1]]!),
		l=Length[vertex[1]]+1},
	Scan[
		(If[
			(det =
				Det[
					Append[
						Transpose[
							Map[
								vertex,
								#]],
						Table[1,{l}]]];
			 det!=0),
			(simplex[i]=#;
			 simplexVolume[i]=c det;
			 ++i)])&,
		KSubsets[Range[NumberOfVertices],l]];
	NumberOfSimplices = i-1];

)
(*
IntersectProperQ[i,j] = True, False
						whether simplex[i] and simplex[j]
						intersect properly or not *)
ClearAll[IntersectProperQ];
IntersectProperQ[i_,j_]:=
	Module[
		{OnlyInI = Complement[
					simplex[i],
					simplex[j]],
		 OnlyInJ = Complement[
		 			simplex[j],
		 			simplex[i]],
		 BothInIJ = Intersection[simplex[i],simplex[j]],
		 FirstInBothIJ,
		 Difference,
		 matrix,
		 lp},
		 Difference = Length[OnlyInI]; 
		 If[
		 	BothInIJ=={},
		 	(matrix =
		 		Map[(vertex[#]-vertex[simplex[i][[1]]])&,
		 			simplex[j]] .
			 	Inverse[
			 		Map[(vertex[#]-vertex[simplex[i][[1]]])&,
			 			Drop[simplex[i],1]]];
			 matrix =
			 	Transpose[matrix];
			 lp = LinearProgramming[
			 	Join[{1},Table[0,{Difference-1}]],
			 	Join[matrix,
		 			{Table[1,{Difference}]},
		 			{Table[-1,{Difference}]},
		 			{-Apply[Plus,matrix]}
			 		],
			 	Join[Table[0,{Difference-1}],{1,-1,-1}]]),
		 	(FirstInBothIJ = BothInIJ[[1]];
		 	 BothInIJ = Drop[BothInIJ,1];
		 	 matrix =
		 		Map[(vertex[#]-vertex[FirstInBothIJ])&,
		 			Join[OnlyInJ,BothInIJ]] .
			 	Inverse[
			 		Map[(vertex[#]-vertex[FirstInBothIJ])&,
			 			Join[OnlyInI,BothInIJ]]];
			 matrix =
			 	Take[
			 		Transpose[
		 				Take[
							matrix,
		 					Difference]],
			 		Difference];
			 lp = LinearProgramming[
			 	Join[{1},Table[0,{Difference-1}]],
			 	Join[matrix,
		 			{Table[1,{Difference}]}
		 			(*,
		 			{Table[-1,{Difference}]}
			 		*)
			 		],
			 	Join[Table[0,{Difference}],{1(*,-1*)}]])];
		 Switch[
		 	Head[lp],
		 	LinearProgramming,
		 	True,
		 	List,
		 	False,
		 	_,
		 	Error]
		 	
		 	(*Print[
		 	Join[{1},Table[0,{Difference-1}]],
		 	Join[matrix,
		 		{Table[1,{Difference}]},
		 		{Table[-1,{Difference}]}],
		 	Join[Table[0,{Difference}],{1,-1}]];*)]
memorizeIntersectProperQ :=

(

ClearAll[tempTable,IntersectProperQTable];
Print[
Timing[
	tempTable=
		Table[
			If[	i>j,
				IntersectProperQ[i,j],
				Null],
			{i,NumberOfSimplices},{j,NumberOfSimplices}];]
];

Print[
Timing[
	IntersectProperQTable=
		Table[
			If[	i>j,
				tempTable[[i,j]],
				If[	i<j,
					tempTable[[j,i]],
					Null]],
			{i,NumberOfSimplices},{j,NumberOfSimplices}];]
];

ClearAll[tempTable];

ClearAll[IntersectProperQ];
IntersectProperQ[i_,j_] :=
	IntersectProperQTable[[i,j]]

)
