From 86f1ed1a4f122eb7dd2db0c312252224de0e222d Mon Sep 17 00:00:00 2001 From: marcellus Date: Sun, 22 Jun 2025 21:49:40 +0200 Subject: notes: 2025-06-22 21:49:40 from w11 --- ...35 Syst\303\250me matriciel -- Exercices.ipynb" | 81 +++++++++++++++------- 1 file changed, 56 insertions(+), 25 deletions(-) (limited to 'PVCM/cama/fr/ma35 Système matriciel -- Exercices.ipynb') diff --git "a/PVCM/cama/fr/ma35 Syst\303\250me matriciel -- Exercices.ipynb" "b/PVCM/cama/fr/ma35 Syst\303\250me matriciel -- Exercices.ipynb" index 2107be6..7ce0cf2 100644 --- "a/PVCM/cama/fr/ma35 Syst\303\250me matriciel -- Exercices.ipynb" +++ "b/PVCM/cama/fr/ma35 Syst\303\250me matriciel -- Exercices.ipynb" @@ -1,25 +1,4 @@ { - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 2, "cells": [ { "cell_type": "markdown", @@ -64,8 +43,39 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x mieux = [1.0000019 0.99999905]\n", + "[1. 3.]\n" + ] + } + ], + "source": [ + "def solve_partial_gauss(A, b):\n", + " A = A.astype(np.float64) # si A a des entiers, on va avoir des erreurs de calculs\n", + " for i in range(len(A)-1):\n", + " pivot = i + np.argmax(np.abs(A[i:,i]))\n", + " A[[i,pivot]] = A[[pivot, i]]\n", + " b[[i,pivot]] = b[[pivot, i]]\n", + " coefs = - A[i+1:,i] / A[i,i] # Normalement il faut tester que A[i,i] != 0\n", + " A[i+1:, i:] += np.outer(coefs, A[i, i:]) # ou np.einsum('i,j -> ij', coefs, A[i, i:])\n", + " b[i+1:] += coefs * b[i]\n", + " # A est maintenant triangulaire surpérieur\n", + " res = np.zeros(len(b), dtype=b.dtype)\n", + " res[-1] = b[-1] / A[-1,-1]\n", + " for i in range(len(A)-1)[::-1]:\n", + " res[i] = (b[i] - A[i,i+1:] @ res[i+1:]) / A[i,i]\n", + " return res\n", + "e = 1E-6\n", + "A = np.array([[e, 1], [1, 2]], dtype='float32')\n", + "b = np.array([1., 3.], dtype='float32')\n", + "x = solve_partial_gauss(A,b)\n", + "print(\"x mieux =\", x)\n", + "print(A @ x )" + ] }, { "cell_type": "markdown", @@ -186,5 +196,26 @@ "outputs": [], "source": [] } - ] -} \ No newline at end of file + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- cgit v1.2.3