Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лаб. 1 КП ОМА

.docx
Скачиваний:
1
Добавлен:
30.08.2024
Размер:
24.46 Кб
Скачать

ЛР1

Упражнение 1

2*3

6

k=3+4 print(k)

7

(k+1)*(k-1)

48

(x+1)*(x-1)

--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [4], in <cell line: 1>() ----> 1 (x+1)*(x-1) NameError: name 'x' is not defined

h=(k+2)*3+\ 3+(k+7) print(h)

44

Упражнение 2

%whos

Variable Type Data/Info ---------------------------- h int 44 k int 7

del(h)

%whos

Variable Type Data/Info ---------------------------- k int 7

%reset

Once deleted, variables cannot be recovered. Proceed (y/[n])? y

%whos

Interactive namespace is empty.

x=2 z=3

%whos

Variable Type Data/Info ---------------------------- x int 2 z int 3

Упражнение 3

x=1 y=2 z=3 t=4

%whos

Variable Type Data/Info ---------------------------- t int 4 x int 1 y int 2 z int 3

del(x)

%whos

Variable Type Data/Info ---------------------------- t int 4 y int 2 z int 3

import numpy as np B=np.array([1, 3, -1]) print(B)

[ 1 3 -1]

import numpy as np A=np.array([[1,2,3,4],[0,1,3,2]]) print(A)

[[1 2 3 4] [0 1 3 2]]

A[:,1]

array([2, 1])

v=np.arange(1,8,2) print(v)

[1 3 5 7]

import numpy as np A=np.arange(4) print('A=',A) B=np.arange(12).reshape(2,6) print('B=',B)

A= [0 1 2 3] B= [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11]]

print(np.shape(A))

(4,)

print(np.shape(B))

(2, 6)

print(B.reshape(2,6))

[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11]]

print(A.size)

4

print(len(B))

2

import numpy as np S=np.array([-1, np.sqrt(2), np.abs(-3)]) print(S)

[-1. 1.41421356 3. ]

Упражнение 4

import numpy as np R=np.array([[1,-2,4,8],[-2,-3,3,1],[6,5,-9,2]]) print(R)

[[ 1 -2 4 8] [-2 -3 3 1] [ 6 5 -9 2]]

R[2,3]=-R[2,3] print(R)

[[ 1 -2 4 8] [-2 -3 3 1] [ 6 5 -9 -2]]

R[0,2]=R[0,2]-4 print(R)

[[ 1 -2 0 8] [-2 -3 3 1] [ 6 5 -9 -2]]

R[:,1]=R[:,1]*2 print(R)

[[ 1 -4 0 8] [ -4 -12 6 2] [ 12 20 -18 -4]]

R[0,:]=R[0,:]*3 print(R)

[[ 3 -12 0 24] [ -4 -12 6 2] [ 12 20 -18 -4]]

import numpy as np k=np.arange(5) print(k) l=np.arange(7) print(l)

[0 1 2 3 4] [0 1 2 3 4 5 6]

import numpy as np r=np.arange(16).reshape(4,4) print(r) t=np.arange(22).reshape(11,2) print(t) u=np.arange(18).reshape(3,6) print(u)

[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] [[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11] [12 13] [14 15] [16 17] [18 19] [20 21]] [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17]]

import numpy as np import numpy.matlib X=np.matlib.ones((2,3)) Y=np.matlib.matrix('1, 2, 3; 3, 2, 1') print(X) print(Y)

[[1. 1. 1.] [1. 1. 1.]] [[1 2 3] [3 2 1]]

Упражнение 5

import numpy as np import numpy.matlib A=np.matlib.matrix('1, 2, 3; 4, 5, 6') B=np.matlib.matrix('1, -2, 1; -2, 3, 4') c=np.matlib.matrix('2') D=np.matlib.ones((2,3)) E=np.matlib.eye(3,3) print(A) print(B) print(c) print(D) print(E)

[[1 2 3] [4 5 6]] [[ 1 -2 1] [-2 3 4]] [[2]] [[1. 1. 1.] [1. 1. 1.]] [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]

print(A+B)

[[ 2 0 4] [ 2 8 10]]

print(A+c)

[[3 4 5] [6 7 8]]

print(A+E)

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In [12], line 1 ----> 1 print(A+E) ValueError: operands could not be broadcast together with shapes (2,3) (3,3)

print(A-B)

[[0 4 2] [6 2 2]]

print(A-c)

[[-1 0 1] [ 2 3 4]]

print(c*A)

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In [15], line 1 ----> 1 print(c*A) File /lib/python3.10/site-packages/numpy/matrixlib/defmatrix.py:218, in matrix.__mul__(self, other) 215 def __mul__(self, other): 216 if isinstance(other, (N.ndarray, list, tuple)) : 217 # This promotes 1-D vectors to row vectors --> 218 return N.dot(self, asmatrix(other)) 219 if isscalar(other) or not hasattr(other, '__rmul__') : 220 return N.dot(self, other) File <__array_function__ internals>:180, in dot(*args, **kwargs) ValueError: shapes (1,1) and (2,3) not aligned: 1 (dim 1) != 2 (dim 0)

print(A*B)

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In [16], line 1 ----> 1 print(A*B) File /lib/python3.10/site-packages/numpy/matrixlib/defmatrix.py:218, in matrix.__mul__(self, other) 215 def __mul__(self, other): 216 if isinstance(other, (N.ndarray, list, tuple)) : 217 # This promotes 1-D vectors to row vectors --> 218 return N.dot(self, asmatrix(other)) 219 if isscalar(other) or not hasattr(other, '__rmul__') : 220 return N.dot(self, other) File <__array_function__ internals>:180, in dot(*args, **kwargs) ValueError: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)

B=B.transpose() print(B)

[[ 1 -2] [-2 3] [ 1 4]]

print(A*B)

[[ 0 16] [ 0 31]]

import numpy as np x = np.array([[1,3,4],[6,1,0]]) A=np.sin(x) print(A)

[[ 0.84147098 0.14112001 -0.7568025 ] [-0.2794155 0.84147098 0. ]]

Упражнение 7

import numpy as np p=np.arange(1,6,2) g=np.sqrt(p) print(g)

[1. 1.73205081 2.23606798]

Упражнение С1

import numpy as np h=np.arange(-2,11,2) print(h) print("h*3=",h*3)

[-2 0 2 4 6 8 10] h*3= [-6 0 6 12 18 24 30]

import numpy as np j=np.arange(45,4,-5) print(j) print(np.shape(j))

[45 40 35 30 25 20 15 10 5] (9,)

Успражнение С2

import numpy as np a1=np.cos(0) print(a1) a2=np.cos(np.pi/6) print(a2) a3=np.cos(np.pi/3) print(a3) a4=np.cos(np.pi/2) print(a4) a5=np.cos((3*np.pi)/2) print(a5) a6=np.cos((2*np.pi)/3) print(a6) a7=np.cos((5*np.pi)/6) print(a7) a8=np.cos(np.pi/4) print(a8) a9=np.cos((2*np.pi)) print(a9)

1.0 0.8660254037844387 0.5000000000000001 6.123233995736766e-17 -1.8369701987210297e-16 -0.4999999999999998 -0.8660254037844387 0.7071067811865476 1.0

import numpy as np x=np.arange(-2,2.5,0.5) y=np.cosh(x)**2-np.sinh(x)**2 print(x) print(y)

[-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [1. 1. 1. 1. 1. 1. 1. 1. 1.]