From 51b1bd06e91830be1d925b5a83df17c3b4d632b1 Mon Sep 17 00:00:00 2001 From: MarcPartensky Date: Sun, 31 Mar 2019 16:27:45 +0200 Subject: [PATCH] Added to polynome the function to get the roots, in all cases. --- Info/.DS_Store | Bin 0 -> 6148 bytes Info/Td/TD11: Marc, Louise et Hugues.py | 130 +++++++++ Info/Td/__pycache__/converter.cpython-36.pyc | Bin 0 -> 7819 bytes Info/Td/converter.py | 260 ++++++++++++++++++ .../tpServignyRUBINdeCERVENSPartenskyTD10.py | 126 +++++++++ Teletype/Polynome.py | 101 +++---- Teletype/matrices.py | 3 +- .../__pycache__/infinity.cpython-36.pyc | Bin 2783 -> 2775 bytes Teletype/mymaths/infinity.py | 2 - 9 files changed, 559 insertions(+), 63 deletions(-) create mode 100644 Info/.DS_Store create mode 100644 Info/Td/TD11: Marc, Louise et Hugues.py create mode 100644 Info/Td/__pycache__/converter.cpython-36.pyc create mode 100644 Info/Td/converter.py create mode 100644 Info/Td/tpServignyRUBINdeCERVENSPartenskyTD10.py diff --git a/Info/.DS_Store b/Info/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d85a630d400d9ab3607a9a63b8039953874f5376 GIT binary patch literal 6148 zcmZQzU|@7AO)+F(5MW?n;9!8z4DAe90Z1N%F(jFwA}k>DKxQQ8=N3T4h^0V!nHWMC zQgYG_gVFSZ#dGsrT#|C~lRyqVp}Rmaximum: #Verifie si la ie valeur de la liste est supérieur a la valeur de maximum. + maximum=liste[i] #Si oui, affecte a maximum la valeur du ie valeur de la liste. + position=i #Recupere la position de cet élément. + return position #Renvoie la position de cet élément. + +def echanger(liste,position1,position2): + """Echange 2 éléments d'une liste de positions données.""" + valeur=liste[position1] #Affecte a valeur la valeur de position1e terme de la liste. + liste[position1]=liste[position2] #Mets en position1 le position2e terme de la liste. + liste[position2]=valeur #Affecte au position2e terme de la liste la valeur de valeur. + return liste #Renvoie la nouvelle liste de termes échangés. + +def TriParSelection(liste): + """Tri une liste.""" + for n in range(len(liste)-1,0,-1): #Fait varier les valeurs de n de len(liste)-1 à 1. + r=reperer(liste,n) #Recupere la position du plus grand élément parmi les n premiers. + liste=echanger(liste,n,r) #Echange les éléments de position n et r. + return liste #Renvoie la liste triée. + +def test(a,b,taille,nombre=10): + """Test:""" + for i in range(nombre): #Fait nombre tests et affiche les résultats. + liste=[random.randint(a,b) for i in range(taille)] + print("Test:",i+1) + print(liste) + print(principale(liste)) + print("") + +#test(0,10,100) + +"""Partie 2: Tri par insertion + +10) +ALGORITHME: TRIPARINSERTION: + +DONNES: L : +LT <- [] +POUR i DE 0 JUSQU A LA LONGUEUR DE LA LONGUEUR DE L FAIRE + AJOUTER A LT LA VALEUR L[i] + k <- i + cle <- LT[-1] + TANT QUE cle0 FAIRE + LT[k] <- LT[k-1] + k <- k-1 + FIN TANT QUE + LT[k] <- cle +FIN POUR +AFFICHER LT + +11) +""" +def TriParInsertion(L): + """Tri une liste par insertion.""" + LT=[] + #Recupere les éléments de L et les ajoute dans LT de façon a ce que LT soit triée + for i in range(len(L)): + #Ajoute la valeur de L considérée et stocke la valeur de la clé + LT.append(L[i]) + k=i + cle=LT[-1] + #Décale la valeur de la clé dans LT tant que la clé est supérieur a ses éléments en partant de la fin + while cle0: + LT[k]=LT[k-1] #Réaffecte la valeur de l'élément k a la valeur de l'élément k-1. + k-=1 + #Une fois que l'on trouve que la clé est inférieure à un élément ou que l'on a considéré tous les éléments de LT + LT[k]=cle + #Renvoie la liste triée + return LT + +""" +13) Justesse du tri par sélection + +Algorithme: Reperer +Pas de boucle + +Algorithme: Echanger +Pas de boucle + +Algorithme: TriParSelection +Invariant de boucle: ... + +Algorithme: Test +Invariant de boucle: ... + + +14) Justesse du tri par insertion + +Algorithme: Tri par insertion +Invariant de boucle: ... + + +15) +""" + +def TriParInsertionEtComplexite(L): + complexite=0 + LT=[] + complexite+=1 + for i in range(len(L)): + LT.append(L[i]) + k=i + cle=LT[-1] + complexite+=4 + while cle0: + LT[k]=LT[k-1] + k-=1 + complexite+=4 + complexite+=1 + LT[k]=cle + complexite+=1 + complexite+=1 + return [LT,complexite] + + +#16) + +L=[random.randint(0,10) for e in range(100)] + +Trie,complexite=TriParInsertionEtComplexite(L) +print("Liste triee:",Trie) +print("Complexite:",complexite) diff --git a/Info/Td/__pycache__/converter.cpython-36.pyc b/Info/Td/__pycache__/converter.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..057f2565fb33c5039ffe06c0ea93f6133a323685 GIT binary patch literal 7819 zcmXr!<>i{h)De@Q#=!8H0SQPkFfceUFfbJFVqjoMVMt-jVTfW#VM<|cVTfW(VM$?4 zVM}38;Yi_3;Y#67;Ys05;Y;C95l9hC5lRtG5lInE5lazIkw}qDkxCbjVoH%tkx7wF zkxP+JQAkltQA%e@Q3jJLDXJ-IV3vA{Mv7*NR*H6tPKs`dUW#steu@ED#xTVw#W=-e zj${;bifM{j3qur(J41?jibV@UibXS16l*G5ie)xa(Y93fRMu3+X2xcQW=2Ma6vki% zO{gT2zu+q*qXRi=`yBxP%4d5D>w@$iY~| z%D}*Ii!HIZIJKzc7Hd*bYGU>+*5sVT;^JEz$@zIDnR%tDx0q8>(?CSdEtb@r%rp>L zoO+8bwIVsSpyU>NT4r8iPEO@5=Cu5xTP$fs`MI~)(sS~Y5_4`bWv1O?%gimvFDkjk zl$m#nr2u3yYe7+FUdb)iqQp#)IYp@@rA2wSm`jQ(Z?TqVWagyaVy(g?kh#pdh-l8)kV_V)|%^m7f0Vt4WP^K*3#zQq|F z8W0fV>FXL)#S!f3=kKQw>>0)C=I9yZdW#h-aEskF!r3(-#Pt@po2Q?nkB_Ubt6#`1 zmH_|Ipj&J%t^uK*!ME7lef*sqeQq%Yd*0&k^bPP23ULjJVsUZw3%; z+bSj-T}{a%HUQ3=CCFwzfsu3=9m(jL@VCN)#XrPO1?M3=Aa6+!^1H>zrO9!NJu|PkAT_z<7H4{DNpXH@QF3Y#$kDf0iwklxOQM8} zQgidmQbUVVb5e_oJ@Zmh^Ga@UI>X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${el^;@@=UU#GhAi$B)@H^S zrdp<2<`lLXrYxRjP&1w#G7DD24AGs!V#AQan#Bv!1#aDPLNW%Tbqg-2!L9l#9!DQ{ z{~*s04_{Z%$Q>6~6$_~D(Bwm8B1qpS5R`jBja+cg5gffm(I7EU&Mt}su|Q)H;GvpZ ztfd77sYT%G9F_&a84r{?iZVfJK@kFuK1fT2BR)PkF()TJ{v#;1ApJ_va0?f>8O_1S z!KlK>!6X9iIH3-!K(zU3irwOfj|bIO@$t8~;^TAkQ%ZAEJrq6kz57G;3A;JOTynv1ePTu_G? z;vrP8foBasRbDY@c#?yWLz{zzgN;LwgPVhmgPntggPVhkgOfv)gPntwgN=iYgNuV1 ztOw+u+{Dbh_;^i@BJijfCjOlen(self.indent_historic): + self.addElementIndentHistoric(i) + if indent1: B.derivate() derivatives.append(deepcopy(B)) + return derivatives + + def roots(self): + """Return the list of all the roots of the polynomial.""" + """Does that by finding where all the derivates cancel, to find new extrema and applying tvi.""" + roots=[] + derivatives=self.allDerivatives() print(derivatives) derivatives.reverse() - oldextrema=[] + old_extrema=[] for derivative in derivatives: - newextrema=[] - neglim=lim(derivative,-Infinity()) - poslim=lim(derivative,Infinity()) - if type(neglim)==Infinity: - neglim=~neglim - if type(poslim)==Infinity: - poslim=~poslim - extrema=[neglim]+oldextrema+[poslim] + new_extrema=[] + neglim=self.definition[0] + poslim=self.definition[1] + extrema=[neglim]+old_extrema+[poslim] for i in range(len(extrema)-1): - print("extrema:",extrema[i],extrema[i+1]) - print("derivatives:",derivative(extrema[i]),derivative(extrema[i+1])) - print("") if derivative(extrema[i])*derivative(extrema[i+1])<=0: - newextrema.append(derivative.gradientDescent(extrema[i],extrema[i+1])) - print("newextrema:",newextrema) - print("") - print("") - oldextrema=newextrema[:] - return newextrema + new_extrema.append(derivative.gradientDescent(extrema[i],extrema[i+1])) + old_extrema=new_extrema[:] + return new_extrema - def gradientDescent(self,a,b,precision=10e-10): - """Return a int value x between a and b for which the polynomial canceled itself.""" - print("descent",self(a),self(b)) - if (self(a)>0 and self(b)>0) or (self(a)<0 and self(b)<0): + def gradientDescent(self,xa,xb,precision=10e-10): + """Return a float value x between a and b for which the polynomial canceled itself.""" + """Does that based on theoreme of intermediate value with dichotomy.""" + if self(xa)*self(xb)>0: raise Exception("The polynomial cannot cancel itself within this interval.") - return None - if a>b: - c=a - a=b - b=c + a=min(xa,xb) + b=max(xa,xb) x=(a+b)/2 while abs(self(x))>precision: - print("descending:",x,self(x)) - if self(x)>0: + if self(x)*self(b)>0: b=x - if self(x)<0: + if self(x)*self(a)>0: a=x x=(a+b)/2 return x @@ -271,21 +254,21 @@ class Polynomial: """Return a string of the decomposition in simple elements of the polynomial.""" pass - def show(self,zone=[-5,5],precision=0.1): - """Show the polynomial using matplotlib.""" - Grapher([self])() - """ + def show(self): + """Show the polynomial with grapher.""" + Grapher([self])() #Instance Grapher only takes the list of the functions in parameter and can be called to display them. + + def matplotlibShow(self,zone=[-5,5],precision=0.1): + """Show the polynomial with matplotlib using optional zone and precision.""" z,Z=zone X=list([precision*x for x in range(int(z/precision),int(Z/precision))]) Y=list([self(x) for x in X]) plt.plot(X,Y,label="Polynomial") plt.show() - print("executed") - """ - - if __name__ == "__main__": + #suite_random = [random.randint(0,10) for i in range(10)] #Create polynome as big as needed. + #X=Polynomial(suite_random) suite1 = [3,2,4,5,1] suite2 = [1,4,1] suite3 = [4,5] @@ -302,6 +285,6 @@ if __name__ == "__main__": print("Unit polynomial of C =",C.unit()) print("HCF (PGCD en francais) of A and B (written A^B) =",A^B) print("LCM (PPCM en francais) of A and B (written A|B) =",A|B) - #print(A.gradientDescent(-10,10)) - print(A.roots()) + print("Roots of A:",A.roots()) + print("Show A on screen:") A.show() diff --git a/Teletype/matrices.py b/Teletype/matrices.py index caea707..07f0e35 100644 --- a/Teletype/matrices.py +++ b/Teletype/matrices.py @@ -8,7 +8,6 @@ from copy import deepcopy def pgcd(a,b): t = b b = a % b - if b == 0: return t else: @@ -71,7 +70,7 @@ def pgcdl(l): if __name__=="__main__": - c=5 #taille de la matrice carre de depart + c=6 #taille de la matrice carre de depart a=np.array([[random.randint(0,10) for i in range(c)] for j in range(c)]) print(a) b=triangular(a) diff --git a/Teletype/mymaths/__pycache__/infinity.cpython-36.pyc b/Teletype/mymaths/__pycache__/infinity.cpython-36.pyc index dbe19768c033e887a05de18c7192b69d0af3adf0..044955d983aadeb6f1300cdd41ee9a9519d2c79a 100644 GIT binary patch delta 167 zcmcaFdR>&$n3tDpYU#w7qK%xK>}qBV3=B1lDU8`nMM5PEDNN0bO^kU8C5$zU&5R|C zH4M#+*~~>+ldahEIk^}Z7#J8i7`Y~IVb|5NVqjn>VSp&hW-1aZVM<|yYGwwDGD9`% zP3GlD;WJ`jU?_%|z{AKjxsjud(P}aW=OjkQ$+J1{F>`QmOs?UIV|C>3sf3=B1lDU8`nMIt2(DNN0bO^hiF!3-sgHH^)S zC5$x;&5YU1MLLr$*z>u07#J8B7&#a@7+EH7VAs{LVPIe=VSp&iW-5{>VM<|y8^8<} zXNDSJFqxerMZlPWfuR^;3J=(nN{%*0n@OCL7@a3i=Df$u$-z0fge#8KnS+sy5daZ# BBufAQ diff --git a/Teletype/mymaths/infinity.py b/Teletype/mymaths/infinity.py index f6adeb4..0d6124d 100644 --- a/Teletype/mymaths/infinity.py +++ b/Teletype/mymaths/infinity.py @@ -42,7 +42,6 @@ class Infinity: def __mul__(self,other): if other==0: raise Exception("Undetermined") - return None else: if sign(other)!=sign(self): return Infinity(0) @@ -51,7 +50,6 @@ class Infinity: def __truediv__(self,other): if type(other)==Infinity: raise Exception("Undetermined") - return None else: if sign(other)!=sign(self): return Infinity(0)