{ "cells": [ { "cell_type": "markdown", "source": [ "# Les vecteurs et matrices\n", "\n", "Les listes peuvent servir de vecteur et matrices. Mais peu de fonctions mathématiques sont applicables sur des listes. En plus, il faudra utiliser un itérateur chaque fois ce qui prendra du temps. On utilise plutôt les `array` de `numpy` pour les vecteurs et matrices. On peut créer un array à partir d'une liste:" ], "metadata": {} }, { "cell_type": "code", "execution_count": 1, "source": [ "import numpy as np \n", "\n", "l = [1,2,3]\n", "a = np.array(l)\n", "a" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([1, 2, 3])" ] }, "metadata": {}, "execution_count": 1 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 2, "source": [ "a.shape" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(3,)" ] }, "metadata": {}, "execution_count": 2 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 3, "source": [ "a[0]" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, "metadata": {}, "execution_count": 3 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "On peut créer des array vides, avec des 1 ou zero" ], "metadata": {} }, { "cell_type": "code", "execution_count": 5, "source": [ "x = np.ones((1,3))\n", "x" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(array([[1., 1., 1.]]), array([[1.63437886, 0.55950035, 0.12555924]]))" ] }, "metadata": {}, "execution_count": 5 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 4, "source": [ "x = np.zeros((1,10))\n", "x" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "metadata": {}, "execution_count": 4 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Une fonction très utile permet de créer une grille sur un interval donné par un certain nombre de points:" ], "metadata": {} }, { "cell_type": "code", "execution_count": 9, "source": [ "x = np.linspace(0,1,9)\n", "x" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([0. , 0.125, 0.25 , 0.375, 0.5 , 0.625, 0.75 , 0.875, 1. ])" ] }, "metadata": {}, "execution_count": 9 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "ou en nombre entier" ], "metadata": {} }, { "cell_type": "code", "execution_count": 8, "source": [ "x = np.arange(1,11)\n", "x" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" ] }, "metadata": {}, "execution_count": 8 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Les opérateurs\n", "\n", "Plusieurs opérateurs matriciels et vectorisés existent avec `numpy`. Voici la multiplication:" ], "metadata": {} }, { "cell_type": "code", "execution_count": 11, "source": [ "x = np.array([2,2,2])\n", "y = np.array([1,1,1])" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "Un produit élément par élément" ], "metadata": {} }, { "cell_type": "code", "execution_count": 12, "source": [ "x*y" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([2, 2, 2])" ] }, "metadata": {}, "execution_count": 12 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Un produit vectoriel" ], "metadata": {} }, { "cell_type": "code", "execution_count": 19, "source": [ "np.dot(x,y)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6" ] }, "metadata": {}, "execution_count": 19 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Avec une notation plus légerte" ], "metadata": {} }, { "cell_type": "code", "execution_count": 20, "source": [ "x @ y" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6" ] }, "metadata": {}, "execution_count": 20 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Un exemple de multiplication matricielle" ], "metadata": {} }, { "cell_type": "code", "execution_count": 42, "source": [ "y = np.array([1,1,1])\n", "y" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([1, 1, 1])" ] }, "metadata": {}, "execution_count": 42 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 43, "source": [ "x = np.array([[1,2,3],[4,5,6]])\n", "x" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "metadata": {}, "execution_count": 43 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 44, "source": [ "x.shape,y.shape" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "((2, 3), (3,))" ] }, "metadata": {}, "execution_count": 44 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 45, "source": [ "x @ y" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 6, 15])" ] }, "metadata": {}, "execution_count": 45 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Transformation de matrices" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "L'équivalent de l'opérateur `vec()`:" ], "metadata": {} }, { "cell_type": "code", "execution_count": 46, "source": [ "x.reshape((6,1))" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[1],\n", " [2],\n", " [3],\n", " [4],\n", " [5],\n", " [6]])" ] }, "metadata": {}, "execution_count": 46 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "La transposition se fait simplement..." ], "metadata": {} }, { "cell_type": "code", "execution_count": 48, "source": [ "x.T" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[1, 4],\n", " [2, 5],\n", " [3, 6]])" ] }, "metadata": {}, "execution_count": 48 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 47, "source": [ "x.transpose()" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[1, 4],\n", " [2, 5],\n", " [3, 6]])" ] }, "metadata": {}, "execution_count": 47 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Les extractions" ], "metadata": {} }, { "cell_type": "code", "execution_count": 55, "source": [ "x[:1,:]" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[1, 2, 3]])" ] }, "metadata": {}, "execution_count": 55 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 54, "source": [ "x[0,:2]" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([1, 2])" ] }, "metadata": {}, "execution_count": 54 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 59, "source": [ "x = np.eye(4)\n", "x,np.diagonal(x)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", " [0., 0., 0., 1.]]),\n", " array([1., 1., 1., 1.]))" ] }, "metadata": {}, "execution_count": 59 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Inverse et decompositions" ], "metadata": {} }, { "cell_type": "code", "execution_count": 60, "source": [ "x = np.array([[0.8,0.2],[0.2,0.8]])\n", "x" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[0.8, 0.2],\n", " [0.2, 0.8]])" ] }, "metadata": {}, "execution_count": 60 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Inverse d'une matrice" ], "metadata": {} }, { "cell_type": "code", "execution_count": 61, "source": [ "np.linalg.inv(x)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[ 1.33333333, -0.33333333],\n", " [-0.33333333, 1.33333333]])" ] }, "metadata": {}, "execution_count": 61 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Décomposition de Cholesky" ], "metadata": {} }, { "cell_type": "code", "execution_count": 62, "source": [ "np.linalg.cholesky(x)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[0.89442719, 0. ],\n", " [0.2236068 , 0.8660254 ]])" ] }, "metadata": {}, "execution_count": 62 } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Valeurs et vecteurs propres (eigenvalues et vecteurs)" ], "metadata": {} }, { "cell_type": "code", "execution_count": 63, "source": [ "np.linalg.eig(x)" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(array([1. , 0.6]),\n", " array([[ 0.70710678, -0.70710678],\n", " [ 0.70710678, 0.70710678]]))" ] }, "metadata": {}, "execution_count": 63 } ], "metadata": {} } ], "metadata": { "orig_nbformat": 4, "language_info": { "name": "python", "version": "3.8.5", "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py" }, "kernelspec": { "name": "python3", "display_name": "Python 3.8.5 64-bit ('base': conda)" }, "interpreter": { "hash": "ba2340ab882356406e091df0706039b4b3cc5191eef6c073d3fb97005dbe0324" } }, "nbformat": 4, "nbformat_minor": 2 }