diff --git a/TEMA7/pic2.png b/TEMA7/pic2.png new file mode 100644 index 0000000..076dcd5 Binary files /dev/null and b/TEMA7/pic2.png differ diff --git a/TEMA7/report.md b/TEMA7/report.md index 145580c..fc27c86 100644 --- a/TEMA7/report.md +++ b/TEMA7/report.md @@ -321,4 +321,217 @@ TypeError: 'tuple' object does not support item assignment (1, 2, 3, 4) ``` +## 5.1. Специальные типы пользовательских функций. Анонимные функции + +```py +>>> import math +>>> anfun1 = lambda: 1.5+math.log10(17.23) +>>> anfun1() +2.7362852774480286 +>>> anfun2 = lambda a,b: a+math.log10(b) +>>> anfun2(17,234) +19.369215857410143 +>>> anfun3=lambda a,b=234: a+math.log10(b) +>>> anfun3(100) +102.36921585741014 +>>> +``` + +## 5.2. Функции-генераторы + +```py +>>> def func5(diap,shag): + """Итератор, возвращающий значения + из диапазона от 1 до diap с шагом shag""" + for j in range(1,diap+1, shag): + yield j + + +>>> for mm in func5(7,3): + print(mm) + + +1 +4 +7 +>>> alp = func5(7,3) +>>> print(alp,__next__()) +>>> print(alp.__next__()) +1 +>>> print(alp.__next__()) +4 +>>> print(alp.__next__()) +7 +>>> print(alp.__next__()) +Traceback (most recent call last): + File "", line 1, in + print(alp.__next__()) +StopIteration +``` + +## 6.1. Примеры на локализацию объектов + +### Пример 1. +```py +>>> glb=10 +>>> def func7(arg): + loc1=15 + glb=8 + return loc1*arg + +>>> res=func7(glb) +>>> res +150 +>>> glb +10 +``` + +### Пример 2. + +```py +>>> def func8(arg): + loc1 = 15 + print(glb) + glb=8 + return(loc1*arg) + +>>> res=func8(glb) +Traceback (most recent call last): + File "", line 1, in + res=func8(glb) + File "", line 3, in func8 + print(glb) +UnboundLocalError: local variable 'glb' referenced before assignment +>>> def func8(arg): + loc1 = 15 + glb=8 + print(glb) + return(loc1*arg) + +>>> res = func8(glb) +8 +>>> res +150 +>>> +``` + +### Пример 3. + +```py +>>> glb = 11 +>>> def func7(arg): + loc1=15 + global glb + print(glb) + glb = 8 + return loc1*arg + +>>> res = func7(glb) +11 +>>> glb +8 +``` + +## 6.2. Выявление локализации + +```py +>>> globals().keys() +dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', 'os', 'anfun1', 'math', 'anfun2', 'anfun3', 'func5', 'mm', 'alp', 'glb', 'func7', 'res', 'func8']) +>>> locals().keys() +dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', 'os', 'anfun1', 'math', 'anfun2', 'anfun3', 'func5', 'mm', 'alp', 'glb', 'func7', 'res', 'func8']) +>>> def func8(arg): + loc1 = 15 + glb=8 + print(globals().keys()) + print(locals().keys()) + return loc1*arg + +>>> hh=func8(glb) +dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', 'os', 'anfun1', 'math', 'anfun2', 'anfun3', 'func5', 'mm', 'alp', 'glb', 'func7', 'res', 'func8']) +dict_keys(['arg', 'loc1', 'glb']) +>>> 'glb' in globals().keys() +True +``` +## 6.3. Локализация объектов при работе с вложенными функциями + +```py +>>> def func9(arg2,arg3): + def func9_1(arg1): + loc1=15 + glb1=8 + print('glob_func9_1:', globals().keys()) + print('loc1_func9_1:', locals().keys()) + return loc1*arg1 + loc1=5 + glb=func9_1(loc1) + print('loc_func9:',locals().keys()) + print('glob_func9:',globals().keys()) + return arg2+arg3*glb + +>>> kk=func9(10,1) +glob_func9_1: dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', 'os', 'anfun1', 'math', 'anfun2', 'anfun3', 'func5', 'mm', 'alp', 'glb', 'func7', 'res', 'func8', 'hh', 'func9']) +loc1_func9_1: dict_keys(['arg1', 'loc1', 'glb1']) +loc_func9: dict_keys(['arg2', 'arg3', 'func9_1', 'loc1', 'glb']) +glob_func9: dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', 'os', 'anfun1', 'math', 'anfun2', 'anfun3', 'func5', 'mm', 'alp', 'glb', 'func7', 'res', 'func8', 'hh', 'func9']) +>>> kk +85 +``` + +## 6.4. + +```py +>>> znach=input('k1,T,k2,Xm,A,F,N=').split(',') +k1,T,k2,Xm,A,F,N=5,20,2,1,10,0.02,50 +>>> T=float(znach[1]) +>>> k2 = float(znach[2]) +>>> k1=float(znach[0]) +>>> Xm = float(znach[3]) +>>> A=float(znach[4]) +>>> F=float(znach[5]) +>>> N=int(znach[6]) +>>> vhod=[] +>>> for i in range(N): + vhod.append(A*math.sin((2*i*math.pi)*F)) + + +>>> vhod +[0.0, 1.2533323356430426, 2.486898871648548, 3.6812455268467796, 4.817536741017153, 5.877852522924732, 6.845471059286887, 7.705132427757892, 8.443279255020151, 9.048270524660197, 9.510565162951535, 9.822872507286887, 9.980267284282716, 9.980267284282716, 9.822872507286887, 9.510565162951536, 9.048270524660195, 8.44327925502015, 7.705132427757892, 6.845471059286888, 5.877852522924733, 4.817536741017152, 3.6812455268467814, 2.486898871648548, 1.2533323356430408, 1.2246467991473533e-15, -1.2533323356430428, -2.486898871648546, -3.681245526846779, -4.817536741017154, -5.87785252292473, -6.845471059286884, -7.705132427757894, -8.443279255020153, -9.048270524660198, -9.510565162951535, -9.822872507286887, -9.980267284282716, -9.980267284282716, -9.822872507286887, -9.510565162951536, -9.048270524660198, -8.44327925502015, -7.705132427757896, -6.84547105928689, -5.877852522924734, -4.817536741017153, -3.6812455268467787, -2.486898871648545, -1.2533323356430466] +>>> def realdvig(xtt,kk1,TT,yti1,ytin1): + yp=kk1*xtt + yti1=yp+yti1 + ytin1=(yti1+TT*ytin1)/(TT+1) + return [yti1,ytin1] + +>>> def tahogen(xtt,kk2,yti2): + yp=kk2*xtt + yti2=yp+yti2 + return yti2 + +>>> def nechus(xtt,gran): + if xtt(-gran): + ytt=0 + elif xtt>=gran: + ytt=xtt-gran + elif xtt<=(-gran): + ytt=xtt+gran + return ytt + +>>> yi1=0;yin1=0;yi2=0 +>>> vyhod=[] +>>> for xt in vhod: + xt1=xt-yi2 + [yi1,yin1]=realdvig(xt1,k1,T,yi1,yin1) + yi2=tahogen(yin1,k2,yi2) + yt=nechus(yin1,Xm) + vyhod.append(yt) + + +>>> print('y=',vyhod) +y= [0, 0, 0.03263241167294595, 0.9745456461939759, 1.4445477037255587, 0.5534963485187296, -0.14317115231403954, -3.810424583957446, -5.935617950854518, -3.1454619295767294, 4.410078865544928, 17.906974115100144, 26.191694213352932, 16.564012161149634, -16.52439077227259, -67.59051021156763, -101.3214249457787, -69.9621783842152, 55.00346225455457, 246.14981490076363, 380.49988175932214, 278.83918136760593, -179.12097686946288, -894.831188510742, -1427.8203056594884, -1106.6971235336005, 553.0427518851675, 3227.436842271835, 5328.628272818676, 4345.723637601036, -1642.536200959916, -11619.299856185102, -19859.46237228057, -16988.531756411103, 4552.501490270824, 41710.04551030272, 73876.07518877511, 66108.68459616054, -11169.337014708513, -149332.61878005945, -274368.6364383781, -256250.17990633397, 20228.386522231223, 533135.2397813796, 1017335.790045514, 989751.144011423, 3441.7330011494814, -1897576.0088700322, -3766140.0629690504, -3810393.13835538] +>>> import pylab +>>> pylab.plot(vyhod) +[] +>>> pylab.show() +``` +![Скриншот полученного графика](pic2.png)