-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinear_Algebra.py
More file actions
158 lines (111 loc) · 4 KB
/
Linear_Algebra.py
File metadata and controls
158 lines (111 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Determinants and Solving System of equations
# 3. Computing Determinants
# The determinant of a square matrix can be computed using np.linalg.det().
# Determinants are Defined Only for Square Matrices
# A determinant is only defined for square matrices, which means the matrix must have the same number of rows and columns (like 2x2, 3x3, etc.). In your case, the matrix:
import numpy as np
# Create a 2D array (square matrix)
matrix = np.array([[1, 2], [3, 4]])
# Compute the determinant
determinant = np.linalg.det(matrix)
print("Determinant of the matrix:", determinant)
# Output: -2.0
# -----------------------------------------------------------------------------------------------
# 4. Solving Linear Equations
# To solve a system of linear equations Ax=b you can use np.linalg.solve(). Here,A is a matrix of coefficients, x is the vector of unknowns, and b is the vector of constants.
import numpy as np
# Coefficient matrix (A)
# it should be also square metrices
A = np.array([[3, 4], [2, 1]])
# Constant matrix (b)
b = np.array([10, 5])
# Solve the linear equations
solution = np.linalg.solve(A, b)
print("Solution to the linear equations:")
print(solution)
# Output: [2. 1.]
# In this example, the system of equations is:
# 3x+4y=10
# 2x+y=5
# the solution x=2 and y=1 satisfies both equations
# ==================================================================================
# Dot Product
# Dot Product of 1D Arrays:
import numpy as np
# 1-D Array = Vector
# 2-D Array = Matrix
# 3-D or Higher Array = Tensor
# Create two 1D arrays (vectors)
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])
# Compute dot product
dot_product = np.dot(vector1, vector2)
print("Dot Product of 1D Arrays:", dot_product)
# Output: 32 (1*4 + 2*5 + 3*6)
# ---------------------------------------------------------------------------------------
# Matrix Multiplication with 2D Arrays:
import numpy as np
# Create two 2D arrays (matrices)
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
# Compute matrix multiplication
matrix_product = np.dot(matrix1, matrix2)
print("Matrix Multiplication using np.dot():")
print(matrix_product)
# Output:
# [[19 22]
# [43 50]]
# ==================================================================================
# Multiplication of Metrices
# Matrix Multiplication with 2D Arrays:
import numpy as np
# Create two 2D arrays (matrices)
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
# Compute matrix multiplication
matrix_product = np.matmul(matrix1, matrix2)
print("Matrix Multiplication using np.matmul():")
print(matrix_product)
# Output:
# [[19 22]
# [43 50]]
# ---------------------------------------------------------------------------------------
# Matrix and Vector Multiplication:
import numpy as np
# Create a matrix and a vector
matrix = np.array([[1, 2, 3], [4, 5, 6]])
vector = np.array([7, 8, 9])
# Compute matrix-vector multiplication
result = np.matmul(matrix, vector)
print("Matrix-Vector Multiplication:")
print(result)
# Output: [50 122]
# ==================================================================================
# Transpose of Metrices
# 2. Transposing Matrices
# The transpose of a matrix swaps its rows and columns. In NumPy, this can be achieved using .T or np.transpose().
import numpy as np
# Create a 2D array (matrix)
matrix = np.array([[1, 2, 3], [4, 5, 6]])
# Transpose the matrix
transposed_matrix = matrix.T
print("Transposed Matrix using .T:")
print(transposed_matrix)
# Output:
# [[1 4]
# [2 5]
# [3 6]]
# ---------------------------------------------------------------------
import numpy as np
# Create a 2D array (matrix)
matrix = np.array([[1, 2, 3], [4, 5, 6]])
# Transpose the matrix
transposed_matrix = np.transpose(matrix)
print("Transposed Matrix using np.transpose():")
print(transposed_matrix)
# Output:
# [[1 4]
# [2 5]
# [3 6]]
# ==================================================================================
# ==================================================================================