import numpy as np
import scipy.optimize as resol
import scipy.integrate as integr
import matplotlib.pyplot as plt



x = np.linspace(-3,3,1000)
def f(t):
    return t**3 -t*3 -1

plt.figure()
plt.plot(x,f(x))
plt.show()

def dichotomie(f,a,b): ##ne trouve qu'une valeur

    while b-a > 0.00001:
        m = (a+b)/2
        if f(a)*f(m)<=0:
            b=m
        else :
            a=m
    return (a,b)

''''>>> resol.fsolve(f, -2.)
array([-1.53208889])

>>> resol.fsolve(f, 0)
array([-0.34729636])

>>> resol.fsolve(f, 2.5)
array([1.87938524])'''

##
def f(v) :## v = (x,y)
    return v[0]**2 + v[1]**2 - 5, v[0]**4 + v[1]**4 - 20

''''
>>> sol = resol.root(f, [5,8])

>>> sol.success
True

>>> sol.x
array([-2.1062981 ,  0.75067192])

>>> sol = resol.root(f, [18,20])

>>> sol.success
True

>>> sol.x
array([-0.75067192,  2.1062981 ])


'''

##
import numpy as np
import scipy.optimize as resol
import scipy.integrate as integr
import matplotlib.pyplot as plt


def f2(x):
    def g(t):
        return (x**2 +t**2)**0.5
    return integr.quad(g,0,1)[0]  ##[1] renvoie la précision

'''>>> f2(2)
(2.0804576388691016, 2.3097719724297655e-14)
'''
l= np.linspace(-2,2,10000)
plt.figure()
for i in range(1,6):
    def fp(x):
        def g(t):
            return (x**2 +t**i)**0.5
        return integr.quad(g,0,1)[0]
    y = []
    #for j in range(len(l)): ## ça marchait pas de faire de f2(l)
        #y.append(fp(l[j]))
    plt.plot(l,[fp(l[j]) for j in range(len(l))]) ## en compréhension
plt.show()


##
import numpy as np
import scipy.optimize as resol
import scipy.integrate as integr
import matplotlib.pyplot as plt


def f(y,t):  ##mettre l'équation sous forme normalisée sinon on peut pas.

    return 1-np.exp(t)*y ## traduit y' = 1-e^t*y



T = np.arange(0, 1.01, 0.01)
X = integr.odeint(f, 1, T)  ## le 1 signigie y(0) = 1 (condition initiale)
plt.plot(T,X)
plt.show()


##
import numpy as np
import scipy.optimize as resol
import scipy.integrate as integr
import matplotlib.pyplot as plt

def f(y,x) :
    return np.array([y[1],y[0]*(1/(1-x))])  ## traduit y''(x) = 1/(1-x) *y(x)

T1 = np.arange(0,0.95, 0.01)# on sépare en 2 listes car integr.odeint prend la première valeur de T1 comme conditions initiales et on veut que ce soit 0 ( y(0) et y'(0) )
T2 = np.arange(0,-1,-0.01)
X1 = integr.odeint(f, np.array([0,1]), T1)
X2 = integr.odeint(f, np.array([0,1]), T2)                                                            #	 cond. initiales : np.array([0,1]) donnent y(0) = 0 et y'(0) = 1





a = [0 for _ in range(101)]
a[1] = 1
for n in range (2,len(a)):
    a[n] = ((n-2)/n)*a[n-1] + 1/(n*(n-1)) * a[n-2]

plt.plot(range(101),a)
plt.show()

T = np.arange(0,0.95,0.01)
def g(x):
    S = 0
    for i in range(11): ## de n = 0 à 10 pour réduire la précision et voir les deux courbes quand on affiche
        S+= a[i]*x**i
    return S
plt.plot(T1, X1[ :,0]) ## sélectionne la première colonne
#plt.plot(T2, X2[ :,0]) ## sélectionne la première colonne

plt.plot(T,g(T))
plt.show()
 ##
import math
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

# surface z = f(x,y)

fig = plt.figure()
ax = fig.add_subplot(111,projection= '3d')
def f(x,y) :
    return -2*x**2+3*x*y +2*y**2 #provient du calcul de X^tMX
f=np.vectorize(f)
X = np.arange(-2, 2, 0.01)
Y = np.arange(-2, 2, 0.01)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
ax.plot_surface(X, Y, Z)
plt.show()

#arc paramétré
def x(t) :
    return np.sin(t)
def y(t) :
    return np.sin(2*t)
T = np.arange(0, 2*np.pi, 0.01)
X = x(T)
Y = y(T)
plt.axis('equal')
plt.plot(X, Y)
plt.show()


def f(x,y) :
    return x**2 + y**4
f=np.vectorize(f)
X = np.arange(-5, 5, 0.1)
Y = np.arange(-5, 5, 0.1)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
plt.axis('equal')
plt.contour(X, Y, Z, [1,4,10])
plt.show()




##
import numpy.random as rd

N = rd.poisson(4,1000)
p = 0.2
S = [0 for _ in range(len(N))]
for i in range(len(N)):
    S[i]= np.sum(rd.geometric(p,N[i]))
assert len(S) != 0
ES = np.sum(S)/len(S)


##
import numpy as np
import numpy.linalg as alg

def An(n) :
    M = np.zeros((n,n)) ## un seul argument
    for i in range(n):
        M[i,i] = 3
        if i >= 1 :
            M[i-1,i]= -1
        if i <= n-2 : # la numérotation des indices commence à 0
            M[i+1,i] = -1
    return M


b = np.array([2,5,8])
S = alg.solve(An(3),b)

inv_A = alg.inv(An(3))
S1 = np.dot(inv_A,b)
 #inv_A*b ne correspond pas au produit matriciel, il faut utiliser np.dot(.,.)

D = []
for n in range(10):
    D.append(alg.det(An(n)))

VP = alg.eigvals(An(4))
VP2 = alg.eig(An(4))
# les vecteurs sont en colonnes de la matrice VP2[1] et pour sélectionner le premier vecteur propre : VP2[1][:,0]

































