@@ -13,21 +13,37 @@ def __str__(self) -> str:
13
13
return self .message
14
14
15
15
@staticmethod
16
- def __get_dimensions (matrix_1 : matrix , matrix_2 : Optional [matrix ] = None ) -> \
17
- Union [tuple [ dimensions , bool ], tuple [dimensions , dimensions , bool , bool ]]:
16
+ def get_dimensions (matrix_1 : matrix , matrix_2 : Optional [matrix ] = None ) -> \
17
+ Union [dimensions , tuple [dimensions , dimensions ]]:
18
18
rows_1 : int = len (matrix_1 )
19
19
cols_1 : int = len (matrix_1 [0 ])
20
20
dim_1 : dimensions = (rows_1 , cols_1 )
21
21
if matrix_2 is not None :
22
22
rows_2 : int = len (matrix_2 )
23
23
cols_2 : int = len (matrix_2 [0 ])
24
24
dim_2 : dimensions = (rows_2 , cols_2 )
25
- same_dim : bool = dim_1 == dim_2
26
- compatible_dim : bool = cols_1 == rows_2
27
- return dim_1 , dim_2 , same_dim , compatible_dim
25
+ return dim_1 , dim_2
28
26
else :
29
- square : bool = rows_1 == cols_1
30
- return dim_1 , square
27
+ return dim_1
28
+
29
+ @staticmethod
30
+ def is_square (matrix_1 : matrix ) -> bool :
31
+ dim : dimensions = MatrixOperations .get_dimensions (matrix_1 )
32
+ return dim [0 ] == dim [1 ]
33
+
34
+ @staticmethod
35
+ def have_same_dimensions (matrix_1 : matrix , matrix_2 : matrix ) -> bool :
36
+ dim_1 : dimensions
37
+ dim_2 : dimensions
38
+ dim_1 , dim_2 = MatrixOperations .get_dimensions (matrix_1 , matrix_2 )
39
+ return dim_1 == dim_2
40
+
41
+ @staticmethod
42
+ def have_multipliable_dimensions (matrix_1 : matrix , matrix_2 : matrix ) -> bool :
43
+ dim_1 : dimensions
44
+ dim_2 : dimensions
45
+ dim_1 , dim_2 = MatrixOperations .get_dimensions (matrix_1 , matrix_2 )
46
+ return dim_1 [1 ] == dim_2 [0 ]
31
47
32
48
# noinspection PyUnusedLocal
33
49
@staticmethod
@@ -42,10 +58,9 @@ def identity(dimension: int) -> matrix:
42
58
def addition (matrix_1 : matrix , matrix_2 : matrix ) -> matrix :
43
59
dim_1 : dimensions
44
60
dim_2 : dimensions
45
- same_dim : bool
46
- compatible_dim : bool
47
- dim_1 , dim_2 , same_dim , compatible_dim = MatrixOperations .__get_dimensions (matrix_1 , matrix_2 )
48
- if not same_dim :
61
+ dim_1 , dim_2 = MatrixOperations .get_dimensions (matrix_1 , matrix_2 )
62
+ have_same_dimensions : bool = MatrixOperations .have_same_dimensions (matrix_1 , matrix_2 )
63
+ if not have_same_dimensions :
49
64
raise MatrixOperations .IncompatibleDimensionsError ("Matrices are not compatible.\n "
50
65
"No. of Rows & Columns in Matrix 1 have to be the same "
51
66
"as No. of Rows & Columns in Matrix 2." )
@@ -60,10 +75,9 @@ def addition(matrix_1: matrix, matrix_2: matrix) -> matrix:
60
75
def subtraction (matrix_1 : matrix , matrix_2 : matrix ) -> matrix :
61
76
dim_1 : dimensions
62
77
dim_2 : dimensions
63
- same_dim : bool
64
- compatible_dim : bool
65
- dim_1 , dim_2 , same_dim , compatible_dim = MatrixOperations .__get_dimensions (matrix_1 , matrix_2 )
66
- if not same_dim :
78
+ dim_1 , dim_2 = MatrixOperations .get_dimensions (matrix_1 , matrix_2 )
79
+ have_same_dimensions : bool = MatrixOperations .have_same_dimensions (matrix_1 , matrix_2 )
80
+ if not have_same_dimensions :
67
81
raise MatrixOperations .IncompatibleDimensionsError ("Matrices are not compatible.\n "
68
82
"No. of Rows & Columns in Matrix 1 have to be the same "
69
83
"as No. of Rows & Columns in Matrix 2." )
@@ -78,10 +92,9 @@ def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix:
78
92
def multiplication (matrix_1 : matrix , matrix_2 : matrix ) -> matrix :
79
93
dim_1 : dimensions
80
94
dim_2 : dimensions
81
- same_dim : bool
82
- compatible_dim : bool
83
- dim_1 , dim_2 , same_dim , compatible_dim = MatrixOperations .__get_dimensions (matrix_1 , matrix_2 )
84
- if not compatible_dim :
95
+ dim_1 , dim_2 = MatrixOperations .get_dimensions (matrix_1 , matrix_2 )
96
+ have_multipliable_dimensions : bool = MatrixOperations .have_multipliable_dimensions (matrix_1 , matrix_2 )
97
+ if not have_multipliable_dimensions :
85
98
raise MatrixOperations .IncompatibleDimensionsError ("Matrices are not compatible.\n "
86
99
"No. of Columns in Matrix 1 have to be the same as No. "
87
100
"of Rows in Matrix 2." )
@@ -95,9 +108,7 @@ def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix:
95
108
# noinspection PyUnusedLocal
96
109
@staticmethod
97
110
def transpose (matrix_1 : matrix ) -> matrix :
98
- dim : dimensions
99
- square : bool
100
- dim , square = MatrixOperations .__get_dimensions (matrix_1 )
111
+ dim : dimensions = MatrixOperations .get_dimensions (matrix_1 )
101
112
transpose_m : matrix = [[0 for cols in range (dim [0 ])] for rows in range (dim [1 ])]
102
113
for i in range (0 , dim [0 ]):
103
114
for j in range (0 , dim [1 ]):
0 commit comments