/
util.py
65 lines (53 loc) · 1.96 KB
/
util.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
#utility functions
import numpy as np
import math
def multiplyMatrices(matrix1, matrix2):
matrix3 = np.zeros(shape=(matrix1.shape[0], matrix2.shape[1]))
if matrix1.shape[1] != matrix2.shape[0]:
print "Cannot multiply these matrices"
return
for rowIndex in range(0, matrix1.shape[0]):
for colIndex in range(0, matrix2.shape[1]):
matrix3[rowIndex, colIndex] = np.dot(matrix1[rowIndex, :], matrix2[:, colIndex])
return matrix3
def triangular_inverse(matrix):
answer = np.copy(matrix)
for currentRow in range(1, answer.shape[0]):
for currentCol in range(0, currentRow):
answer[currentRow, currentCol] = -answer[currentRow, currentCol]
return answer
def vector_length(matrix):
return math.sqrt(np.dot(matrix[:, 0], matrix[:, 0]))
def matrix_max_norm(matrix):
maxNorm = math.fabs(matrix[0, 0])
for rowIndex in range(0, matrix.shape[0]):
for colIndex in range(0, matrix.shape[1]):
if math.fabs(matrix[rowIndex, colIndex]) > math.fabs(maxNorm):
maxNorm = matrix[rowIndex, colIndex]
return math.fabs(maxNorm)
def determinant2x2(matrix):
if matrix.shape[0] != 2 and matrix.shape[1] != 2:
print "this method only works with 2x2 matricies"
return
else:
return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]
def inverse2x2(matrix):
if matrix.shape[0] != 2 and matrix.shape[1] != 2:
print "this method only works with 2x2 matricies"
return
a = matrix[0, 0]
b = matrix[0, 1]
c = matrix[1, 0]
d = matrix[1, 1]
matrixR = np.matrix([
[d, -b],
[-c, a]
])
return 1/determinant2x2(matrix)*matrixR
def trace2x2(matrix):
if matrix.shape[0] != 2 and matrix.shape[1] != 2:
print "this method only works with 2x2 matricies"
return
return matrix[0,0]+matrix[1,1]
def vector_length(matrix):
return math.sqrt(np.dot(matrix[:, 0], matrix[:, 0]))