xxxxxxxxxx
x,y,k=var('x,y,k')
m,n=144,3; seg=[-.5,-.48,..,.5]
xy=arg(sum(((x+I*y)^n+10^(-3)*(x+I*y))^factorial(k),k,1,3))
f(x,y)=cos(m*xy)*sin(xy)
fl=[f(x,y) for x in seg for y in seg]
list_plot(fl,size=3,axes=False,figsize=(6,6))
xxxxxxxxxx
x,y,k=var('x,y,k')
m,n=144,3; seg=[-.5,-.48,..,.5]
xy=arg(sum(((x+I*y)^n+10^(-3)*(x+I*y))^factorial(k),k,1,3))
f(x,y)=cos(m*xy)*sin(xy)
fl=[f(x,y) for x in seg for y in seg]
list_plot(fl,size=3,axes=False,figsize=(6,6))
xxxxxxxxxx
z,k=var('z,k'); m,n=144,16
zn=arg(sum((z^n+10^(-3)*z)^factorial(k),k,1,4))
complex_plot(cos(m*zn)*sin(zn),(-1,1),(-1,1),
axes=False,plot_points=200,figsize=(6,6))
xxxxxxxxxx
def gen(max=Infinity):
n=1; xyn=[.1,.1]
while n<=max:
yield xyn; n+=1
xn,yn=xyn[0],xyn[1]
xyn=[.7*xn+yn,-.8+xn^2]
N=3000; XY=gen(N); XYN=[el for el in XY]
cols=[colormaps.autumn(1.*i/N)[:3]
for i in [1..N]]
p=polygon([[-1.5,-1],[-1.5,1.5],[1,1.5],
[1,-1],[-1.5,-1]],
color='silver',alpha=.05)
p+sum([point([XYN[i]],size=1,color=cols[i])
for i in [1..N-1]])
xxxxxxxxxx
def gen(max=Infinity):
n=1; xyn=[.1,.01]
while n<=max:
yield xyn; n+=1; xn,yn=xyn[0],xyn[1]
xyn=[xn+.684*(xn-xn^2+yn),
yn+.684*(yn-yn^2+xn)]
N=3000; XY=gen(N); XYN=[el for el in XY]
cools=[colormaps.cool(1.*i/N)[:3]
for i in [1..N]]
p=polygon([[-1,-1],[-1,3],[3,3],[3,-1],[-1,-1]],
color='steelblue',alpha=.1)
p+sum([point([XYN[i]],size=1,color=cools[i])
for i in [1..N-1]])
xxxxxxxxxx
var('t,i'); n=3
tmin,tmax=[0,0,0],[8.95*pi,12.18*pi,11.77*pi]
f=[sin(1.9*t),1.5*sin(2.3*t),2*sin(1.7*t)]
sum([polar_plot(random()*f[i-1],(tmin[i-1],tmax[i-1]),
color=(0,.07*(i+j),.15*(i+j)),thickness=.5)
for i in [1..n] for j in [1..n]]).show(axes=False)
xxxxxxxxxx
var('t,i'); tmin,tmax,n=-pi,pi,12
f1(t,i)=(randint(5,10)+.9*cos(randint(11,20)*t+pi*i/n))
f2(t,i)=(1+.1*cos(randint(25,100)*t+pi*i/n))
f3(t,i)=(1+.01*cos(randint(200,300)*t+pi*i/n))
f(t,i)=f1(t,i)*f2(t,i)*f3(t,i)*(1+sin(t+pi*i/n))
sum([polar_plot(f(t,i),(tmin,tmax),
thickness=.5,color=hue(i/n))
for i in [1..2*n]]).show(axes=False,figsize=6)
xxxxxxxxxx
var('t,i,a,b,c,d'); tmin,tmax,n=-pi,pi,4
f1(t,i,a,b,c,d)=(a+.9*cos(b*t+pi*i/n))*(1+.1*cos(c*t+pi*i/n))
f2(t,i,a,b,c,d)=(1+.05*cos(d*t+pi*i/n))*(1+sin(t+pi*i/n))
[a,b,c,d]=[randint(8,11),randint(12,24),
randint(25,81),randint(216,256)]
sum([polar_plot(f1(t,i,a,b,c,d)*f2(t,i,a,b,c,d),(tmin,tmax),
thickness=.3,axes=False,color=hue(i/(3*n)))
for i in [1..6*n]])
xxxxxxxxxx
print(r.eval("""
bin2dec<-function(n) {
sapply(strsplit(as.character(n),split=''),
function(x)
sum(as.numeric(x)*2**(rev(seq_along(x)-1))))}
bin2dec(c('11100110010','10001','1010101111','1001001','10'))"""))
xxxxxxxxxx
var('u,v'); a,b,c=8,4,3
u_min,u_max,v_min,v_max=-.4,.4,-2*pi,2*pi
fx=a*cos(v)*sinh(u)-b*cos(c*v)*sinh(c*u)
fy=a*sin(v)*sinh(u)+b*sin(c*v)*sinh(c*u)
fz=cos(a*v)*cosh(a*u)
parametric_plot3d(
[fx,fy,fz],(u_min,u_max),(v_min,v_max),plotpoints=400,
color='crimson',opacity=.5,mesh=True,frame=False)
xxxxxxxxxx
var('u,v'); a,b,c=4,5,1
u_min,u_max,v_min,v_max=0,2*pi,0,2*pi
fx,fy,fz=u*cos(a*v),u*sin(b*v),-v*cos(c*u)
sum([parametric_plot3d(
[fx*i,fy*j,fz],(u_min,u_max),(v_min,v_max),plotpoints=300,
color='silver',opacity=.3,mesh=True,frame=False)
for i in [-1,1] for j in [-1,1]])
xxxxxxxxxx
html("""
<div id='img' title='An Image Example'
style='border:15px double white; width:250px; height:200px; \
overflow:auto; padding:5px; background-color:#ff66cc'>
<img src='https://raw.githubusercontent.com/"""+\
"""OlgaBelitskaya/data/main/flowers/00_001.png'
width='98%' height='95%'/>
</div>""")
xxxxxxxxxx
from sklearn.feature_extraction.text import CountVectorizer
corpus=['Have you already set your goals for the New Year?',
'Do you want to lose ten kilos, '+\
'run a marathon or speak fluent English?',
'Some experts believe that you need systems, not goals.',
'A system is something you do on a regular basis. ',
'This means focusing on what you can control '+\
'(your actions) rather than what you can’t.',
'For example, do not focus on losing ten kilos.',
'Focus on shopping for healthy food and '+\
'cooking something light every day.',
'Do not focus on the marathon.',
'Focus on the training schedule.',
'Invent a system to improve your English, '+\
'one step at a time.',
'Good luck!']
c_vectorizer=CountVectorizer(min_df=1)
corpus_features=c_vectorizer.fit_transform(corpus)
corpus_array=corpus_features.toarray().astype('int16')
c_analyzer=c_vectorizer.build_analyzer()
import pylab as pl; pl.figure(figsize=(6,5))
pl.title('Word Occurrences in Sentences',fontsize=12)
for i in range(len(corpus_array)):
pl.scatter(range(len(corpus_array[i])),
(corpus_array[i]*.5+i),marker='*')
pl.tight_layout(); pl.grid(); pl.show()
xxxxxxxxxx
A,B=1+round(random(),2),round(.01+random(),2)
N=500; X=[.1*i*random() for i in range(N)]
Y=[A*x+B+10*random()*(-1)^randint(0,1) for x in X]
var('a,b,x'); F=a*x+b
ff=find_fit(list(zip(X,Y)),F,variables=[x])
a,b=float(str(ff[0])[5:]),float(str(ff[1])[5:])
F=[a*x+b for x in X]
pf=points(zip(X,F),color='#ff36ff',size=5,
legend_label='fitted line')
pr=points(zip(X,Y),color='#3636ff',marker='*',
size=9,legend_label='real data')
(pf+pr).show(figsize=(6,5),gridlines=True,
title=['[A == %s,B == %s]'%(A,B),ff])
xxxxxxxxxx
@interact
def _(N=(16,4,-1)):
p1=sum([polar_plot(log(floor(x)),(x,k*pi/2,(k+1)*pi/2),
color=colormaps.winter(N*k)[:3],thickness=1,
exclude=[1..int((k+1)*pi/2)]) for k in [1..N]])
p2=sum([polar_plot(log(floor(x)),(x,k*pi/2,(k+1)*pi/2),
color=colormaps.spring(N*k)[:3],thickness=1,
exclude=[1..int(k*pi/2)]) for k in [1..N]])
p1.show(figsize=(4,4)); p2.show(figsize=(4,4))
xxxxxxxxxx
@interact
def _(N=(9,1,-1)):
s1=r'<center><font color=%s size=5>'%'#3636ff'
s2=r'Derivatives till the %sth Order</font></center>'
var('x'); pretty_print(html(s1+s2%str(N)))
f,g=exp(x)*x,1/(x+1); F,G=[f],[g]
for i in range(N):
f=f.diff().factor(); g=g.diff().factor()
F.append(f); G.append(g)
p1=sum([plot(F[i],(-2,1),color=colormaps.jet(30*i)[:3],
legend_label=str(i)) for i in [0..N]])
p2=sum([plot(G[i],(0,3),color=colormaps.jet(30*i)[:3],
legend_label=str(i)) for i in [0..N]])
p1.show(title=r'$f=x \cdot e^x$',fontsize=12,
figsize=(5,5),gridlines=True)
p2.show(title=r'$f=\frac{1}{x+1}$',fontsize=12,
figsize=(5,5),gridlines=True,ymin=-20,ymax=20)
xxxxxxxxxx
@interact
def _(N=(11,0,-1)):
var('t');
p=sum([polar_plot((.5*j+1)*sin(6*t)^2,(pi*i/6,pi*(i+1)/6),
color=colormaps.jet(20*i)[:3])
for i in [0..N] for j in range(3)])
show(p,figsize=6,gridlines=True,
ymin=-2,ymax=2,xmin=-2,xmax=2)
xxxxxxxxxx
%%html
<text style='color:#3636ff;'>
$\mathscr{
\begin{bmatrix}
\begin{array}{c}
G = \alpha * A * B^T \ + \ \beta * C^T * D \\
\hline &\;&\;&\;&\;&\;&\;&\;
\end{array} \\
\begin{array}{c}
\alpha = 2, \ \beta = -5, \\
A_{(2,3)} = \begin{pmatrix}
-3 & -3 & -6 \\ -4 & -7 & 8 \\
\end{pmatrix}, \
B_{(4,3)} = \begin{pmatrix}
2 & 8 & 7 \\ -5 & -6 & -2 \\
-8 & 4 & -8 \\ 7 & 4 & -6 \\
\end{pmatrix}, \\
C_{(2,2)} = \begin{pmatrix}
-2 & -4 \\ 3 & -5 \\
\end{pmatrix}, \
D_{(2,4)} = \begin{pmatrix}
-2 & -3 & -1 & 3 \\ -2 & -9 & 8 & 9 \\
\end{pmatrix}. \\
\end{array} \\
\begin{array}{c}
\hline &\;&\;&\;&\;&\;&\;&\;&\;&\;
&\;&\;&\;&\;&\;&\;&\; \\
G = ?
\end{array}
\end{bmatrix}}$
</text>
xxxxxxxxxx
%matplotlib inline
import pylab as pl,numpy as np
columns=['Fresh','Milk','Grocery','Frozen',
'Detergents_Paper','Delicatessen']
rows=['Customer 1','Customer 2','Customer 3']
colors=['#3636ff','#36ff36','#ff3636']
data=[[26373,36423,22019,5154,4337,16523],
[16165,4230,7595,201,4003,57],
[14276,803,3045,485,100,518]]
pl.figure(figsize=(7,4))
pl.ylabel('value'); pl.xticks([])
x=np.arange(len(columns))+.2
y=np.array([0.]*len(columns))
bar_width=.5; cell_text=[]
for i in range(len(rows)):
pl.bar(x,data[i],bar_width,bottom=y,color=colors[i])
y=y+data[i]
cell_text.append(['%1.0f'%(d)for d in data[i]])
pl.table(cellText=cell_text,
rowLabels=rows,rowColours=colors,
colLabels=columns,loc='bottom')
pl.title('Samples of the Wholesale Customers Dataset')
pl.subplots_adjust(left=.1,bottom=.05);
xxxxxxxxxx
var('x'); t=15
f=cos(x); g=sqrt(x)*sin(x); h=cos(sqrt(x)*sin(x))
fun=[f,f.diff(),g,g.diff(),h,h.diff()]
c=[colormaps.hsv(35*i)[:3] for i in [0..5]]
l=[r'$f$',r'$f \prime$',r'$g$',
r'$g \prime$',r'$h$',r'$h \prime$']
p=plot(fun,(0,t),color=c,thickness=2,fill='axis',
fillcolor=c,fillalpha=0.1,legend_label=l)
p.show(figsize=(6,5),legend_fancybox=True,
legend_loc=3,legend_font_size=16,gridlines=True)
xxxxxxxxxx
f=lambda x,y: sin(.3+x^3*y^2)*exp(.4+x/pi)+cos(.5+x^2*y^3)
c=[colormaps['terrain'](.1^2*i)[:3] for i in range(100)]
plot3d(f,(-pi,pi),(-pi,pi),color=c,
adaptive=True,mesh=True,frame=False)
xxxxxxxxxx
@interact
def _(N=(8,1,-1)):
g=Graphics()
for i in [1..N]:
a1,a2,a3,a4,a6=i-5,i+5,i-1,i+1,i+3
c=colormaps.jet(30*i)[:3];
E=EllipticCurve([a1,a2,a3,a4,a6]);
g+=plot(E,color=c,plot_points=50,
legend_label='$%s$'%latex(E))
g.show(gridlines=True,ymin=-30,ymax=30,figsize=(5.5,5))
xxxxxxxxxx
%%html
<style>
.text1 {color:darkred; font-size:150%;
text-shadow:3px 3px 3px #aaa;}
.text2 {color:darkslategray; font-size:150%;
text-shadow:3px 3px 3px #bbb;}
.text3 {color:steelblue; font-size:150%;
text-shadow:3px 3px 3px #ccc;}
</style>
<p class='text1'>
📕 $\mathfrak {Choose \ your \ style!}$</p>
<p class='text2'>
📓 $\mathbb {Choose \ your \ style!}$</p>
<p class='text3'>
📘 $\mathscr {Choose \ your \ style!}$</p>
xxxxxxxxxx
%%html
<p>Calculation the number of symbols in strings:</p>
<p id='script_output'>evaluate the next code cell</p>
xxxxxxxxxx
%%javascript
function getInteger(min,max) {
return Math.floor(Math.random()*(max-min+1))+min;};
var string='@@@***морозИсолнцеДЕНЬчудестный***@@@'+getInteger(1,19);
document.getElementById('script_output')
.innerHTML=string+' - '+string.length+' symbols';
xxxxxxxxxx
@interact
def _(a=(2,4,1),b=(4,8,1),c=(4,8,1),n=(2,8,1),m=(2,8,1)):
var('t'); r=cos(a*t)^n+sin(b*t)^m+1/c; col='#3636ff'
string='<center><font color=%s>$r = %s$</font></center>'
pretty_print(html(string%(col,latex(r))))
p=parametric_plot((sin(t)*r,cos(t)*r),(0,2*pi),
color=col,fill=True,fillcolor=col)
p+=plot(r,(0,2*pi),linestyle='--',color=col,gridlines=True)
show(p,figsize=(7,7),
xmin=-2.2-1/c,xmax=6.4+1/c,ymin=-2-1/c,ymax=2+1/c)
xxxxxxxxxx
Md=Manifold(2,'Md')
U=Md.open_subset('U'); V=Md.open_subset('V')
XU.<x,y>=U.chart(); XV.<xp,yp>=V.chart('xp:x’ yp:y’')
Md.declare_union(U,V)
XU_to_XV=XU.transition_map(
XV,(2*x/(x^2+y^2),2*y/(x^2+y^2)),
intersection_name='W',
restrictions1=x^2+y^2!=0,
restrictions2=xp^2+yp^2!=0)
R3=Manifold(3,'R^3',r'\mathbb{R}^3')
XR3.<X,Y,Z>=R3.chart()
Delta1=Md.diff_map(
R3,{(XU,XR3):[2*x/(1+x^2+y^2),2*y/(1+x^2+y^2),
(x^2+y^2-1)/(1+x^2+y^2)],
(XV,XR3):[3*xp/(1+xp^2+yp^2),3*yp/(1+xp^2+yp^2),
(1-xp^2-yp^2)/(1+xp^2+yp^2)]},
name='Delta1',latex_name=r'\Delta_1')
Delta2=Md.diff_map(
R3,{(XU, XR3):[x/(1+x^2+y^2),y/(1+x^2+y^2),
(x^2+y^2-1)/(1+x^2+y^2)],
(XV, XR3):[4*xp/(1+xp^2+yp^2),4*yp/(1+xp^2+yp^2),
(1-xp^2-yp^2)/(1+xp^2+yp^2)]},
name='Delta2',latex_name=r'\Delta_2')
cols=['darkblue','#ff3636','darkred','#3636ff']
elements=[(XU,Delta1),(XV,Delta1),(XU,Delta2),(XV,Delta2)]
p=sum([elements[i][0].plot(
chart=XR3,mapping=elements[i][1],
number_values=50,color=cols[i],label_axes=False)
for i in range(4)])
p.show(frame=False)
xxxxxxxxxx
def tf1(i,k,t):
f1=(1/k[0]+sin(k[1]+k[2]*t))*(1/k[0]+cos(k[1]+k[2]*t))
f2=(1/k[0]-sin(k[1]+k[2]*t)^3)*(1/k[1]-cos(k[1]+k[2]*t)^3)
return [i*f1*f2*cos(t),i*f1*f2*sin(t)]
def tf2(i,k,t):
return [(i+cos(i*k[0]*t)+cos(k[0]*t))*cos(t)/k[1]/k[2],
(i+cos(i*k[0]*t)+cos(k[0]*t))*sin(t)/k[1]/k[2]]
var('t'); K=[randint(2,16) for i in range(3)]
p1=sum([parametric_plot(tf1(i,K,t),(t,0,2*pi),color=hue(i/6))
for i in range(1,5)])
p2=sum([parametric_plot(tf2(i,K,t),(t,0,2*pi),color=hue(i/6))
for i in range(1,5)])
(p1+p2).show(title=K,axes=False,figsize=6)
xxxxxxxxxx
@interact
def _(N=(24,1,-1)):
var('x'); f=sin(x)*exp(1)^(-x)
p=plot(f,-1,6,thickness=5,color='#3636ff',alpha=.5)+\
sum([plot(f.taylor(x,0,i),-1,6,hue=cos(i/24),thickness=1,
legend_label=str(i)) for i in [1..N]])
ti=r'$Taylor \ Series \ f=sin \ x \cdot e^{-x}$'
p.show(ymin=-.5,ymax=.5,figsize=(5.5,5),
gridlines=True,title=ti,fontsize=12)