
"""simple docstring"""

from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_A						=			{"""configuration_focalnet""": ["""FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FocalNetConfig"""]}


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _A						=			[
         """FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """FocalNetForImageClassification""",
         """FocalNetForMaskedImageModeling""",
         """FocalNetBackbone""",
         """FocalNetModel""",
         """FocalNetPreTrainedModel""",
     ]

if TYPE_CHECKING:
     from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_focalnet import (
              FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST,
              FocalNetBackbone,
              FocalNetForImageClassification,
              FocalNetForMaskedImageModeling,
              FocalNetModel,
              FocalNetPreTrainedModel,
          )

else:
     import sys

     _A						=			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)








"""simple docstring"""

import numpy as np
def 		UpperCAmelCase (		a_,						a_,						a_ = 1E-12,						a_ = 100,						):





		'''simple docstring'''


		assert np.shape(a_      )[0] == np.shape(a_      )[1]
		# Ensure proper dimensionality.
		assert np.shape(a_      )[0] == np.shape(a_      )[0]
		# Ensure inputs are either both complex or both real
		assert np.iscomplexobj(a_      ) == np.iscomplexobj(a_      )
		lowerCamelCase					:      Optional[int]          =   np.iscomplexobj(a_      )
		if is_complex:
				# Ensure complex input_matrix is Hermitian
				assert np.array_equal(a_,						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.

		lowerCamelCase					:      Union[str, Any]          =   False
		lowerCamelCase					:      List[str]          =   0
		lowerCamelCase					:      Any          =   0
		lowerCamelCase					:      Dict          =   1E12

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

				# Check convergence.
				lowerCamelCase					:      Optional[int]          =   np.abs(lambda_ - lambda_previous      ) / lambda_
				iterations += 1

				if error <= error_tol or iterations >= max_iterations:
						lowerCamelCase					:      int          =   True

				lowerCamelCase					:      Optional[Any]          =   lambda_

		if is_complex:
				lowerCamelCase					:      Any          =   np.real(lambda_      )

		return lambda_, vector
def 		UpperCAmelCase (		):





		'''simple docstring'''


		lowerCamelCase					:      str          =   np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]      )
		lowerCamelCase					:      str          =   np.array([41, 4, 20]      )
		lowerCamelCase					:      Optional[Any]          =   real_input_matrix.astype(np.complexaaa      )
		lowerCamelCase					:      Dict          =   np.triu(1j * complex_input_matrix,						1      )
		complex_input_matrix += imag_matrix
		complex_input_matrix += -1 * imag_matrix.T
		lowerCamelCase					:      List[Any]          =   np.array([41, 4, 20]      ).astype(np.complexaaa      )

		for problem_type in ["real", "complex"]:
				if problem_type == "real":
						lowerCamelCase					:      str          =   real_input_matrix
						lowerCamelCase					:      Any          =   real_vector
				elif problem_type == "complex":
						lowerCamelCase					:      str          =   complex_input_matrix
						lowerCamelCase					:      Dict          =   complex_vector

				# Our implementation.
				lowerCamelCase     ,   lowerCamelCase					:      List[str]          =   power_iteration(a_,						a_      )

				# Numpy implementation.

				# Get eigenvalues and eigenvectors using built-in numpy
				# eigh (eigh used for symmetric or hermetian matrices).
				lowerCamelCase     ,   lowerCamelCase					:      Optional[Any]          =   np.linalg.eigh(a_      )
				# Last eigenvalue is the maximum one.
				lowerCamelCase					:      Dict          =   eigen_values[-1]
				# Last column in this matrix is eigenvector corresponding to largest eigenvalue.
				lowerCamelCase					:      List[str]          =   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(a_      ) - np.abs(a_      )      ) <= 1E-6


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					test_power_iteration()






