Best Python code snippet using hypothesis
HeatDiffusion.py
Source:HeatDiffusion.py
...16 lower[N-1] = 017 upper[0] = 018 return lower, diagonal, upper19# Initialization of boundary condition etc.20def initialize_b(T):21 h = 2 * L / N22 result = (-1*e*h**2) / D * np.ones((N, 1))23 result[0] = T24 result[N-1] = T25 return result26# Function to multiply a trigonal matrix and a vector27def trigonal_multiplication(lower, diagonal, upper, vector):28 result = np.zeros((N, 1))29 result[0] = diagonal[0] * vector[0]30 result[N-1] = diagonal[N-1] * vector[N-1]31 for i in range(1, N-1):32 result[i] = lower[i] * vector[i-1] + diagonal[i] * vector[i] + upper[i] * vector[i+1]33 return result34# Function that implements the forward-elimination, backward-substitution algorithm35def forward_elimination(lower, diagonal, upper, b):36 for i in range(0, N-2):37 if diagonal[i] > 0:38 m = (-1) * np.abs(lower[i+1]/diagonal[i])39 else:40 m = np.abs(lower[i + 1] / diagonal[i])41 lower[i+1] = lower[i+1] + m * diagonal[i]42 diagonal[i+1] = diagonal[i+1] + m * upper[i]43 b[i+1] = b[i+1] + m * b[i]44 return diagonal, b45# Function that implements the backward-substitution step46# The output vector contains the values for T_i47def backward_substitution(diagonal, b):48 result = np.zeros((N, 1))49 result[N-1] = b[N-1]50 result[0] = b[0]51 for i in range(2, N):52 result[N-i] = (b[N-i] - result[N-i+1])/diagonal[N-i]53 return result54# Function that implements a Jacobi iteration55def jacobi_step(lower, diagonal, upper, x, b):56 zero = np.zeros((N, 1))57 p = trigonal_multiplication(lower, zero, upper, x)58 x = trigonal_multiplication(zero, 1./diagonal, zero, b + p)59 return x60# Create array with spacing h and N points61def create_x_array():62 h = 2*L/N63 result = np.zeros((N, 1))64 for i in range(-int(np.floor(N/2)), int(np.floor(N/2))):65 result[int(np.floor(N/2)+i)] = h*i66 return result67# Function to run the full matrix algorithm68def run_matrix_algorithm():69 l, d, u = initialize_matrix(N)70 b = initialize_b(T_0)71 a, b = forward_elimination(l, d, u, b)72 y = backward_substitution(a, b)73 # Verify solution and calculate residual74 l, d, u = initialize_matrix(N)75 b = initialize_b(T_0)76 print('Residual:', trigonal_multiplication(l, d, u, y) - b)77 x = create_x_array()78 plt.plot(x, y, 'k--')79# Function to run the full jacobi algorithm80def run_jacobi_algorithm(steps):81 x = create_x_array()82 l, d, u = initialize_matrix(N)83 b = initialize_b(T_0)84 y = np.zeros((N, 1))85 for i in range(0, steps):86 y = jacobi_step((-1) * l, d, (-1) * u, y, b)87 plt.plot(x, y)88# Initializes the restriction matrices89def initialize_restriction_matrices():90 R1 = np.multiply((1/4), [[2.,1.,0,0,0,0,0,0,0],91 [0,1.,2.,1.,0,0,0,0,0],92 [0,0,0,1.,2.,1.,0,0,0],93 [0,0,0,0,0,1.,2.,1.,0],94 [0,0,0,0,0,0,0,1.,2.]])95 R2 = np.multiply((1/4), [[2.,1.,0,0,0],96 [0,1.,2.,1.,0],97 [0,0,0,1.,2.]])...
test_clusterInitializers.py
Source:test_clusterInitializers.py
1import unittest2import numpy as np3import os4import sys56from .context import cluster_initializers78# from bmdcluster.initializers.cluster_initializers import initialize_A9# from bmdcluster.initializers.cluster_initializers import initialize_B101112class Testinitialize_A(unittest.TestCase):1314 def setUp(self):15 self.n = 41617 def test_initialize_A_assertions(self):1819 with self.subTest('Check data_cluster size assertion'):20 # Check that assertion error raised when the number of data clusters is greater21 # than or equal to the size of the dataset.22 with self.assertRaises(AssertionError):23 cluster_initializers.initialize_A(n = self.n, n_clusters = self.n)24 25 with self.subTest('Check init_ratio assertions'):2627 # Check that assertion error is raised when the init_ratio is outside of28 # the interval (0,1].2930 with self.assertRaises(AssertionError):31 cluster_initializers.initialize_A(n = self.n, n_clusters = self.n - 1, init_ratio = 1.1)3233 with self.assertRaises(AssertionError):34 cluster_initializers.initialize_A(n = self.n, n_clusters = self.n - 1, init_ratio = 0)35363738 def test_initialize_A_outputs(self):3940 with self.subTest('Check sum of entries'):41 # When init_ratio not set, each point should be assigned exactly one cluster.42 # Check sum of elements of cluster assignment matrix A.43 A = cluster_initializers.initialize_A(self.n, self.n-1)44 self.assertEqual(A.sum(), self.n)45464748 with self.subTest('Check init_ratio'):49 # Check that when init_ratio is set, the number of assigned clusters is50 # the expected number.51 A = cluster_initializers.initialize_A(n = self.n, n_clusters = self.n - 1, init_ratio = 0.5)52 self.assertEqual(A.sum(), self.n // 2)53545556 with self.subTest('Check bootstrap list passing'):57 # Test passing of list of tuples containing the positions of entries58 # to be set in the returned matrix.5960 A_expected = np.array([[1,0],61 [0,0],62 [0,1],63 [0,0]])646566 A = cluster_initializers.initialize_A(n = self.n, n_clusters = 2, bootstrap = [(0,0),(2,1)])67 self.assertTrue(np.array_equal(A, A_expected))68697071class TestInitializeB(unittest.TestCase):7273 def setUp(self):74 self.m = 3757677 def test_initializeB_output(self):7879 with self.subTest('Check B_ident'):80 # Check feature cluster matrix B is initialized to identity when B_ident set to True.81 B = cluster_initializers.initialize_B(self.m, B_ident = True)82 self.assertTrue(np.array_equal(np.identity(self.m), B))8384 85 # def test_check_assertions(self):8687 # with self.assertRaises(AssertionError):88 # initialize_B(self.m, B_ident = False, f_clusters = self.m + 1)8990 # with self.assertRaises(AssertionError):91 # initialize_B(self.m, B_ident = False, f_clusters = 1)9293 #@unittest.skip("No longer using keyword arguments in initialize_B")94 def test_initializeB_assertions(self):9596 with self.subTest('Check missing keyword argument'):97 # Check that MissingKeywordArgument raised when B_ident set to False and98 # without additional keyword arguments.99 with self.assertRaises(KeyError):100 cluster_initializers.initialize_B(self.m, B_ident = False)101102 with self.subTest('Check assertions'):103 # Check that when f_clusters is passed, that AssertionError is raised104 # f_clusters is not in the interval (1, m].105106 with self.assertRaises(AssertionError):107 cluster_initializers.initialize_B(self.m, B_ident = False, f_clusters = self.m + 1)108109 with self.assertRaises(AssertionError):110 cluster_initializers.initialize_B(self.m, B_ident = False, f_clusters = 1)111112if __name__ == '__main__':
...
Exercise3.3.py
Source:Exercise3.3.py
...35 A[i][i] = 536 return A373839def initialize_b(n):40 B = [1] * n41 B[0] = 342 B[-1] = 343 return B444546def find(a, b, x):47 norma = a[0][0] * x[0] + a[0][1] * x[1] - b[0]48 n = len(a)4950 for i in range(1, n):51 temp = 052 temp += a[i][i - 1] * x[i - 1] + a[i][i] * x[i]53 if i + 1 < n:54 temp += a[i][i + 1] * x[i + 1]5556 temp -= b[i]57 norma = max(abs(norma), abs(temp))5859 if abs(norma) >= 0.5e-4:60 return True61 else:62 return False636465def sumForCholesky(a, i, x0, xn):66 s = 067 if i - 1 >= 0:68 s += a[i][i-1] * xn[i-1]69 if i + 1 < len(a):70 s += a[i][i+1] * x0[i+1]71 return s727374def gauss_seidel(a, b):75 x0 = [0] * len(a)76 xn = [0] * len(a)77 flag = True78 counter = 07980 while flag:81 for i in range(0, len(a)):82 athrisma = sumForCholesky(a, i, x0, xn)83 xn[i] = (b[i] - athrisma) / a[i][i]84 counter += 185 flag = find(a, b, xn)86 for i in range(0, len(a)):87 x0[i] = xn[i]8889 print("ΧÏειάÏÏηκαν", counter, "ÎÏαναλήÏÎµÎ¹Ï ")90 return x0919293a10 = initialize_a(10) # ÎÏÏικοÏοιεί Ïον Ïίνακα Î10 (10x10) ÏÏοιÏεία94b10 = initialize_b(10) # ÎÏÏικοÏοιεί Ïον Ïίνακα Î10 (10) ÏÏοιÏεία95a10000 = initialize_a(10000) # ÎÏÏικοÏοιεί Ïον Ïίνακα Î10000 (10000x10000)96b10000 = initialize_b(10000)9798print("Îια n=10 : ")99# printAB(a10, b10) # ΤÏ
ÏÏνει Ïον εÏαÏ
ξημÎνο Ïίνακα100x10 = gauss_seidel(a10, b10) # Îάνει Gauss_Seidel και εÏιÏÏÏÎÏει Îναν Ïίνακα με ÏÎ¹Ï ÏίζεÏ101# Î ÏÏ
νάÏÏηÏη Gauss_Seidel ÏÏιν ολοκληÏÏθεί ÏÏ
ÏÏνει Ïον αÏÎ¹Î¸Î¼Ï ÏÏν εÏαναλήÏεÏν ÏοÏ
ÏÏειάÏÏηκαν102print_result(x10) # ÏÏ
ÏÏνει Ïον Ïίνακα με ÏÎ¹Ï ÏίζεÏ103104print("Îια n=10000 : ")105gauss_seidel(a10000, b10000) # ΠαÏÏμοια με εÏάνÏ106# ÎµÎ´Ï Î´ÎµÎ½ ÏÏ
ÏÏνοÏ
με Ïον Ïίνακα καθÏÏ ÎµÎ¯Î½Î±Î¹ ÏÎ¿Î»Ï Î¼ÎµÎ³Î¬Î»Î¿Ï107108print()
...
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!!