Best Python code snippet using autotest_python
alternative_calc_gamma3.py
Source:alternative_calc_gamma3.py
1#!usr/bin/env python2import sys3import numpy as np4import parameter as para5from weight import UP,DOWN,IN,OUT6import weight, plot7from logger import *8import r_index 9import calculator as calc10def SimpleGG(G, _map):11 #half integer tin and tout12 GammaG=np.zeros([2, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j13 G.FFT("R", "T")14 Uspin=UP15 Gspin=UP16 sub=017 r=018 for t1 in range(_map.MaxTauBin):19 tg1=t120 sign1=121 G1=G.Data[Uspin,sub,Gspin,sub,r,tg1]*sign122 for t2 in range(_map.MaxTauBin):23 tg2=-t2-124 sign2=125 if tg2<0:26 tg2+=_map.MaxTauBin27 sign2=-sign228 G2=sign2*G.Data[Gspin,sub,Uspin,sub,r,tg2]29 GammaG[Gspin, r, t1, t2]=G1*G2 30 return GammaG31def GGW(GammaG,W,_map):32 W.FFT("R","T")33 #GammaG=SimpleGG(G,_map)34 OrderSign = -135 spinUP=_map.Spin2Index(UP,UP)36 spinDOWN=_map.Spin2Index(DOWN,DOWN)37 spinUPDOWN=_map.Spin2Index(UP,DOWN)38 spinDOWNUP=_map.Spin2Index(DOWN,UP)39 sub=040 GGW=np.zeros([2, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j41 Wshift=weight.Weight("SmoothT", _map, "FourSpins", "Symmetric", "R","T")42 for t in range(_map.MaxTauBin):43 t1=t-144 if t1<0:45 t1+=_map.MaxTauBin46 Wshift.Data[:,:,:,:,:,t]=0.5*(W.Data[:,:,:,:,:,t1]+W.Data[:,:,:,:,:,t])47 for t1 in range(_map.MaxTauBin):48 for t2 in range(_map.MaxTauBin):49 dt = t1 - t250 if dt <0:51 dt = dt + _map.MaxTauBin52 GGW[UP, :, t1,t2] = GammaG[UP,:,t1,t2]*Wshift.Data[spinUP,sub,spinUP,sub,0,dt]53 GGW[UP, :, t1,t2] += GammaG[DOWN,:,t1,t2]*Wshift.Data[spinUPDOWN,sub,spinDOWNUP,sub,0,dt]54 GGW[DOWN, :, t1,t2] = GammaG[UP,:,t1,t2]*Wshift.Data[spinDOWNUP,sub,spinUPDOWN,sub,0,dt]55 GGW[DOWN, :, t1,t2] += GammaG[DOWN,:,t1,t2]*Wshift.Data[spinDOWN,sub,spinDOWN,sub,0,dt]56 return GGW*OrderSign57def AddTwoGToGammaG(GammaG, G, _map):58 #integer tin and tout59 G.FFT("R","T")60 spinUP=_map.Spin2Index(UP,UP)61 spinDOWN=_map.Spin2Index(DOWN,DOWN)62 sub=063 r=064 GGGammaG=np.zeros([2, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j65 for t in range(_map.MaxTauBin):66 for tout in range(_map.MaxTauBin):67 tgout=t-tout-168 signout=169 if tgout<0:70 tgout+=_map.MaxTauBin71 signout*=-172 Gout=0.5*signout*G.Data[:,sub,:,sub,r,tgout]73 tgout=t-tout74 signout=175 if tgout<0:76 tgout+=_map.MaxTauBin77 signout*=-178 Gout+=0.5*signout*G.Data[:,sub,:,sub,r,tgout]79 for tin in range(_map.MaxTauBin):80 GGGammaG[UP, :, t, tin]+=Gout[UP,UP]*GammaG[UP,:,tout,tin]81 GGGammaG[DOWN, :, t, tin]+=Gout[DOWN,DOWN]*GammaG[DOWN,:,tout,tin]82 GGGammaGNew=np.zeros([2, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j83 for t in range(_map.MaxTauBin):84 for tin in range(_map.MaxTauBin):85 tgin=tin-t 86 signin=187 if tgin<0:88 tgin+=_map.MaxTauBin89 signin*=-190 Gin=0.5*signin*G.Data[:,sub,:,sub,r,tgin]91 tgin=tin-t-1 92 signin=193 if tgin<0:94 tgin+=_map.MaxTauBin95 signin*=-196 Gin+=0.5*signin*G.Data[:,sub,:,sub,r,tgin]97 for tout in range(_map.MaxTauBin):98 GGGammaGNew[UP, :, tout, t]+=Gin[UP,UP]*GGGammaG[UP,:,tout,tin]99 GGGammaGNew[DOWN, :, tout, t]+=Gin[DOWN,DOWN]*GGGammaG[DOWN,:,tout,tin]100 return GGGammaGNew*_map.Beta**2/_map.MaxTauBin**2101def AddG_To_GammaG(GammaG, G, _map):102 #integer tin and tout103 G.FFT("R","T")104 FermiLoopSign=-1105 spinUP=_map.Spin2Index(UP,UP)106 spinDOWN=_map.Spin2Index(DOWN,DOWN)107 sub=0108 GGammaG = np.zeros([6, _map.Vol, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j109 for tout in range(_map.MaxTauBin):110 for tin in range(_map.MaxTauBin):111 tgout = tin - tout -1112 sign=1113 if tgout<0:114 tgout+=_map.MaxTauBin115 sign*=-1116 Gout = 0.5*sign*G.Data[:,sub,:,sub,0,tgout]117 tgout = tin - tout118 sign=1119 if tgout<0:120 tgout+=_map.MaxTauBin121 sign*=-1122 Gout += 0.5*sign*G.Data[:,sub,:,sub,0,tgout]123 for r in range(_map.Vol):124 ## UP UP UP UP125 GGammaG[0, r, r, tout, tin] = Gout[UP,UP]*GammaG[UP,r,tout,tin]126 ## DOWN DOWN DOWN DOWN127 GGammaG[1, r, r, tout, tin] = Gout[DOWN,DOWN]*GammaG[DOWN,r,tout,tin]128 ## in:UP DOWN out:DOWN UP129 GGammaG[5, r, r, tout, tin] = Gout[UP, UP]*GammaG[DOWN,r,tout,tin]130 ## in:DOWN UP out:UP DOWN 131 GGammaG[4, r, r, tout, tin] = Gout[DOWN,DOWN]*GammaG[UP,r,tout,tin]132 ### reverse133 tgout = tout - tin -1134 sign=1135 if tgout<0:136 tgout+=_map.MaxTauBin137 sign*=-1138 Gout = 0.5*sign*G.Data[:,sub,:,sub,0,tgout]139 tgout = tout - tin140 sign=1141 if tgout<0:142 tgout+=_map.MaxTauBin143 sign*=-1144 Gout += 0.5*sign*G.Data[:,sub,:,sub,0,tgout]145 for r in range(_map.Vol):146 ## UP UP UP UP147 GGammaG[0, r, r, tout, tin] += Gout[UP,UP]*GammaG[UP,r,tin,tout]148 ## DOWN DOWN DOWN DOWN149 GGammaG[1, r, r, tout, tin] += Gout[DOWN,DOWN]*GammaG[DOWN,r,tin,tout]150 ## in:UP DOWN out:DOWN UP151 GGammaG[5, r, r, tout, tin] += Gout[DOWN, DOWN]*GammaG[UP,r,tin,tout]152 ## in:DOWN UP out:UP DOWN 153 GGammaG[4, r, r, tout, tin] += Gout[UP,UP]*GammaG[DOWN,r,tin,tout]154 return FermiLoopSign*GGammaG155def GenerateSpinIndex(_map):156 UPUP=_map.Spin2Index(UP,UP)157 DOWNDOWN=_map.Spin2Index(DOWN,DOWN)158 UPDOWN=_map.Spin2Index(UP,DOWN)159 DOWNUP=_map.Spin2Index(DOWN,UP)160 spinindex=np.array([UP,DOWN])161 spin2index=np.array([UPUP,DOWNDOWN, UPDOWN, DOWNUP])162 return (spinindex, spin2index)163def FastWWGammaW(GGammaG, W0, W, _map):164 import gamma3165 sub = 0166 W.FFT("R","T")167 W0.FFT("R")168 rIndex=np.zeros([_map.Vol, _map.Vol])169 for r in range(_map.Vol):170 for rout in range(_map.Vol):171 rIndex[r, rout] = int(r_index.CoordiIndex(r, rout, _map))172 spinindex, spin2index=GenerateSpinIndex(_map)173 # print "before", GGammaG[:,0,0,0,0]174 WWGammaW=gamma3.fast_wwgammaw(GGammaG, W0.Data[:,sub,:,sub,:], W.Data[:,sub,:,sub,:,:], _map.Beta, rIndex, spinindex, spin2index, _map.Vol, _map.MaxTauBin)175 # print WWGammaW[:,0,0,0,0]176 # print "after", GGammaG[:,0,0,0,0]177 return WWGammaW178def FFTGammaW(GammaW, _map, BackForth):179 OldShape=GammaW.shape180 NewShape=(6, _map.L[0], _map.L[1], _map.L[0], _map.L[1], _map.MaxTauBin, _map.MaxTauBin)181 GammaW=GammaW.reshape(NewShape)182 if BackForth==1:183 GammaW=np.fft.fftn(GammaW, axes=[1,2,3,4,5,6]) 184 elif BackForth==-1:185 GammaW=np.fft.ifftn(GammaW, axes=[1,2,3,4,5,6]) 186 GammaW=GammaW.reshape(OldShape)187 return GammaW188def FFTWshift(Wshift, _map, BackForth):189 OldShape=Wshift.shape190 NewShape=(4,4, _map.L[0], _map.L[1], _map.MaxTauBin)191 Wshift=Wshift.reshape(NewShape)192 if BackForth==1:193 Wshift=np.fft.fftn(Wshift, axes=[2,3,4]) 194 elif BackForth==-1:195 Wshift=np.fft.ifftn(Wshift, axes=[2,3,4]) 196 Wshift=Wshift.reshape(OldShape)197 return Wshift198def FourierWWGammaW(GGammaG, W0, W, _map):199 # import gamma3200 GGammaG=np.array(GGammaG)201 sub = 0202 UPUP=_map.Spin2Index(UP,UP)203 DOWNDOWN=_map.Spin2Index(DOWN,DOWN)204 UPDOWN=_map.Spin2Index(UP,DOWN)205 DOWNUP=_map.Spin2Index(DOWN,UP)206 spinindex, spin2index=GenerateSpinIndex(_map)207 W.FFT("R","T")208 W0.FFT("R", "T")209 Wshift=weight.Weight("SmoothT", _map, "FourSpins", "Symmetric", "R","T")210 for t in range(_map.MaxTauBin):211 t1=t-1212 if t1<0:213 t1+=_map.MaxTauBin214 Wshift.Data[:,:,:,:,:,t]=0.5*(W.Data[:,:,:,:,:,t1]+W.Data[:,:,:,:,:,t])215 Wtot=np.array(Wshift.Data[:,0,:,0,:,:])*_map.Beta/_map.MaxTauBin216 Wtot[:,:,:,0]+=W0.Data[:,0,:,0,:]217 Wtot=FFTWshift(Wtot, _map, 1)218 GGammaG=FFTGammaW(GGammaG, _map, 1)219 WGammaW=np.zeros([6, _map.Vol, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j220 print "calculating WGammaW with fourier..."221 TempGammaW=np.array(GGammaG)222 TempGammaW[0,:,:,:,:]=GGammaG[0,:,:,:,:]223 TempGammaW[1,:,:,:,:]=GGammaG[1,:,:,:,:]224 TempGammaW[2,:,:,:,:]=GGammaG[1,:,:,:,:]225 TempGammaW[3,:,:,:,:]=GGammaG[0,:,:,:,:]226 TempGammaW[4,:,:,:,:]=GGammaG[5,:,:,:,:]227 TempGammaW[5,:,:,:,:]=GGammaG[4,:,:,:,:]228 Wout = np.zeros((6,Wtot.shape[2],1,Wtot.shape[3],1)) +0.0*1j229 Wout[0,:,0,:,0] = Wtot[UPUP, UPUP, :, :]230 Wout[1,:,0,:,0] = Wtot[DOWNDOWN, DOWNDOWN, :, :]231 Wout[2,:,0,:,0] = Wtot[UPUP, DOWNDOWN, :, :]232 Wout[3,:,0,:,0] = Wtot[DOWNDOWN, UPUP, :, :]233 Wout[4,:,0,:,0] = Wtot[UPDOWN, DOWNUP, :, :]234 Wout[5,:,0,:,0] = Wtot[DOWNUP, UPDOWN, :, :]235 WGammaW = Wout * TempGammaW236 # for kout in range(_map.Vol):237 # for wout in range(_map.MaxTauBin):238 239 # # UPUP UPUP240 # WGammaW[0, kout, :, wout, :] = Wout[UPUP, UPUP, kout, wout] * GGammaG[0, kout, :, wout, :]241 # # DOWNDOWN DOWNDOWN242 # WGammaW[1, kout, :, wout, :] = Wout[DOWNDOWN, DOWNDOWN, kout, wout] * GGammaG[1, kout, :, wout, :]243 # # out:UPUP in:DOWNDOWN 244 # WGammaW[2, kout, :, wout, :] = Wout[UPUP, DOWNDOWN, kout, wout] * GGammaG[1, kout, :, wout, :]245 # # out:DOWNDOWN in:UPUP246 # WGammaW[3, kout, :, wout, :] = Wout[DOWNDOWN, UPUP, kout, wout] * GGammaG[0, kout, :, wout, :]247 # # out:UPDOWN in:DOWNUP248 # WGammaW[4, kout, :, wout, :] = Wout[UPDOWN, DOWNUP, kout, wout] * GGammaG[5, kout, :, wout, :]249 # # out:DOWNUP in:UPDOWN250 # WGammaW[5, kout, :, wout, :] = Wout[DOWNUP, UPDOWN, kout, wout] * GGammaG[4, kout, :, wout, :]251 print "calculating WWGammaW with fourier..."252 Win = np.zeros((6,1,Wtot.shape[2],1,Wtot.shape[3])) +0.0*1j253 Win[0,0,:,0,:] = Wtot[UPUP, UPUP, :, :]254 Win[1,0,:,0,:] = Wtot[DOWNDOWN, UPUP, :, :]255 Win[2,0,:,0,:] = Wtot[DOWNDOWN, UPUP, :, :]256 Win[3,0,:,0,:] = Wtot[UPUP, UPUP, :, :]257 Win[4,0,:,0,:] = Wtot[UPDOWN, DOWNUP, :, :]258 Win[5,0,:,0,:] = Wtot[DOWNUP, UPDOWN, :, :]259 TempGammaW[0,:,:,:,:] = WGammaW[0,:,:,:,:]260 TempGammaW[1,:,:,:,:] = WGammaW[3,:,:,:,:]261 TempGammaW[2,:,:,:,:] = WGammaW[0,:,:,:,:]262 TempGammaW[3,:,:,:,:] = WGammaW[3,:,:,:,:]263 TempGammaW[4,:,:,:,:] = WGammaW[4,:,:,:,:]264 TempGammaW[5,:,:,:,:] = WGammaW[5,:,:,:,:]265 266 WWGammaW = Win * TempGammaW267 Win[0,0,:,0,:] = Wtot[UPUP, DOWNDOWN, :, :]268 Win[1,0,:,0,:] = Wtot[DOWNDOWN, DOWNDOWN, :, :]269 Win[2,0,:,0,:] = Wtot[DOWNDOWN, DOWNDOWN, :, :]270 Win[3,0,:,0,:] = Wtot[UPUP, DOWNDOWN, :, :]271 TempGammaW[0,...] = WGammaW[2,...]272 TempGammaW[1,...] = WGammaW[1,...]273 TempGammaW[2,...] = WGammaW[2,...]274 TempGammaW[3,...] = WGammaW[1,...]275 WWGammaW[0:4,...] += Win[0:4,...] * TempGammaW[0:4,...]276 # WWGammaW = np.zeros([6, _map.Vol, _map.Vol, _map.MaxTauBin, _map.MaxTauBin]) + 0.0*1j277 # for kin in range(_map.Vol):278 # for win in range(_map.MaxTauBin):279 # ## out:UPUP in:UPUP280 # WWGammaW[0, :, kin, :, win] = Win[UPUP, UPUP, kin, win] * WGammaW[0, :, kin, :, win] + Win[UPUP, DOWNDOWN,kin, win] * WGammaW[2, :, kin, :, win]281 # ## out:DOWNDOWN in:DOWNDOWN282 # WWGammaW[1, :, kin, :, win] = Win[DOWNDOWN, UPUP,kin, win] * WGammaW[3, :, kin, :, win] + Win[DOWNDOWN, DOWNDOWN,kin, win] * WGammaW[1, :, kin, :, win]283 # ## out:UPUP in:DOWNDOWN284 # WWGammaW[2, :, kin, :, win] = Win[DOWNDOWN, UPUP,kin, win] * WGammaW[0, :, kin, :, win] + Win[DOWNDOWN, DOWNDOWN,kin, win] * WGammaW[2, :, kin, :, win]285 # ## out:DOWNDOWN in:UPUP286 # WWGammaW[3, :, kin, :, win] = Win[UPUP, UPUP,kin, win] * WGammaW[3, :, kin, :, win] + Win[UPUP, DOWNDOWN, kin, win] * WGammaW[1, :, kin, :, win]287 # ## out:UPDOWN in:DOWNUP288 # WWGammaW[4, :, kin, :, win] = Win[UPDOWN, DOWNUP, kin, win] * WGammaW[4, :, kin, :, win]289 # ## out:DOWNUP in:UPDOWN290 # WWGammaW[5, :, kin, :, win] = Win[DOWNUP, UPDOWN, kin, win] * WGammaW[5, :, kin, :, win]291 WWGammaW=FFTGammaW(WWGammaW, _map, -1)292 W0.FFT("R","T")293 W.FFT("R","T")294 print "calculating WWGammaW with fourier done!"295 return -1.0*WWGammaW296def FastFourierWWGammaW(GGammaG, W0, W, _map):297 import gamma3298 # GGammaG=np.array(GGammaG)299 sub = 0300 UPUP=_map.Spin2Index(UP,UP)301 DOWNDOWN=_map.Spin2Index(DOWN,DOWN)302 UPDOWN=_map.Spin2Index(UP,DOWN)303 DOWNUP=_map.Spin2Index(DOWN,UP)304 spinindex, spin2index=GenerateSpinIndex(_map)305 W.FFT("R","T")306 W0.FFT("R", "T")307 Wshift=weight.Weight("SmoothT", _map, "FourSpins", "Symmetric", "R","T")308 for t in range(_map.MaxTauBin):309 t1=t-1310 if t1<0:311 t1+=_map.MaxTauBin312 Wshift.Data[:,:,:,:,:,t]=0.5*(W.Data[:,:,:,:,:,t1]+W.Data[:,:,:,:,:,t])313 Wtot=np.array(Wshift.Data[:,0,:,0,:,:])*_map.Beta/_map.MaxTauBin314 Wtot[:,:,:,0]+=W0.Data[:,0,:,0,:]315 Wtot=FFTWshift(Wtot, _map, 1)316 GGammaG=FFTGammaW(GGammaG, _map, 1)317 WWGammaW=gamma3.fast_fourier_wwgammaw(GGammaG, Wtot, _map.Beta, spinindex, spin2index, _map.Vol, _map.MaxTauBin)318 WWGammaW=FFTGammaW(WWGammaW, _map, -1)319 W0.FFT("R","T")320 W.FFT("R","T")321 return WWGammaW322def WWGammaW(GGammaG, W0, W, _map):323 sub = 0324 W.FFT("R","T")325 W0.FFT("R")326 UPUP=_map.Spin2Index(UP,UP)327 DOWNDOWN=_map.Spin2Index(DOWN,DOWN)328 UPDOWN=_map.Spin2Index(UP,DOWN)329 DOWNUP=_map.Spin2Index(DOWN,UP)330 print "calculating WGammaW..."331 WGammaW=np.zeros([6, _map.Vol, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j332 for r in range(_map.Vol):333 for rout in range(_map.Vol):334 dr_out = int(r_index.CoordiIndex(r, rout, _map))335 for t in range(_map.MaxTauBin):336 for tout in range(_map.MaxTauBin):337 dt_out = t - tout -1338 if dt_out<0:339 dt_out+=_map.MaxTauBin340 Wout = 0.5*W.Data[:,sub,:,sub,dr_out,dt_out]*(_map.Beta/_map.MaxTauBin)341 dt_out = t - tout342 if dt_out<0:343 dt_out+=_map.MaxTauBin344 Wout += 0.5*W.Data[:,sub,:,sub,dr_out,dt_out]*(_map.Beta/_map.MaxTauBin)345 if t == tout:346 Wout += W0.Data[:,sub,:,sub,dr_out]347 #For Delta W test348 # if t!= tout:349 # continue350 # else:351 # Wout = W0.Data[:,sub,:,sub,dr_out]352 # UPUP UPUP353 WGammaW[0, r, :, t, :] += Wout[UPUP, UPUP] * GGammaG[0, rout, :, tout, :]354 # DOWNDOWN DOWNDOWN355 WGammaW[1, r, :, t, :] += Wout[DOWNDOWN, DOWNDOWN] * GGammaG[1, rout, :, tout, :]356 # out:UPUP in:DOWNDOWN 357 WGammaW[2, r, :, t, :] += Wout[UPUP, DOWNDOWN] * GGammaG[1, rout, :, tout, :]358 # out:DOWNDOWN in:UPUP359 WGammaW[3, r, :, t, :] += Wout[DOWNDOWN, UPUP] * GGammaG[0, rout, :, tout, :]360 # out:UPDOWN in:DOWNUP361 WGammaW[4, r, :, t, :] += Wout[UPDOWN, DOWNUP] * GGammaG[5, rout, :, tout, :]362 # out:DOWNUP in:UPDOWN363 WGammaW[5, r, :, t, :] += Wout[DOWNUP, UPDOWN] * GGammaG[4, rout, :, tout, :]364 # print "ConventionalWGammaW, type0, tau1=0, dyson=\n", WGammaW[0, 1, 1, 0, :]365 # print "ConventionalWGammaW, type0, tau1=1, dyson=\n", WGammaW[0, 1, 1, 1, :]366 # print "ConventionalWGammaW, type0, tau2=0, dyson=\n", WGammaW[0, 1, 1, :, 0]367 # print "ConventionalWGammaW, type0, tau2=1, dyson=\n", WGammaW[0, 1, 1, :, 1]368 # print "ConventionalWGammaW, type0, diagonal, dyson=\n", WGammaW[0, 1, 1, :, :].diagonal()369 print "calculating WWGammaW..."370 WWGammaW = np.zeros([6, _map.Vol, _map.Vol, _map.MaxTauBin, _map.MaxTauBin]) + 0.0*1j371 for r in range(_map.Vol):372 for rin in range(_map.Vol):373 dr_in = int(r_index.CoordiIndex(rin, r, _map))374 for t in range(_map.MaxTauBin):375 for tin in range(_map.MaxTauBin):376 dt_in = tin - t377 if dt_in < 0:378 dt_in +=_map.MaxTauBin379 Win = 0.5*W.Data[:,sub,:,sub,dr_in,dt_in]*(_map.Beta/_map.MaxTauBin)380 dt_in = tin - t-1381 if dt_in < 0:382 dt_in +=_map.MaxTauBin383 Win += 0.5*W.Data[:,sub,:,sub,dr_in,dt_in]*(_map.Beta/_map.MaxTauBin)384 if t == tin:385 Win += W0.Data[:,sub,:,sub, dr_in]386 ##For DeltaW test387 # if t!= tin:388 # continue389 # else:390 # Win = W0.Data[:,sub,:,sub,dr_in]391 ## out:UPUP in:UPUP392 WWGammaW[0, :, r, :, t] += Win[UPUP, UPUP] * WGammaW[0, :, rin, :, tin]393 WWGammaW[0, :, r, :, t] += Win[UPUP, DOWNDOWN] * WGammaW[2, :, rin, :, tin]394 ## out:DOWNDOWN in:DOWNDOWN395 WWGammaW[1, :, r, :, t] += Win[DOWNDOWN, UPUP] * WGammaW[3, :, rin, :, tin]396 WWGammaW[1, :, r, :, t] += Win[DOWNDOWN, DOWNDOWN] * WGammaW[1, :, rin, :, tin]397 ## out:UPUP in:DOWNDOWN398 WWGammaW[2, :, r, :, t] += Win[DOWNDOWN, UPUP] * WGammaW[0, :, rin, :, tin]399 WWGammaW[2, :, r, :, t] += Win[DOWNDOWN, DOWNDOWN] * WGammaW[2, :, rin, :, tin]400 ## out:DOWNDOWN in:UPUP401 WWGammaW[3, :, r, :, t] += Win[UPUP, UPUP] * WGammaW[3, :, rin, :, tin]402 WWGammaW[3, :, r, :, t] += Win[UPUP, DOWNDOWN] * WGammaW[1, :, rin, :, tin]403 ## out:UPDOWN in:DOWNUP404 WWGammaW[4, :, r, :, t] += Win[UPDOWN, DOWNUP] * WGammaW[4, :, rin, :, tin]405 ## out:DOWNUP in:UPDOWN406 WWGammaW[5, :, r, :, t] += Win[DOWNUP, UPDOWN] * WGammaW[5, :, rin, :, tin]407 return -1.0*WWGammaW408def GammaWToGammaG(GammaW, G, _map):409 #integer tin and tout410 G.FFT("R","T")411 spinUP=_map.Spin2Index(UP,UP)412 spinDOWN=_map.Spin2Index(DOWN,DOWN)413 sub=0414 GGammaW = np.zeros([2, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j415 for r in range(_map.Vol):416 for tout in range(_map.MaxTauBin):417 for tin in range(_map.MaxTauBin):418 tgout = tout - tin -1419 sign=1420 if tgout<0:421 tgout+=_map.MaxTauBin422 sign*=-1423 Gout = 0.5*sign*G.Data[:,sub,:,sub,0,tgout]424 tgout = tout - tin425 sign=1426 if tgout<0:427 tgout+=_map.MaxTauBin428 sign*=-1429 Gout += 0.5*sign*G.Data[:,sub,:,sub,0,tgout]430 GGammaW[UP, r, tout, tin] += Gout[UP,UP]*GammaW[0,r,r,tout,tin]431 GGammaW[UP, r, tout, tin] += Gout[DOWN,DOWN]*GammaW[5,r,r,tout,tin]432 GGammaW[DOWN, r, tout, tin] += Gout[DOWN,DOWN]*GammaW[1,r,r,tout,tin]433 GGammaW[DOWN, r, tout, tin] += Gout[UP,UP]*GammaW[4,r,r,tout,tin]434 # GammaG=AddTwoGToGammaG(GGammaW, G, _map)435 # return GammaG436 return GGammaW437def shift(r, L):438 if r<0:439 r+=L440 elif r>=L:441 r-=L442 return r443def FastGammaG_RPA(GammaG, G, W0, _map):444 import gamma3445 #OrderSign=-1, FermiLoopSign=-1, therefore TotalSign=1446 #integer tin and tout447 print "calculating FirstOrder GammaG using fortran fast RPA..."448 W0.FFT("R", "T")449 G.FFT("R", "T")450 spinindex, spin2index=GenerateSpinIndex(_map)451 rIndex=np.zeros([_map.Vol, _map.Vol])452 sub=0453 GammaGNew=np.zeros([2, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j454 for r in range(_map.Vol):455 for rout in range(_map.Vol):456 rIndex[r, rout] = int(r_index.CoordiIndex(r, rout, _map))457 GammaGNew=gamma3.fast_gammag_rpa(GammaGNew, GammaG, G.Data[:,sub,:,sub,0,:], W0.Data[:,sub,:,sub,:], _map.Beta, rIndex, spinindex, spin2index, _map.Vol, _map.MaxTauBin)458 # print GammaGNew459 print "calculating FirstOrder GammaG done!"460 return GammaGNew461def Calculate_RPA(Chi, Polar, W0, _map):462 ChiNew=np.zeros([2, _map.Vol, _map.MaxTauBin])+0.0*1j463 spinUP=_map.Spin2Index(UP,UP)464 spinDOWN=_map.Spin2Index(DOWN,DOWN)465 print "calculating RPA..."466 # # # print "GammaG[UP,UP]=\n", GammaG[UP,0,:,-1]467 W0.FFT("R", "T")468 sub=0469 r=0470 Neighbors=[]471 Lx, Ly=_map.L472 for Gx in range(Lx):473 for dx in [-1,0,1]:474 x=shift(Gx+dx, Lx)475 for Gy in range(Ly):476 for dy in [-1,0,1]:477 y=shift(Gy+dy, Ly)478 dx_shift=shift(dx, Lx)479 dy_shift=shift(dy, Ly)480 i=_map.CoordiIndex([Gx,Gy])481 j=_map.CoordiIndex([x,y])482 k=_map.CoordiIndex([dx_shift,dy_shift])483 Neighbors.append([i,j,W0.Data[:,sub,:,sub,k]])484 # print W0.Data[spinUP,sub,spinUP,sub,k], dx, dy485 # print W0.Data[:,sub,:,sub,k], dx, dy486 AvgPolar = np.zeros(Polar.shape)+0.0*1j487 for t in range(_map.MaxTauBin):488 t1 = t - 1489 if t1 < 0:490 t1 += _map.MaxTauBin491 AvgPolar[:, :, t]=0.5*( Polar[:, :, t1] + Polar[:, :, t])492 for t3 in range(_map.MaxTauBin):493 for tout in range(_map.MaxTauBin):494 dtout = tout-t3495 if dtout<0:496 dtout += _map.MaxTauBin497 for r in range(_map.Vol):498 for r1, r2, V in Neighbors:499 dr = int(r_index.CoordiIndex(r, r1, _map))500 ChiNew[UP,r,tout] += ( AvgPolar[UP,dr,dtout] * V[spinUP,spinUP] + AvgPolar[DOWN,dr,dtout] * V[spinDOWN,spinUP]) * Chi[UP, r2, t3] 501 ChiNew[UP,r,tout] += ( AvgPolar[UP,dr,dtout] * V[spinUP,spinDOWN] + AvgPolar[DOWN,dr,dtout] * V[spinDOWN,spinDOWN]) * Chi[DOWN, r2, t3] 502 ChiNew[DOWN,r,tout] += ( AvgPolar[DOWN,dr,dtout] * V[spinUP,spinUP] + AvgPolar[UP,dr,dtout] * V[spinDOWN,spinUP]) * Chi[UP, r2, t3] 503 ChiNew[DOWN,r,tout] += ( AvgPolar[DOWN,dr,dtout] * V[spinUP,spinDOWN] + AvgPolar[UP,dr,dtout] * V[spinDOWN,spinDOWN]) * Chi[DOWN, r2, t3] 504 ChiNew*=_map.Beta/_map.MaxTauBin*(-1.0)505 print "calculating RPA done!"506 return ChiNew507def GammaG_FirstOrder(GammaG, G, W0, _map):508 #OrderSign=-1, FermiLoopSign=-1, therefore TotalSign=1509 #integer tin and tout510 GG=SimpleGG(G,_map)511 GammaGNew=np.zeros([2, _map.Vol, _map.MaxTauBin, _map.MaxTauBin])+0.0*1j512 spinUP=_map.Spin2Index(UP,UP)513 spinDOWN=_map.Spin2Index(DOWN,DOWN)514 print "calculating FirstOrder GammaG..."515 # # # print "GammaG[UP,UP]=\n", GammaG[UP,0,:,-1]516 W0.FFT("R", "T")517 G.FFT("R", "T")518 sub=0519 r=0520 Neighbors=[]521 Lx, Ly=_map.L522 for Gx in range(Lx):523 for dx in [-1,0,1]:524 x=shift(Gx+dx, Lx)525 #if x<0:526 #x+=Lx527 #elif x>=Lx:528 #x-=Lx529 for Gy in range(Ly):530 for dy in [-1,0,1]:531 y=shift(Gy+dy, Ly)532 #if y<0:533 #y+=Ly534 #elif y>=Ly:535 #y-=Ly536 dx_shift=shift(dx, Lx)537 dy_shift=shift(dy, Ly)538 #if dx<0539 #dx_shift+=Lx540 #if dy<0:541 #dy_shift+=Ly542 i=_map.CoordiIndex([Gx,Gy])543 j=_map.CoordiIndex([x,y])544 k=_map.CoordiIndex([dx_shift,dy_shift])545 Neighbors.append([i,j,W0.Data[:,sub,:,sub,k]])546 # print W0.Data[spinUP,sub,spinUP,sub,k], dx, dy547 # print W0.Data[:,sub,:,sub,k], dx, dy548 for t3 in range(_map.MaxTauBin):549 dt=t3550 Polar=0.5*GammaG[:, :, dt, dt]551 dt=t3-1552 if dt<0:553 dt+=_map.MaxTauBin554 Polar+=0.5*GammaG[:, :, dt, dt]555 556 for tin in range(_map.MaxTauBin):557 # dtin=t3-tin558 # sign=1559 # if dtin<0:560 # dtin+=_map.MaxTauBin561 # sign*=-1562 # G1=0.5*sign*G.Data[UP,sub,UP,sub,0,dtin]563 dtin=t3-tin-1564 sign=1565 if dtin<0:566 dtin+=_map.MaxTauBin567 sign*=-1568 # G1+=0.5*sign*G.Data[UP,sub,UP,sub,0,dtin]569 G1=sign*G.Data[UP,sub,UP,sub,0,dtin]570 for tout in range(_map.MaxTauBin):571 # dtout=tout-t3-1572 # sign=1573 # if dtout<0:574 # dtout+=_map.MaxTauBin575 # sign*=-1576 # G2=0.5*sign*G.Data[UP,sub,UP,sub,0,dtout]577 dtout=tout-t3578 sign=1579 if dtout<0:580 dtout+=_map.MaxTauBin581 sign*=-1582 # G2+=0.5*sign*G.Data[UP,sub,UP,sub,0,dtout]583 G2=sign*G.Data[UP,sub,UP,sub,0,dtout]584 GG=G1*G2585 586 for r1,r2,V in Neighbors:587 # GammaGNew[UP,r1,tout,tin]+=GG*(V[spinUP,spinUP]*GammaG[UP,r2,t3,t3]+V[spinUP,spinDOWN]*GammaG[DOWN,r2,t3,t3])588 # GammaGNew[DOWN,r1,tout,tin]+=GG*(V[spinDOWN,spinUP]*GammaG[UP,r2,t3,t3]+V[spinDOWN,spinDOWN]*GammaG[DOWN,r2,t3,t3])589 GammaGNew[UP,r1,tout,tin] += GG *(V[spinUP,spinUP] *Polar[UP,r2] + V[spinUP,spinDOWN] * Polar[DOWN,r2])590 GammaGNew[DOWN,r1,tout,tin] += GG *(V[spinDOWN,spinUP] *Polar[UP,r2] + V[spinDOWN,spinDOWN] * Polar[DOWN,r2])591 GammaGNew*=_map.Beta/_map.MaxTauBin592 print "calculating FirstOrder GammaG done!"593 return GammaGNew594def FullGammaG(IrGammaG, W0, _map):595 sub=0596 BKPolar=weight.Weight("SmoothT", _map, "FourSpins", "Symmetric","R","T")597 UPUP=_map.Spin2Index(UP,UP)598 DOWNDOWN=_map.Spin2Index(DOWN,DOWN)599 UPDOWN=_map.Spin2Index(UP,DOWN)600 DOWNUP=_map.Spin2Index(DOWN,UP)601 IrGammaGuu=np.zeros((_map.Vol, _map.MaxTauBin))+0.0*1j602 IrGammaGdu=np.zeros((_map.Vol, _map.MaxTauBin))+0.0*1j603 for i in range(_map.MaxTauBin):604 IrGammaGuu[:, i]=IrGammaG[UP, :, i, i]605 IrGammaGdu[:, i]=IrGammaG[DOWN, :, i, i]606 BKPolar.Data[UPUP, sub, UPUP, sub, :,:]=IrGammaGuu 607 BKPolar.Data[DOWNDOWN, sub, DOWNDOWN, sub, :,:]=IrGammaGuu 608 BKPolar.Data[DOWNDOWN, sub, UPUP, sub, :,:]=IrGammaGdu 609 BKPolar.Data[UPUP, sub, DOWNDOWN, sub, :,:]=IrGammaGdu 610 # print "BKPolar[UP,UP]=\n", BKPolar.Data[UPUP,0,UPUP,0,0,:]611 BKPolar.FFT("K", "W")612 W0.FFT("K")613 Denorm,JP=calc.Calculate_Denorminator(W0, BKPolar, _map)614 # JPJ=np.einsum("ijklvt,klmnv->ijmnvt", JP, W0.Data)615 BKChiTensor=weight.Weight("SmoothT", _map, "FourSpins", "Symmetric", "K","W")616 lu_piv,Determ=weight.LUFactor(Denorm)617 Check_Denorminator(Denorm, Determ, _map)618 BKChiTensor.LUSolve(lu_piv, -BKPolar.Data)619 return BKChiTensor, Determ620def Calculate_Chi(ChiTensor, _map):621 NSpin, NSublat=ChiTensor.NSpin, ChiTensor.NSublat622 SxSx=np.zeros((NSpin,NSpin))623 SySy=np.zeros((NSpin,NSpin))624 SzSz=np.zeros((NSpin,NSpin))625 UU=_map.Spin2Index(UP,UP) 626 UD=_map.Spin2Index(UP,DOWN) 627 DU=_map.Spin2Index(DOWN, UP) 628 DD=_map.Spin2Index(DOWN, DOWN) 629 SxSx[UD, UD]=SxSx[DU, DU]=1630 SxSx[UD, DU]=SxSx[DU, UD]=1631 SySy[UD, UD]= SySy[DU, DU]=-1632 SySy[UD, DU]= SySy[DU, UD]=1633 SzSz[UU, UU]= SzSz[DD, DD]=1634 SzSz[UU, DD]= SzSz[DD, UU]=-1635 Chi=weight.Weight("SmoothT", _map, "NoSpin", "Symmetric", ChiTensor.SpaceDomain, ChiTensor.TimeDomain)636 Chi.Data=0.0637 SS=[SzSz/4.0]638 for i in range(len(SS)):639 temp=np.einsum("ik, kminvt->mnvt", SS[i], ChiTensor.Data)640 Chi.Data+=temp.reshape([1, NSublat, 1, NSublat, _map.Vol, _map.MaxTauBin]) 641 return Chi642def Check_Denorminator(Denorm, Determ, _map):643 pos=np.where(Determ==Determ.min())644 x,t=pos[0][0], pos[1][0]645 log.info("Checking denorminator of GammaG")646 log.info("The minmum {0} is at K={1} and Omega={2}".format(Determ.min(), _map.IndexToCoordi(x), t))647 SpSub,Vol,Time=Denorm.shape[0]*Denorm.shape[1], Denorm.shape[-2], Denorm.shape[-1]648 Denorm=Denorm.reshape([SpSub,SpSub,Vol,Time])649 log.info("The 1/linalg.cond is {0}".format(1.0/np.linalg.cond(Denorm[...,x,t])))650 if Determ.min().real<0.0 and Determ.min().imag<1.0e-4:...
builder.py
Source:builder.py
...42 self._fix_building(width, height)43 self._fix_street()44 self._fix_road(width, height)45 self._fix_facing()46 def mini_map(self) -> str:47 return str(self)48 def publish(self) -> None:49 cache: Mapping[str, dict] = {}50 for x in range(0, self._map.width):51 for y in range(0, self._map.height):52 if not self._map.empty(x, y):53 entity = Entity.eid()54 cache[f'{x}x{y}'] = {55 'entity': entity,56 'vertex': Vertex(x, y)57 }58 Stats.update_text(59 'room',60 entity,61 Stats.text(62 'entity', 63 self._map.cell(x, y).name.lower()64 )65 )66 for c in cache.values():67 exits = {}68 entity = c['entity']69 vertex = c['vertex']70 block = self._map.cell(vertex.x, vertex.y)71 if block in (72 Block.STREET_V,73 Block.CROSSROAD,74 Block.CORNER_SW,75 Block.CORNER_SE,76 Block.T_BOTTOM,77 Block.T_LEFT,78 Block.T_RIGHT79 ):80 if vertex.y > 0:81 exits['n'] = cache[f'{vertex.x}x{vertex.y - 1}']['entity']82 if block in (83 Block.STREET_V,84 Block.CROSSROAD,85 Block.CORNER_NW,86 Block.CORNER_NE,87 Block.T_TOP,88 Block.T_LEFT,89 Block.T_RIGHT90 ):91 if vertex.y < self._map.height - 1:92 exits['s'] = cache[f'{vertex.x}x{vertex.y + 1}']['entity']93 if block in (94 Block.STREET_H,95 Block.CROSSROAD,96 Block.CORNER_SW,97 Block.CORNER_NW,98 Block.T_TOP,99 Block.T_BOTTOM,100 Block.T_LEFT101 ):102 if vertex.x < self._map.width - 1:103 exits['e'] = cache[f'{vertex.x + 1}x{vertex.y}']['entity']104 if block in (105 Block.STREET_H,106 Block.CROSSROAD,107 Block.CORNER_SE,108 Block.CORNER_NE,109 Block.T_TOP,110 Block.T_BOTTOM,111 Block.T_RIGHT112 ):113 if vertex.x > 0:114 exits['w'] = cache[f'{vertex.x - 1}x{vertex.y}']['entity']115 Stats.update_text('exit', entity, exits)116 for b in self._buildings:117 if b.facing == Facing.EAST:118 exit = { 'e': cache[f'{b.x + 1}x{b.y}']['entity'] }119 elif b.facing == Facing.WEST:120 exit = { 'w': cache[f'{b.x - 1}x{b.y}']['entity'] }121 elif b.facing == Facing.NORTH:122 exit = { 'n': cache[f'{b.x}x{b.y - 1}']['entity'] }123 elif b.facing == Facing.SOUTH:124 exit = { 's': cache[f'{b.x}x{b.y + 1}']['entity'] }125 Stats.update_text('exit', cache[f'{b.x}x{b.y}']['entity'], exits)126 Stats.save('exit')127 Stats.save('room')128 f = Path(f'./tools/data/{Entity.eid()}.map')129 with f.open(mode='w', encoding='utf-8') as fout:130 fout.write(self.mini_map())131 def _check_facing(self, s: Vertex, d: Vertex) -> Facing:132 facing: Facing = Facing.NORTH133 if s.x > d.x:134 facing = Facing.WEST135 elif s.x < d.x:136 facing = Facing.EAST137 elif s.y > d.y:138 facing = Facing.NORTH139 else:140 facing = Facing.SOUTH141 return facing142 def _fix_building(self, w: int, h: int):143 x = randint(int(w * .20), int(w * .80))144 y = randint(int(h * .20), int(h * .80))145 for b in Zone.BUILDING:146 while (147 not self._map.cell(x, y) == Block.BLOCK or148 self._on_axis(x, y)149 ):150 x = randint(int(w * .20), int(w * .80))151 y = randint(int(h * .20), int(h * .80))152 self._map.update(x, y, b)153 self._buildings.append(Building(x, y, b))154 def _fix_corner_b2t(self, x: int, y: int) -> None:155 if self._map.empty(x - 1, y):156 if self._map.empty(x + 1, y):157 b = Block.STREET_V158 else:159 b = Block.CORNER_SW160 else:161 if self._map.empty(x + 1, y):162 b = Block.CORNER_SE163 else:164 b = Block.T_BOTTOM165 self._map.update(x, y, b)166 def _fix_corner_l2r(self, x: int, y: int) -> None:167 if self._map.empty(x, y - 1):168 if self._map.empty(x, y + 1):169 b = Block.STREET_H170 else:171 b = Block.CORNER_NW172 else:173 if self._map.empty(x, y + 1):174 b = Block.CORNER_SW175 else:176 b = Block.T_LEFT177 self._map.update(x, y, b)178 def _fix_corner_r2l(self, x: int, y: int) -> None:179 if self._map.empty(x, y - 1):180 if self._map.empty(x, y + 1):181 b = Block.STREET_H182 else:183 b = Block.CORNER_NE184 else:185 if self._map.empty(x, y + 1):186 b = Block.CORNER_SE187 else:188 b = Block.T_RIGHT189 self._map.update(x, y, b)190 def _fix_corner_t2b(self, x: int, y: int) -> None:191 if self._map.empty(x - 1, y):192 if self._map.empty(x + 1, y):193 b = Block.STREET_V194 else:195 b = Block.CORNER_NW196 else:197 if self._map.empty(x + 1, y):198 b = Block.CORNER_NE199 else:200 b = Block.T_TOP201 self._map.update(x, y, b)202 def _fix_facing(self):203 for b in self._buildings:204 if not b.facing == Facing.NORTH:205 block = self._map.cell(b.x, b.y - 1)206 if block == Block.CROSSROAD:207 self._map.update(b.x, b.y - 1, Block.T_BOTTOM)208 elif block == Block.T_TOP:209 self._map.update(b.x, b.y - 1, Block.STREET_H)210 if not b.facing == Facing.WEST:211 block = self._map.cell(b.x - 1, b.y)212 if block == Block.CROSSROAD:213 self._map.update(b.x - 1, b.y, Block.T_RIGHT)214 elif block == Block.T_LEFT:215 self._map.update(b.x - 1, b.y, Block.STREET_V)216 if not b.facing == Facing.SOUTH:217 block = self._map.cell(b.x, b.y + 1)218 if block == Block.CROSSROAD:219 self._map.update(b.x, b.y + 1, Block.T_TOP)220 elif block == Block.T_BOTTOM:221 self._map.update(b.x, b.y + 1, Block.STREET_H)222 if not b.facing == Facing.EAST:223 block = self._map.cell(b.x + 1, b.y)224 if block == Block.CROSSROAD:225 self._map.update(b.x + 1, b.y, Block.T_LEFT)226 elif block == Block.T_RIGHT:227 self._map.update(b.x + 1, b.y, Block.STREET_V)228 def _fix_hline(self, y: int, s: int, d: int) -> None:229 for x in range(s, d):230 if self._map.empty(x, y - 1):231 if self._map.empty(x, y + 1):232 self._map.update(x, y, Block.STREET_H)233 else:234 self._map.update(x, y, Block.T_TOP)235 else:236 if self._map.empty(x, y + 1):237 self._map.update(x, y, Block.T_BOTTOM)238 else:239 self._map.update(x, y, Block.CROSSROAD)240 def _fix_lane(self, path: List[Vertex]) -> None:241 u = path[0]242 for v in path[1:]:243 if v.x == u.x:244 self._map.update(v.x, v.y, Block.STREET_V)245 else:246 self._map.update(v.x, v.y, Block.STREET_H)247 u = v248 def _fix_path(self, path: List[Vertex]) -> None:249 u = path[0]250 for i in range(1, len(path) - 1):251 v = path[i]252 t = path[i + 1]253 if v.y > t.y:254 if v.x < u.x:255 self._map.update(v.x, v.y, Block.CORNER_SW)256 elif v.x > u.x:257 self._map.update(v.x, v.y, Block.CORNER_SE)258 else:259 self._map.update(v.x, v.y, Block.STREET_V)260 elif v.y < t.y:261 if v.x < u.x:262 self._map.update(v.x, v.y, Block.CORNER_NW)263 elif v.x > u.x:264 self._map.update(v.x, v.y, Block.CORNER_NE)265 else:266 self._map.update(v.x, v.y, Block.STREET_V)267 elif v.x < t.x:268 if v.y < u.y:269 self._map.update(v.x, v.y, Block.CORNER_NW)270 elif v.y > u.y:271 self._map.update(v.x, v.y, Block.CORNER_SW)272 else:273 self._map.update(v.x, v.y, Block.STREET_H)274 elif v.x > t.x:275 if v.y < u.y:276 self._map.update(v.x, v.y, Block.CORNER_NE)277 elif v.y > u.y:278 self._map.update(v.x, v.y, Block.CORNER_SE)279 else:280 self._map.update(v.x, v.y, Block.STREET_H)281 u = v282 def _fix_road(self, w: int, h: int) -> None:283 x = self._crossroads[0].x284 y = 0285 if randint(0, 1):286 while (287 self._map.cell(x, y) == Block.BLOCK and288 y < self._crossroads[0].y289 ):290 y += 1291 self._fix_corner_t2b(x, y)292 self._fix_vline(x, y + 1, h)293 else:294 y = h - 1295 while (296 self._map.cell(x, y) == Block.BLOCK and297 y > self._crossroads[1].y298 ):299 y -= 1300 self._fix_corner_b2t(x, y)301 self._fix_vline(x, 0, y)302 x = self._crossroads[1].x303 y = 0304 if randint(0, 1):305 while (306 self._map.cell(x, y) == Block.BLOCK and307 y < self._crossroads[0].y308 ):309 y += 1310 self._fix_corner_t2b(x, y)311 self._fix_vline(x, y + 1, h)312 else:313 y = h - 1314 while (315 self._map.cell(x, y) == Block.BLOCK and316 y > self._crossroads[1].y317 ):318 y -= 1319 self._fix_corner_b2t(x, y)320 self._fix_vline(x, 0, y)321 x = 0322 y = self._crossroads[0].y323 if randint(0, 1):324 while (325 self._map.cell(x, y) == Block.BLOCK and326 x < self._crossroads[0].x327 ):328 x += 1329 self._fix_corner_l2r(x, y)330 self._fix_hline(y, x + 1, w)331 else:332 x = w - 1333 while (334 self._map.cell(x, y) == Block.BLOCK and335 x > self._crossroads[1].x336 ):337 x -= 1338 self._fix_corner_r2l(x, y)339 self._fix_hline(y, 0, x)340 x = 0341 y = self._crossroads[1].y342 if randint(0, 1):343 while (344 self._map.cell(x, y) == Block.BLOCK and345 x < self._crossroads[0].x346 ):347 x += 1348 self._fix_corner_l2r(x, y)349 self._fix_hline(y, x + 1, w)350 else:351 x = w - 1352 while (353 self._map.cell(x, y) == Block.BLOCK and354 x > self._crossroads[1].x355 ):356 x -= 1357 self._fix_corner_r2l(x, y)358 self._fix_hline(y, 0, x)359 def _fix_street(self):360 for src in self._buildings:361 pathes = [362 self._map.find_path(src, dst) for dst in (363 Vertex(src.x, self._crossroads[0].y),364 Vertex(src.x, self._crossroads[1].y),365 Vertex(self._crossroads[0].x, src.y),366 Vertex(self._crossroads[1].x, src.y)367 )368 ]369 cost = len(pathes[0])370 index = 0371 for i, path in enumerate(pathes):372 if len(path) < cost:373 cost = len(path)374 index = i375 elif len(path) == cost:376 index = index if randint(0, 1) else i377 path = pathes[index]378 self._fix_lane(path)379 src.facing = self._check_facing(path[0], path[1])380 self._fix_path(path)381 def _fix_vline(self, x: int, s: int, d: int) -> None:382 for y in range(s, d):383 if self._map.empty(x - 1, y):384 if self._map.empty(x + 1, y):385 self._map.update(x, y, Block.STREET_V)386 else:387 self._map.update(x, y, Block.T_LEFT)388 else:389 if self._map.empty(x + 1, y):390 self._map.update(x, y, Block.T_RIGHT)391 else:392 self._map.update(x, y, Block.CROSSROAD)393 def _on_axis(self, x: int, y: int) -> bool:394 on = True395 for v in self._crossroads:396 if x == v.x or y == v.y:397 break398 else:399 on = False400 return on401 def __repr__(self) -> str:402 return str(self._map)403if __name__ == '__main__':404 zone = Zone(19, 19)405 zone.publish()406 print(zone.mini_map())...
UniquePathII.py
Source:UniquePathII.py
1"""2A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).3The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).4Now consider if some obstacles are added to the grids. How many unique paths would there be?5An obstacle and empty space is marked as 1 and 0 respectively in the grid.6Note: m and n will be at most 100.7Example 1:8Input:9[10 [0,0,0],11 [0,1,0],12 [0,0,0]13]14Output: 215Explanation:16There is one obstacle in the middle of the 3x3 grid above.17There are two ways to reach the bottom-right corner:181. Right -> Right -> Down -> Down192. Down -> Down -> Right -> Right20ä¸Unique Path ç¸æ¯å°±æ¯å¤äºä¸éçè·¯ï¼è¿ä¸ªåè¾¹çå¤ççæææ¯ä¸æ ·çãä¸è¿ä¸éæ使ç¨çæ°å1ï¼æ以é¢å¤çä¸ä¸ã21å
¶ä»çä¸åä¹å¤ï¼22å¨1ä¸ä¼å° 0 0 å为1ï¼ä½å¦ææ¤å¤ä¹ç´æ¥ä½¿ç¨çè¯ä¼é æä¸é¢æ
åµçé误ï¼23[[1]]æ¶é¢å¤ç为[['x']]ï¼ä½éåå¦æä¸åå¤çä¼ç´æ¥å为path为1ï¼ä½ç»ææ¯0ã24åæ ·çè¿æ[[1,0]]ï¼[[0,1]]25[0, 1]26[1, 0]27éè¦æ³¨æçç¹ï¼281. åªè¦å¤æå¼å§/ç»æç¹ä¸º 1 å¯ä»¥ç´æ¥è¿å 0ã292. å¦ææ¤ç¹ä¸éåä¸å¿
è¿è¡æ¤ç¹çå åè¿ç®ã30ä¼åå°ç»ï¼311. å°½éé¿å
å¤ä¸ªå¤æã32 if x + y == 0 and i + j == 0:33 _map[i][j] = 134 else:35 _map[i][j] = x + y36æ¯æ¬¡é½éè¦ä¸¤æ¬¡å¤æï¼37 if x + y != 0:38 _map[i][j] = x + y39 elif i + j == 0:40 _map[i][j] = 141æ¹æè¿æ ·æææ¯ä¸æ ·ç,ä½åå°äºæ¯æ¬¡æå¿
é¡»è¿è¡çå¤ææ°éã42æ¹å¨åçæææåææ¾ï¼43第ä¸ä¸ªå¹³åèæ¶ 36msã44第äºä¸ªåªæ 24msã452. å
å®æåä¼åã46ä¸è¦æ³å¤ªå¤ï¼å
å®æåå¨ä¼åé度ã47æå¼å§çåæ³æ¯ 两个å¤æ + é¢å¤ç 1 + åªå¤æå
¥å£ï¼èµ°å®äºåå¤æåºå£ã48è·éä¹åï¼åç°å¯ä»¥ç´æ¥å¤æåºå£æ¯ä¸æ¯ä¹ä¸º 1ã49两个å¤æä¹å¯ä»¥ç¼©ä¸º1~2个ã50é¢å¤ç1æ´æ¯ä¸å¿
è¦ã51ä½å¦æåè¿æ¥ï¼æä¸è¿è¡é¢å¤ç 1ï¼ä¸å
å两个å¤æãåèµ·æ¥æ¯å¾é¾çï¼å¾å¤å¼èçå°æ¹é½æ¯æ¨¡ç³çã52ç°å¨è¿ä¸ªçæ¬å·²ç»å¯ä»¥äºï¼è¿å¯ä»¥ä¼åçå°æ¹ï¼53é¢å¤ç第ä¸æ¨ªæåç«æï¼è¿æ ·å¯ä»¥ä¸éè¦è¾¹çå¤æï¼ä¹å°±ä¼åå°3~4个å¤ææ°éã54æµè¯å°åï¼55https://leetcode.com/problems/unique-paths-ii/description/56beat 76%57"""58class Solution(object):59 def uniquePathsWithObstacles(self, _map):60 """61 :type obstacleGrid: List[List[int]]62 :rtype: int63 """64 65 if _map[0][0] == 1:66 return 067 68 if _map[-1][-1] == 1:69 return 070 71 72 n, m = len(_map), len(_map[0])73 for i in range(n):74 for j in range(m):75 if _map[i][j] == 1:76 _map[i][j] = 'x'77 continue78 79 x = _map[i-1][j] if i - 1 >= 0 and _map[i-1][j] != 'x' else 080 y = _map[i][j-1] if j - 1 >= 0 and _map[i][j-1] != 'x' else 081 82 if x + y != 0:83 _map[i][j] = x + y84 elif i + j == 0:85 _map[i][j] = 186 return _map[n-1][m-1]87 # def uniquePathsWithObstacles(self, _map):88 # """89 # :type obstacleGrid: List[List[int]]90 # :rtype: int91 # """92 93 # if _map[0][0] == 1:94 # return 095 # if _map[-1][-1] == 1:96 # return 097 98 # for i, d in enumerate(_map):99 # for j, d2 in enumerate(d):100 # if d2 == 1:101 # _map[i][j] = 'x'102 103 # n, m = len(_map), len(_map[0])104 # for i in range(n):105 # for j in range(m):106 # if _map[i][j] == 'x':107 # continue108 # x = _map[i-1][j] if i - 1 >= 0 and _map[i-1][j] != 'x' else 0109 # y = _map[i][j-1] if j - 1 >= 0 and _map[i][j-1] != 'x' else 0110 111 # if x + y == 0 and i + j == 0:112 # _map[i][j] = 1113 # else:114 # _map[i][j] = x + y...
sol2.py
Source:sol2.py
1with open("/home/rwr/advent/day8/input.txt", "r") as input_file:2 lines = input_file.read().splitlines()3 total = 04 for line in lines:5 signals, outputs = line.split(" | ")6 signals = sorted(signals.split(" "), key=len)7 print(signals)8 _map = {}9 _map[1] = signals[0]10 _map[4] = signals[2]11 _map[7] = signals[1]12 _map[8] = signals[-1]13 signals.remove(_map[1])14 signals.remove(_map[4])15 signals.remove(_map[7])16 signals.remove(_map[8])17 right = _map[1]18 mid_left = set(_map[4]) - set(_map[1])19 for s in signals:20 if len(s) == 6 and len(set(_map[4]) - set(s)) == 0:21 _map[9] = s22 signals.remove(s)23 for s in signals:24 if len(s) == 6 and len(set(s).intersection(mid_left)) == 1:25 _map[0] = s26 signals.remove(s)27 for s in signals:28 if len(s) == 6:29 _map[6] = s30 for s in signals:31 if len(s) == 5 and len(set(s).intersection(mid_left)) == 2:32 _map[5] = s33 signals.remove(s)34 for s in signals:35 if len(s) == 5 and len(set(s).intersection(right)) == 2:36 _map[3] = s37 signals.remove(s)38 _map[2] = signals[0]39 print(_map)40 for idx in range(len(_map)):41 _map[idx] = sorted(_map[idx])42 result = ""43 for o in outputs.split(" "):44 for idx in range(len(_map)):45 if sorted(o) == _map[idx]:46 result += str(idx)47 break48 print(f"{outputs} ", end="")49 print(result)50 print("--------------")51 total += int(result)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!