importkandinskyimportion# Dimensions ecran NumWorks
W=320H=222# Plan complexe : x et y entre -2 et 2
X_MIN,X_MAX=-2.0,2.0Y_MIN,Y_MAX=-1.5,1.5defcomplex_to_pixel(z):"""Convertit un nombre complexe en coordonnees pixel."""px=int((z.real-X_MIN)/(X_MAX-X_MIN)*(W-1))py=int((Y_MAX-z.imag)/(Y_MAX-Y_MIN)*(H-1))returnpx,pydeffind_roots(coeffs):"""
Trouve les racines d'un polynome par la methode de Durand-Kerner.
coeffs[0] = coefficient de x^n, coeffs[-1] = terme constant.
"""n=len(coeffs)-1ifn==0:return[]ifn==1:ifcoeffs[0]!=0:return[complex(-coeffs[1]/coeffs[0])]return[]# Normalisation
a=[c/coeffs[0]forcincoeffs]# Initialisation des racines (cercle unite decale)
importcmathroots=[]forkinrange(n):angle=2*cmath.pi*k/n+0.1roots.append(0.4*cmath.exp(complex(0,1)*angle))# Iterations Durand-Kerner
for_inrange(60):new_roots=[]foriinrange(n):# Evaluation du polynome en roots[i]
p=complex(a[0])forjinrange(1,n+1):p=p*roots[i]+a[j]# Produit des differences
denom=complex(1)forjinrange(n):ifj!=i:denom*=(roots[i]-roots[j])ifabs(denom)<1e-12:new_roots.append(roots[i])else:new_roots.append(roots[i]-p/denom)roots=new_rootsreturnrootsdefdraw_axes():"""Dessine les axes et le fond."""# Fond noir
kandinsky.fill_rect(0,0,W,H,(0,0,0))# Axe x (partie imaginaire = 0)
py=int((Y_MAX)/(Y_MAX-Y_MIN)*(H-1))kandinsky.fill_rect(0,py,W,1,(60,60,60))# Axe y (partie reelle = 0)
px=int((-X_MIN)/(X_MAX-X_MIN)*(W-1))kandinsky.fill_rect(px,0,1,H,(60,60,60))# Cercle unite (approx avec points)
importcmathforiinrange(200):angle=2*cmath.pi*i/200z=cmath.exp(complex(0,1)*angle)px2,py2=complex_to_pixel(z)if0<=px2<Wand0<=py2<H:kandinsky.set_pixel(px2,py2,(40,40,80))# Palette de couleurs par degre
COLORS=[(255,100,100),# degre 1 : rouge
(255,180,50),# degre 2 : orange
(255,255,50),# degre 3 : jaune
(80,255,80),# degre 4 : vert
(50,200,255),# degre 5 : cyan
(150,100,255),# degre 6 : violet
(255,100,200),# degre 7 : rose
(200,255,150),# degre 8 : vert clair
]defrun():draw_axes()MAX_DEGREE=10fordeginrange(1,MAX_DEGREE+1):color=COLORS[(deg-1)%len(COLORS)]n_coeffs=deg+1# Enumere tous les polynomes de Littlewood de ce degre
total=1<<n_coeffs# 2^(deg+1)
formaskinrange(total):# Construit les coefficients : +1 ou -1
coeffs=[]forbitinrange(n_coeffs-1,-1,-1):if (mask>>bit)&1:coeffs.append(1)else:coeffs.append(-1)# Calcule les racines
roots=find_roots(coeffs)# Trace les racines
forzinroots:px,py=complex_to_pixel(z)if0<=px<Wand0<=py<H:kandinsky.set_pixel(px,py,color)run()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.