Skip to content

Commit d1d1bcc

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent d5ab844 commit d1d1bcc

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

matrix/matrix_class.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, rows: list[list[float]]) -> None:
2121
"least one and the same number of values, each of which must be of type "
2222
"int or float."
2323
)
24-
24+
2525
# Validate matrix structure and content
2626
if rows:
2727
cols = len(rows[0])
@@ -65,10 +65,14 @@ def is_square(self) -> bool:
6565
def identity(self) -> Matrix:
6666
"""Generate identity matrix of same dimensions"""
6767
values = [
68-
[0.0 if column_num != row_num else 1.0 for column_num in range(self.num_rows)]
68+
[
69+
0.0 if column_num != row_num else 1.0
70+
for column_num in range(self.num_rows)
71+
]
6972
for row_num in range(self.num_rows)
7073
]
7174
return Matrix(values)
75+
7276
def determinant(self) -> float:
7377
"""Calculate matrix determinant. Returns 0 for non-square matrices."""
7478
if not self.is_square:
@@ -130,7 +134,10 @@ def adjugate(self) -> Matrix:
130134
"""Generate adjugate matrix (transpose of cofactor matrix)"""
131135
return Matrix(
132136
[
133-
[self.cofactors().rows[column][row] for column in range(self.num_columns)]
137+
[
138+
self.cofactors().rows[column][row]
139+
for column in range(self.num_columns)
140+
]
134141
for row in range(self.num_rows)
135142
]
136143
)
@@ -155,8 +162,7 @@ def __str__(self) -> str:
155162
return (
156163
"["
157164
+ "\n ".join(
158-
"[" + ". ".join(str(val) for val in row) + ".]"
159-
for row in self.rows
165+
"[" + ". ".join(str(val) for val in row) + ".]" for row in self.rows
160166
)
161167
+ "]"
162168
)
@@ -171,12 +177,13 @@ def add_row(self, row: list[float], position: int | None = None) -> None:
171177
raise TypeError("Row elements must be int or float")
172178
if len(row) != self.num_columns:
173179
raise ValueError("Row length must match matrix columns")
174-
180+
175181
if position is None:
176182
self.rows.append(row)
177183
else:
178184
# Fix RUF005: Use iterable unpacking instead of concatenation
179185
self.rows = [*self.rows[:position], row, *self.rows[position:]]
186+
180187
def add_column(self, column: list[float], position: int | None = None) -> None:
181188
"""Add column to matrix. Validates type and length."""
182189
if not isinstance(column, list):
@@ -186,14 +193,18 @@ def add_column(self, column: list[float], position: int | None = None) -> None:
186193
raise TypeError("Column elements must be int or float")
187194
if len(column) != self.num_rows:
188195
raise ValueError("Column length must match matrix rows")
189-
196+
190197
if position is None:
191198
for i, value in enumerate(column):
192199
self.rows[i].append(value)
193200
else:
194201
# Fix RUF005: Use iterable unpacking instead of concatenation
195202
for i, value in enumerate(column):
196-
self.rows[i] = [*self.rows[i][:position], value, *self.rows[i][position:]]
203+
self.rows[i] = [
204+
*self.rows[i][:position],
205+
value,
206+
*self.rows[i][position:],
207+
]
197208

198209
# MATRIX OPERATIONS
199210
def __eq__(self, other: object) -> bool:
@@ -231,6 +242,7 @@ def __sub__(self, other: Matrix) -> Matrix:
231242
for i in range(self.num_rows)
232243
]
233244
)
245+
234246
def __mul__(self, other: Matrix | float) -> Matrix:
235247
"""Matrix multiplication (scalar or matrix)"""
236248
if isinstance(other, (int, float)):
@@ -248,9 +260,7 @@ def __mul__(self, other: Matrix | float) -> Matrix:
248260
for row in self.rows
249261
]
250262
)
251-
raise TypeError(
252-
"Matrix can only be multiplied by scalar or another matrix"
253-
)
263+
raise TypeError("Matrix can only be multiplied by scalar or another matrix")
254264

255265
def __pow__(self, exponent: int) -> Matrix:
256266
"""Matrix exponentiation. Requires square matrix."""

0 commit comments

Comments
 (0)