-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatrix.py
139 lines (117 loc) · 3.95 KB
/
matrix.py
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
import numpy as np
import sympy
# reverse the string
def reverse(string):
string = string[::-1]
return string
# Get the matrix from the latex code
def get_matrix(matrix_latex):
if (matrix_latex == ""):
return np.array([])
try:
# split matrix into rows
rows = matrix_latex.strip().split('\n')[1:-1]
num_rows = len(rows)
for i in range(num_rows - 1):
rows[i] = rows[i][:len(rows[i]) - 2]
# split each row into columns and convert to numpy array
num_columns = len(rows[0].split('&'))
matrix = np.zeros((num_rows, num_columns))
for i, row in enumerate(rows):
for j, entry in enumerate(row.split('&')):
matrix[i, j] = float(entry.strip())
return matrix
except:
# return empty numpy array if there is an unhandled error
return np.array([0])
# Print the matrix in latex code
def matrix_to_latex(matrix):
rows, cols = matrix.shape
latex_code = "\\left[\\begin{array}{" + "r" * rows + "}\n"
for i in range(rows):
for j in range(cols - 1):
if (j != cols - 1):
double_entry = matrix[i, j]
entry = "{:.2f}".format(double_entry)
latex_code += entry + " & "
else:
double_entry = matrix[i, j]
entry = "{:.2f}".format(double_entry)
latex_code += entry
double_entry = matrix[i, cols - 1]
entry = "{:.2f}".format(double_entry)
latex_code += entry
if (i != rows - 1):
latex_code += " \\\\"
latex_code += "\n"
latex_code += "\\end{array}\\right]"
return latex_code
# Get rows of matrix
def get_rows(matrix):
rows = matrix.shape[0]
return rows
# Get columns of matrix
def get_columns(matrix):
columns = matrix.shape[1]
return columns
# Get rank of matrix
def get_rank(matrix):
if matrix.shape == (0,):
return 0
rank = np.linalg.matrix_rank(matrix)
return rank
# Get determinant of matrix
def get_determinant(matrix):
if matrix.shape == (0,):
return 0
if (matrix.shape[0] != matrix.shape[1]):
return 0
else:
determinant = np.linalg.det(matrix)
return round(determinant, 2)
# Get eigenvalues of matrix
def get_eigenvalues(matrix):
if matrix.shape == (0,) or matrix.shape == (1,):
return 0
if (get_rows(matrix) != get_columns(matrix)):
return 0
else:
eigenvalues = np.linalg.eigvals(matrix)
for i, eigenvalue in enumerate(eigenvalues):
eigenvalues[i] = np.round(eigenvalue, 2)
# put eigenvalues into a string including the latex code
str = ""
for i, eigenvalue in enumerate(eigenvalues):
str_eigenvalue = "{:.2f}".format(eigenvalue)
str_eigenvalue = str_eigenvalue.replace('j', 'i')
# if the part with the i is 0, then just remove it
if (str_eigenvalue[-5:] == "0.00i"):
str_eigenvalue = str_eigenvalue[:-6]
# change all js to i in the string
str += "\\lambda_{} = {}".format(i + 1, str_eigenvalue)
if (i != len(eigenvalues) - 1):
str += "\\\\"
str += "\n"
return str
# Get inverse of matrix
def get_inverse(matrix):
if matrix.shape == (0,):
return 0
if (get_rows(matrix) != get_columns(matrix)):
return 0
else:
rank = get_rank(matrix)
if (rank != get_rows(matrix)):
return 0
else:
inverse = np.linalg.inv(matrix)
return matrix_to_latex(inverse)
# Find the echelon form of the matrix
def get_echelon(matrix):
if matrix.shape == (0,):
return 0
echelon = sympy.Matrix(matrix).echelon_form()
for i in range(echelon.shape[0]):
for j in range(echelon.shape[1]):
echelon[i, j] = round(echelon[i, j], 2)
return matrix_to_latex(echelon)