55from typing import Union
66
77
8-
98class Matrix :
109 """
1110 Matrix object generated from a 2D array where each element is an array representing
@@ -24,7 +23,7 @@ def __init__(self, rows: list[list[float]]) -> None:
2423 "least one and the same number of values, each of which must be of type "
2524 "int or float."
2625 )
27-
26+
2827 # Validate matrix structure and content
2928 if rows :
3029 cols = len (rows [0 ])
@@ -134,7 +133,10 @@ def adjugate(self) -> Matrix:
134133 """Generate adjugate matrix (transpose of cofactor matrix)"""
135134 return Matrix (
136135 [
137- [self .cofactors ().rows [column ][row ] for column in range (self .num_columns )]
136+ [
137+ self .cofactors ().rows [column ][row ]
138+ for column in range (self .num_columns )
139+ ]
138140 for row in range (self .num_rows )
139141 ]
140142 )
@@ -159,8 +161,7 @@ def __str__(self) -> str:
159161 return (
160162 "["
161163 + "\n " .join (
162- "[" + ". " .join (str (val ) for val in row ) + ".]"
163- for row in self .rows
164+ "[" + ". " .join (str (val ) for val in row ) + ".]" for row in self .rows
164165 )
165166 + "]"
166167 )
@@ -175,7 +176,7 @@ def add_row(self, row: list[float], position: int | None = None) -> None:
175176 raise TypeError ("Row elements must be int or float" )
176177 if len (row ) != self .num_columns :
177178 raise ValueError ("Row length must match matrix columns" )
178-
179+
179180 if position is None :
180181 self .rows .append (row )
181182 else :
@@ -190,14 +191,18 @@ def add_column(self, column: list[float], position: int | None = None) -> None:
190191 if not isinstance (value , (int , float )):
191192 raise TypeError ("Column elements must be int or float" )
192193 if len (column ) != self .num_rows :
193- raise ValueError ("Column length must match matrix rows" )
194+ raise ValueError ("Column length must match matrix rows" )
194195 if position is None :
195196 for i , value in enumerate (column ):
196197 self .rows [i ].append (value )
197198 else :
198199 # Fix RUF005: Use iterable unpacking instead of concatenation
199200 for i , value in enumerate (column ):
200- self .rows [i ] = [* self .rows [i ][:position ], value , * self .rows [i ][position :]]
201+ self .rows [i ] = [
202+ * self .rows [i ][:position ],
203+ value ,
204+ * self .rows [i ][position :],
205+ ]
201206
202207 # MATRIX OPERATIONS
203208 def __eq__ (self , other : object ) -> bool :
@@ -253,9 +258,7 @@ def __mul__(self, other: Union[Matrix, float]) -> Matrix:
253258 for row in self .rows
254259 ]
255260 )
256- raise TypeError (
257- "Matrix can only be multiplied by scalar or another matrix"
258- )
261+ raise TypeError ("Matrix can only be multiplied by scalar or another matrix" )
259262
260263 def __pow__ (self , exponent : int ) -> Matrix :
261264 """Matrix exponentiation. Requires square matrix."""
@@ -275,6 +278,7 @@ def __pow__(self, exponent: int) -> Matrix:
275278 for _ in range (exponent - 1 ):
276279 result *= self
277280 return result
281+
278282 @classmethod
279283 def dot_product (cls , row : list [float ], column : list [float ]) -> float :
280284 """Calculate dot product of two vectors"""
0 commit comments