from itertools import permutations
import numpy as np
import csv
import matplotlib as mpl
import matplotlib.pyplot as plt
var('x')
data=[.7,.2,.3,.4,.5,.6,.7,.8,.9,.10]
#data=[540.5,543.25,544.75,548.75,544.5,559.5,553.25,558,551,542.5,538,543.5,547.25,544.5,536,530.5,536.5,523,527.5,542,551,537.75,545.5,554,544,548.5,574,567.25,567.5,565.5,569,577.5,575,574,576.25,573.25,567.25,570.25,564,558,537.75,536.5,528.75,531.5,496.5,507,515,521,528.5,520.5,509,489.75,500,495.5,500,503,519.5,526,518,504.5,497,497,498,486,499,505,507,508.5,508.5,508.5,512.5,503.5,514,517,513,516.5,512.5,509.5,514,513,506.5,508,512,506.5,508.5,505,519,511.5,507,507,500.5,493,489.5,491,487,492,490,501.5,487,480,466.5,468.5,457.5,461,461,482.5,473,470,470,464.5,456,440.5,431.5,431,433.5,433,431,439.5,446,436,424.5,431.75,424,428.75,425.5,419.75,421.5,420,418.5,412.75,418.5,440.5,431,433.75,438.5,425.75,418.5,440.5,439.25,438.75,436.25,430.25,432.25,420,422,420.5,416.75,400.5,390.25,382.75,397.75,386.25,403.75,402.75,400,416.75,418,402.5,377.75,401,403,402.25,381.25,394.25,395.5,401.5,418,419.5,409.25,409.25,412.75,415.5,423.25,424.25,431.25,436.5,422.25,418,395.25,391.75,400,396.5,393,382.25,369.5,376,383.25,368.75,374.25,380,367,369,384.25,383.25,395,409,424.75,423.5,420.5,416.75,419.75,439,434.5,424,419.5,413.75,423.75,430,420.5,414.25,417.5,423.5,433.5,443.5,437.75,434.5,445.25,430,418.25,405,389,369.5,367.25,355.75,365.25,377.25,368.75,359.25,345.5,324.25,334.75,322.75,331.75,345.75,329.75,317.75,342.5,336,341.25,343.75,322.5,328.25,324.5,334.5,342,338,338.5,324.25,320.25,302.5,310,307.25,309.5,308.75,315,315,315,309.25,307.5,309.75,309.75,316.75,310.5,302.75,291.75,283.75,273.25,268,284.5,288.25,277.5,289.5,293.5,292.5,279.25,296,308.5,326.5,317.25,313,329,333,332,318,312,293,294.75,291.25,295.75,288.75,291.75,299,289.75,278.25,286,291.75,282,272,261.5,278.5,284.25,277.25,265.5,257.5,251.5,250.5,265,245,253.5,241.75,238,242.5,230.5,226.5,217.5,230.25,223,230,230,230,241.75,240.25,247.25,240,234.25,249.25,247.5,238.25,235,240,231.25,222.25,227.25,223.5,215.5,208.5,220.75,217.5,223.75,214.75,210,205,212.75,221.25,222.5,221,226.5,243,248.75,248.75,239,242,238.5,228.25,231,228.5,218,224,233,222.75,207.75,202.75,212.25,206.5,206.5,214.75,218.25,215,232.5,228.75,230.75,243.75,254.25,233.5,227.75,224.5,227.75,240,250.5,239.25,238,225.75,226,218.75,212.25,211,229,211.25,213,215.25,207.5,203.5,209,197,206,217.25,221.75,210.75,199.1,207.25,210.75,225.25,227.25,242,239,246.25,263.25,242,246.75,234.5,248.5,263,255.25,259,255,267.75,268,255,275.75,282.5,271,255.5,253.75,261,252.75,244.5,240.25,232.25,242,242,245.25,243,247.75,251,261.75,273.25,262.5,250,245,253,263.25,260,251,257.5,261.25,258.75,243.25,217,218.75,218.5,210.5,207.5,198.7,193.4,169.4,168.2,173.5,178.1,165.4,145,128.2,120.9,125,109.9,119.8,128.9,119.1,117.7,127.1,135.7,141.9,148,138.5,132,124.5,117.9,126.3,135.5,136.1,139.6,149.4,148.2,130.5,146,157,154,143.1,145.5,145.3,139,141.6,131.2,124.2,129.5,140,141.9,145.5,146.6,155.1,139.7,157.1,156.9,155.2,154.4,172.5,169.2,172.9,165,163,159.4,163,172,172.5,170.9,170.3,171.6,169.6,169.6,169.6,173.3,177.5,179.7,179.7,180.2,182.3,184.1,176.4,177.3,178.5,176.8,175.4,165.9,157.8,166.3,164.6,152,154,150.9,146.2,133.8,132.6,139.4,130.9,120.1,116.1,120.8,125.2,127.6,141,139.3,142.1,144.1,141.2,143.9,141,133.4,134.2,132.4,127.4,127.3,126.9,131.8,134.8,137.2,133,126.5,135.4,126,128.4,125.9,135,140,134.3,133.1,141.7,138.3,136.8,143,144.5,147.4,145.6,140.4,141,136.7,135.5,140.8,147,155.7,165.1,158.9,153.8,159.6,160.9,160.9,160.9,162.3,156.7,160.9,174,161.6,162.5,167.8,166.3,163.9,151.2,143.1,148.6,147.9,148,148,165.9,173.9,169.7,169.4,162.4,163.1,153.4,160,154.6,160,165.7,168.8,162.8,156.7,156.7,157,159.3,156.8,155.5,156.7,158.1,152.1,149.4,152.4,146.8,145.6,145.6,143,144.9,136.8,136.6,132,134.5,136.4,124.6,123.8,125.3,126.6,129.3,127.5,124.7,124,119,125.5,123.9,122.2,118,119.9,120,126.6,128.1,128.8,132.1,136,132.8,136,137.3,138.4,136.9,132.3,128.4,130.9,134.3,142.4,145.5,150.2,161.7,169.9,171,168.8,164,170.9,170.9,175.6,173.9,173.2,172,175.5,188.2,189.9,196.6,191.7,187.5,191.5,191.5,187.8,181.5,184,189.4,192.8,201,211,215.7,221.4,217,217,227.4,239.9,235.7,233.1,231.2,229.9,220,213.6,215.8,216.6,220.5,217.8,210.3,217,222.8,223,221.3,218.9,221.8,219.6,224.9,220.9,214.1,213.8,216,218.5,209.5,209.8,199.7,193.7,183.3,189.5,181.8,179.9,179.9,191.8,186.3,198.8,199.3,196.4,200,215,217,215.3,208.5,206.4,201,202.6,203.9,204,202.1,193.2,193.8,195.3,199,202.4,206.2,212,207.4,204.6,200.1,202.3,201.3,201,196.6,197,192.2,188.5,193,189.1,188.2,192,192,192,189,187.3,186.9]
def f(x):
ans=2*x^2
ans=ans-int(ans)
return ans
def rw(x):
ans=x+(random()-.5)
return ans
def permute_extract(n,v):
hist_out=[]
temp=0
d={}
for i in permutations(range(n)):
d[i]=temp
hist_out.append([i,0])
temp=temp+1
hist_list=[]
for i in range(len(v)-n):
t_list=[]
for j in range(n):
t_list.append(v[i+j])
per=tuple(np.argsort(t_list))
ind=d[per]
hist_out[ind][1]=hist_out[ind][1]+1
hist_list.append(hist_out)
print(hist_out)
return hist_out,hist_list
@interact
def _(i_type=selector(['Periodic Function','Data','Random Walk'],buttons=True,label="Input type"),dff=checkbox(default='True',label="Use Data for f?"),n=slider(1,20, step_size=1,default=3,label="Permutation Length:"),ipoints=input_box(default=10,label='Inital Points:'),length=input_box(default=100,label='Number of Iterations:'),dpoints=input_box(default=10,label="Number of Data Points:"),ua=checkbox(default=True,label="Display Animations?"),ani=input_box(default=20,label="Length of Animation:"),auto_update=False):
if i_type=='Periodic Function':
init_points=[]
for i in range(ipoints):
init_points.append(random())
if dff==True:
init_points=data
ipoints=len(data)
data_points=[]
data_points2=[]
for i in range(ipoints):
data_points.append([init_points[i]])
for j in range(length):
data_points[i].append(f(data_points[i][j]))
data_points2.append(f(data_points[i][j]))
p=plot(f,(x,0,1),thickness=3)
p.show()
plist=[]
for i in range(ipoints):
for j in range(length-1):
plist.append((data_points[i][j],data_points[i][j+1]))
xlist=[]
ylist=[]
pt=[]
for i in range(len(plist)):
xlist.append(plist[i][0])
ylist.append(plist[i][1])
pt.append(point(plist[i], rgbcolor=(1,0,0), pointsize=80))
a=min(xlist)-1
b=max(xlist)+1
pt[0]=point(plist[0], rgbcolor=(0,1,0), pointsize=80)
h=p+pt[0]
hlist=[]
for i in range(length): #used to be len(plist)
h=h+pt[i]
if 2*i<2*ani:
hlist.append(h)
h.show()
if ua==True:
hn=animate(hlist)
hn.show(delay=60)
full_hist,hist_list=permute_extract(n,data_points2)
hlh=[]
for i in range(len(full_hist)):
hlh.append(full_hist[i][1])
hlh=bar_chart(hlh)
hlh.show()
#hlhl=[]
#for i in range(len(hist_list)):
# hlhl.append([])
# for j in range(len(full_hist)):
# hlhl[i].append(hist_list[i][j][1])
# hlhl[i]=bar_chart(hlhl[i])
#hlhl=animate(hlhl)
#hlhl.show()
if i_type=='Data':
plist=[]
for i in range(dpoints):
plist.append((i,data[i]))
xlist=[]
ylist=[]
pt=[]
for i in range(len(plist)):
xlist.append(plist[i][0])
ylist.append(plist[i][1])
pt.append(point(plist[i], rgbcolor=(1,0,0), pointsize=80))
a=min(xlist)-1
b=max(xlist)+1
h=pt[0]
for i in range(len(plist)):
h=h+pt[i]
h.show()
full_hist,hist_list=permute_extract(n,data)
hlh=[]
for i in range(len(full_hist)):
hlh.append(full_hist[i][1])
hlh=bar_chart(hlh)
hlh.show()
hlhl=[]
# for i in range(len(hist_list)):
# hlhl.append([])
# for j in range(len(full_hist)):
# hlhl[i].append(hist_list[i][j][1])
# hlhl[i]=bar_chart(hlhl[i])
#hlhl=animate(hlhl)
#hlhl.show()
if i_type=='Random Walk':
init_points=[]
for i in range(ipoints):
init_points.append(random())
if dff==True:
init_points=data
ipoints=len(data)
data_points=[]
data_points2=[]
for i in range(ipoints):
data_points.append([init_points[i]])
for j in range(length):
data_points[i].append(rw(data_points[i][j]))
data_points2.append(data_points[i][j])
#p=plot(f,(x,0,1),thickness=3)
#p.show()
plist=[]
plist2=[]
for i in range(ipoints):
for j in range(length-1):
plist.append((data_points[i][j],data_points[i][j+1]))
plist2.append((data_points[i][j],0))
xlist=[]
ylist=[]
pt=[]
pt2=[]
for i in range(len(plist)):
xlist.append(plist[i][0])
ylist.append(plist[i][1])
pt.append(point(plist[i], rgbcolor=(1,0,0), pointsize=80,xmin=-2,xmax=2,ymin=-2,ymax=2))
pt2.append(point(plist2[i], rgbcolor=(1,0,0), pointsize=80,xmin=-2,xmax=2))
a=min(xlist)-1
b=max(xlist)+1
pt[0]=point(plist[0], rgbcolor=(0,1,0), pointsize=80)
h=pt[0]
hlist=[]
k=pt2[0]
klist=[]
for i in range(length): #used to be len(plist)
h=h+pt[i]
k=k+pt2[i]
if i < ani:
hlist.append(h)
klist.append(k)
h.show()
k.show()
if ua==True:
hn=animate(hlist)
hn.show(delay=60)
kn=animate(klist)
kn.show(delay=60)
full_hist,hist_list=permute_extract(n,data_points2)
hlh=[]
for i in range(len(full_hist)):
hlh.append(full_hist[i][1])
hlh=bar_chart(hlh)
hlh.show()
#hlhl=[]
#for i in range(len(hist_list)):
# hlhl.append([])
# for j in range(len(full_hist)):
# hlhl[i].append(hist_list[i][j][1])
# hlhl[i]=bar_chart(hlhl[i])
#hlhl=animate(hlhl)
#hlhl.show()