(*******************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which
typically will have the same name as this file except ending in
".nb" instead of ".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent
to using the Evaluate Initialization Cells menu command in the front
end.

DO NOT EDIT THIS FILE.  This entire file is regenerated
automatically each time the parent Notebook file is saved in the
Mathematica front end.  Any changes you make to this file will be
overwritten.
***********************************************************************)











includeRow[Ri_,r_]:=
    Mod[Floor[Ri/r],2];



satisfyingRows[Ri_,s_]:=
    Switch[s,
      0,includeRow[Ri,1],
      1,includeRow[Ri,2]+
        includeRow[Ri,4]+
        includeRow[Ri,16],
      2,includeRow[Ri,8]+
        includeRow[Ri,32]+
        includeRow[Ri,64],
      3,includeRow[Ri,128]];



appSAT[Ri_]:=
    Sum[satisfyingRows[Ri,s]*x^s*(1-x)^(3-s),
      {s,0,3}];



combineTwo[R1_,R2_]:=
    t1*appSAT[R1]+t2*appSAT[R2];





maxX[F_,t1V_]:=
    Maximize[
      {F/.t1\[Rule]t1V/.t2\[Rule](1-t1V),
        0\[LessEqual]x\[LessEqual]1},
      {x}];



















maxX1[F_]:=
    With[
      {f0=F/.x\[Rule]0,
        f1=F/.x\[Rule]1},
      Piecewise[
        {{1,f1>f0}},
        0]];



maxX2[F_]:=
    With[
      {xSP=Root[(D[F,x]/.x\[Rule]#)&,1]},
      With[
        {f0=F/.x\[Rule]0,
          f1=F/.x\[Rule]1,
          fSP=F/.x\[Rule]xSP},
        Piecewise[
          {{xSP,fSP>f0&&fSP>f1},
            {1,f1>f0&&f1\[GreaterEqual]fSP}},
          0]]];



Off[General::"spell1"];
maxX3[F_]:=
    With[
      {xSP1=Root[(D[F,x]/.x\[Rule]#)&,1],
        xSP2=Root[(D[F,x]/.x\[Rule]#)&,2]},
      With[
        {f0=F/.x\[Rule]0,
          f1=F/.x\[Rule]1,
          fSP1=F/.x\[Rule]xSP1,
          fSP2=F/.x\[Rule]xSP2},
        Piecewise[{{xSP2,fSP2>f0&&fSP2>f1&&fSP2>fSP1},
            {xSP1,fSP1>f0&&fSP1>f1&&fSP1\[GreaterEqual]fSP2},
            {1,f1>f0&&f1\[GreaterEqual]fSP1&&f1\[GreaterEqual]fSP2}},
          0]]];
On[General::"spell1"];



whichMaxX[F_]:=
    Switch[Exponent[F,x],
      0,0,
      1,maxX1[F],
      2,maxX2[F],
      3,maxX3[F]];





minT[F_]:=Minimize[
      {F/.x\[Rule]whichMaxX[F],
        0\[LessEqual]t1\[LessEqual]1,
        0\[LessEqual]t2\[LessEqual]1,
        t1+t2\[Equal]1},
      {t1,t2}];



































sameDegree[F_]:=
    Exponent[F/.t1\[Rule]0/.t2\[Rule]1,x]\[Equal]
      Exponent[F/.t1\[Rule]1/.t2\[Rule]0,x];



maxCoeff[F_]:=
    Refine[Coefficient[F,x,Exponent[F,x]]\[Equal]0,
      0\[LessEqual]t1\[LessEqual]1&&0\[LessEqual]t2\[LessEqual]1&&
        t1+t2\[Equal]1];









criticalPoint[maxC_]:=
    Part[
      Solve[
        {maxC,t1+t2\[Equal]1},
        {t1,t2}],
      1,1,2];













resultAddT[F_,t1V_]:=
    FlattenAt[
      Insert[
        maxX[F,t1V],
        {t1\[Rule]t1V,t2\[Rule](1-t1V)},
        {2,1}],
      {2,1}];



Off[General::"spell1"];
resultAddX[F_]:=
    With[
      {r=minT[F]},
      Insert[r,
        Part[
          maxX[F,Part[r,2,1,2]],
          2,1],
        {2,-1}]];
On[General::"spell1"];

















minMax[R1_,R2_]:=
    With[
      {F=combineTwo[R1,R2]},
      Which[
        OddQ[R1]&&OddQ[R2],
        {1,{t1\[Rule]t1,t2\[Rule]1-t1,x\[Rule]0}},
        R1\[GreaterEqual]128&&R2\[GreaterEqual]128,
        {1,{t1\[Rule]t1,t2\[Rule]1-t1,x\[Rule]1}},
        R1\[Equal]R2,
        resultT[F,1],
        True,
        With[
          {maxC=maxCoeff[F]},
          If[
            Not[sameDegree[F]]||
              TrueQ[Not[maxC]],
            resultAddX[F],
            Part[
              Sort[
                {resultAddX[F],
                  resultAddT[F,criticalPoint[maxC]]},
                (Part[#1,1]<Part[#2,1])&],1]]]]];













