Skip to content

Commit cc04dd2

Browse files
committed
Stripped all other functionality from get_dimensions() and split them into independent public functions
Made get_dimensions() public
1 parent 673d4a7 commit cc04dd2

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

matrix_operations.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,37 @@ def __str__(self) -> str:
1313
return self.message
1414

1515
@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]]:
1818
rows_1: int = len(matrix_1)
1919
cols_1: int = len(matrix_1[0])
2020
dim_1: dimensions = (rows_1, cols_1)
2121
if matrix_2 is not None:
2222
rows_2: int = len(matrix_2)
2323
cols_2: int = len(matrix_2[0])
2424
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
2826
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]
3147

3248
# noinspection PyUnusedLocal
3349
@staticmethod
@@ -42,10 +58,9 @@ def identity(dimension: int) -> matrix:
4258
def addition(matrix_1: matrix, matrix_2: matrix) -> matrix:
4359
dim_1: dimensions
4460
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:
4964
raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n"
5065
"No. of Rows & Columns in Matrix 1 have to be the same "
5166
"as No. of Rows & Columns in Matrix 2.")
@@ -60,10 +75,9 @@ def addition(matrix_1: matrix, matrix_2: matrix) -> matrix:
6075
def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix:
6176
dim_1: dimensions
6277
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:
6781
raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n"
6882
"No. of Rows & Columns in Matrix 1 have to be the same "
6983
"as No. of Rows & Columns in Matrix 2.")
@@ -78,10 +92,9 @@ def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix:
7892
def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix:
7993
dim_1: dimensions
8094
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:
8598
raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n"
8699
"No. of Columns in Matrix 1 have to be the same as No. "
87100
"of Rows in Matrix 2.")
@@ -95,9 +108,7 @@ def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix:
95108
# noinspection PyUnusedLocal
96109
@staticmethod
97110
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)
101112
transpose_m: matrix = [[0 for cols in range(dim[0])] for rows in range(dim[1])]
102113
for i in range(0, dim[0]):
103114
for j in range(0, dim[1]):

0 commit comments

Comments
 (0)