Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Quelques constructions classiques pour éviter de recoder des variantes d'algorithmes.
5classiques.
6"""
9def numpy_matrix2list(mat):
10 """
11 Convertit une matrice `numpy <http://www.numpy.org/>`_ en liste.
13 @param mat matrix
14 @return liste de listes
16 .. exref::
17 :title: opérations avec numpy.matrix
18 :tag: Computer Science
20 Voici quelques écritures classiques avec le module
21 `numpy <http://www.numpy.org/>`_.
23 ::
25 import numpy as np
26 mat = np.matrix ( [[1,2],[3,4]] ) # crée une matrice 2*2
27 s = mat.shape # égale à (nombre de lignes, nombre de colonnes)
28 l = mat [0,:] # retourne la première ligne
29 c = mat [:,0] # retourne la première colonne
30 iv = mat.I # inverse la matrice
31 mat [:,0] = mat [:,1] # la première ligne est égale à la seconde
32 o = np.ones ( (10,10) ) # crée un matrice de 1 10x10
33 d = np.diag (mat) # extrait la diagonale d'une matrice
34 dd = np.matrix (d) # transforme d en matrice
35 t = mat.transpose () # obtient la transposée
36 e = mat [0,0] # obtient de première élément
37 k = mat * mat # produit matriciel
38 k = mat @ mat # produit matriciel à partir de Python 3.5
39 m = mat * 4 # multiplie la matrice par 4
40 mx = np.max (mat [0,:]) # obtient le maximum de la première ligne
41 s = np.sum (mat [0,:]) # somme de la première ligne
44 mat = np.diagflat(np.ones((1,4)))
45 print(mat) # matrice diagonale
46 t = mat == 0
47 print(t) # matrice de booléens
48 mat [ mat == 0 ] = 4
49 print(mat) # ...
50 print(iv) # ...
51 """
52 return mat.tolist()