Best Python code snippet using hypothesis
Lanczos.py
Source:Lanczos.py
...99 def copy_to_a_from_b(self,b):100 self.vr[:]=b.vr101 def set_value(self, n, val):102 self.vr[n]=val103 def set_to_zero(self):104 self.vr[:]=0105 106 def set_to_one(self):107 self.vr[:]=1108 109 def set_all_random(self, v):110 self.vr[:]=[random.random() for i in range(len(self.vr)) ]111 112 113 def scalare(self,b ):114 resR =numpy.sum(self.vr*b.vr)115 return resR116 def sqrtscalare(self,b):117 assert( self is b)118 return numpy.sqrt( self.scalare(b) )119 def normalizzaauto(self):120 norma = self.sqrtscalare(self)121 self.vr[:]=self.vr/norma122 123 def normalizza(self,norma):124 self.vr.normalizza(norma)125 def rescale(self,fact):126 if(fact==0.0):127 self.set_to_zero()128 else:129 norma=1.0/fact130 self.normalizza(norma)131 132 def add_from_vect(self, b):133 self.vr[:]=self.vr+b.vr 134 135 136 def add_from_vect_with_fact(self, b, fact):137 self.vr[:]=self.vr+numpy.array([fact],self.tipo)*b.vr138def REAL(a):139 if( type(a)==type(1.0+1.0j) ):140 return a.real141 else:142 return a143def Real(x):144 if( x.__class__ == complex):145 return x.real146 else:147 return x148class Lanczos:149 dump_count=0;150 countdumpab=0151 152 def __init__(self, sparse, metrica=None, tol=1.0e-15):153 self.matrice=sparse154 self.metrica=metrica155 self.class4sparse = sparse.__class__156 try:157 self.class4vect = self.class4sparse.getClass4Vect()158 except:159 self.class4vect = sparse.getClass4Vect()160 self.nsteps = 0161 self.dim=self.matrice.dim162 self.q = None163 self.alpha = None164 self.beta = None165 self.omega=None166 167 self.tol = tol168 self.maxIt = 50169 self.old = False170 171 def diagoCustom(self, minDim=5, shift=None):172 if shift is None:173 self.matrice.gohersch()174 shift = - self.matrice.goherschMax()175 self.cerca(minDim, shift)176 for ne in range(len(self.eval)):177 self.eval[ne] = self.eval[ne] - shift178 def passeggia(self, k, m, start, gram=0, NT=4):179 if k<0 or m>self.nsteps:180 print("k = ",k," m = ",m," nsteps = ",self.nsteps)181 print("Something wrong in passe k<0 or m>nsteps")182 raise ValueError(\183 "Lanczos. Something wrong in passe k<0 or m>nsteps")184 185 sn = math.sqrt(float(self.dim))186 eu = 1.1e-16187 eusn = eu*sn188 if k==0:189 190 self.class4vect.copy_to_a_from_b(self.q[0],start)191 192 if self.metrica is None:193 self.q[0].normalizzaauto()194 else:195 self.tmp4met.set_to_zero()196 self.metrica.Moltiplica( self.tmp4met , self.q[0] )197 tmpfat = math.sqrt(REAL(self.class4vect.scalare(self.q[0] , self.tmp4met)))198 self.q[0].normalizza(tmpfat)199 # self.q[0].dumptofile("q_0")200 p= self.class4vect(self.dim)201 for i in range(k,m):202 p.set_to_zero()203 # self.q[i].dumptofile("qbef_"+str(self.dump_count) )204 205 self.matrice.Moltiplica(p,self.q[i])206 if self.metrica is not None:207 208 self.tmp4met.set_to_zero()209 self.metrica.Moltiplica(self.tmp4met , self.q[i])210 self.alpha[i] = REAL(self.class4vect.scalare(p , self.tmp4met))211 212 else:213 self.alpha[i] = REAL(self.class4vect.scalare(p , self.q[i]))214 215 # p.dumptofile("p"+str(self.dump_count) )216 self.dump_count+=1217 218 if i==k:219 p.add_from_vect_with_fact( self.q[k] , -self.alpha[k] ) 220 for l in range(k):221 p.add_from_vect_with_fact( self.q[l] , -self.beta[l] ) 222 else:223 # self.q[i].dumptofile("q_i"+str(self.dump_count) )224 # self.q[i-1].dumptofile("q_im1"+str(self.dump_count) )225 p.add_from_vect_with_fact(self.q[i] , -self.alpha[i] ) 226 p.add_from_vect_with_fact(self.q[i-1] , -self.beta[i-1] ) 227 # p.dumptofile("pp"+str(self.dump_count) )228 self.dump_count+=1229 230 if self.metrica is not None:231 self.tmp4met.set_to_zero()232 self.metrica.Moltiplica(self.tmp4met , p)233 self.beta[i] = math.sqrt(REAL(self.class4vect.scalare(p , self.tmp4met)))234 235 else:236 self.beta[i]=self.class4vect.sqrtscalare(p,p) 237 self.omega[i,i]=1.238 max0 = 0.0239 if self.beta[i] != 0: #and not scipy.isnan(self.beta[i]):240 for j in range(i+1):241 self.omega[i+1,j] = eusn242 if j<k:243 add = 2 * eusn + abs(self.alpha[j]-self.alpha[i]) \244 * abs(self.omega[i,j])245 if i!=k:246 add += self.beta[j]*abs(self.omega[i,k])247 if i>0 and j!=i-1:248 add += self.beta[i-1]*abs(self.omega[i-1,j])249 self.omega[i+1,j] += add / self.beta[i]250 251 elif j==k : 252 add = 2 * eusn + abs(self.alpha[j]-self.alpha[i]) \253 * abs(self.omega[i,j])254 for w in range(k):255 add += self.beta[w]*abs(self.omega[i,w])256 if i!=k+1:257 add += self.beta[k]*abs(self.omega[i,k+1])258 if i>0 and i!=k+1:259 add += self.beta[i-1]*abs(self.omega[i-1,k])260 self.omega[i+1,j] += add / self.beta[i]261 elif j<i :262 add = 2 * eusn + abs(self.alpha[j]-self.alpha[i]) \263 * abs(self.omega[i,j])264 if i!=j+1:265 add += self.beta[j]*abs(self.omega[i,j+1])266 if i>0 and j>0:267 add += self.beta[j-1]*abs(self.omega[i-1,j-1])268 if i>0 and i!=j+1:269 add += self.beta[i-1]*abs(self.omega[i-1,j])270 self.omega[i+1,j] += add / self.beta[i]271 272 else:273 add = eusn274 if i>0:275 add += self.beta[i-1]*abs(self.omega[i,i-1] )276 self.omega[i+1,j] += add / self.beta[i]277 self.omega[j,i+1] = self.omega[i+1,j]278 max0 += self.omega[i+1,j]**2279 if self.beta[i]==0 or max0>eu or gram :280## if self.beta[i]==0 or max0>0. :281 if i>0:282 #print " GRAMO self.beta[i]==0 or max0>eu and i>0", i," ", self.dump_count283 self.GramSchmidt(self.q[i],i, NT)284 285 if self.metrica is None:286 self.q[i].normalizzaauto()287 else:288 self.tmp4met.set_to_zero()289 self.metrica.Moltiplica( self.tmp4met , self.q[i] )290 tmpfat = math.sqrt(REAL(self.class4vect.scalare(self.q[i] , self.tmp4met)))291 self.q[i].normalizza(tmpfat)292 p.set_to_zero()293 294 self.matrice.Moltiplica(p, self.q[i])295 if self.metrica is not None:296 297 self.tmp4met.set_to_zero()298 self.metrica.Moltiplica(self.tmp4met , self.q[i])299 self.alpha[i] = REAL(self.class4vect.scalare(p , self.tmp4met))300 301 else:302 self.alpha[i] = REAL(self.class4vect.scalare(p , self.q[i]))303 #print " GRAMO bis ", i304## print self.alpha[:20]305## raise " OK " 306 307 if self.metrica is None:308 self.GramSchmidt(p,i+1,NT)309 310 self.beta[i] = self.class4vect.sqrtscalare(p,p)311 p.normalizzaauto()312 else:313 314 # p.add_from_vect_with_fact( self.q[i] , -self.alpha[i] )315 # p.add_from_vect_with_fact( self.q[i-1] , -self.beta[i-1] )316 317 self.GramSchmidt(p,i+1,NT)318 self.tmp4met.set_to_zero()319 self.metrica.Moltiplica(self.tmp4met , p)320 tmpfat = math.sqrt(REAL(self.class4vect.scalare(p , self.tmp4met)))321 self.beta[i] = tmpfat322 p.normalizza(tmpfat)323 if i>0:324 condition = eu * \325 math.sqrt(self.dim * (self.alpha[i]**2+\326 self.beta[i-1]**2))327 else:328 condition = eu * math.sqrt(self.dim * (self.alpha[i]**2))329 if self.beta[i]< condition:330 331 self.beta[i]=0.332 333 p.set_all_random(1.0)334 335 self.GramSchmidt(p,i+1)336 if self.metrica is None:337 p.normalizzaauto()338 else:339 self.tmp4met.set_to_zero()340 self.metrica.Moltiplica( self.tmp4met , p )341 tmpfat = math.sqrt(REAL(self.class4vect.scalare(p , self.tmp4met)))342 p.normalizza(tmpfat)343 for l in range(i) :344 self.omega[i,l]=self.omega[l,i]=eusn345 for l in range(i+1):346 self.omega[i+1,l]=self.omega[l,i+1]=eusn347 348 else:349 if self.metrica is None:350 p.normalizzaauto()351 else:352 self.tmp4met.set_to_zero()353 self.metrica.Moltiplica( self.tmp4met , p )354 tmpfat = math.sqrt(REAL(self.class4vect.scalare(p , self.tmp4met)))355 p.normalizza(tmpfat)356 357 # p.dumptofile("normprelude"+str(self.dump_count))358 self.class4vect.copy_to_a_from_b(self.q[i+1],p)359 360 def GramSchmidt(self, vect , n, NT=4):361 for h in range(NT):362 if self.metrica is None :363 for i in range(n):364 s=self.class4vect.scalare( self.q[i], vect)365 vect.add_from_vect_with_fact(self.q[i],-s)366 else:367 self.tmp4met.set_to_zero()368 self.metrica.Moltiplica(self.tmp4met, vect)369 for i in range(n):370 371 s=self.class4vect.scalare( self.q[i], self.tmp4met)372 vect.add_from_vect_with_fact(self.q[i],-s)373 374 def allocaMemory(self):375 self.alpha = numpy.zeros(self.nsteps,numpy.float64)376 self.beta = numpy.zeros(self.nsteps,numpy.float64)377 self.omega = numpy.zeros((self.nsteps+1,self.nsteps+1),numpy.float64)378 self.evect = numpy.zeros((self.nsteps,self.nsteps),numpy.float64)379 self.eval = numpy.zeros((self.nsteps),numpy.float64)380 self.oldalpha = numpy.zeros((self.nsteps),numpy.float64)381 ...
Threshold.py
Source:Threshold.py
...27 def set_relative_area(self, relative_area):28 try:29 self.relative_area = str(float(relative_area))30 except (ValueError, TypeError):31 self.relative_area = self.set_to_zero()32 def set_saturation(self, saturation):33 try:34 self.saturation = str(float(saturation))35 except (ValueError, TypeError):36 self.saturation = self.set_to_zero()37 def set_peak_probability(self, peak_probability):38 try:39 self.peak_probability = str(float(peak_probability))40 except (ValueError, TypeError):41 self.peak_probability = self.set_to_zero()42 def set_absolute_area(self, absolute_area):43 try:44 self.absolute_area = str(float(absolute_area))45 except (ValueError, TypeError):46 self.absolute_area = self.set_to_zero()47 def set_min_merge_difference(self, min_merge_difference):48 try:49 self.min_merge_difference = str(float(min_merge_difference))50 except (ValueError, TypeError):51 self.min_merge_difference = self.set_to_zero()52 def set_absolute_height(self, absolute_height):53 try:54 self.absolute_height = str(float(absolute_height))55 except Exception as e:56 self.absolute_height = self.set_to_zero()57 def set_relative_height(self, relative_height):58 try:59 self.relative_height = str(float(relative_height))60 except (ValueError, TypeError):61 self.relative_height = self.set_to_zero()62 def set_signal_to_noise(self, signal_to_noise):63 try:64 self.signal_to_noise = str(float(signal_to_noise))65 except (ValueError, TypeError):66 self.signal_to_noise = self.set_to_zero()67 def set_second_derivative(self, second_derivative):68 try:69 self.second_derivative = str(float(second_derivative))70 except (ValueError, TypeError):71 self.second_derivative = self.set_to_zero()72 def set_first_derivative(self, first_derivative):73 try:74 self.first_derivative = str(float(first_derivative))75 except (ValueError, TypeError):76 self.first_derivative = str(0)77 def set_relative_low_std_area(self, relative_low_std_area):78 try:79 self.relative_low_std_area = str(float(relative_low_std_area))80 except (ValueError, TypeError):81 self.relative_low_std_area = ""82 def set_relative_low_std_height(self, relative_low_std_height):83 try:84 self.relative_low_std_height = str(float(relative_low_std_height))85 except (ValueError, TypeError):86 self.relative_low_std_height = ""87 def set_to_zero(self):...
63. Unique Paths II.py
Source:63. Unique Paths II.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 space is marked as 1 and 0 respectively in the grid.6Example 1:7Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]8Output: 29Explanation: There is one obstacle in the middle of the 3x3 grid above.10There are two ways to reach the bottom-right corner:111. Right -> Right -> Down -> Down122. Down -> Down -> Right -> Right13Example 2:14Input: obstacleGrid = [[0,1],[0,0]]15Output: 116"""17class Solution(object):18 def uniquePathsWithObstacles(self, obstacleGrid):19 """20 :type obstacleGrid: List[List[int]]21 :rtype: int22 """23 if not obstacleGrid:24 return 025 m = len(obstacleGrid) # number of rows26 n = len(obstacleGrid[0]) # number of cols27 if obstacleGrid[0][0] == 1 or obstacleGrid[m - 1][n - 1] == 1:28 return 029 dp = [[0 for _ in range(n)] for _ in range(m)]30 # initialize the last row31 set_to_zero = False32 for c in range(n - 1, -1, -1):33 if obstacleGrid[m - 1][c] == 1:34 set_to_zero = True35 dp[m - 1][c] = 0 if set_to_zero else 136 # initialize the lasr col37 set_to_zero = False38 for r in range(m - 1, -1, -1):39 if obstacleGrid[r][n - 1] == 1:40 set_to_zero = True41 dp[r][n - 1] = 0 if set_to_zero else 142 for r in range(m - 2, -1, -1):43 for c in range(n - 2, -1, -1):44 if obstacleGrid[r][c] == 1:45 dp[r][c] = 046 else:47 dp[r][c] = dp[r + 1][c] + dp[r][c + 1]...
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!!