



"""simple docstring"""






from __future__ import annotations

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



class     UpperCAmelCase    :



   def __init__(       self    :	Union[str, Any]							,       __lowerCamelCase    :	Collection[float] | None = None				):



     """simple docstring"""
     if components is None:
       _snake_case   	=      []
     _snake_case   	=      list(__lowerCamelCase				)



   def __len__(       self    :	str				):



     """simple docstring"""
     return len(self.__components				)



   def __str__(       self    :	str				):



     """simple docstring"""
     return "(" + ",".join(map(__lowerCamelCase							,       self.__components				)				) + ")"



   def __add__(       self    :	int							,       __lowerCamelCase    :	Vector				):



     """simple docstring"""
     _snake_case   	=      len(self				)
     if size == len(__lowerCamelCase				):
       _snake_case   	=      [self.__components[i] + other.component(__lowerCamelCase				) for i in range(__lowerCamelCase				)]
       return Vector(__lowerCamelCase				)
     else:
       raise Exception('''must have the same size'''				)



   def __sub__(       self    :	Any							,       __lowerCamelCase    :	Vector				):



     """simple docstring"""
     _snake_case   	=      len(self				)
     if size == len(__lowerCamelCase				):
       _snake_case   	=      [self.__components[i] - other.component(__lowerCamelCase				) for i in range(__lowerCamelCase				)]
       return Vector(__lowerCamelCase				)
     else:  # error case
       raise Exception('''must have the same size'''				)



   @overload
   def __mul__(       self    :	Optional[int]							,       __lowerCamelCase    :	float				):



     """simple docstring"""
     ...



   @overload
   def __mul__(       self    :	Optional[Any]							,       __lowerCamelCase    :	Vector				):



     """simple docstring"""
     ...



   def __mul__(       self    :	List[Any]							,       __lowerCamelCase    :	float | Vector				):



     """simple docstring"""
     if isinstance(__lowerCamelCase							,       (float, int)				):
       _snake_case   	=      [c * other for c in self.__components]
       return Vector(__lowerCamelCase				)
     elif isinstance(__lowerCamelCase							,       __lowerCamelCase				) and len(self				) == len(__lowerCamelCase				):
       _snake_case   	=      len(self				)
       _snake_case   	=      [self.__components[i] * other.component(__lowerCamelCase				) for i in range(__lowerCamelCase				)]
       return sum(__lowerCamelCase				)
     else:  # error case
       raise Exception('''invalid operand!'''				)



   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     return Vector(self.__components				)



   def    __UpperCAmelCase    (       self    :	Tuple							,       __lowerCamelCase    :	int				):



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



   def    __UpperCAmelCase    (       self    :	str							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	float				):



     """simple docstring"""
     assert -len(self.__components				) <= pos < len(self.__components				)
     _snake_case   	=      value



   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     if len(self.__components				) == 0:
       raise Exception('''Vector is empty'''				)
     _snake_case   	=      [c**2 for c in self.__components]
     return math.sqrt(sum(__lowerCamelCase				)				)





   def    __UpperCAmelCase    (       self    :	Optional[int]							,       __lowerCamelCase    :	Vector							,       __lowerCamelCase    :	bool = False				):



     """simple docstring"""
     _snake_case   	=      self * other
     _snake_case   	=      self.euclidean_length() * other.euclidean_length()
     if deg:
       return math.degrees(math.acos(num / den				)				)
     else:
       return math.acos(num / den				)




def 			snake_case		(		lowerCAmelCase_	)						->				Vector:
  assert isinstance(lowerCAmelCase_  ,						lowerCAmelCase_	)
  return Vector([0] * dimension	)




def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_	)						->				Vector:
  assert isinstance(lowerCAmelCase_  ,						lowerCAmelCase_	) and (isinstance(lowerCAmelCase_  ,						lowerCAmelCase_	))
  _snake_case   	=      [0] * dimension
  _snake_case   	=      1
  return Vector(lowerCAmelCase_	)




def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_	)						->				Vector:
  assert (
      isinstance(lowerCAmelCase_  ,						lowerCAmelCase_	)
      and isinstance(lowerCAmelCase_  ,						lowerCAmelCase_	)
      and (isinstance(lowerCAmelCase_  ,						(int, float)	))
  )
  return x * scalar + y




def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_	)						->				Vector:
  random.seed(lowerCAmelCase_	)
  _snake_case   	=      [random.randint(lowerCAmelCase_  ,						lowerCAmelCase_	) for _ in range(lowerCAmelCase_	)]
  return Vector(lowerCAmelCase_	)



class     UpperCAmelCase    :



   def __init__(       self    :	int							,       __lowerCamelCase    :	list[list[float]]							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     _snake_case   	=      matrix
     _snake_case   	=      w
     _snake_case   	=      h



   def __str__(       self    :	Dict				):



     """simple docstring"""
     _snake_case   	=      ''''''
     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    :	str							,       __lowerCamelCase    :	Matrix				):



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



   def __sub__(       self    :	Optional[int]							,       __lowerCamelCase    :	Matrix				):



     """simple docstring"""
     if self.__width == other.width() and self.__height == other.height():
       _snake_case   	=      []
       for i in range(self.__height				):
         _snake_case   	=      [
             self.__matrix[i][j] - other.component(__lowerCamelCase							,       __lowerCamelCase				)
             for j in range(self.__width				)
         ]
         matrix.append(__lowerCamelCase				)
       return Matrix(__lowerCamelCase							,       self.__width							,       self.__height				)
     else:
       raise Exception('''matrices must have the same dimension!'''				)



   @overload
   def __mul__(       self    :	List[Any]							,       __lowerCamelCase    :	float				):



     """simple docstring"""
     ...



   @overload
   def __mul__(       self    :	Optional[Any]							,       __lowerCamelCase    :	Vector				):



     """simple docstring"""
     ...



   def __mul__(       self    :	Tuple							,       __lowerCamelCase    :	float | Vector				):



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



   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     return self.__height



   def    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     return self.__width



   def    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int				):



     """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    __UpperCAmelCase    (       self    :	Tuple							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	float				):



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



   def    __UpperCAmelCase    (       self    :	str							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     if self.__height != self.__width:
       raise Exception('''Matrix is not square'''				)
     _snake_case   	=      self.__matrix[:x] + self.__matrix[x + 1 :]
     for i in range(len(__lowerCamelCase				)				):
       _snake_case   	=      minor[i][:y] + minor[i][y + 1 :]
     return Matrix(__lowerCamelCase							,       self.__width - 1							,       self.__height - 1				).determinant()



   def    __UpperCAmelCase    (       self    :	Optional[int]							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	int				):



     """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(__lowerCamelCase							,       __lowerCamelCase				)
     else:
       raise Exception('''Indices out of bounds'''				)





   def    __UpperCAmelCase    (       self    :	Optional[int]				):



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




def 			snake_case		(		lowerCAmelCase_	)						->				Matrix:
  _snake_case   	=      [[0] * n for _ in range(lowerCAmelCase_	)]
  return Matrix(lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_	)







def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_	)						->				Matrix:
  random.seed(lowerCAmelCase_	)
  _snake_case   	=      [
      [random.randint(lowerCAmelCase_  ,						lowerCAmelCase_	) for _ in range(lowerCAmelCase_	)] for _ in range(lowerCAmelCase_	)
  ]
  return Matrix(lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_	)









import math

import tensorflow as tf
from packaging import version






def 	__lowerCamelCase						(							lowerCamelCase__		:						Any      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )
			lowerCamelCase    		=  0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0      )  ,							x.dtype      )      ))

			return x * cdf






def 	__lowerCamelCase						(							lowerCamelCase__		:						List[str]      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )
			lowerCamelCase    		=  tf.cast(math.pi  ,							x.dtype      )
			lowerCamelCase    		=  tf.cast(0.0_4_4_7_1_5  ,							x.dtype      )
			lowerCamelCase    		=  0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi      ) * (x + coeff * tf.pow(lowerCamelCase__  ,							3      ))      ))

			return x * cdf






def 	__lowerCamelCase						(							lowerCamelCase__		:						int      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )

			return x * tf.tanh(tf.math.softplus(lowerCamelCase__      )      )






def 	__lowerCamelCase						(							lowerCamelCase__		:						Union[str, Any]      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )
			lowerCamelCase    		=  tf.cast(0.0_4_4_7_1_5  ,							x.dtype      )
			lowerCamelCase    		=  tf.cast(0.7_9_7_8_8_4_5_6_0_8  ,							x.dtype      )

			return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)      ))






def 	__lowerCamelCase						(							lowerCamelCase__		:						Optional[int]      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )
			lowerCamelCase    		=  tf.cast(1.7_0_2  ,							x.dtype      )
			return x * tf.math.sigmoid(coeff * x      )






def 	__lowerCamelCase						(							lowerCamelCase__		:						Dict      ):





			'''simple docstring'''




			return tf.clip_by_value(_gelu(lowerCamelCase__      )  ,							-10  ,							10      )






def 	__lowerCamelCase						(							lowerCamelCase__		:						int  ,							lowerCamelCase__		:						Dict=-1      ):





			'''simple docstring'''




			lowerCamelCase		,		lowerCamelCase    		=  tf.split(lowerCamelCase__  ,							2  ,							axis=lowerCamelCase__      )
			return a * tf.math.sigmoid(lowerCamelCase__      )


if version.parse(tf.version.VERSION) >= version.parse("2.4"):






		def 	__lowerCamelCase						(							lowerCamelCase__		:						Union[str, Any]      ):





					'''simple docstring'''




					return tf.keras.activations.gelu(lowerCamelCase__  ,							approximate=lowerCamelCase__      )

		UpperCAmelCase   :    Union[str, Any]     =		tf.keras.activations.gelu



		UpperCAmelCase   :    int     =		approximate_gelu_wrap
else:
		UpperCAmelCase   :    Dict     =		_gelu
		UpperCAmelCase   :    Dict     =		_gelu_new


UpperCAmelCase   :    List[str]     =		{
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}






def 	__lowerCamelCase						(							lowerCamelCase__		:						Tuple      ):





			'''simple docstring'''




			if activation_string in ACTaFN:
						return ACTaFN[activation_string]
			else:
						raise KeyError(f'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()      )}'      )

