diff --git a/S2/TP4/Makefile b/S2/TP4/Makefile index b659ce3..c21cd48 100644 --- a/S2/TP4/Makefile +++ b/S2/TP4/Makefile @@ -1,5 +1,5 @@ tri_insertion_moyen.txt: analyse_en_moyenne.py - python3 $< > $@ + python3 $< --brut > $@ clean: rm tri_insertion_moyen.txt \ No newline at end of file diff --git a/S2/TP4/analyse_en_moyenne.py b/S2/TP4/analyse_en_moyenne.py old mode 100644 new mode 100755 index 527eaf7..d096102 --- a/S2/TP4/analyse_en_moyenne.py +++ b/S2/TP4/analyse_en_moyenne.py @@ -16,8 +16,48 @@ http://www.fil.univ-lille1.fr/~L1S2API/CoursTP/tp4_tri.html """ -from analyse_tris import nbre_moyen_tri_insertion +# Analyse des arguments +import argparse -m = 100 -for n in range(1, 101): - print("{n:<3} {m:>14}".format(n=n, m=nbre_moyen_tri_insertion(m, n))) +parser = argparse.ArgumentParser(description='Analyse le coût moyen du tri par insertion.') +parser.add_argument('--brut', action='store_true', help="afficher les données brutes") +parser.add_argument('--poly', action='store_true', help="calculer la regression pôlynominale") +parser.add_argument('--graph', action='store_true', help="voir les données sous forme de graphique") +parser.add_argument('-m', type=int, default=100, help="Changer la valeur de m") + +args = parser.parse_args() + + +m = args.m +from analyse_tris import nbre_moyen_tri_insertion +xData = list(range(1, 101)) +yData = [nbre_moyen_tri_insertion(m, i) for i in xData] # Peut prendre un certain temps + +# Affichage des données +if args.brut: + for i in range(len(xData)): + print("{:<3} {:>14}".format(xData[i], yData[i])) + +# Régression polynominale +from numpy import polyfit +if args.poly: + polynome = polyfit(xData, yData, 2) + if args.brut: + print("Polynôme : {}".format(polynome)) + + +# Affichage +from matplotlib import pyplot +if args.graph: + pyplot.plot(xData, yData, 'x') + if args.poly: + if args.poly: + def f(x): + """ + float → float + Retourne un point de la regression polynominale de l'analyse du tri. + CU : polynome est défini et est de degré 2 + """ + return polynome[0] * x ** 2 + polynome[1] * x + polynome[2] + pyplot.plot(xData, [f(x) for x in xData], '-') + pyplot.show() diff --git a/S2/TP4/analyse_tris.py b/S2/TP4/analyse_tris.py old mode 100644 new mode 100755 index 7f40395..b9612aa --- a/S2/TP4/analyse_tris.py +++ b/S2/TP4/analyse_tris.py @@ -367,5 +367,8 @@ section("Avec Gnuplot") question(1) reponse("Graphiquement, on trouve que cette fonction a pour expression 0,287x²-2x+1\n\ -Cette fonction correspond à la commande : \n\ -gnuplot> plot 'tri_insertion_moyen.txt', 0.287*x**2-2*x +1 with lines") +Cette fonction correspond à la commande suivante : \n\ +gnuplot> plot 'tri_insertion_moyen.txt', 0.287*x**2-2*x +1 with lines\n\ +On peut aussi trouver des valeurs plus précises en faisant une regression pôlynominale des données \ +avec la fonction polyfit de numpy. Je vous invite à lancer la commande suivante :\n\ +python3 analyse_en_moyenne.py --brut --poly --graph")