Best Python code snippet using assertpy_python
math.py
Source:math.py
...53 ["comm-mul", Mul(a, b), Mul(b, a)],54 ["assoc-add", Add(a, Add(b, c)), Add(Add(a, b), c)],55 ["assoc-mul", Mul(a, Mul(b, c)), Mul(Mul(a, b), c)],56 ["sub-canon", Sub(a, b), Add(a, Mul(-1, b))],57 # rw!("div-canon"; "(/ ?a ?b)" => "(* ?a (pow ?b -1))" if is_not_zero("?b")),58 # // rw!("canon-sub"; "(+ ?a (* -1 ?b))" => "(- ?a ?b)"),59 # // rw!("canon-div"; "(* ?a (pow ?b -1))" => "(/ ?a ?b)" if is_not_zero("?b")),60 ["zero-add", Add(a, 0), a],61 ["zero-mul", Mul(a, 0), 0],62 ["one-mul", Mul(a, 1), a],63 ["add-zero", a, Add(a, 0)],64 ["mul-one", a, Mul(a, 1)],65 ["cancel-sub", Sub(a, a), 0],66 # rw!("cancel-div"; "(/ ?a ?a)" => "1" if is_not_zero("?a")),67 ["distribute", Mul(a, Add(b, c)), Add(Mul(a, b), Mul(a, c))],68 ["factor", Add(Mul(a, b), Mul(a, c)), Mul(a, Add(b, c))],69 ["pow-mul", Mul(Pow(a, b), Pow(a, c)), Pow(a, Add(b, c))],70 # rw!("pow0"; "(pow ?x 0)" => "1"71 # if is_not_zero("?x")),72 ["pow1", Pow(x, 1), x],73 ["pow2", Pow(x, 2), Mul(x, x)],74 # rw!("pow-recip"; "(pow ?x -1)" => "(/ 1 ?x)"75 # if is_not_zero("?x")),76 # rw!("recip-mul-div"; "(* ?x (/ 1 ?x))" => "1" if is_not_zero("?x")),77 # rw!("d-variable"; "(d ?x ?x)" => "1" if is_sym("?x")),78 # rw!("d-constant"; "(d ?x ?c)" => "0" if is_sym("?x") if is_const_or_distinct_var("?c", "?x")),79 ["d-add", Diff(x, Add(a, b)), Add(Diff(x, a), Diff(x, b))],80 ["d-mul", Diff(x, Mul(a, b)), Add(Mul(a, Diff(x, b)), Mul(b, Diff(x, a)))],81 ["d-sin", Diff(x, Sin(x)), Cos(x)],82 ["d-cos", Diff(x, Cos(x)), Mul(-1, Sin(x))],83 # rw!("d-ln"; "(d ?x (ln ?x))" => "(/ 1 ?x)" if is_not_zero("?x")),84 # rw!("d-power";85 # "(d ?x (pow ?f ?g))" =>86 # "(* (pow ?f ?g)87 # (+ (* (d ?x ?f)88 # (/ ?g ?f))89 # (* (d ?x ?g)90 # (ln ?f))))"91 # if is_not_zero("?f")92 # if is_not_zero("?g")93 # ),94 ["i-one", Integral(1, x), x],95 # rw!("i-power-const"; "(i (pow ?x ?c) ?x)" =>96 # "(/ (pow ?x (+ ?c 1)) (+ ?c 1))" if is_const("?c")),97 ["i-cos", Integral(Cos(x), x), Sin(x)],98 ["i-sin", Integral(Sin(x), x), Mul(-1, Cos(x))],99 ["i-sum", Integral(Add(f, g), x), Add(Integral(f, x), Integral(g, x))],100 ["i-dif", Integral(Sub(f, g), x), Sub(Integral(f, x), Integral(g, x))],101 ["i-parts", Integral(Mul(a, b), x),102 Sub(Mul(a, Integral(b, x)), Integral(Mul(Diff(x, a), Integral(b, x)), x))],103]104# Turn the lists into rewrites105rules = list()106for l in list_rules:...
multi_normal.py
Source:multi_normal.py
1import numpy as np2import scipy.stats3from core.utils import verification4@verification('a1', 'aa', 'a1')5def pdf(mean: np.ndarray, cov: np.ndarray, x: np.ndarray) -> float:6 """7 Computes the pdf of a multivariate normal at the specified location.8 """9 p = scipy.stats.multivariate_normal.pdf(x.flatten(), mean.flatten(), cov, allow_singular=True)10 return p11@verification('a1', 'aa', 'ba', 'aa')12def posterior(mean: np.ndarray, cov:np.ndarray, x: np.ndarray, cov_known: np.ndarray) -> (np.ndarray, np.ndarray):13 """14 Computes the parameters of the posterior distribution in case the true covariance matrix is known.15 """16 n = len(x)17 mean_x = x.mean(axis=0)[:, None]18 inverse = np.linalg.inv(cov + (1 / n) * cov_known)19 mu_post = cov @ inverse * mean_x + (1 / n) * cov_known @ inverse @ mean20 cov_post = cov @ inverse @ cov_known * (1 / n)21 return np.diag(mu_post)[:, None], cov_post22@verification('a1', 'aa', 'a1', 'aa')23def kl_divergence(mean1: np.ndarray, cov1: np.ndarray, mean2: np.ndarray, cov2: np.ndarray) -> float:24 """25 Computes the KL-Divergence between two multivariate normal distributions.26 """27 # Removing zeros in diagonal of covariance matrix28 is_not_zero = (np.diag(cov1) != 0) & (np.diag(cov2) != 0)29 mean1 = mean1[is_not_zero, :]30 mean2 = mean2[is_not_zero, :]31 cov1 = cov1[is_not_zero, :][:, is_not_zero]32 cov2 = cov2[is_not_zero, :][:, is_not_zero]33 # Computing KL-divergence34 n = len(mean1)35 return 1 / 2 * (np.log(np.linalg.det(cov2) / np.linalg.det(cov1)) - n +36 np.trace(np.linalg.inv(cov2) @ cov1) +37 (mean2 - mean1).T @ np.linalg.inv(cov2) @ (mean2 - mean1))[0][0]38@verification('a1', 'ba')39def ttest_1sample(mean: np.ndarray, x: np.ndarray) -> float: # TODO: Check computation40 x = np.asarray(x)41 nobs, k_vars = x.shape42 mean_x = x.mean(0)43 cov = np.cov(x, rowvar=False, ddof=1)44 diff = mean_x - mean.flatten()45 t2 = nobs * diff.dot(np.linalg.solve(cov, diff))46 factor = (nobs - 1) * k_vars / (nobs - k_vars)47 statistic = t2 / factor48 df = (k_vars, nobs - k_vars)49 pvalue = scipy.stats.f.sf(statistic, df[0], df[1])50 return pvalue51if __name__ == '__main__':...
million.py
Source:million.py
1num = 10000002is_not_zero = True3iterated = 04while is_not_zero:5 num //= 26 iterated += 17 if num == 0:8 is_not_zero = False...
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!!