Page MenuHomec4science

ch3.py
No OneTemporary

File Metadata

Created
Wed, May 8, 01:54
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 16:42:29 2019
@author: jecker, tenderini, ronssin
"""
from __future__ import division
import sys
sys.path.insert(0, './../')
import numpy as np
from IPython.display import display, Latex,HTML,Math
from ipywidgets import interact_manual, Layout
from ipywidgets import interactive, HBox, VBox, widgets, interact, FloatSlider
import plotly
import plotly.graph_objs as go
sys.path.append('../Librairie')
import AL_Fct as al
plotly.offline.init_notebook_mode(connected=True)
from IPython.core.magic import register_cell_magic
import ipywidgets as widgets
import random
from plotly.subplots import make_subplots
import sympy as sp
from sympy.printing.latex import LatexPrinter
sp.init_printing()
import scipy.optimize as opt
from itertools import zip_longest
def concepts_cle(v,u,lam):
#Initial settings, change to np.array
o=[[0],[0]]
u=np.array(u)
v=np.array(v)
data = []
t = np.linspace(-5, 5, 51)
s = np.linspace(0, 1, 10)
peak = go.Scatter(x=u[0], y=u[1], marker=dict(symbol=6, size=12, color='teal'), showlegend=False)
peak2 = go.Scatter(x=v[0], y=v[1], marker=dict(symbol=6, size=12, color='slateblue'), showlegend=False)
vector = go.Scatter(x=o[0] + s * u[0], y=o[1] + s * u[1], mode='lines',
line=dict(width=3, color='teal'), name='Vecteur u')
vector2 = go.Scatter(x=o[0] + s * v[0], y=o[1] + s * v[1], mode='lines',
line=dict(width=3, color='slateblue'), name='Vecteur v')
zero = go.Scatter(x=t*0, y=t*0, name='Origine', marker=dict(symbol=6, size=12, color='black'),
showlegend=False)
data.append(vector)
data.append(vector2)
data.append(zero)
data.append(peak)
data.append(peak2)
#règle du parallelogramme
trace1= go.Scatter(x=u[0] + s * v[0], y=u[1] + s * v[1], mode='lines',
line=dict(width=1, color='slateblue', dash='dash'), name='"v"')
trace2= go.Scatter(x=v[0] + s * u[0], y=v[1] + s * u[1], mode='lines',
line=dict(width=1, color='teal', dash='dash'), name='"u"')
w=u+v
sumUV=go.Scatter(x=o[0] + s *w[0], y=o[1] + s * w[1], mode='lines',
line=dict(width=1, color='orangered', dash='dash'), name='u+v',hoverinfo='none')
peakUV = go.Scatter(x=w[0], y=w[1], marker=dict(symbol=6, size=12, color='orangered'), showlegend=False)
data.append(trace1)
data.append(trace2)
data.append(sumUV)
data.append(peakUV)
fig = go.FigureWidget(data=data)
fig.update_layout(
title="Addition de deux vecteurs",
autosize=False,
width=700,
height=700,
font=dict(
size=10,
color="RebeccaPurple"
),
legend=dict(font=dict(size=12)),
scene=dict(aspectmode="data")
)
plotly.offline.iplot(fig)
#multiplication par lambda
data=[]
peak = go.Scatter(x=u[0], y=u[1], marker=dict(symbol=6, size=12, color='teal'), opacity=1,showlegend=False)
data.append(peak)
vector = go.Scatter(x=o[0] + s * u[0], y=o[1] + s * u[1], mode='lines',
line=dict(width=3, color='teal'), opacity=1,name='Vecteur $u$')
data.append(vector)
peak2 = go.Scatter(x=lam*u[0], y=lam*u[1], marker=dict(symbol=6, size=12, color='orangered'),opacity=0.5, showlegend=False)
data.append(peak2)
vector2 = go.Scatter(x=o[0] + lam*s * u[0], y=o[1] + lam*s * u[1], mode='lines',
line=dict(width=3, color='orangered'), opacity=0.5,name='Vecteur $\lambda u$')
data.append(vector2)
fig = go.FigureWidget(data=data)
fig.update_layout(
title="Multiplication par un scalaire",
autosize=False,
width=700,
height=700,
font=dict(
size=10,
color="RebeccaPurple"
),
legend=dict(font=dict(size=12)),
scene=dict(aspectmode="data")
)
plotly.offline.iplot(fig)
return
def Ex1Chapitre3_1(xL,xR,coeff):
step=0.25
l=len(coeff)
x=sp.Symbol('x')
poly= 0
poly=sp.Add(*[coeff[i]*x**i for i in range(l)], evaluate=False)
#if all zero of if only one coeff!=0, then s_print_Add is strange
count=len([i for i in coeff if i!=0])
print('Le polynôme entré est')
if str(count)=='0' or str(count)=='1':
display(Math(sp.latex(poly)))
else:
s=LatexPrinter(dict(order='none'))
display(Math(s._print_Add(poly)))
polyt=sp.lambdify(x, poly)
#add here enter the bounds
t = np.arange(xL,xR,step)
fig = go.Figure(data=go.Scatter(x=t, y=polyt(t)))
fig.update_layout(
autosize=False,
width=700,
height=700,
font=dict(
size=10,
color="RebeccaPurple"
),
legend=dict(font=dict(size=12)),
scene=dict(aspectmode="data")
)
fig.show()
return poly,step
def preprocess(polyt,xmin,xmax,step):
first_sign = polyt(xmin) > 0 # True if f(xmin) > 0, otherwise False
x = xmin + step
while x <= xmax: # This loop detects when the function changes its sign
fstep = polyt(x)
if first_sign and fstep < 0:
return x
elif not(first_sign) and fstep > 0:
return x
x += step
return x
def finding_roots(poly,xL,xR,step):
x=sp.Symbol('x')
polyt=sp.lambdify(x, poly)
xmid = preprocess(polyt,xL,xR,step)
z=[]
if xmid==xR or xmid>xR:
print("Le polynôme n'a pas de zéro entre ", xL, " et ", xR)
else:
z.append(opt.brentq(polyt,xL,xmid)) #first root
while xmid <xR:
#there is antother root
x=preprocess(polyt,xmid,xR,step)
if x>xR:
break
z.append(opt.brentq(polyt,xmid,x)) #poly(xR) and poly(xL)must have different signs
xmid=x
racine=['%g'%(x) for x in z]
return racine
def plot_poly(xL,xR,listOfCoeff):
step=0.25
x=sp.Symbol('x')
data=[]
for i in range(len(listOfCoeff)):
coeff=listOfCoeff[i]
l=len(coeff)
poly= 0
poly=sp.Add(*[coeff[i]*x**i for i in range(l)], evaluate=False)
#if all zero of if only one coeff!=0, then s_print_Add is strange
polyt=sp.lambdify(x, poly)
#add here enter the bounds
t =np.arange(xL,xR,step)
if type(polyt(t))==int:
polyt=[polyt(t) ]*len(t)
data.append(go.Scatter(x=t, y=polyt,name=sp.latex(poly)))
else:
data.append(go.Scatter(x=t, y=polyt(t),name=sp.latex(poly)))
i+=1
fig = go.Figure(data)
fig.update_layout(
autosize=False,
width=700,
height=700,
font=dict(
size=10,
color="RebeccaPurple"
),
legend=dict(font=dict(size=12)),
scene=dict(aspectmode="data")
)
fig.show()
def Expl1Chapitre3_1(xL,xR,coeff_p, coeff_q):
res = widgets.SelectMultiple(
options=['polynôme nul', 'somme des deux polynômes p+q', 'multiple de p', r"multiple de q"],
description='Choisissez ce que vous voulez afficher',
layout=Layout(width='35%', height='70px'),
disabled=False,
)
lamb=widgets.IntText(
value=1,
step=1,
description=r'Multiple de p par \(\lambda=\)',
disabled=False
)
mu=widgets.IntText(
value=1,
step=1,
description=r'Multiple de p par \(\mu=\)',
disabled=False
)
def coefficient(res,lamb,mu):
c=[coeff_p,coeff_q]
if 'polynôme nul'in res:
coeff_0=[0]
c.append(coeff_0)
if 'somme des deux polynômes p+q'in res:
coeff_somme=[x+y for x,y in zip_longest(coeff_p, coeff_q, fillvalue=0)]
c.append(coeff_somme)
if 'multiple de p' in res:
coeff_lp=[x*lamb for x in coeff_p]
c.append(coeff_lp)
if "multiple de q" in res:
coeff_mq=[x*mu for x in coeff_q]
c.append(coeff_mq)
plot_poly(xL,xR,c)
return
interact_manual(coefficient,res=res, lamb=lamb,mu=mu)
return

Event Timeline