SharedIDZ.ipynbOpen in CoCalc
import numpy as np



vt = [np.random.randint(-4,4) for x in range(20)];vt

[1, -2, -1, 1, -2, -1, -2, -1, -1, -3, 1, -2, 3, -3, 0, -3, 3, 2, 3, 0]
vo = [x for x in vt if x != 0 and (x%2 != 0)];vo

[1, -1, 1, -1, -1, -1, -3, 1, 3, -3, -3, 3, 3]
####################

import string
print(chr(128522))

😊
index = 0
matriza_vvoda = []
while index == 0:
matriza_vvoda += [input().split(' ')]
last = matriza_vvoda.pop()
if last == ['end']:
index = 1
else:
matriza_vvoda += [last]
mt = []
mt += [matriza_vvoda[i][0] for i in range(len(matriza_vvoda))]
m = [' '.join(matriza_vvoda[i]) for i in range(len(matriza_vvoda))]

res = []
for i in range(len(m)):
space = len([c for c in m[i] if c in ' '])
A = len([c for c in m[i] if c in string.ascii_uppercase])
a = len([c for c in m[i] if c in string.ascii_lowercase])
res += ['пробелов = {0}, прописных = {1}, строчных = {2}'.format(space,A,a)]

res

['пробелов = 2, прописных = 3, строчных = 6', 'пробелов = 1, прописных = 1, строчных = 4']
###############################

import numpy.linalg as li
N = 4; M = 5

eyn = lambda i, j: (-1)**(i + j)
ma = np.fromfunction(eyn,(N,M));ma

array([[ 1., -1., 1., -1., 1.], [-1., 1., -1., 1., -1.], [ 1., -1., 1., -1., 1.], [-1., 1., -1., 1., -1.]])
hor = []; hor += [sum(ma[i] < 0) for i in range(len(ma))];hor = (np.array(hor)).reshape((4,1))
mu = ma.transpose()
ver = []; ver += [sum(mu[i] < 0) for i in range(len(mu))]

hor

array([[2], [3], [2], [3]])
ver

[2, 2, 2, 2, 2]
np.vstack((np.hstack((ma,hor)),ver+[0]))

array([[ 1., -1., 1., -1., 1., 2.], [-1., 1., -1., 1., -1., 3.], [ 1., -1., 1., -1., 1., 2.], [-1., 1., -1., 1., -1., 3.], [ 2., 2., 2., 2., 2., 0.]])
###############################

A = np.reshape([np.random.randint(2,6) for x in range(60)],(4,15));A = np.array(A);A

array([[2, 2, 4, 4, 2, 5, 5, 4, 4, 3, 3, 5, 4, 2, 4], [4, 5, 2, 5, 4, 5, 5, 5, 3, 4, 5, 4, 3, 3, 3], [4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 5], [2, 4, 3, 4, 4, 3, 4, 4, 5, 3, 5, 4, 2, 3, 2]])
fiv = []; fiv += [sum(A[i] == 5) for i in range(len(A))];fiv = (np.array(fiv)).reshape((4,1));fiv

array([[3], [6], [1], [2]])
np.hstack((A,fiv))

array([[2, 2, 4, 4, 2, 5, 5, 4, 4, 3, 3, 5, 4, 2, 4, 3], [4, 5, 2, 5, 4, 5, 5, 5, 3, 4, 5, 4, 3, 3, 3, 6], [4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 5, 1], [2, 4, 3, 4, 4, 3, 4, 4, 5, 3, 5, 4, 2, 3, 2, 2]])
################################

import matplotlib.pyplot as pl

func = lambda x: 2*np.log(x) - 1/x
#1.4

x = np.linspace(0.2,5,100)
pl.plot(x,func(x))
pl.annotate('root', xy=(1.418, 0), xytext=(2, 3), arrowprops=dict(facecolor='black', shrink=0.05),)
pl.grid(1)
pl.show()


def search(f, stP, finP, ind):
ind += 1
mP=average(stP, finP)
if nearcl(stP, finP):
return (mP, ind)
test = f(mP)
if test>0:
return search(f, stP, mP, ind)
elif test < 0:
return search(f, mP, finP, ind)
return (mP, ind)

def average(x, y):
return (x+y)/2
def nearcl(x, y):
if abs(x-y)<0.00001:
return 1
return 0

def halfInterval(f, a ,b):
ind = 0
aVal=f(a)
bVal=f(b)
if aVal>0 and bVal<0:
return search(f, b, a, ind)
elif aVal<0 and bVal>0:
return search(f, a, b, ind)
else:
return 0

halfInterval(func, 0.1, 10)

(1.4215329647064208, 21)
print(ind) ###########################

0
######################################################################пора вторые очки надевать

func = lambda x: 2*np.log(x) - 1/x

def chord(sp, fp, ep):
ind = 0
while(abs(fp - sp) > ep):
ind += 1
sp = fp - (fp - sp)*func(fp)/(func(fp) - func(sp))
fp = sp + (sp - fp)*func(sp)/(func(sp) - func(fp))
return (fp, ind)

chord(1, 2, 0.001)########################

(1.4212949093366554, 3)
func = lambda x: 2*np.log(x) - 1/x
dif = lambda x: 2/x + 1/x**2

def kas(x0, func, dif, e):
ind = 0
while 1:
ind += 1
x1 = x0 - (func(x0) / dif(x0))
if abs(x1 - x0) < e:
return (x1, ind)
x0 = x1

kas(1, func, dif, 0.001)#######################

(1.421529935869652, 4)
func = lambda x: 2*np.log(x) - 1/x
dif = lambda x: 2/x + 1/x**2

def iter(sp, fp, ep):
ind = 0
fp = sp - func(sp)/dif(sp)
while(abs(fp - sp) > ep):
ind += 1
sp = fp - func(fp)/dif(fp)
fp = sp - func(sp)/dif(sp)
return (fp, ind)

iter(1,2,0.001)##################

(1.4215299358831166, 2)
##############################################################

import numpy as np
import numpy.linalg as li

A = [[2,-1,1], [3,5,-2], [1,-4,10]];A
b = np.array([-3,1,0]);b

array([-3, 1, 0])
C = np.reshape([-A[i][j]/A[i][i] for i in range(len(A[0])) for j in range(len(A))],(3,3)) + np.eye(3);print(C)
d = [b[i]/A[i][i] for i in range(len(b))];d

[[ 0. 0.5 -0.5] [-0.6 0. 0.4] [-0.1 0.4 0. ]]
[-1.5, 0.20000000000000001, 0.0]
li.solve(A,b)

array([-1.21212121, 1.16161616, 0.58585859])
def lighiter(q1, q2, q3, ep=0.001):
q = np.array([q1,q2,q3])
x = C.dot(q) + d
while(abs(x[0] - q[0]) > ep and abs(x[1] - q[1]) > ep and abs(x[2] - q[2]) > ep ):
q = C.dot(x) + d
x = C.dot(q) + d
return x

q = li.solve(A,b)
x = C.dot(q) + d;x

array([-1.21212121, 1.16161616, 0.58585859])
lighiter(-1.2, 1, 0.5, 0.0001) ###########################

array([-1.212134 , 1.1616362 , 0.58584048])
def zeid(q1, q2, q3, ep=0.001):
q = np.array([q1,q2,q3])
x1 = C[0][0]*q1 + C[0][1]*q2 + C[0][2]*q3 + d[0]
x2 = C[1][0]*x1 + C[1][1]*q2 + C[1][2]*q3 + d[1]
x3 = C[2][0]*x1 + C[2][1]*x2 + C[2][2]*q3 + d[2]
while(abs(x1 - q1) > ep and abs(x2 - q2) > ep and abs(x3 - q3) > ep):
x1 = C[0][0]*q1 + C[0][1]*q2 + C[0][2]*q3 + d[0]
x2 = C[1][0]*x1 + C[1][1]*q2 + C[1][2]*q3 + d[1]
x3 = C[2][0]*x1 + C[2][1]*x2 + C[2][2]*q3 + d[2]
q1 = C[0][0]*x1 + C[0][1]*x2 + C[0][2]*x3 + d[0]
q2 = C[1][0]*q1 + C[1][1]*x2 + C[1][2]*x3 + d[1]
q3 = C[2][0]*q1 + C[2][1]*q2 + C[2][2]*x3 + d[2]
return [x1, x2, x3]

zeid(-1.2, 1, 0.5, 0.0001) ################################

[-1.211525, 1.1619349999999999, 0.58592650000000002]
#num8

fn = lambda x: np.log(x/2)
dif = lambda x: 1/x
dif2 = lambda x: -1/x**2
a = 4.5; b = 10; m = 5.03

rdif = lambda x, d: (fn(x + d) - fn(x))/d
ldif = lambda x, d: (fn(x) - fn(x - d))/d
ddif = lambda x, d: (fn(x - 2*d) - 8*fn(x - d) + 8*fn(x + d) - fn(x + 2*d))/(12*d)
diff = lambda x, d: (fn(x + d) - fn(x - d))/(2*d)

print(dif(m), '=', rdif(m,5.029), '=', ldif(m, 5.0), '=', ddif(m, 5.029), '=', diff(m, 5.029))

0.19880715705765406 = 0.13781025493 = 1.02439557629 = nan = 0.916307718745
/ext/anaconda3/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in log if __name__ == '__main__':
ddiff = lambda x, d: (fn(x - d) - 2*fn(x) + fn(x + d))/d**2;ddiff(m,5.029)

-0.30960328646457025
x = np.linspace(a, b, 50);
xn = np.hstack((x[1:],[10.1]))
f = fn(x); d = dif(x); dd = dif2(x)
dn = rdif(x,xn)
ddn = -ddiff(x,xn)

/ext/anaconda3/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in log if __name__ == '__main__':
print(x)

[ 4.5 4.6122449 4.7244898 4.83673469 4.94897959 5.06122449 5.17346939 5.28571429 5.39795918 5.51020408 5.62244898 5.73469388 5.84693878 5.95918367 6.07142857 6.18367347 6.29591837 6.40816327 6.52040816 6.63265306 6.74489796 6.85714286 6.96938776 7.08163265 7.19387755 7.30612245 7.41836735 7.53061224 7.64285714 7.75510204 7.86734694 7.97959184 8.09183673 8.20408163 8.31632653 8.42857143 8.54081633 8.65306122 8.76530612 8.87755102 8.98979592 9.10204082 9.21428571 9.32653061 9.43877551 9.55102041 9.66326531 9.7755102 9.8877551 10. ]
print(xn)

[ 4.6122449 4.7244898 4.83673469 4.94897959 5.06122449 5.17346939 5.28571429 5.39795918 5.51020408 5.62244898 5.73469388 5.84693878 5.95918367 6.07142857 6.18367347 6.29591837 6.40816327 6.52040816 6.63265306 6.74489796 6.85714286 6.96938776 7.08163265 7.19387755 7.30612245 7.41836735 7.53061224 7.64285714 7.75510204 7.86734694 7.97959184 8.09183673 8.20408163 8.31632653 8.42857143 8.54081633 8.65306122 8.76530612 8.87755102 8.98979592 9.10204082 9.21428571 9.32653061 9.43877551 9.55102041 9.66326531 9.7755102 9.8877551 10. 10.1 ]
import matplotlib.pyplot as plt

plt.figure(figsize=(10,20))
plt.plot(x,f,'ro-',x,d,'bD-',x,dd,'gs-',x,d,'y',x,dd, 'r*')
plt.grid(1)
plt.show()

plt.plot(x,dd,'gs-',x,ddn, 'ro-')
plt.show()

#######################################

fn = lambda x, u: np.cos(u)/(1.25 + x) - 0.1*u**3
lin = np.linspace(0,1,1000)
x0 = 0;u0 = 0;du0 = fn(0,0)
h = 0.001

def eulr(x, u):
y = []
for i in range(len(lin)):
u += h*fn(x, u)
x += h
y += [u]
return yyu = eulr(0,0)

len(lin), len(yu)

(1000, 1000)
plt.plot(lin,yu)

[<matplotlib.lines.Line2D at 0x7fef9f626588>]
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def fun(y, t, a):
"""Define the right-hand side of equation dy/dt = a*y"""
f = np.cos(y)/(1.25 + t) - 0.1*y**3
return f

# Initial condition
y0 = 0

# Times at which the solution is to be computed.
t = np.linspace(0, 1, 51)

# Parameter value to use in fun.
a = -2.5

# Solve the equation.
y = odeint(fun, y0, t, args=(a,))

# Plot the solution.  odeint is generally used to solve a system
# of equations, so it returns an array with shape (len(t), len(y0)).
# In this case, len(y0) is 1, so y[:,0] gives us the solution.
plt.plot(t, y[:,0])
plt.xlabel('t')
plt.ylabel('y')
plt.show()

fn = lambda x, u: np.cos(u)/(1.25 + x) - 0.1*u**3
lin = np.linspace(0,1,10000)
x0 = 0; u0 = 0; du0 = fn(0,0)
h = 1/len(lin)

def RulkaKutka(f, x0, y0, x1, n):
vx = [0] * (n)
vy = [0] * (n)
h = (x1 - x0) / float(n)
vx[0] = x = x0
vy[0] = y = y0
for i in range(1, n):
k1 = h * f(x, y)
k2 = h * f(x + 0.5 * h, y + 0.5 * k1)
k3 = h * f(x + 0.5 * h, y + 0.5 * k2)
k4 = h * f(x + h, y + k3)
vx[i] = x = x0 + i * h
vy[i] = y = y + (k1 + k2 + k2 + k3 + k3 + k4) / 6
return vy


ty = RulkaKutka(fn,0,0,1,1000)

plt.plot(lin,ty)

[<matplotlib.lines.Line2D at 0x7fef947a1390>]