





import numpy as np

def       A__(		__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase = 1E-12	,			__lowerCAmelCase = 1_00	,			):
				assert np.shape(__lowerCAmelCase )[0] == np.shape(__lowerCAmelCase )[1]
				# Ensure proper dimensionality.
				assert np.shape(__lowerCAmelCase )[0] == np.shape(__lowerCAmelCase )[0]
				# Ensure inputs are either both complex or both real
				assert np.iscomplexobj(__lowerCAmelCase ) == np.iscomplexobj(__lowerCAmelCase )
				_snake_case :			Union[str, Any]  						=   np.iscomplexobj(__lowerCAmelCase )
				if is_complex:
								# Ensure complex input_matrix is Hermitian
								assert np.array_equal(__lowerCAmelCase	,			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.

				_snake_case :			Optional[Any]  						=   False
				_snake_case :			Optional[int]  						=   0
				_snake_case :			List[Any]  						=   0
				_snake_case :			Dict  						=   1E12

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

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

								if error <= error_tol or iterations >= max_iterations:
												_snake_case :			Any  						=   True

								_snake_case :			int  						=   lambda_

				if is_complex:
								_snake_case :			Optional[int]  						=   np.real(lambda_ )

				return lambda_, vector

def       A__(		):
				_snake_case :			Optional[int]  						=   np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]] )
				_snake_case :			int  						=   np.array([41, 4, 20] )
				_snake_case :			Optional[Any]  						=   real_input_matrix.astype(np.complexaaa )
				_snake_case :			int  						=   np.triu(1J * complex_input_matrix	,			1 )
				complex_input_matrix += imag_matrix
				complex_input_matrix += -1 * imag_matrix.T
				_snake_case :			List[Any]  						=   np.array([41, 4, 20] ).astype(np.complexaaa )

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

								# Our implementation.
								_snake_case						,			_snake_case :			Optional[Any]  						=   power_iteration(__lowerCAmelCase	,			__lowerCAmelCase )

								# Numpy implementation.

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


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		test_power_iteration()













def       A__(		__lowerCAmelCase ):

				if not isinstance(__lowerCAmelCase	,			__lowerCAmelCase ):
								raise TypeError('only integers accepted as input' )
				else:
								_snake_case :			Any  						=   str(abs(__lowerCAmelCase ) )
								_snake_case :			List[str]  						=   [list(__lowerCAmelCase ) for char in range(len(__lowerCAmelCase ) )]
								for index in range(len(__lowerCAmelCase ) ):
												num_transpositions[index].pop(__lowerCAmelCase )
								return max(
								    int(''.join(list(__lowerCAmelCase ) ) ) for transposition in num_transpositions )


if __name__ == "__main__":
		__import__('''doctest''').testmod()






