
"""simple docstring"""


import math

import tensorflow as tf
from packaging import version



def 						lowerCamelCase__      (				UpperCAmelCase_					)->				List[str]:







       """simple docstring"""
       UpperCamelCase       			=		tf.convert_to_tensor(UpperCAmelCase_					)
       UpperCamelCase       			=		0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0					)		,   x.dtype					)					))

       return x * cdf



def 						lowerCamelCase__      (				UpperCAmelCase_					)->				int:







       """simple docstring"""
       UpperCamelCase       			=		tf.convert_to_tensor(UpperCAmelCase_					)
       UpperCamelCase       			=		tf.cast(math.pi		,   x.dtype					)
       UpperCamelCase       			=		tf.cast(0.04_4715		,   x.dtype					)
       UpperCamelCase       			=		0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi					) * (x + coeff * tf.pow(UpperCAmelCase_		,   3					))					))

       return x * cdf



def 						lowerCamelCase__      (				UpperCAmelCase_					)->				int:







       """simple docstring"""
       UpperCamelCase       			=		tf.convert_to_tensor(UpperCAmelCase_					)

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



def 						lowerCamelCase__      (				UpperCAmelCase_					)->				Optional[int]:







       """simple docstring"""
       UpperCamelCase       			=		tf.convert_to_tensor(UpperCAmelCase_					)
       UpperCamelCase       			=		tf.cast(0.04_4715		,   x.dtype					)
       UpperCamelCase       			=		tf.cast(0.79_7884_5608		,   x.dtype					)

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



def 						lowerCamelCase__      (				UpperCAmelCase_					)->				Tuple:







       """simple docstring"""
       UpperCamelCase       			=		tf.convert_to_tensor(UpperCAmelCase_					)
       UpperCamelCase       			=		tf.cast(1.702		,   x.dtype					)
       return x * tf.math.sigmoid(coeff * x					)



def 						lowerCamelCase__      (				UpperCAmelCase_					)->				Dict:







       """simple docstring"""
       return tf.clip_by_value(_gelu(UpperCAmelCase_					)		,   -10		,   10					)



def 						lowerCamelCase__      (				UpperCAmelCase_		,   UpperCAmelCase_=-1					)->				List[str]:







       """simple docstring"""
       UpperCamelCase			,							UpperCamelCase       			=		tf.split(UpperCAmelCase_		,   2		,   axis=UpperCAmelCase_					)
       return a * tf.math.sigmoid(UpperCAmelCase_					)


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



      def 						lowerCamelCase__      (				UpperCAmelCase_					)->				Tuple:







             """simple docstring"""
             return tf.keras.activations.gelu(UpperCAmelCase_		,   approximate=UpperCAmelCase_					)

      SCREAMING_SNAKE_CASE			  =  tf.keras.activations.gelu

      SCREAMING_SNAKE_CASE			  =  approximate_gelu_wrap
else:
      SCREAMING_SNAKE_CASE			  =  _gelu
      SCREAMING_SNAKE_CASE			  =  _gelu_new


SCREAMING_SNAKE_CASE			  =  {
    """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__      (				UpperCAmelCase_					)->				List[str]:







       """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()					)}"					)







"""simple docstring"""


import numpy as np



def 						lowerCamelCase__      (				UpperCAmelCase_		,   UpperCAmelCase_		,   UpperCAmelCase_ = 1E-12		,   UpperCAmelCase_ = 1_00		,   )->				tuple[float, np.ndarray]:







       """simple docstring"""
       assert np.shape(UpperCAmelCase_					)[0] == np.shape(UpperCAmelCase_					)[1]
       # Ensure proper dimensionality.
       assert np.shape(UpperCAmelCase_					)[0] == np.shape(UpperCAmelCase_					)[0]
       # Ensure inputs are either both complex or both real
       assert np.iscomplexobj(UpperCAmelCase_					) == np.iscomplexobj(UpperCAmelCase_					)
       UpperCamelCase       			=		np.iscomplexobj(UpperCAmelCase_					)
       if is_complex:
              # Ensure complex input_matrix is Hermitian
              assert np.array_equal(UpperCAmelCase_		,   input_matrix.conj().T					)

       # Set convergence to False. Will define convergence when we exceed max_iterations
       # or when we have small changes from one iteration to next.

       UpperCamelCase       			=		False
       UpperCamelCase       			=		0
       UpperCamelCase       			=		0
       UpperCamelCase       			=		1E12

       while not convergence:
              # Multiple matrix by the vector.
              UpperCamelCase       			=		np.dot(UpperCAmelCase_		,   UpperCAmelCase_					)
              # Normalize the resulting output vector.
              UpperCamelCase       			=		w / np.linalg.norm(UpperCAmelCase_					)
              # Find rayleigh quotient
              # (faster than usual b/c we know vector is normalized already)
              UpperCamelCase       			=		vector.conj().T if is_complex else vector.T
              UpperCamelCase       			=		np.dot(UpperCAmelCase_		,   np.dot(UpperCAmelCase_		,   UpperCAmelCase_					)					)

              # Check convergence.
              UpperCamelCase       			=		np.abs(lambda_ - lambda_previous					) / lambda_
              iterations += 1

              if error <= error_tol or iterations >= max_iterations:
                     UpperCamelCase       			=		True

              UpperCamelCase       			=		lambda_

       if is_complex:
              UpperCamelCase       			=		np.real(lambda_					)

       return lambda_, vector



def 						lowerCamelCase__      (				)->				None:







       """simple docstring"""
       UpperCamelCase       			=		np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]					)
       UpperCamelCase       			=		np.array([41, 4, 20]					)
       UpperCamelCase       			=		real_input_matrix.astype(np.complexaaa					)
       UpperCamelCase       			=		np.triu(1j * complex_input_matrix		,   1					)
       complex_input_matrix += imag_matrix
       complex_input_matrix += -1 * imag_matrix.T
       UpperCamelCase       			=		np.array([41, 4, 20]					).astype(np.complexaaa					)

       for problem_type in ["real", "complex"]:
              if problem_type == "real":
                     UpperCamelCase       			=		real_input_matrix
                     UpperCamelCase       			=		real_vector
              elif problem_type == "complex":
                     UpperCamelCase       			=		complex_input_matrix
                     UpperCamelCase       			=		complex_vector

              # Our implementation.
              UpperCamelCase			,							UpperCamelCase       			=		power_iteration(UpperCAmelCase_		,   UpperCAmelCase_					)

              # Numpy implementation.

              # Get eigenvalues and eigenvectors using built-in numpy
              # eigh (eigh used for symmetric or hermetian matrices).
              UpperCamelCase			,							UpperCamelCase       			=		np.linalg.eigh(UpperCAmelCase_					)
              # Last eigenvalue is the maximum one.
              UpperCamelCase       			=		eigen_values[-1]
              # Last column in this matrix is eigenvector corresponding to largest eigenvalue.
              UpperCamelCase       			=		eigen_vectors[:, -1]

              # Check our implementation and numpy gives close answers.
              assert np.abs(eigen_value - eigen_value_max					) <= 1E-6
              # Take absolute values element wise of each eigenvector.
              # as they are only unique to a minus sign.
              assert np.linalg.norm(np.abs(UpperCAmelCase_					) - np.abs(UpperCAmelCase_					)					) <= 1E-6


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      test_power_iteration()





