Browse Source

Import python projects

DricomDragon 4 years ago
parent
commit
65de7ca90d

+ 4 - 0
computeEntropy/Data/hello.txt

@@ -0,0 +1,4 @@
+What you may know as a file is slightly different in Python.
+In Windows, for example, a file can be any item manipulated, edited or created by the user/OS. That means files can be
+. There are several types, but the most common is the comma {,} or newline character.
+It ends the current line and tells the interpreter a new one has begun.

+ 40 - 0
computeEntropy/main.py

@@ -0,0 +1,40 @@
+from math import log
+
+charCount = {}
+numberOfBits = 0
+
+
+def count(line):
+    global numberOfBits
+
+    for c in line:
+        # Size increases
+        numberOfBits += 1
+
+        # Count
+        if c in charCount:
+            charCount[c] += 1
+        else:
+            charCount[c] = 1
+
+with open("Data/Tunngle_Setup_v5.8.4.exe", 'rb') as f :
+
+    print("Reading file")
+    for l in f:
+        count(l)
+
+    print("The map :")
+    print(charCount)
+
+    print("Number of characters :")
+    print(len(charCount))
+
+    print("Number of chars :")
+    print(numberOfBits)
+
+    print("Computing entropy")
+    entropy = 0.0
+    for key in charCount:
+        f = charCount[key] / numberOfBits
+        entropy -= f * log(f, 2)
+    print(entropy)

+ 32 - 0
hilbertDistance/integral.py

@@ -0,0 +1,32 @@
+# Jovian Hersemeule
+
+def integral(f, a=0, b=1, n=100):
+    """
+    Calcule l'intégrale de a à b avec la méthodes des triangles.
+    :param f: La fonction à intégrer.
+    :param a: La borne inférieure.
+    :param b: La borne supérieure.
+    :param n: Nombre de pas.
+    :return: La valeur numérique de l'intégrale.
+    """
+    print("Calcul de l'intégrale de ", a, " à ", b)
+
+    # Préparation
+    somme = 0
+    pas = (b - a) / n
+    x1 = a
+    x2 = a
+
+    # Calcul
+    for k in range(n - 1):
+        x1 = x2
+        x2 = x1 + pas
+
+        somme += f(x1) * pas
+        somme += (f(x2) - f(x1)) * pas / 2
+
+    # Fin
+    return somme
+
+
+print("END")

+ 1 - 0
hilbertDistance/main.py

@@ -0,0 +1 @@
+

+ 77 - 0
hilbertDistance/minseeker.py

@@ -0,0 +1,77 @@
+# Jovian Hersemeule
+
+def dicho(f, a, b, epsilon=0.01, n=10, deep=0, deep_max=10, debug=False, prefix=""):
+    """
+    Cherche le minimum entre a et b.
+    :param f: Fonction numérique.
+    :param a: Borne inférieure.
+    :param b: Borne supérieure.
+    :param epsilon: Marge tolérée.
+    :param n: Découpage.
+    :param deep: Profondeur d'appel.
+    :param deep_max: Profondeur d'appel maximale.
+    :param debug: Si vrai alors fait des print.
+    :param prefix: Chaine affichées en tête de ligne.
+    :return: x0 où f(x0) est le minimum.
+    """
+    # Profondeur
+    deep_next = deep + 1
+    if deep_next <= deep_max:
+        if debug:
+            print(prefix, "Lancement du calcul à profondeur ", deep_next, " entre ", a, " et ", b, ".")
+    else:
+        if debug:
+            print(prefix, "Avortment du calcul : profondeur maximale dépassée.")
+        return a
+
+    # Recherche
+    pas = (b - a) / n
+    mini = f(a)
+    loc = a
+    x = a
+    img = 0
+
+    for k in range(n - 1):
+        x += pas
+
+        img = f(x)
+        if img < mini:
+            if debug:
+                print(prefix, "Nouveau minimum trouvé. Ancien : x0 = ", loc, " et y0 = ", mini, "Nouveau : x0 = ", x,
+                      " et y0 = ", img)
+            mini = img
+            loc = x
+        elif debug:
+            print(prefix, "Ce n'est pas un nouveau minimum.")
+
+    # Erreur acceptable
+    if abs(f(loc + pas / 2) - f(loc)) + abs(f(loc - pas / 2) - f(loc)) < epsilon:
+        if debug:
+            print(prefix, "Minimum trouvé en ", loc, " de valeur ", mini, "\n")
+        return loc
+    else:
+        return dicho(f, loc - pas, loc + pas, epsilon=epsilon, n=n, deep=deep_next, debug=debug, prefix=prefix)
+
+
+def minSeeker(f, x1, x2, y1, y2, epsilon=0.01, debug=False):
+    """
+    Cherche à minimiser f( x, y ).
+    :param f: Une fonction numérique à deux paramètres réels.
+    :param x1, x2, y1, y2: Les plages de valeurs de recherche.
+    :param epsilon: Marge tolérée.
+    :param debug: Si vrai alors fait des print.
+    :return: ( v, a, b ) où v = f(a , b) le minimum cherché.
+    """
+    if debug:
+        print("Exécution de minSeeker.")
+        print("x dans ", x1, " : ", x2)
+        print("y dans ", y1, " : ", y2, "\n")
+
+    minLine = lambda x: dicho(lambda t: f(x, t), y1, y2, epsilon=epsilon, debug=debug, prefix="|   > ")
+
+    ans = dicho(minLine, x1, x2, epsilon=epsilon, debug=debug, prefix="> ")
+
+    if debug:
+        print("Fin de l'exécution de minSeeker. Minimum trouvé : ", ans, ".\n")
+
+    return ans