====== Snippets de fonctions mathématiques en Python ======
==== Tests statistiques ====
* https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind_from_stats.html
* [[https://machinelearningmastery.com/statistical-hypothesis-tests-in-python-cheat-sheet/]]
==== linspace ====
Remplacement de la fonction ''linspace'' de NumPy ([[https://stackoverflow.com/questions/12334442/does-python-have-a-linspace-function-in-its-std-lib|source]]):
def linspace(a, b, n=1):
if n < 2:
return b
diff = (float(b) - a)/(n - 1)
return [diff * i + a for i in range(n)]
==== Intersection de deux droites ====
Première intersection de deux droites, dont les abscisses sont identiques:
def find_first_intersection(x, y1, y2):
for i in range(len(y1)-1):
if (y1[i] < y2[i] and y1[i+1] > y2[i+1]) or (y1[i] > y2[i] and y1[i+1] < y2[i+1]):
return x[i+1] + (y2[i+1] - y1[i+1]) * (x[i+1] - x[i]) / (y1[i+1] - y1[i] - y2[i+1] + y2[i])
return None
==== Formule d'une fonction affine à partir de deux points ====
''y = ( (yB - yA) / (xB- xA) ) * (x - xA) + yA''
Pour une formule du type $y=ax+b$: $a=\frac{y_1-y_2}{x_1-x_2}$ et $b = y_1 - ax_1$
==== Somme terme à terme d'une liste de listes ====
* [[https://www.geeksforgeeks.org/python-ways-to-sum-list-of-lists-and-return-sum-list/]]
l = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
res = [sum(i) for i in zip(*l)]