import numpy as np # import la librairie numpy (calcul scientifique)
import matplotlib.pyplot as plt # librairie pour afficher les graphs.
%matplotlib inline
def ps_mat(x,y):
return np.dot(x,y) # ou np.dot(x,y.T)
# ou np.sum(x*y)
def ps_iter(x,y):
s = 0
for i in np.arange(len(x)):
s += x[i] * y[i]
return s
x = np.arange(10)
y = np.arange(10)
ps_mat(x,y)
ps_iter(x,y)
%timeit ps_mat(np.arange(123456),np.arange(123456))
%timeit ps_iter(np.arange(123456),np.arange(123456))
def MonteCarlo(N=1000, scatter = True):
x = np.random.rand(N)
y = np.random.rand(N)
z = x**2 + y**2
n1 = (z<1).tolist().count(True)
if scatter:
plt.figure()
plt.scatter(x[z<1], y[z<1], c='red')
plt.scatter(x[z>1], y[z>1], c='blue')
return n1/N
MonteCarlo(1000)
list_N = np.linspace(50,100000,1000)
p = []
for N in list_N:
p.append(MonteCarlo(N, scatter = False)*4)
plt.plot(list_N,p)