
from __future__ import annotations



class       _snake_case   :


 def __init__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   List[Any]    		=      TypeError(
       """Matrices must be formed from a list of zero or more lists containing at """
       """least one and the same number of values, each of which must be of type """
       """int or float.""")
   if len(SCREAMING_SNAKE_CASE_) != 0:
     lowercase__  :   Dict    		=      len(rows[0])
     if cols == 0:
       raise error
     for row in rows:
       if len(SCREAMING_SNAKE_CASE_) != cols:
         raise error
       for value in row:
         if not isinstance(SCREAMING_SNAKE_CASE_					,  (int, float)):
           raise error
     lowercase__  :   List[Any]    		=      rows
   else:
     lowercase__  :   Optional[int]    		=      []


 def      lowercase__     (					self):


   '''simple docstring'''




   return [[row[i] for row in self.rows] for i in range(len(self.rows[0]))]


 @property
 def      lowercase__     (					self):


   '''simple docstring'''




   return len(self.rows)


 @property
 def      lowercase__     (					self):


   '''simple docstring'''




   return len(self.rows[0])


 @property
 def      lowercase__     (					self):


   '''simple docstring'''




   return (self.num_rows, self.num_columns)


 @property
 def      lowercase__     (					self):


   '''simple docstring'''




   return self.order[0] == self.order[1]


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Dict    		=      [
       [0 if column_num != row_num else 1 for column_num in range(self.num_rows)]
       for row_num in range(self.num_rows)
   ]
   return Matrix(SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   if not self.is_square:
     return 0
   if self.order == (0, 0):
     return 1
   if self.order == (1, 1):
     return int(self.rows[0][0])
   if self.order == (2, 2):
     return int(
         (self.rows[0][0] * self.rows[1][1])
         - (self.rows[0][1] * self.rows[1][0]))
   else:
     return sum(
         self.rows[0][column] * self.cofactors().rows[0][column]
         for column in range(self.num_columns))


 def      lowercase__     (					self):


   '''simple docstring'''




   return bool(self.determinant())


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   str    		=      [
       [
           self.rows[other_row][other_column]
           for other_column in range(self.num_columns)
           if other_column != column
       ]
       for other_row in range(self.num_rows)
       if other_row != row
   ]
   return Matrix(SCREAMING_SNAKE_CASE_).determinant()


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if (row + column) % 2 == 0:
     return self.get_minor(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)
   return -1 * self.get_minor(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   return Matrix(
       [
           [self.get_minor(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_) for column in range(self.num_columns)]
           for row in range(self.num_rows)
       ])


 def      lowercase__     (					self):


   '''simple docstring'''




   return Matrix(
       [
           [
               self.minors().rows[row][column]
               if (row + column) % 2 == 0
               else self.minors().rows[row][column] * -1
               for column in range(self.minors().num_columns)
           ]
           for row in range(self.minors().num_rows)
       ])


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      [
       [self.cofactors().rows[column][row] for column in range(self.num_columns)]
       for row in range(self.num_rows)
   ]
   return Matrix(SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.determinant()
   if not determinant:
     raise TypeError("""Only matrices with a non-zero determinant have an inverse""")
   return self.adjugate() * (1 / determinant)


 def __repr__(					self):


   '''simple docstring'''




   return str(self.rows)


 def __str__(					self):


   '''simple docstring'''




   if self.num_rows == 0:
     return "[]"
   if self.num_rows == 1:
     return "[[" + ". ".join(str(self.rows[0])) + "]]"
   return (
       "["
       + "\n ".join(
           [
               """[""" + """. """.join([str(SCREAMING_SNAKE_CASE_) for value in row]) + """.]"""
               for row in self.rows
           ])
       + "]"
   )


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_ = None):


   '''simple docstring'''




   lowercase__  :   int    		=      TypeError("""Row must be a list containing all ints and/or floats""")
   if not isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):
     raise type_error
   for value in row:
     if not isinstance(SCREAMING_SNAKE_CASE_					,  (int, float)):
       raise type_error
   if len(SCREAMING_SNAKE_CASE_) != self.num_columns:
     raise ValueError(
         """Row must be equal in length to the other rows in the matrix""")
   if position is None:
     self.rows.append(SCREAMING_SNAKE_CASE_)
   else:
     lowercase__  :   Optional[Any]    		=      self.rows[0:position] + [row] + self.rows[position:]


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_ = None):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      TypeError(
       """Column must be a list containing all ints and/or floats""")
   if not isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):
     raise type_error
   for value in column:
     if not isinstance(SCREAMING_SNAKE_CASE_					,  (int, float)):
       raise type_error
   if len(SCREAMING_SNAKE_CASE_) != self.num_rows:
     raise ValueError(
         """Column must be equal in length to the other columns in the matrix""")
   if position is None:
     lowercase__  :   Dict    		=      [self.rows[i] + [column[i]] for i in range(self.num_rows)]
   else:
     lowercase__  :   Tuple    		=      [
         self.rows[i][0:position] + [column[i]] + self.rows[i][position:]
         for i in range(self.num_rows)
     ]


 def __eq__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if not isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):
     return NotImplemented
   return self.rows == other.rows


 def __ne__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   return not self == other


 def __neg__(					self):


   '''simple docstring'''




   return self * -1


 def __add__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if self.order != other.order:
     raise ValueError("""Addition requires matrices of the same order""")
   return Matrix(
       [
           [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns)]
           for i in range(self.num_rows)
       ])


 def __sub__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if self.order != other.order:
     raise ValueError("""Subtraction requires matrices of the same order""")
   return Matrix(
       [
           [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns)]
           for i in range(self.num_rows)
       ])


 def __mul__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if isinstance(SCREAMING_SNAKE_CASE_					,  (int, float)):
     return Matrix(
         [[int(element * other) for element in row] for row in self.rows])
   elif isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):
     if self.num_columns != other.num_rows:
       raise ValueError(
           """The number of columns in the first matrix must """
           """be equal to the number of rows in the second""")
     return Matrix(
         [
             [Matrix.dot_product(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_) for column in other.columns()]
             for row in self.rows
         ])
   else:
     raise TypeError(
         """A Matrix can only be multiplied by an int, float, or another matrix""")


 def __pow__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if not isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):
     raise TypeError("""A Matrix can only be raised to the power of an int""")
   if not self.is_square:
     raise ValueError("""Only square matrices can be raised to a power""")
   if other == 0:
     return self.identity()
   if other < 0:
     if self.is_invertable():
       return self.inverse() ** (-other)
     raise ValueError(
         """Only invertable matrices can be raised to a negative power""")
   lowercase__  :   List[Any]    		=      self
   for _ in range(other - 1):
     result *= self
   return result



 @classmethod
 def      lowercase__     (					cls					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   return sum(row[i] * column[i] for i in range(len(SCREAMING_SNAKE_CASE_)))


if __name__ == "__main__":
     import doctest

     doctest.testmod()








from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload



class       _snake_case   :


 def __init__(					self					,  SCREAMING_SNAKE_CASE_ = None):


   '''simple docstring'''




   if components is None:
     lowercase__  :   List[str]    		=      []
   lowercase__  :   Dict    		=      list(SCREAMING_SNAKE_CASE_)


 def __len__(					self):


   '''simple docstring'''




   return len(self.__components)


 def __str__(					self):


   '''simple docstring'''




   return "(" + ",".join(map(SCREAMING_SNAKE_CASE_					,  self.__components)) + ")"


 def __add__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Optional[Any]    		=      len(self)
   if size == len(SCREAMING_SNAKE_CASE_):
     lowercase__  :   List[str]    		=      [self.__components[i] + other.component(SCREAMING_SNAKE_CASE_) for i in range(SCREAMING_SNAKE_CASE_)]
     return Vector(SCREAMING_SNAKE_CASE_)
   else:
     raise Exception("""must have the same size""")


 def __sub__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   List[Any]    		=      len(self)
   if size == len(SCREAMING_SNAKE_CASE_):
     lowercase__  :   Optional[Any]    		=      [self.__components[i] - other.component(SCREAMING_SNAKE_CASE_) for i in range(SCREAMING_SNAKE_CASE_)]
     return Vector(SCREAMING_SNAKE_CASE_)
   else:  # error case
     raise Exception("""must have the same size""")


 @overload
 def __mul__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   ...


 @overload
 def __mul__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   ...


 def __mul__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if isinstance(SCREAMING_SNAKE_CASE_					,  (float, int)):
     lowercase__  :   Optional[int]    		=      [c * other for c in self.__components]
     return Vector(SCREAMING_SNAKE_CASE_)
   elif isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_) and len(self) == len(SCREAMING_SNAKE_CASE_):
     lowercase__  :   Dict    		=      len(self)
     lowercase__  :   Optional[Any]    		=      [self.__components[i] * other.component(SCREAMING_SNAKE_CASE_) for i in range(SCREAMING_SNAKE_CASE_)]
     return sum(SCREAMING_SNAKE_CASE_)
   else:  # error case
     raise Exception("""invalid operand!""")


 def      lowercase__     (					self):


   '''simple docstring'''




   return Vector(self.__components)


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_) and -len(self.__components) <= i < len(self.__components):
     return self.__components[i]
   else:
     raise Exception("""index out of range""")


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   assert -len(self.__components) <= pos < len(self.__components)
   lowercase__  :   List[Any]    		=      value


 def      lowercase__     (					self):


   '''simple docstring'''




   if len(self.__components) == 0:
     raise Exception("""Vector is empty""")
   lowercase__  :   Union[str, Any]    		=      [c**2 for c in self.__components]
   return math.sqrt(sum(SCREAMING_SNAKE_CASE_))



 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_ = False):


   '''simple docstring'''




   lowercase__  :   Union[str, Any]    		=      self * other
   lowercase__  :   Optional[Any]    		=      self.euclidean_length() * other.euclidean_length()
   if deg:
     return math.degrees(math.acos(num / den))
   else:
     return math.acos(num / den)

def 		UpperCamelCase	(      lowercase_   )		->	Vector:


  '''simple docstring'''






  assert isinstance(lowercase_ , lowercase_   )
  return Vector([0] * dimension   )

def 		UpperCamelCase	(      lowercase_ , lowercase_   )		->	Vector:


  '''simple docstring'''






  assert isinstance(lowercase_ , lowercase_   ) and (isinstance(lowercase_ , lowercase_   ))
  lowercase__  :   Union[str, Any]    		=      [0] * dimension
  lowercase__  :   Any    		=      1
  return Vector(lowercase_   )

def 		UpperCamelCase	(      lowercase_ , lowercase_ , lowercase_   )		->	Vector:


  '''simple docstring'''






  assert (
      isinstance(lowercase_ , lowercase_   )
      and isinstance(lowercase_ , lowercase_   )
      and (isinstance(lowercase_ , (int, float)   ))
  )
  return x * scalar + y

def 		UpperCamelCase	(      lowercase_ , lowercase_ , lowercase_   )		->	Vector:


  '''simple docstring'''






  random.seed(lowercase_   )
  lowercase__  :   int    		=      [random.randint(lowercase_ , lowercase_   ) for _ in range(lowercase_   )]
  return Vector(lowercase_   )



class       _snake_case   :


 def __init__(					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   List[Any]    		=      matrix
   lowercase__  :   Any    		=      w
   lowercase__  :   Any    		=      h


 def __str__(					self):


   '''simple docstring'''




   lowercase__  :   str    		=      """"""
   for i in range(self.__height):
     ans += "|"
     for j in range(self.__width):
       if j < self.__width - 1:
         ans += str(self.__matrix[i][j]) + ","
       else:
         ans += str(self.__matrix[i][j]) + "|\n"
   return ans


 def __add__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if self.__width == other.width() and self.__height == other.height():
     lowercase__  :   Tuple    		=      []
     for i in range(self.__height):
       lowercase__  :   Tuple    		=      [
           self.__matrix[i][j] + other.component(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)
           for j in range(self.__width)
       ]
       matrix.append(SCREAMING_SNAKE_CASE_)
     return Matrix(SCREAMING_SNAKE_CASE_					,  self.__width					,  self.__height)
   else:
     raise Exception("""matrix must have the same dimension!""")


 def __sub__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if self.__width == other.width() and self.__height == other.height():
     lowercase__  :   Optional[int]    		=      []
     for i in range(self.__height):
       lowercase__  :   List[str]    		=      [
           self.__matrix[i][j] - other.component(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)
           for j in range(self.__width)
       ]
       matrix.append(SCREAMING_SNAKE_CASE_)
     return Matrix(SCREAMING_SNAKE_CASE_					,  self.__width					,  self.__height)
   else:
     raise Exception("""matrices must have the same dimension!""")


 @overload
 def __mul__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   ...


 @overload
 def __mul__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   ...


 def __mul__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):  # matrix-vector
     if len(SCREAMING_SNAKE_CASE_) == self.__width:
       lowercase__  :   List[Any]    		=      zero_vector(self.__height)
       for i in range(self.__height):
         lowercase__  :   Union[str, Any]    		=      [
             self.__matrix[i][j] * other.component(SCREAMING_SNAKE_CASE_)
             for j in range(self.__width)
         ]
         ans.change_component(SCREAMING_SNAKE_CASE_					,  sum(SCREAMING_SNAKE_CASE_))
       return ans
     else:
       raise Exception(
           """vector must have the same size as the """
           """number of columns of the matrix!""")
   elif isinstance(SCREAMING_SNAKE_CASE_					,  (int, float)):  # matrix-scalar
     lowercase__  :   Tuple    		=      [
         [self.__matrix[i][j] * other for j in range(self.__width)]
         for i in range(self.__height)
     ]
     return Matrix(SCREAMING_SNAKE_CASE_					,  self.__width					,  self.__height)
   return None


 def      lowercase__     (					self):


   '''simple docstring'''




   return self.__height


 def      lowercase__     (					self):


   '''simple docstring'''




   return self.__width


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if 0 <= x < self.__height and 0 <= y < self.__width:
     return self.__matrix[x][y]
   else:
     raise Exception("""change_component: indices out of bounds""")


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if 0 <= x < self.__height and 0 <= y < self.__width:
     lowercase__  :   Tuple    		=      value
   else:
     raise Exception("""change_component: indices out of bounds""")


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if self.__height != self.__width:
     raise Exception("""Matrix is not square""")
   lowercase__  :   List[Any]    		=      self.__matrix[:x] + self.__matrix[x + 1 :]
   for i in range(len(SCREAMING_SNAKE_CASE_)):
     lowercase__  :   List[str]    		=      minor[i][:y] + minor[i][y + 1 :]
   return Matrix(SCREAMING_SNAKE_CASE_					,  self.__width - 1					,  self.__height - 1).determinant()


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if self.__height != self.__width:
     raise Exception("""Matrix is not square""")
   if 0 <= x < self.__height and 0 <= y < self.__width:
     return (-1) ** (x + y) * self.minor(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)
   else:
     raise Exception("""Indices out of bounds""")



 def      lowercase__     (					self):


   '''simple docstring'''




   if self.__height != self.__width:
     raise Exception("""Matrix is not square""")
   if self.__height < 1:
     raise Exception("""Matrix has no element""")
   elif self.__height == 1:
     return self.__matrix[0][0]
   elif self.__height == 2:
     return (
         self.__matrix[0][0] * self.__matrix[1][1]
         - self.__matrix[0][1] * self.__matrix[1][0]
     )
   else:
     lowercase__  :   Optional[int]    		=      [
         self.__matrix[0][y] * self.cofactor(0					,  SCREAMING_SNAKE_CASE_) for y in range(self.__width)
     ]
     return sum(SCREAMING_SNAKE_CASE_)

def 		UpperCamelCase	(      lowercase_   )		->	Matrix:


  '''simple docstring'''






  lowercase__  :   list[list[float]]    		=      [[0] * n for _ in range(lowercase_   )]
  return Matrix(lowercase_ , lowercase_ , lowercase_   )



def 		UpperCamelCase	(      lowercase_ , lowercase_ , lowercase_ , lowercase_   )		->	Matrix:


  '''simple docstring'''






  random.seed(lowercase_   )
  lowercase__  :   list[list[float]]    		=      [
      [random.randint(lowercase_ , lowercase_   ) for _ in range(lowercase_   )] for _ in range(lowercase_   )
  ]
  return Matrix(lowercase_ , lowercase_ , lowercase_   )






