#u'napis' koduje napis w unicode, który zachowuje się bardzo podobnie do str list(wystapienia(u'Ale To Już byŁo',[u'aLe',u'był',u'o'])) def wystapienia(tekst,baza): for i in range(len(tekst)): for b in baza: if tekst[i:i+len(b)].lower()==b.lower(): yield (b,i) def fun(x): return (x-3)*(x-5)*(x-7)+85 #Uzyskane wyniki mogą różnić się nieznacznie w zależnośći od implementacji print calka(fun,1,5,10) print calka_numpy(fun,1,5,50) from scipy.integrate import quad Ns = (5,10,15,20,25) def calka(f,a,b,N): x = linspace(a, b, N) y = [f(xi) for xi in x] s = 0 for k in range(len(x)-1): s+=(x[k+1]-x[k])*(y[k+1]+y[k]) return s/2 def calka_numpy(f,a,b,N): x = linspace(a, b, N) y = f(x) return np.sum((x[1:]-x[:-1])*(y[1:]+y[:-1]))/2 from scipy.integrate import quad a,b = 1,5 abs(np.array([calka(fun,a,b,N) for N in Ns])-quad(fun,a,b)[0]) #Dodatkowy kod z obrazkiem, to nie jest część rozwiązania from pylab import * a, b = 1, 5 x = linspace(0,10,1000) y = fun(x) xint = numpy.append(x[logical_and(x>=a, x<=b)][::150],b) yint = numpy.append(y[logical_and(x>=a, x<=b)][::150],fun(b)) plot(x, y, lw=2) axis([0, 8, 0, 140]) fill_between(xint, 0, yint, facecolor='gray', alpha=0.4) text(0.5 * (a + b), 30,r"$\int_a^b f(x)dx$", horizontalalignment='center', fontsize=20); podzielne_stat([5,8,10,15],[2,3,5]) def podzielne_stat(nlst,dzielniki): ile=dict() maxr=dict() for d in dzielniki: ile[d]=0 maxr[d]=0 for n in nlst: for d in dzielniki: r=n%d if r==0: ile[d]+=1 if r>maxr[d]: maxr[d]=r for d in ile.keys(): ile[d]=ile[d]*100/len(nlst) print 'Procent podzielnych:',ile print 'Maks. reszta:',maxr import numpy as np from numpy import sum, log, log2 zd=Zdarzenia(1.,3.,11.,4.,-1.) print "Prob1 =",zd.prob print "H1 =", zd.entropia() zd2=Zdarzenia_lg2(1.,3.,11.,4.,-1.) print "Prob2 =",zd2.prob print "H2 =", zd2.entropia() import numpy as np from numpy import sum, log, log2 class Zdarzenia(object): def __init__(self,*wagi): wagi = [w for w in wagi if w>0] sw = sum(wagi) self._p = np.array([float(w)/sw for w in wagi]) @property def prob(self): return self._p def entropia(self): return -sum(self._p*log(self._p)) class Zdarzenia_lg2(Zdarzenia): def entropia(self): return -sum(self._p*log2(self._p)) """ Compute the coherence of two signals """ import numpy as np import matplotlib.pyplot as plt # make a little extra space between the subplots plt.subplots_adjust(wspace=0.5) dt = 0.01 t = np.arange(0, 30, dt) nse1 = np.random.randn(len(t)) # white noise 1 nse2 = np.random.randn(len(t)) # white noise 2 r = np.exp(-t/0.05) cnse1 = np.convolve(nse1, r, mode='same')*dt # colored noise 1 cnse2 = np.convolve(nse2, r, mode='same')*dt # colored noise 2 # two signals with a coherent part and a random part s1 = 0.01*np.sin(2*np.pi*10*t) + cnse1 s2 = 0.01*np.sin(2*np.pi*10*t) + cnse2 plt.figure(figsize=(8,6),facecolor='w') plt.subplot(211) plt.plot(t, s1, 'b-', t, s2, 'g-') plt.xlim(0,5) plt.xlabel('time') plt.ylabel('s1 and s2') plt.grid(True) plt.subplot(212) cxy, f = plt.cohere(s1, s2, 256, 1./dt) plt.ylabel('coherence') plt.show() #s1, s2 wzięte z przykładu powyżej coherence3(s1,s2,s1+s2) def coherence3(s1,s2,s3): # make a little extra space between the subplots plt.figure(figsize=(10,8),facecolor='w') plt.subplots_adjust(wspace=0.5) plt.subplot(311) plt.plot(t, s1, 'b-', t, s2, 'g-',t, s3, 'r-') plt.xlim(0,5) plt.xlabel('time') plt.ylabel('s1 and s2 and s3') plt.grid(True) plt.subplot(312) cxy, f = plt.cohere(s1, s2, 256, 1./dt) plt.ylabel('coherence') plt.subplot(313) cxy, f = plt.cohere(s2, s3, 256, 1./dt) plt.ylabel('coherence') plt.show()