
Лаб. 2 КП АлГем
.docxлр2
import numpy as np from sympy import *
Пример 1
arr=np.array([[1,2],[3,4]]) print(arr)
[[1 2] [3 4]]
arr=np.matrix([[1,2],[3,4]]) print(arr)
[[1 2] [3 4]]
arr=np.matrix('1 2 3; 4 5 6; 7 8 9') print(arr)
[[1 2 3] [4 5 6] [7 8 9]]
a=Matrix([[1,2],[3,4]]) a
Matrix([ [1, 2], [3, 4]])
Вычисление определителя
arr=np.matrix('1 2; 3 4') detA=arr[0, 0] * arr[1, 1] - arr[0, 1] * arr[1, 0] print(detA)
-2
detA=np.linalg.det(arr) print(detA) det(a)
-2.0000000000000004
-2
Упражнение 2.1
v=Matrix([[-1, 4],[-5, 2]]) det(v)
18
a,b=symbols('a b') v=Matrix([[a+b, a-b],[a+b, a-b]]) det(v)
0
x=symbols('x') v=Matrix([[x, x+1],[-4, x+1]]) det(v)
x**2 + 5*x + 4
Упражнение 2.2
v=Matrix([[1,2,3],[4,5,6],[7,8,1]]) det(v)
24
v=Matrix([[3,4,-5],[8,7,-2],[2,-1,8]]) det(v)
0
a,x,b,c=symbols(' a x b c') v=Matrix([[a+x,x,x],[x,b+x,x],[x,x,c+x]]) det(v)
a*b*c + a*b*x + a*c*x + b*c*x
from sympy import * a, b, c=symbols('a b c') v=Matrix([[sin(a),cos(a),1],[sin(b),cos(b),1],[sin(c),cos(c),1]]) det(v)
sin(a)*cos(b) - sin(a)*cos(c) - sin(b)*cos(a) + sin(b)*cos(c) + sin(c)*cos(a) - sin(c)*cos(b)
Пример 2
A=np.matrix('3 -1 2; 1 4 -1; 2 3 1') print(A) B=np.matrix('-4; 10; 8') print(B) A_det=np.linalg.det(A) print(A_det) X_m=np.matrix(A) X_m[:,0]=B print(X_m) Y_m=np.matrix(A) Y_m[:,1]=B print(Y_m) Z_m=np.matrix(A) Z_m[:,2]=B print(Z_m) x=np.linalg.det(X_m)/A_det y=np.linalg.det(Y_m)/A_det z=np.linalg.det(Z_m)/A_det print(x) print(y) print(z)
[[ 3 -1 2] [ 1 4 -1] [ 2 3 1]] [[-4] [10] [ 8]] 13.999999999999996 [[-4 -1 2] [10 4 -1] [ 8 3 1]] [[ 3 -4 2] [ 1 10 -1] [ 2 8 1]] [[ 3 -1 -4] [ 1 4 10] [ 2 3 8]] -1.0000000000000009 3.0000000000000013 1.0
Упражнение 2.3
A=np.matrix('3 -5; 2 7') print(A) B=np.matrix('13; 81') print(B) A_det=np.linalg.det(A) print(A_det) if A_det!=0: Xm=np.matrix(A) Xm[:,0]=B print(Xm) Ym=np.matrix(A) Ym[:,1]=B print(Ym) x=np.linalg.det(Xm)/A_det y=np.linalg.det(Ym)/A_det print(round(x)) print(round(y)) s=np.matrix([[round(x)],[round(y)]]) print(s) f=A*s-B print(f) if f[0,0]==0 and f[1,0]==0: print("Верно") else: print("Ошибка") else: print("detA=0")
[[ 3 -5] [ 2 7]] [[13] [81]] 31.0 [[13 -5] [81 7]] [[ 3 13] [ 2 81]] 16 7 [[16] [ 7]] [[0] [0]] Верно
A=np.matrix('3 -4; -6 8') print(A) B=np.matrix('-6; 12') print(B) A_det=np.linalg.det(A) print(A_det) if A_det!=0: Xm=np.matrix(A) Xm[:,0]=B print(Xm) Ym=np.matrix(A) Ym[:,1]=B print(Ym) x=np.linalg.det(Xm)/A_det y=np.linalg.det(Ym)/A_det print(round(x)) print(round(y)) s=np.matrix([[round(x)],[round(y)]]) print(s) f=A*s-B print(f) if f[0,0]==0 and f[1,0]==0: print("Верно") else: print("Ошибка") else: print("detA=0")
[[ 3 -4] [-6 8]] [[-6] [12]] 0.0 detA=0
A=np.matrix('7 2 3; 5 -3 2; 10 -11 5') print(A) B=np.matrix('15; 15; 36') print(B) A_det=np.linalg.det(A) print(A_det) if A_det!=0: Xm=np.matrix(A) Xm[:,0]=B print(Xm) Ym=np.matrix(A) Ym[:,1]=B print(Ym) Zm=np.matrix(A) Zm[:,2]=B print(Zm) x=np.linalg.det(Xm)/A_det y=np.linalg.det(Ym)/A_det z=np.linalg.det(Zm)/A_det print(round(x)) print(round(y)) print(round(z)) s=np.matrix([[round(x)],[round(y)],[round(z)]]) print(s) f=A*s-B print(f) if f[0,0]==0 and f[1,0]==0 and f[2,0]==0: print("Верно") else: print("Ошибка") else: print("detA=0")
[[ 7 2 3] [ 5 -3 2] [ 10 -11 5]] [[15] [15] [36]] -36.0 [[ 15 2 3] [ 15 -3 2] [ 36 -11 5]] [[ 7 15 3] [ 5 15 2] [10 36 5]] [[ 7 2 15] [ 5 -3 15] [ 10 -11 36]] 2 -1 1 [[ 2] [-1] [ 1]] [[0] [0] [0]] Верно
A=np.matrix('2 1 0; 1 0 3; 0 5 -1') print(A) B=np.matrix('5; 16; 10') print(B) A_det=np.linalg.det(A) print(A_det) if A_det!=0: Xm=np.matrix(A) Xm[:,0]=B print(Xm) Ym=np.matrix(A) Ym[:,1]=B print(Ym) Zm=np.matrix(A) Zm[:,2]=B print(Zm) x=np.linalg.det(Xm)/A_det y=np.linalg.det(Ym)/A_det z=np.linalg.det(Zm)/A_det print(round(x)) print(round(y)) print(round(z)) s=np.matrix([[round(x)],[round(y)],[round(z)]]) print(s) f=A*s-B print(f) if f[0,0]==0 and f[1,0]==0 and f[2,0]==0: print("Верно") else: print("Ошибка") else: print("detA=0")
[[ 2 1 0] [ 1 0 3] [ 0 5 -1]] [[ 5] [16] [10]] -28.99999999999999 [[ 5 1 0] [16 0 3] [10 5 -1]] [[ 2 5 0] [ 1 16 3] [ 0 10 -1]] [[ 2 1 5] [ 1 0 16] [ 0 5 10]] 1 3 5 [[1] [3] [5]] [[0] [0] [0]] Верно
Упражнение 2.4
from sympy import * A=Matrix([[3,-5],[2,7]]) B=Matrix([[13],[81]]) k=det(A) print("detA",k) if k!=0: Xm=Matrix(A) Xm[:,0]=B Ym=Matrix(A) Ym[:,1]=B x=det(Xm)/k y=det(Ym)/k print(round(x)) print(round(y)) s=Matrix([[round(x)],[round(y)]]) f=A*s-B if f[0,0]==0 and f[1,0]==0: print("Верно") else: print("Ошибка") else: print("detA=0")
detA 31 16 7 Верно
from sympy import * A=Matrix([[7, 2, 3],[5, -3, 2],[10, -11, 5]]) B=Matrix([[15],[15],[36]]) k=det(A) print("detA",k) if k!=0: Xm=Matrix(A) Xm[:,0]=B Ym=Matrix(A) Ym[:,1]=B Zm=Matrix(A) Zm[:,2]=B x=det(Xm)/k y=det(Ym)/k z=det(Zm)/k print(round(x)) print(round(y)) print(round(z)) s=Matrix([[round(x)],[round(y)],[round(z)]]) f=A*s-B if f[0,0]==0 and f[1,0]==0 and f[2,0]==0: print("Верно") else: print("Ошибка") else: print("detA=0")
detA -36 2 -1 1 Верно
Пример 3
import numpy as np A=np.matrix('2 1 0; 1 0 3; 0 5 -1') A_inv=np.linalg.inv(A) Ainv=A**-1 print(A_inv) print(Ainv)
[[ 0.51724138 -0.03448276 -0.10344828] [-0.03448276 0.06896552 0.20689655] [-0.17241379 0.34482759 0.03448276]] [[ 0.51724138 -0.03448276 -0.10344828] [-0.03448276 0.06896552 0.20689655] [-0.17241379 0.34482759 0.03448276]]
A=np.matrix('3 -1 2; 1 4 -1; 2 3 1') B=np.matrix('-4; 10; 8') print("A",A) print("B",B) A_inv=np.linalg.inv(A) print("Ainv",A_inv) X=A_inv.dot(B) print(X)
A [[ 3 -1 2] [ 1 4 -1] [ 2 3 1]] B [[-4] [10] [ 8]] Ainv [[ 0.5 0.5 -0.5 ] [-0.21428571 -0.07142857 0.35714286] [-0.35714286 -0.78571429 0.92857143]] [[-1.] [ 3.] [ 1.]]
Упражнение 2.5
A=np.matrix('3 -5; 2 7') print(A) B=np.matrix('13; 81') print(B) k=np.linalg.det(A) if k!=0: Ai=A**-1 print(Ai) x=Ai*B print(x) else: print("Обратной матрицы не существует")
[[ 3 -5] [ 2 7]] [[13] [81]] [[ 0.22580645 0.16129032] [-0.06451613 0.09677419]] [[16.] [ 7.]]
A=np.matrix('3 -4; -6 8') print(A) B=np.matrix('-6; 12') print(B) k=np.linalg.det(A) if k!=0: Ai=A**-1 print(Ai) x=Ai*B print(x) else: print("Обратной матрицы не существует")
[[ 3 -4] [-6 8]] [[-6] [12]] Обратной матрицы не существует
A=np.matrix('7 2 3; 5 -3 2; 10 -11 5') print(A) B=np.matrix('15; 15; 36') print(B) k=np.linalg.det(A) if k!=0: Ai=A**-1 print(Ai) x=Ai*B print(x) else: print("Обратной матрицы не существует")
[[ 7 2 3] [ 5 -3 2] [ 10 -11 5]] [[15] [15] [36]] [[-0.19444444 1.19444444 -0.36111111] [ 0.13888889 -0.13888889 -0.02777778] [ 0.69444444 -2.69444444 0.86111111]] [[ 2.] [-1.] [ 1.]]
A=np.matrix('2 1 0; 1 0 3; 0 5 -1') print(A) B=np.matrix('5; 16; 10') print(B) k=np.linalg.det(A) if k!=0: Ai=A**-1 print(Ai) x=Ai*B print(x) else: print("Обратной матрицы не существует")
[[ 2 1 0] [ 1 0 3] [ 0 5 -1]] [[ 5] [16] [10]] [[ 0.51724138 -0.03448276 -0.10344828] [-0.03448276 0.06896552 0.20689655] [-0.17241379 0.34482759 0.03448276]] [[1.] [3.] [5.]]