diff --git a/SPE/IPT/TP0 METHODES NUMERIQUES/main.py b/SPE/IPT/TP0 METHODES NUMERIQUES/main.py index 59ac9bc..c2a7486 100644 --- a/SPE/IPT/TP0 METHODES NUMERIQUES/main.py +++ b/SPE/IPT/TP0 METHODES NUMERIQUES/main.py @@ -11,97 +11,187 @@ class Question: def __exit__(self, exc_type, exc_value, exc_traceback): print((self.level*2)*' ' + f"<- {self.number}. : {self.name} -- Fin\n") +if False: + with Question(1): + with Question(1,1): + def dichotomie(f, n, a=0, b=2): + iterations = 0 + while abs(b-a) >= 2*10**(-n): + iterations += 1 + c = (a+b) / 2 + if f(c) == 0: + return c + elif f(a)*f(c) > 0: + a = c + else: + b = c + return c, iterations + f = lambda x: x**3 - 3*x**2 + 1 + print(dichotomie(f,5,0,1.5)) + print(dichotomie(np.sin, 3, 3, 4)) + print(dichotomie(np.sin, 10, 3, 4)) + + with Question(2,1): + def newton(f, fp, x0, nbiter, epsilon=1e-10): + for i in range(nbiter): + x1 = x0 - f(x0) / fp(x0) + if abs(x1 - x0) <= epsilon: + break + x0 = x1 + return x1 + print(newton(lambda x: x**3 - 3*x**2 + 1, lambda x: 3*x**2 - 6*x, 1.5, 200, epsilon=1e-100)) + print(newton(np.sin, np.cos, 3, 100)) + print(abs(newton(np.sin, np.cos, 3, 100) - dichotomie(np.sin, 10, 3, 4)[0])) + + + with Question(2): + from math import cos, exp, sin, log + from scipy.integrate import quad + from time import time + with Question(1,2): + def rectangles(f, a, b, n): + res = 0 + for k in range(n): + res += f(a + k*(b - a)/n) + return res*(b - a)/n + def f1(x): + return x + def f2(x): + return x**10 + f3 = cos + f4 = exp + print(rectangles(f1, 0, 1, 1000), quad(f1, 0, 1)[0]) + print(rectangles(f2, 0, 1, 1000), quad(f2, 0, 1)[0]) + print(rectangles(f3, 0, np.pi/2, 1000), quad(f3, 0, np.pi/2)[0]) + print(rectangles(f4, -3, 3, 1000), quad(f4, -3, 3)[0]) + + + with Question(2,2): + def trapezes(f, a, b, n): + res = 0 + for k in range(n): + res += f(a + k*(b - a)/n) + f(a + (k+1)*(b - a)/n) + return res*(b - a)/(2*n) + print(trapezes(f1, 0, 1, 1000), quad(f1, 0, 1)[0]) + print(trapezes(f2, 0, 1, 1000), quad(f2, 0, 1)[0]) + print(trapezes(f3, 0, np.pi/2, 1000), quad(f3, 0, np.pi/2)[0]) + print(trapezes(f4, -3, 3, 1000), quad(f4, -3, 3)[0]) + + + with Question(3, 2): + def simpson(f, a, b, n): + res = 0 + for k in range(n): + res += f(a + k*(b - a)/n) + 4*f(((a+k*(b - a)/n) + (a + (k+1)*(b - a)/n))/2) + f(a + (k+1)*(b - a)/n) + return res*(b - a)/(6*n) + print(simpson(f1, 0, 1, 1000), quad(f1, 0, 1)[0]) + print(simpson(f2, 0, 1, 1000), quad(f2, 0, 1)[0]) + print(simpson(f3, 0, np.pi/2, 1000), quad(f3, 0, np.pi/2)[0]) + print(simpson(f4, -3, 3, 1000), quad(f4, -3, 3)[0]) + + + with Question(3): + import matplotlib.pyplot as plt + with Question(1, 2): + def euler(F, t0, y0, t1, n): + res = [y0]*(n+1) + for i in range(1,n+1): + res[i] = res[i-1] + (t1-t0)/n * F(t0 + i*(t1-t0)/n, res[i-1]) + return res + def expo_euler(t, yt): + return yt + ne = 100 + n = 10 + plt.plot(np.linspace(0, 1, num=n+1), euler(expo_euler, 0, 1, 1, n), 'bo') + plt.plot(np.linspace(0, 1, num=ne+1), np.exp(np.linspace(0, 1, num=ne+1)), 'r') + plt.show() + + +print("--- Calcul Matriciel ---\n\n") with Question(1): - with Question(1,1): - def dichotomie(f, n, a=0, b=2): - iterations = 0 - while abs(b-a) >= 2*10**(-n): - iterations += 1 - c = (a+b) / 2 - if f(c) == 0: - return c - elif f(a)*f(c) > 0: - a = c + with Question(2, 2): + with Question(1,3): + def matrice_nulle(n, m): + return [[0 for _ in range(m)] for _ in range(n)] + with Question(2,3): + def dimension(A): + if len(A): + return len(A),len(A[0]) else: - b = c - return c, iterations - f = lambda x: x**3 - 3*x**2 + 1 - print(dichotomie(f,5,0,1.5)) - print(dichotomie(np.sin, 3, 3, 4)) - print(dichotomie(np.sin, 10, 3, 4)) - - - with Question(2,1): - def newton(f, fp, x0, nbiter, epsilon=1e-10): - for i in range(nbiter): - x1 = x0 - f(x0) / fp(x0) - if abs(x1 - x0) <= epsilon: - break - x0 = x1 - return x1 - print(newton(lambda x: x**3 - 3*x**2 + 1, lambda x: 3*x**2 - 6*x, 1.5, 200, epsilon=1e-100)) - print(newton(np.sin, np.cos, 3, 100)) - print(abs(newton(np.sin, np.cos, 3, 100) - dichotomie(np.sin, 10, 3, 4)[0])) - - -with Question(2): - from math import cos, exp, sin, log - from scipy.integrate import quad - from time import time - with Question(1,2): - def rectangles(f, a, b, n): - res = 0 - for k in range(n): - res += f(a + k*(b - a)/n) - return res*(b - a)/n - def f1(x): - return x - def f2(x): - return x**10 - f3 = cos - f4 = exp - print(rectangles(f1, 0, 1, 1000), quad(f1, 0, 1)[0]) - print(rectangles(f2, 0, 1, 1000), quad(f2, 0, 1)[0]) - print(rectangles(f3, 0, np.pi/2, 1000), quad(f3, 0, np.pi/2)[0]) - print(rectangles(f4, -3, 3, 1000), quad(f4, -3, 3)[0]) - - - with Question(2,2): - def trapezes(f, a, b, n): - res = 0 - for k in range(n): - res += f(a + k*(b - a)/n) + f(a + (k+1)*(b - a)/n) - return res*(b - a)/(2*n) - print(trapezes(f1, 0, 1, 1000), quad(f1, 0, 1)[0]) - print(trapezes(f2, 0, 1, 1000), quad(f2, 0, 1)[0]) - print(trapezes(f3, 0, np.pi/2, 1000), quad(f3, 0, np.pi/2)[0]) - print(trapezes(f4, -3, 3, 1000), quad(f4, -3, 3)[0]) - - + return 0 + with Question(3,3): + def addition(A, B): + n,m = dimension(A) + return [[A[i][j] + B[i][j] for j in range(m)] for i in range(n)] + A3=[[1,2,3], + [0,1,0], + [-1,2,-6]] + B3=[[-1,5,0], + [1,0,-2], + [1,2,3]] + print(addition(A3, B3), '\n', np.array(A3)+np.array(B3)) + print("Le résultat pour cette exemple est le même.") + with Question(4,3): + def transposee(A): + n,m = dimension(A) + return [[A[j][i]for j in range(n)] for i in range(m)] + print(transposee([[1,2,3], + [4,5,6]])) + print(list(map(list,zip(*[[1,2,3],[4,5,6]])))) # Parce que c'est amusant de chercher à faire une version courte. + with Question(5,3): + def multiple(A, coef): + n,m = dimension(A) + return [[coef * A[i][j] for j in range(m)] for i in range(n)] + print(multiple([[1,2], + [3,4]], 2)) + with Question(6,3): + def multiplication(A, B): + n,p = dimension(A) + _,m = dimension(B) + return [[sum(A[i][k] * B[k][j] for k in range(p))for j in range(m)] for i in range(n)] + A6 = [[1,2,3], + [4,5,6]] + B6 = [[3,6], + [2,5], + [1,4]] + print(multiplication(A6,B6)) + with Question(7,3): + def puissance(A, n): + _,m = dimension(A) + res = [[1 if j==i else 0 for j in range(m)] for i in range(m)] + for _ in range(0, n): + print(n) + res = multiplication(res, A) + return res + print(puissance([[0,1], + [0,0]], 2)) with Question(3, 2): - def simpson(f, a, b, n): - res = 0 - for k in range(n): - res += f(a + k*(b - a)/n) + 4*f(((a+k*(b - a)/n) + (a + (k+1)*(b - a)/n))/2) + f(a + (k+1)*(b - a)/n) - return res*(b - a)/(6*n) - print(simpson(f1, 0, 1, 1000), quad(f1, 0, 1)[0]) - print(simpson(f2, 0, 1, 1000), quad(f2, 0, 1)[0]) - print(simpson(f3, 0, np.pi/2, 1000), quad(f3, 0, np.pi/2)[0]) - print(simpson(f4, -3, 3, 1000), quad(f4, -3, 3)[0]) - - -with Question(3): - import matplotlib.pyplot as plt - with Question(1, 2): - def euler(F, t0, y0, t1, n): - res = [y0]*(n+1) - for i in range(1,n+1): - res[i] = res[i-1] + (t1-t0)/n * F(t0 + i*(t1-t0)/n, res[i-1]) - return res - def expo_euler(t, yt): - return yt - n = 100 - plt.plot(np.linspace(0, 1, num=n+1), euler(expo_euler, 0, 1, 1, n)) - plt.plot(np.linspace(0, 1, num=n+1), np.exp(np.linspace(0, 1, num=n+1))) - plt.show() + with Question(2,3): + with Question(1,4): + def echange_ligne(A, i, j): + A[i], A[j] = A[j], A[i] + A1 = [[1,2,3], + [4,5,6]] + echange_ligne(A1, 0, 1) + print(A1) + with Question(2,4): + def transvection(A, i, j, mu): + A[i] = addition([A[i]], multiple([A[j]], mu))[0] + transvection(A1, 0, 1, -1) + print(A1) + with Question(3, 4): + def pivot_partiel(A, j0): + n, m = dimension(A) + if n true + |'q' -> false + |'r' -> false + |_ -> true;; + +let rec interpretation contexte formule = match formule with + | Var v-> contexte v + | Non f -> not (interpretation contexte f) + | Implique (a,b) -> not (interpretation contexte a) || (interpretation contexte b) + | Equivaut (a,b) -> not (interpretation contexte a) = (interpretation contexte b) + | Et [] -> true + | Et (h::t) -> (interpretation contexte h) && (interpretation contexte (Et t)) + | Ou [] -> false + | Ou (h::t) -> (interpretation contexte h) || (interpretation contexte (Ou t));; + +interpretation (exemple_de_contexte) (Et [Var 'q'; Non (Var 'r')]);; \ No newline at end of file diff --git a/SPE/OPT/TD 1/main.ml b/SPE/OPT/TD 1/main.ml new file mode 100644 index 0000000..0c03b84 --- /dev/null +++ b/SPE/OPT/TD 1/main.ml @@ -0,0 +1,12 @@ +(*1 *) +let rec est_un_systeme c = match c with + | [] -> false + | [h] -> h=1 + | h1::h2::t -> h1 > h2 && est_un_systeme (h2::t);; +est_un_systeme [3;4;3];; + +(*2*) + +(* Mq $x \geq M(x)$ par l'absurde + Supposons qu'on a M(x) > x + alors il existe $k \in \mathbb{N}^m$ tel que diff --git a/SPE/OPT/TD 1/main.tex b/SPE/OPT/TD 1/main.tex new file mode 100644 index 0000000..bc4e8ed --- /dev/null +++ b/SPE/OPT/TD 1/main.tex @@ -0,0 +1,41 @@ +\documentclass[a4paper,10pt]{article} +%\documentclass[a4paper,10pt]{scrartcl} + +\usepackage[utf8]{inputenc} +\usepackage{mathtools} +\usepackage{amssymb} +\usepackage{amsmath} +\title{TD Option Info} +\author{} +\date{} + +\pdfinfo{% + /Title (TD option info) + /Author () + /Creator () + /Producer () + /Subject () + /Keywords () +} + +\begin{document} +\maketitle + + \section{Systèmes de pièces} + \subsection{} + \section{} + Montrons qu'on a $ x \geq M(x) $\\ + $M(x)$ est défini, alors il existe $k \in \mathbb{N}^m$ tel que $x = \sum_{i=1}^m k_i c_i$ avec $\sum_{i=1}^m k_i = M(x)$\\ + On sait que $\forall\text{ } 1\leq i\leq m, c_i >= 1$ + Donc $x = \sum_{i=0}^m k_i c_i \geq \sum_{i=0}^m k_i = M(x)$\\\\ + Montrons qu'on a $\lceil\frac{x}{c_1} \rceil \leq M(x)$\\ + Par définition $\lceil\frac{x}{c_1} \rceil \leq \frac{x}{c_1} + 1$\\ + Montrons qu'on a $\frac{x}{c_1} + 1 \leq M(x)$\\ + $M(x)$ est défini, alors il existe $k \in \mathbb{N}^m$ tel que $x = \sum_{i=1}^m k_i c_i$ avec $\sum_{i=1}^m k_i = M(x)$\\ + Ainsi, $\frac{x}{c_1} + 1 = \left( \sum_{i=1}^m \frac{k_i c_i}{c_1}\right) + 1 = \left( \sum_{i=2}^m \frac{k_i c_i}{c_1}\right) + k_1 + 1 $\\ + or on a $c_1 > c_2 > ... > c_m$ donc + $\left( \sum_{i=2}^m \frac{k_i c_i}{c_1}\right) + k_1 + 1 \leq \left( \sum_{i=2}^m k_i\right) + k_1 + 1 = M(x) + 1 $\\ + CQFD. + \subsection{} + +\end{document} diff --git a/SPE/OPT/TP 1/main.ml b/SPE/OPT/TP 1/main.ml new file mode 100644 index 0000000..610df05 --- /dev/null +++ b/SPE/OPT/TP 1/main.ml @@ -0,0 +1,46 @@ +(*Q1*) +let h_exemple s = (Char.code (Char.uppercase_ascii s.[0])) - 65;; +h_exemple "z";; +type ('a, 'b) dict = { + hache: 'a -> int; + table_hachage: ('a *'b) list array; + largeur: int};; +(*Q2*) +let creer_dict_vide n hache = { + hache=hache; + table_hachage=(Array.make n []); + largeur=n};; + +let d_exemple = creer_dict_vide 25 h_exemple;; +(*Q3*) + +let contient cle d = + let h = d.hache cle in + List.mem cle (List.map fst d.table_hachage.(h));; +contient "blablab" d_exemple;; +let inserer cle enregistrement d = + if contient cle d then failwith "Déjà dans le dictionnaire" else (); + let h = d.hache cle in + d.table_hachage.(h) <- ((cle, enregistrement)::d.table_hachage.(h));; + +let valeur cle d = + let rec aux acc = match acc with + |[] -> raise Not_found + |(c, enregistrement)::t -> if c=cle then enregistrement else aux t in + aux d.table_hachage.(d.hache cle);; + +let supprimer cle d = + let h = d.hache cle in + let rec aux acc li = match li with + | [] -> raise Not_found + | (c, e)::t -> if c=cle then acc@t else aux ((c,e)::acc) t in + d.table_hachage.(h) <- aux [] d.table_hachage.(h);; +let modifier cle enregistrement d = + supprimer cle d; + inserer cle enregistrement d;; + +inserer "rire" "voilà" d_exemple;; +valeur "mais" d_exemple;; +supprimer "rigolo" d_exemple;; +modifier "blablab" "truc" d_exemple;; +d_exemple;; \ No newline at end of file diff --git a/SPE/OPT/TP 2/main.ml b/SPE/OPT/TP 2/main.ml new file mode 100644 index 0000000..d71ea85 --- /dev/null +++ b/SPE/OPT/TP 2/main.ml @@ -0,0 +1,42 @@ +type formule = + | Var of int + | Not of formule + | Imply of formule*formule + | Equiv of formule*formule + | And of formule list + | Or of formule list;; + +let f = And [Or [Var 0; Var 1; Var 2]; Not (Var 0)];; +let g = Equiv (Var 0, And [Var 1; Var 2]);; +let rec evaluer_tab t form = match form with + | Var i -> t.(i) + | Not f -> not (evaluer_tab t f) + | Imply (f1,f2) -> (not (evaluer_tab t f1)) || (evaluer_tab t f2) + | Equiv (f1,f2) -> (evaluer_tab t f1) = (evaluer_tab t f2) + | And [] -> true + | And (h::tail) -> (evaluer_tab t h) && (evaluer_tab t (And tail)) + | Or [] -> false + | Or (h::tail) -> (evaluer_tab t h) || (evaluer_tab t (Or tail));; +5 lsr 1;; + +let evaluer n c f = + let contexte = Array.make n false in + let j = ref c in + for i = 0 to n-1 do + contexte.(i) <- !j mod 2 = 0; + j := !j lsr 1 + done; + evaluer_tab contexte f;; +evaluer 3 0 f;; + +let table_de_verite n f = + let rec puis n a = match n with + | 0 -> 1 + | _ -> a * (puis (n-1) a) in + let res = Array.make (puis n 2) false in + for c=0 to ((Array.length res) -1) do + res.(c) <- evaluer n c f + done; + res;; +table_de_verite 3 g;; +