-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixValidator.py
More file actions
80 lines (68 loc) · 3 KB
/
MatrixValidator.py
File metadata and controls
80 lines (68 loc) · 3 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
class MatrixValidator:
def validateConstructor(dim: 'tuple[int]', vals: 'list[list[int]]' = None) -> 'None':
if (vals == None):
return
elif (type(vals[0]) != int and type(vals[0]) != float):
if (len(vals) != dim[0]):
raise Exception("Invalid number of rows")
if (len(vals[0]) != dim[1]):
raise Exception("Invalid number of columns")
else:
if (len(vals) != dim[0] * dim[1]):
raise Exception("Dimension not fitting to vals")
def validateGetItem(dim: 'tuple[int]', coord: 'tuple[int]'):
if (type(coord) == int):
if (dim[0] == 1):
coord = (0, coord)
elif (dim[1] == 1):
coord = (coord, 0)
else:
raise Exception("Can't reference element with 1 index")
elif (len(coord) != 2):
raise Exception("Invalid tuple")
i = coord[0]
j = coord[1]
if (type(i) != str and (i >= self.dim[0] or j < 0)):
raise Exception("Index i larger than dimension")
if (type(j) != str and (j >= self.dim[1] or j < 0)):
raise Exception("Index j larger than dimension")
def validateSetItem(dim: tuple[int], coord: tuple[int], a):
if (len(coord) != 2):
raise Exception("Invalid tuple")
i = coord[0]
j = coord[1]
if (type(i) == str):
if (type(j) == str):
raise Exception("Dimension of matrix is wrong")
if (a.dim != (dim[0], 1)):
raise Exception("Dimension of matrix is wrong")
if (type(j) == str):
if (a.dim != (1, self.dim[1])):
raise Exception("Dimension of mtrix is wrong")
return
if (i >= self.dim[0]):
raise Exception("Index i larger than dimension")
if (j >= self.dim[1]):
raise Exception("Index j larger than dimension")
def validateAdd(dim0, dim1):
if (dim0 != dim1):
raise Exception("Matrices must have the same dimensions")
def validateMul(dim0, dim1):
if (dim0[1] != dim1[0]):
raise Exception("Invalid matrix dimensions")
def validateExtendByColumnVector(dim0, dim1):
if (dim1[1] != 1):
raise Exception("Not a column vector")
if (dim1[0] != dim0[0]):
raise Exception("vector and matrix not in same vector space")
def validateExtendByRowVector(dim0, dim1):
if (dim1[0] != 1):
raise Exception("Not a column vector")
if (dim1[1] != dim0[1]):
raise Exception("vector and matrix not in same vector space")
def validateExtendRows(dim0, dim1):
if (dim1[0] != dim0[0]):
raise Exception("matrices not in same vector space")
def validateExtendCols(dim0, dim1):
if (dim1[1] != dim0[1]):
raise Exception("matrices not in same vector space")