<jupyter_start><jupyter_text>Примеры:<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Вектор:  Вектор-строка:<jupyter_code>v_hor_np = np.array([1, 2])
print(v_hor_np )
v_hor_zeros_v1 = np.zeros((5,))
print(v_hor_zeros_v1 )
v_hor_zeros_v2 = np.zeros((1, 5))
print(v_hor_zeros_v2 )
v_hor_one_v1 = np.ones((5,))
print(v_hor_one_v1)
v_hor_one_v2 = np.ones((1, 5))
print(v_hor_one_v2)<jupyter_output>[1. 1. 1. 1. 1.]
[[1. 1. 1. 1. 1.]]<jupyter_text>Вектор-столбец:<jupyter_code>v_vert_np = np.array([[1], [2]])
print(v_vert_np)
v_vert_zeros = np.zeros((5, 1))
print(v_vert_zeros)
v_vert_ones = np.ones((5, 1))
print(v_vert_ones)<jupyter_output>[[1.]
 [1.]
 [1.]
 [1.]
 [1.]]<jupyter_text>Квадратная матрица:<jupyter_code>m_sqr_arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(m_sqr_arr)
m_sqr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m_sqr_arr = np.array(m_sqr)
print(m_sqr_arr)
m_sqr_mx = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(m_sqr_mx)
m_sqr_mx = np.matrix('1 2 3; 4 5 6; 7 8 9')
print(m_sqr_mx)<jupyter_output>[[1 2 3]
 [4 5 6]
 [7 8 9]]<jupyter_text>Диагональная матрица:<jupyter_code>m_diag = [[1, 0, 0], [0, 5, 0], [0, 0, 9]]
m_diag_np = np.matrix(m_diag)
print(m_diag_np)
m_sqr_mx = np.matrix('1 2 3; 4 5 6; 7 8 9')
diag = np.diag(m_sqr_mx)
print(diag)
m_diag_np = np.diag(np.diag(m_sqr_mx))
print(m_diag_np)<jupyter_output>[[1 0 0]
 [0 5 0]
 [0 0 9]]<jupyter_text>Единичная матрица:<jupyter_code>m_e = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
m_e_np = np.matrix(m_e)
print(m_e_np)
m_eye = np.eye(3)
print(m_eye)
m_idnt = np.identity(3)
print(m_idnt)<jupyter_output>[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]<jupyter_text>Нулевая матрица:<jupyter_code>m_zeros = np.zeros((3, 3))
print(m_zeros)<jupyter_output>[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]<jupyter_text>Задание матрицы в общем виде:<jupyter_code>m_mx = np.matrix('1 2 3; 4 5 6')
print(m_mx)
m_var = np.zeros((2, 5))
print(m_var)<jupyter_output>[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]<jupyter_text>Транспонирование матрицы:<jupyter_code>A = np.matrix('1 2 3; 4 5 6')
print(A)
A_t = A.transpose()
print(A_t)
print(A.T)
A = np.matrix('1 2 3; 4 5 6')
print(A)
R = (A.T).T
print(R)
A = np.matrix('1 2 3; 4 5 6')
B = np.matrix('7 8 9; 0 7 5')
L = (A + B).T
R = A.T + B.T
print(L)
print(R)
A = np.matrix('1 2; 3 4')
B = np.matrix('5 6; 7 8')
L = (A.dot(B)).T
R = (B.T).dot(A.T)
print(L)
print(R)
A = np.matrix('1 2 3; 4 5 6')
k = 3
L = (k * A).T
R = k * (A.T)
print(L)
print(R)
A = np.matrix('1 2; 3 4')
A_det = np.linalg.det(A)
A_T_det = np.linalg.det(A.T)
print(format(A_det, '.9g'))
print(format(A_T_det, '.9g'))<jupyter_output>-2
-2<jupyter_text>Действия над матрицами:  Умножение матрицы на число:<jupyter_code>A = np.matrix('1 2 3; 4 5 6')
C = 3 * A
print(C)
A = np.matrix('1 2; 3 4')
L = 1 * A
R = A
print(L)
print(R)
A = np.matrix('1 2; 3 4')
Z = np.matrix('0 0; 0 0')
L = 0 * A
R = Z
print(L)
print(R)
A = np.matrix('1 2; 3 4')
p = 2
q = 3
L = (p + q) * A
R = p * A + q * A
print(L)
print(R)
A = np.matrix('1 2; 3 4')
p = 2
q = 3
L = (p * q) * A
R = p * (q * A)
print(L)
print(R)
A = np.matrix('1 2; 3 4')
B = np.matrix('5 6; 7 8')
k = 3
L = k * (A + B)
R = k * A + k * B
print(L)
print(R)<jupyter_output>[[18 24]
 [30 36]]
[[18 24]
 [30 36]]<jupyter_text>Сложение матриц:<jupyter_code>A = np.matrix('1 6 3; 8 2 7')
B = np.matrix('8 1 5; 6 9 12')
C = A + B
print(C)
A = np.matrix('1 2; 3 4')
B = np.matrix('5 6; 7 8')
L = A + B
R = B + A
print(L)
print(R)
A = np.matrix('1 2; 3 4')
B = np.matrix('5 6; 7 8')
C = np.matrix('1 7; 9 3')
L = A + (B + C)
R = (A + B) + C
print(L)
print(R)
A = np.matrix('1 2; 3 4')
Z = np.matrix('0 0; 0 0')
L = A + (-1)*A
print(L)
print(Z)<jupyter_output>[[0 0]
 [0 0]]
[[0 0]
 [0 0]]<jupyter_text>Умножение матриц:<jupyter_code>A = np.matrix('1 2 3; 4 5 6')
B = np.matrix('7 8; 9 1; 2 3')
C = A.dot(B)
print(C)
A = np.matrix('1 2; 3 4')
B = np.matrix('5 6; 7 8')
C = np.matrix('2 4; 7 8')
L = A.dot(B.dot(C))
R = (A.dot(B)).dot(C)
print(L)
print(R)
A = np.matrix('1 2; 3 4')
B = np.matrix('5 6; 7 8')
C = np.matrix('2 4; 7 8')
L = A.dot(B + C)
R = A.dot(B) + A.dot(C)
print(L)
print(R)
A = np.matrix('1 2; 3 4')
B = np.matrix('5 6; 7 8')
L = A.dot(B)
R = B.dot(A)
print(L)
print(R)
A = np.matrix('1 2; 3 4')
E = np.matrix('1 0; 0 1')
L = E.dot(A)
R = A.dot(E)
print(L)
print(R)
print(A)
A = np.matrix('1 2; 3 4')
Z = np.matrix('0 0; 0 0')
L = Z.dot(A)
R = A.dot(Z)
print(L)
print(R)
print(Z)<jupyter_output>[[0 0]
 [0 0]]
[[0 0]
 [0 0]]
[[0 0]
 [0 0]]<jupyter_text>Определитель матрицы:<jupyter_code>A = np.matrix('-4 -1 2; 10 4 -1; 8 3 1')
print(A)
np.linalg.det(A)
A = np.matrix('-4 -1 2; 10 4 -1; 8 3 1')
print(A)
print(A.T)
det_A = round(np.linalg.det(A), 3)
det_A_t = round(np.linalg.det(A.T), 3)
print(det_A)
print(det_A_t)
A = np.matrix('-4 -1 2; 0 0 0; 8 3 1')
print(A)
np.linalg.det(A)
A = np.matrix('-4 -1 2; 10 4 -1; 8 3 1')
print(A)
B = np.matrix('10 4 -1; -4 -1 2; 8 3 1')
print(B)
round(np.linalg.det(A), 3)
round(np.linalg.det(B), 3)
A = np.matrix('-4 -1 2; -4 -1 2; 8 3 1')
print(A)
np.linalg.det(A)
A = np.matrix('-4 -1 2; 10 4 -1; 8 3 1')
print(A)
k = 2
B = A.copy()
B[2, :] = k * B[2, :]
print(B)
det_A = round(np.linalg.det(A), 3)
det_B = round(np.linalg.det(B), 3)
det_A * k
det_B
A = np.matrix('-4 -1 2; -4 -1 2; 8 3 1')
B = np.matrix('-4 -1 2; 8 3 2; 8 3 1')
C = A.copy()
C[1, :] += B[1, :]
print(C)
print(A)
print(B)
round(np.linalg.det(C), 3)
round(np.linalg.det(A), 3) + round(np.linalg.det(B), 3)
A = np.matrix('-4 -1 2; 10 4 -1; 8 3 1')
k = 2
B = A.copy()
B[1, :] = B[1, :] + k * B[0, :]
print(A)
print(B)
round(np.linalg.det(A), 3)
round(np.linalg.det(B), 3)
A = np.matrix('-4 -1 2; 10 4 -1; 8 3 1')
print(A)
k = 2
A[1, :] = A[0, :] + k * A[2, :]
round(np.linalg.det(A), 3)
A = np.matrix('-4 -1 2; 10 4 -1; 8 3 1')
print(A)
k = 2
A[1, :] = k * A[0, :]
print(A)
round(np.linalg.det(A), 3)<jupyter_output>[[-4 -1  2]
 [10  4 -1]
 [ 8  3  1]]
[[-4 -1  2]
 [-8 -2  4]
 [ 8  3  1]]<jupyter_text>Обратная матрица:<jupyter_code>A = np.matrix('1 -3; 2 5')
A_inv = np.linalg.inv(A)
print(A_inv)
A = np.matrix('1. -3.; 2. 5.')
A_inv = np.linalg.inv(A)
A_inv_inv = np.linalg.inv(A_inv)
print(A)
print(A_inv_inv)
A = np.matrix('1. -3.; 2. 5.')
L = np.linalg.inv(A.T)
R = (np.linalg.inv(A)).T
print(L)
print(R)
A = np.matrix('1. -3.; 2. 5.')
B = np.matrix('7. 6.; 1. 8.')
L = np.linalg.inv(A.dot(B))
R = np.linalg.inv(B).dot(np.linalg.inv(A))
print(L)
print(R)<jupyter_output>[[ 0.09454545  0.03272727]
 [-0.03454545  0.00727273]]
[[ 0.09454545  0.03272727]
 [-0.03454545  0.00727273]]<jupyter_text>Ранг матрицы:<jupyter_code>m_eye = np.eye(4)
print(m_eye)
rank = np.linalg.matrix_rank(m_eye)
print(rank)
m_eye[3][3] = 0
print(m_eye)
rank = np.linalg.matrix_rank(m_eye)
print(rank)<jupyter_output>[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 0.]]
3
StarcoderdataJupyterStructuredCleanDedup