tableau[A_,target_,basis_]:=Module[{m,N,lastcol,Aaug,G,var,Bvar,innertab,tab,targetp},
(*Input: matrix A with last column of values, target vector, desired new basis for tableau*)
{m,N}=Dimensions[A];(*Last column in A is the vector b, so N=n+1*)
If[Length[target]==N,
If[Det[A[[Range[m],basis]]]!=0,
lastcol=Join[Table[0,{m}],{1}];(*Append trivial target column*)
Aaug=Append[A,target];(*Append target row*)
G=Inverse[Transpose[Append[Transpose[Aaug[[Range[m+1],basis]]],lastcol]]];(*Pivoting matrix*)
innertab=G.Aaug;(*new tableau*)
If[VectorGreaterEqual[{innertab[[Range[m],N]],Table[0,{m}]}],
(*Creates variable labels for rows and columns, add margins*)
var=Table["x"<>ToString[i],{i,1,N-1}];
Bvar=Append[Table["x"<>ToString[basis[[i]]],{i,1,Length[basis]}]," "];
var=Prepend[Append[var," "]," "];
tab=Prepend[MapThread[Prepend,{innertab,Bvar}],var];
Return[tab//TableForm],(*Output: Equivalent tableau written with prescribed basic variables*)
Message[tableau::argerrfb,basis];(*Last column fails to be nonnegative*)
],
Message[tableau::argerrb,basis];(*User input of basis is not a basis*)
$Failed],
Message[tableau::argerr];(*User input has wrong dimensions*)
$Failed]
];
(*Possible error messages*)
tableau::argerr="Dimensions of A and target do not match";
tableau::argerrb="`1` is not a basis";
tableau::argerrfb="`1` is not a feasible basis";

thetas[tab_,j_]:=Module[{m,n,thetas,i},
(*Input: tableau tab and choice j of entering variable*)
{m,n}=Dimensions[tab[[1]]];
(*Creates list of theta ratios*)
thetas={};
For[i=2,i<=m-1,i++,
If[tab[[1,i,j+1]]>0,
thetas=Join[thetas,{{i,tab[[1,i,n]]/tab[[1,i,j+1]]}}];
];
If[tab[[1,i,j+1]]<0,
thetas=Join[thetas,{{i,"(unconstrained)"}}];
];
];
Return[TableForm[Table[{"θ ratio for",tab[[1,thetas[[i,1]],1]],":",thetas[[i,2]]},{i,1,Length[thetas]}]]];
(*Output: theta-ratios for current basic variables with respect to entering variable j*)
];