mirror of
https://github.com/BreizhHardware/py_A2.git
synced 2026-01-18 16:47:38 +01:00
Tp rational fin
This commit is contained in:
66
TP Modules Scientifiques/tp3.py
Normal file
66
TP Modules Scientifiques/tp3.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import matplotlib.pyplot as plot
|
||||
import numpy as np
|
||||
import math
|
||||
|
||||
|
||||
def generer_abscisses(a, b, n):
|
||||
return np.linspace(a, b, n)
|
||||
|
||||
|
||||
def tracer_courbe(f, a, b, n):
|
||||
x = generer_abscisses(a, b, n)
|
||||
y = f(x)
|
||||
plot.plot(x, y)
|
||||
plot.show()
|
||||
|
||||
|
||||
def f1(x):
|
||||
return 2 * x + 3
|
||||
|
||||
|
||||
def f2(x):
|
||||
return x ** 2
|
||||
|
||||
|
||||
def plusieurs_courbes():
|
||||
x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
|
||||
y1 = np.sin(x)
|
||||
y2 = np.cos(x)
|
||||
plot.plot(x, y1, 'b', x, y2, 'r')
|
||||
plot.show()
|
||||
|
||||
|
||||
def dessiner_cercle(x, y, r):
|
||||
theta = np.linspace(0, 2 * np.pi, 100)
|
||||
x = x + r * np.cos(theta)
|
||||
y = y + r * np.sin(theta)
|
||||
plot.axis("equal")
|
||||
plot.plot(x, y)
|
||||
return plot
|
||||
|
||||
|
||||
def cercle_dans_cercle():
|
||||
plot.axis("equal")
|
||||
fig, ax = plot.subplots()
|
||||
for i in range(10):
|
||||
cercle = dessiner_cercle(0, 0, i + 1)
|
||||
ax.add_patch(cercle)
|
||||
plot.show()
|
||||
|
||||
|
||||
|
||||
|
||||
# Jeu de test
|
||||
print(generer_abscisses(1, 10, 4))
|
||||
print(generer_abscisses(1, 10, 10))
|
||||
tracer_courbe(f1, -5, 20, 100)
|
||||
tracer_courbe(f2, -2, 2, 100)
|
||||
plusieurs_courbes()
|
||||
l = [1, 42, -3]
|
||||
a = np.array(l)
|
||||
print(a) # affiche [1 42 -3]
|
||||
cos_a = np.cos(a) # notez qu’il s’agit bien de np.cos() et pas de math.cos()
|
||||
print(cos_a) # affiche [ 0.54030231 -0.66939722 -0.9899925 ]
|
||||
cercle = dessiner_cercle(0, 0, 1)
|
||||
cercle.show()
|
||||
cercle_dans_cercle()
|
||||
52
TP class/main.py
Normal file
52
TP class/main.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from rational import Rational
|
||||
def __main__():
|
||||
print("--------------------Rational--------------------")
|
||||
r1 = Rational()
|
||||
r1.set_numerator(1)
|
||||
r1.set_denominator(2)
|
||||
r2 = Rational()
|
||||
r2.set_numerator(1)
|
||||
r2.set_denominator(3)
|
||||
print("--------------------r1--------------------")
|
||||
print(r1)
|
||||
print("--------------------r2--------------------")
|
||||
print(r2)
|
||||
r1.simplify()
|
||||
r2.simplify()
|
||||
print("--------------------r1.simplify--------------------")
|
||||
print(r1)
|
||||
print("--------------------r2.simplify--------------------")
|
||||
print(r2)
|
||||
print("--------------------r1 == r2--------------------")
|
||||
print(r1 == r2)
|
||||
print("--------------------r1 * r2--------------------")
|
||||
r3 = r1 * r2
|
||||
print(r3)
|
||||
print("--------------------r1 / r2--------------------")
|
||||
r4 = r1 / r2
|
||||
print(r4)
|
||||
print("--------------------r1 + r2--------------------")
|
||||
r5 = r1 + r2
|
||||
print(r5)
|
||||
print("--------------------r3--------------------")
|
||||
r3 = Rational()
|
||||
r3.set_numerator(2346)
|
||||
r3.set_denominator(1548)
|
||||
print(r3)
|
||||
r3.simplify()
|
||||
print(r3)
|
||||
print("--------------------r4--------------------")
|
||||
r4 = Rational()
|
||||
r4.set_numerator(9745)
|
||||
r4.set_denominator(546)
|
||||
print(r4)
|
||||
r4.simplify()
|
||||
print(r4)
|
||||
print("--------------------r3 + r4--------------------")
|
||||
r5 = r3 + r4
|
||||
print(r5)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
__main__()
|
||||
63
TP class/rational.py
Normal file
63
TP class/rational.py
Normal file
@@ -0,0 +1,63 @@
|
||||
class Rational:
|
||||
def __init__(self):
|
||||
self.__numerator = 0
|
||||
self.__denominator = 1
|
||||
|
||||
def __pgcd(self, numerator, denominator):
|
||||
while denominator != 0:
|
||||
numerator, denominator = denominator, numerator % denominator
|
||||
return numerator
|
||||
|
||||
def __ppcm(self, numerator, denominator):
|
||||
return numerator * denominator // self.__pgcd(numerator, denominator)
|
||||
|
||||
def set_numerator(self, numerator):
|
||||
self.__numerator = numerator
|
||||
|
||||
def set_denominator(self, denominator):
|
||||
if denominator == 0:
|
||||
print("Error: denominator can't be 0")
|
||||
return
|
||||
self.__denominator = denominator
|
||||
|
||||
def get_numerateur(self):
|
||||
return self.__numerator
|
||||
|
||||
def get_denominator(self):
|
||||
return self.__denominator
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.__numerator}/{self.__denominator}"
|
||||
|
||||
def simplify(self):
|
||||
pgcd = self.__pgcd(self.__numerator, self.__denominator)
|
||||
self.__numerator //= pgcd
|
||||
self.__denominator //= pgcd
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.__numerator * other.__denominator == self.__denominator * other.__numerator
|
||||
|
||||
def __mul__(self, other):
|
||||
numerator = self.__numerator * other.__denominator
|
||||
denominator = self.__denominator * other.__denominator
|
||||
r1 = Rational()
|
||||
r1.set_numerator(numerator)
|
||||
r1.set_denominator(denominator)
|
||||
return r1
|
||||
|
||||
def __truediv__(self, other):
|
||||
numerator = self.__numerator * other.__denominator
|
||||
denominator = self.__denominator * other.__numerator
|
||||
r1 = Rational()
|
||||
r1.set_numerator(numerator)
|
||||
r1.set_denominator(denominator)
|
||||
return r1
|
||||
|
||||
def __add__(self, other):
|
||||
numerator = (self.__numerator * (self.__ppcm(self.__denominator, other.__denominator) // self.__denominator) +
|
||||
other.__numerator * (self.__ppcm(self.__denominator, other.__denominator) // other.__denominator))
|
||||
denominator = self.__ppcm(self.__denominator, other.__denominator)
|
||||
r1 = Rational()
|
||||
r1.set_numerator(numerator)
|
||||
r1.set_denominator(denominator)
|
||||
return r1
|
||||
19
TP class/trucAvecJJ.py
Normal file
19
TP class/trucAvecJJ.py
Normal file
@@ -0,0 +1,19 @@
|
||||
class Test:
|
||||
def __init__(self, n):
|
||||
self.__n = n
|
||||
|
||||
def afficher_n(self):
|
||||
print(self.__n)
|
||||
|
||||
def __str__(self):
|
||||
return "n = " + str(self.__n)
|
||||
|
||||
def __add__(self, other):
|
||||
return self.__n + other.__n
|
||||
|
||||
|
||||
T1 = Test(2)
|
||||
T1.afficher_n()
|
||||
print(T1)
|
||||
T2 = Test(3)
|
||||
print(T1 + T2)
|
||||
Reference in New Issue
Block a user