22
33from __future__ import annotations
44
5- from typing import Union
6-
75
86class Matrix :
97 """
108 Matrix object generated from a 2D array where each element is an array representing
119 a row. Supports both integer and float values.
1210 """
1311
14- __hash__ = None # Fix PLW1641: Mark class as unhashable
12+ __hash__ : None = None # Fix PLW1641: Mark class as unhashable with type annotation
1513
1614 def __init__ (self , rows : list [list [float ]]) -> None :
1715 """
@@ -23,7 +21,7 @@ def __init__(self, rows: list[list[float]]) -> None:
2321 "least one and the same number of values, each of which must be of type "
2422 "int or float."
2523 )
26-
24+
2725 # Validate matrix structure and content
2826 if rows :
2927 cols = len (rows [0 ])
@@ -67,11 +65,10 @@ def is_square(self) -> bool:
6765 def identity (self ) -> Matrix :
6866 """Generate identity matrix of same dimensions"""
6967 values = [
70- [0 if column_num != row_num else 1 for column_num in range (self .num_rows )]
68+ [0.0 if column_num != row_num else 1.0 for column_num in range (self .num_rows )]
7169 for row_num in range (self .num_rows )
7270 ]
7371 return Matrix (values )
74-
7572 def determinant (self ) -> float :
7673 """Calculate matrix determinant. Returns 0 for non-square matrices."""
7774 if not self .is_square :
@@ -133,10 +130,7 @@ def adjugate(self) -> Matrix:
133130 """Generate adjugate matrix (transpose of cofactor matrix)"""
134131 return Matrix (
135132 [
136- [
137- self .cofactors ().rows [column ][row ]
138- for column in range (self .num_columns )
139- ]
133+ [self .cofactors ().rows [column ][row ] for column in range (self .num_columns )]
140134 for row in range (self .num_rows )
141135 ]
142136 )
@@ -161,7 +155,8 @@ def __str__(self) -> str:
161155 return (
162156 "["
163157 + "\n " .join (
164- "[" + ". " .join (str (val ) for val in row ) + ".]" for row in self .rows
158+ "[" + ". " .join (str (val ) for val in row ) + ".]"
159+ for row in self .rows
165160 )
166161 + "]"
167162 )
@@ -176,13 +171,12 @@ def add_row(self, row: list[float], position: int | None = None) -> None:
176171 raise TypeError ("Row elements must be int or float" )
177172 if len (row ) != self .num_columns :
178173 raise ValueError ("Row length must match matrix columns" )
179-
174+
180175 if position is None :
181176 self .rows .append (row )
182177 else :
183178 # Fix RUF005: Use iterable unpacking instead of concatenation
184179 self .rows = [* self .rows [:position ], row , * self .rows [position :]]
185-
186180 def add_column (self , column : list [float ], position : int | None = None ) -> None :
187181 """Add column to matrix. Validates type and length."""
188182 if not isinstance (column , list ):
@@ -192,17 +186,14 @@ def add_column(self, column: list[float], position: int | None = None) -> None:
192186 raise TypeError ("Column elements must be int or float" )
193187 if len (column ) != self .num_rows :
194188 raise ValueError ("Column length must match matrix rows" )
189+
195190 if position is None :
196191 for i , value in enumerate (column ):
197192 self .rows [i ].append (value )
198193 else :
199194 # Fix RUF005: Use iterable unpacking instead of concatenation
200195 for i , value in enumerate (column ):
201- self .rows [i ] = [
202- * self .rows [i ][:position ],
203- value ,
204- * self .rows [i ][position :],
205- ]
196+ self .rows [i ] = [* self .rows [i ][:position ], value , * self .rows [i ][position :]]
206197
207198 # MATRIX OPERATIONS
208199 def __eq__ (self , other : object ) -> bool :
@@ -240,8 +231,7 @@ def __sub__(self, other: Matrix) -> Matrix:
240231 for i in range (self .num_rows )
241232 ]
242233 )
243-
244- def __mul__ (self , other : Union [Matrix , float ]) -> Matrix :
234+ def __mul__ (self , other : Matrix | float ) -> Matrix :
245235 """Matrix multiplication (scalar or matrix)"""
246236 if isinstance (other , (int , float )):
247237 # Preserve float precision by removing int conversion
@@ -258,7 +248,9 @@ def __mul__(self, other: Union[Matrix, float]) -> Matrix:
258248 for row in self .rows
259249 ]
260250 )
261- raise TypeError ("Matrix can only be multiplied by scalar or another matrix" )
251+ raise TypeError (
252+ "Matrix can only be multiplied by scalar or another matrix"
253+ )
262254
263255 def __pow__ (self , exponent : int ) -> Matrix :
264256 """Matrix exponentiation. Requires square matrix."""
0 commit comments