trouvé sur : http://www.robotran.be/PyRobotran/Doc/intro_py.html
import numpy as np
tab=np.ones((2,3),dtype='i') #create an integer matrix of 2 lines and 3 rows of 1
print tab
tab=np.ones((2,3)) #create a float matrix of 2 lines and 3 rows of 1
print tab
tab=np.zeros((2,3))
print tab
tab=np.empty((2,3))
print tab
tab=np.identity(3) #create a identity matrix 3*3
print tab
tab=np.linspace(0,4,4) #create an array of 4 elements spread over the interval 0 and 4
print tab
tab=np.arange(10.0) #create an array of 10 elements begining to zero and distant from each other of 1 unit
print tab
tab=np.array([[1,2],[3,4]]) #create an array on basis of the array given in argument
print tab
tab=np.zeros(5) # only one line of zero
print tab
tab=np.array([1,2,3]) # one line
print tab
# get an element in a array which have been set as one line
tab=np.array([[1,2,3],[4,5,6]])
print tab
elem=tab[1][2] # getting the element of line 2 and row 3 (the index begin at zero!)
print elem
elem=tab[1,2]
print elem
# getting an element in an array set as one line
elem=tab[1]
print elem
elem=tab[1:3] # take the element from 2 (include) to 4 (not include)
print elem
# size
print tab.shape #return a tuple: (number of lines, number of rows)
print tab.size # number of elements in the array
tab=np.empty((2,3))
c=np.ones((2,3))
b=tab+3 # add 3 to each element
b=tab*3 # mutliply each element by 3
b=c+tab # make the addition term by term of the 2 matrix
b=tab/c # division term by term
b=np.zeros((4,4))
b[0:3,0:3] # take a part of the array: from the first line (included) to the third one (not included)
b=np.sin(tab) # sinus element by element
b=np.cos(tab)
b=np.exp(tab)
b=np.log(tab)
b=np.log10(tab)
tab.mean()
tab.min() # global minimum of the matrix
tab.min(0) # minimum of each row
tab.min(1) # minimum of each line
tab.max()
tab.sum()
tab.sort() # sort by increasing order
np.sort(tab) # same but don't change the object
a=np.array([[1,2],[3,4]])
b=np.array([[5,3],[1,4]])
a*b # element by element multiplication
np.dot(a,b) # matrix multiplication
a.transpose() # return the transpose of a. Don't change a
a.T # same
a.flat[:] # return the vector (only one line) with the element of the a matrix
a.reshape((1,4)) # reshape the matrix. The number of element can't change
b=np.linalg.inv(a) #return the inverse
from matplotlib import pyplot as plt
plt.close('all') # ferme toutes les figures
# Common plot
x=np.linspace(0,2*np.pi,100)
y=x
z=np.sin(x)
plt.figure(1) # new figure
plt.plot(x,y,'g') # set x=y to the buffer
plt.plot(x,z,'rx-') # add z=sin(x) to the buffer
plt.xlabel('Axe des X')
plt.ylabel('Axe des Y')
plt.title('Figure de test')
plt.legend(('y=x','y=sin(x)'),'best')
plt.show() # we print everything!
# multiplot
plt.figure(2) #new figure
plt.subplot(121) # same as matlab
plt.plot(x,y,'r') # set to the buffer
plt.subplot(122)
plt.plot(x,y*y,'g')
plt.show()
# Multiplot with Legend
plt.figure(2)
sub1=plt.subplot(121)
plt.plot(x,y,'r')
sub1.set_title('Graphe1')
sub2=plt.subplot(122)
plt.plot(x,y*y,'g')
sub2.set_title('Graphe2')
plt.show()
# Logarithm plot
plt.figure(3)
plt.semilogx(x,y) # X log scale
plt.figure(4)
plt.semilogy(x,y) # Y log scale
plt.figure(5)
plt.loglog(x,y) # X and Y log scale
plt.show()
import scipy.linalg as spl
import scipy.sparse.linalg as spp
b=np.array([1,2])
A=np.array([[1,2],[3,4]])
# resolution by inversion
Ainv=np.linalg.inv(A)
x=np.dot(Ainv,b)
# LU factorisation: long way
lu,piv=spl.lu_factor(A)
x=spl.lu_solve((lu,piv),b)
# LU factorisation: short way
x=np.linalg.solve(A,b)
# QR decomposition
x0=np.array([0,0])
(x,info)=spp.bicg(A,b, x0,tol=1.0e-5,maxiter=500)
from scipy.integrate import ode
import numpy as np
from matplotlib import pyplot as plt
class Time:
def __init__(self):
self.tsim=0
self.y=[]
# example: masse-spring system: m xdd + c xd + kx = 0 et y =[x xd]
def dfdt(t,y,time):
c=1
m=5
k=100
yd=np.array(y)
yd[0]=y[1]
yd[1]=-c/m*y[1] - k/m*y[0]
time.tsim=t
time.y=y
return yd
t0=0
y0=np.array([1,0])
t1=10.0
backend='vode'
time=Time()
solver = ode(dfdt).set_integrator(backend)
solver.set_initial_value(y0, t0).set_f_params(time)
sol = []
while solver.successful() and solver.t < t1:
solver.integrate(t1, step=True)
sol.append([solver.t, solver.y[0],solver.y[1]])
sol = np.array(sol)
plt.clf()
plt.plot(sol[:,0], sol[:,1], 'b.-')
plt.show()