





from __future__ import annotations

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

class     __SCREAMING_SNAKE_CASE   :






							def __init__(  self					,	__lowerCAmelCase = None					):
											if components is None:
															UpperCamelCase__			       =     []
											UpperCamelCase__			       =     list(lowercase_					)






							def __len__(  self					):
											return len(self.__components					)






							def __str__(  self					):
											return "(" + ",".join(map(lowercase_					,	self.__components					)					) + ")"






							def __add__(  self					,	__lowerCAmelCase					):
											UpperCamelCase__			       =     len(self					)
											if size == len(lowercase_					):
															UpperCamelCase__			       =     [self.__components[i] + other.component(lowercase_					) for i in range(lowercase_					)]
															return Vector(lowercase_					)
											else:
															raise Exception("""must have the same size"""					)






							def __sub__(  self					,	__lowerCAmelCase					):
											UpperCamelCase__			       =     len(self					)
											if size == len(lowercase_					):
															UpperCamelCase__			       =     [self.__components[i] - other.component(lowercase_					) for i in range(lowercase_					)]
															return Vector(lowercase_					)
											else:  # error case
															raise Exception("""must have the same size"""					)






							@overload
							def __mul__(  self					,	__lowerCAmelCase					):
											...






							@overload
							def __mul__(  self					,	__lowerCAmelCase					):
											...






							def __mul__(  self					,	__lowerCAmelCase					):
											if isinstance(lowercase_					,	(float, int)					):
															UpperCamelCase__			       =     [c * other for c in self.__components]
															return Vector(lowercase_					)
											elif isinstance(lowercase_					,	lowercase_					) and len(self					) == len(lowercase_					):
															UpperCamelCase__			       =     len(self					)
															UpperCamelCase__			       =     [self.__components[i] * other.component(lowercase_					) for i in range(lowercase_					)]
															return sum(lowercase_					)
											else:  # error case
															raise Exception("""invalid operand!"""					)






							def 			_lowerCamelCase						(  self					):
											return Vector(self.__components					)






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






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase					):
											assert -len(self.__components					) <= pos < len(self.__components					)
											UpperCamelCase__			       =     value






							def 			_lowerCamelCase						(  self					):
											if len(self.__components					) == 0:
															raise Exception("""Vector is empty"""					)
											UpperCamelCase__			       =     [c**2 for c in self.__components]
											return math.sqrt(sum(lowercase_					)					)







							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase = False					):
											UpperCamelCase__			       =     self * other
											UpperCamelCase__			       =     self.euclidean_length() * other.euclidean_length()
											if deg:
															return math.degrees(math.acos(num / den					)					)
											else:
															return math.acos(num / den					)






def       _UpperCamelCase       (a__				:int    ):






				"""simple docstring"""





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





def       _UpperCamelCase       (a__				:Optional[int]				,     a__				:List[Any]    ):






				"""simple docstring"""





				assert isinstance(A_				,     A_    ) and (isinstance(A_				,     A_    ))
				UpperCamelCase__			       =     [0] * dimension
				UpperCamelCase__			       =     1
				return Vector(A_    )





def       _UpperCamelCase       (a__				:Any				,     a__				:Any				,     a__				:Optional[int]    ):






				"""simple docstring"""





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





def       _UpperCamelCase       (a__				:Optional[Any]				,     a__				:str				,     a__				:List[str]    ):






				"""simple docstring"""





				random.seed(A_    )
				UpperCamelCase__			       =     [random.randint(A_				,     A_    ) for _ in range(A_    )]
				return Vector(A_    )

class     __SCREAMING_SNAKE_CASE   :






							def __init__(  self					,	__lowerCAmelCase					,	__lowerCAmelCase					,	__lowerCAmelCase					):
											UpperCamelCase__			       =     matrix
											UpperCamelCase__			       =     w
											UpperCamelCase__			       =     h






							def __str__(  self					):
											UpperCamelCase__			       =     ''''''
											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					,	__lowerCAmelCase					):
											if self.__width == other.width() and self.__height == other.height():
															UpperCamelCase__			       =     []
															for i in range(self.__height					):
																			UpperCamelCase__			       =     [
																			    self.__matrix[i][j] + other.component(lowercase_					,	lowercase_					)
																			    for j in range(self.__width					)
																			]
																			matrix.append(lowercase_					)
															return Matrix(lowercase_					,	self.__width					,	self.__height					)
											else:
															raise Exception("""matrix must have the same dimension!"""					)






							def __sub__(  self					,	__lowerCAmelCase					):
											if self.__width == other.width() and self.__height == other.height():
															UpperCamelCase__			       =     []
															for i in range(self.__height					):
																			UpperCamelCase__			       =     [
																			    self.__matrix[i][j] - other.component(lowercase_					,	lowercase_					)
																			    for j in range(self.__width					)
																			]
																			matrix.append(lowercase_					)
															return Matrix(lowercase_					,	self.__width					,	self.__height					)
											else:
															raise Exception("""matrices must have the same dimension!"""					)






							@overload
							def __mul__(  self					,	__lowerCAmelCase					):
											...






							@overload
							def __mul__(  self					,	__lowerCAmelCase					):
											...






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






							def 			_lowerCamelCase						(  self					):
											return self.__height






							def 			_lowerCamelCase						(  self					):
											return self.__width






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase					):
											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 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase					,	__lowerCAmelCase					):
											if 0 <= x < self.__height and 0 <= y < self.__width:
															UpperCamelCase__			       =     value
											else:
															raise Exception("""change_component: indices out of bounds"""					)






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase					):
											if self.__height != self.__width:
															raise Exception("""Matrix is not square"""					)
											UpperCamelCase__			       =     self.__matrix[:x] + self.__matrix[x + 1 :]
											for i in range(len(lowercase_					)					):
															UpperCamelCase__			       =     minor[i][:y] + minor[i][y + 1 :]
											return Matrix(lowercase_					,	self.__width - 1					,	self.__height - 1					).determinant()






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase					):
											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(lowercase_					,	lowercase_					)
											else:
															raise Exception("""Indices out of bounds"""					)







							def 			_lowerCamelCase						(  self					):
											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:
															UpperCamelCase__			       =     [
															    self.__matrix[0][y] * self.cofactor(0					,	lowercase_					) for y in range(self.__width					)
															]
															return sum(lowercase_					)






def       _UpperCamelCase       (a__				:List[str]    ):






				"""simple docstring"""





				UpperCamelCase__			       =     [[0] * n for _ in range(A_    )]
				return Matrix(A_				,     A_				,     A_    )





def       _UpperCamelCase       (a__				:Optional[Any]				,     a__				:Any				,     a__				:Any				,     a__				:Tuple    ):






				"""simple docstring"""





				random.seed(A_    )
				UpperCamelCase__			       =     [
				    [random.randint(A_				,     A_    ) for _ in range(A_    )] for _ in range(A_    )
				]
				return Matrix(A_				,     A_				,     A_    )











def       _UpperCamelCase       (a__				:dict    ):






							"""simple docstring"""





							UpperCamelCase__			       =     set()
							# To detect a back edge, keep track of vertices currently in the recursion stack
							UpperCamelCase__			       =     set()
							return any(
							    node not in visited and depth_first_search(a__				,     a__				,     a__				,     a__    )
							    for node in graph    )





def       _UpperCamelCase       (a__				:dict				,     a__				:int				,     a__				:set				,     a__				:set    ):






							"""simple docstring"""





							visited.add(a__    )
							rec_stk.add(a__    )

							for node in graph[vertex]:
														if node not in visited:
																					if depth_first_search(a__				,     a__				,     a__				,     a__    ):
																												return True
														elif node in rec_stk:
																					return True

    # The node needs to be removed from recursion stack before function ends
							rec_stk.remove(a__    )
							return False


if __name__ == "__main__":
					from doctest import testmod

					testmod()




