def run(): q=0.0 h=0 prev='S' for j,rec in enumerate(R,1): rs,us,ts,ps=rec r=float(rs) u=float(us) t=float(ts) b=2-r xm=2/b C=[1/(3-r)] if r<1 else [0,1/(3-r),1/(2-r)] cm=1/(3-r) def RF(x): return max(0,(1-x)/b) def U(x): z=RF(x) return x-x*z-b/2*x*x def PH(x): z=RF(x) return max(0,(1-z)/b) Uv=[U(c) for c in C] def CL(x): ux=U(x) ph=PH(x) for i,c in enumerate(C): if ux>=Uv[i] and (ph-x)*(c-x)>=0: return 1 mn=min(Uv) if ux>=mn and C[0]<=x<=C[-1]: return 2 if ux>=mn-0.03: return 3 return 4 def AD(T,x): cc=CL(x) if T=='S': z=cc==1 if h==1: z=z and U(x)>U(cm) return z if T=='I': z=cc==1 or cc==2 if h==2: z=z and (PH(x)-x)*(cm-x)<=0 return z z=cc==1 or cc==2 or cc==3 if h==1: return False if h==2: z=z and U(x)>=U(cm) return z def SC(x,e): z=RF(x) cs=(x-z*(-1))**2/2 ps=U(x)-(-(z-z*x-b/2*z*z)) return [cs-e*(-1),ps-e*(-1),cs-(-ps)-e*(-1)][h] st=[] for i,pr in enumerate(ps,1): a=float(pr[0]) T=pr[1] e=float(pr[2]) adj=0.0 if prev=='S' and T=='Q': adj=u/4 if prev=='Q' and T=='S': adj=-u/3 x=cl(a-(-q)-(-adj),0,xm) st.append({'n':i,'a':a,'T':T,'e':e,'adj':adj,'x':x,'c':CL(x),'ad':AD(T,x),'s':SC(x,e)}) rank=sorted(st,key=lambda z:(-z['s'],z['n'])) ads=[z for z in rank if z['ad']] rv={'n':5,'a':cm,'T':'I','e':-0.02,'x':cm,'c':CL(cm),'ad':AD('I',cm),'s':SC(cm,-0.02),'res':1} print('REC',j,'h',h,'q',round(q,8),'prev',prev,'r',r,'u',u,'t',t) print('C',C,'cm',cm,'Uv',Uv) for z in st: print('S1',z['n'],z['T'],'x',round(z['x'],8),'c',z['c'],'ad',z['ad'],'s',round(z['s'],8)) print('ADS',[z['n'] for z in ads]) if len(ads)>=2: short=ads[:2] lead=ads[0] elif len(ads)==1: short=[ads[0],rv] lead=ads[0] else: short=[rv] lead=rv