





import numpy as np





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__			,			UpperCamelCase__			,			UpperCamelCase__ = 1E-12			,			UpperCamelCase__ = 100			,			)  ->				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 						__SCREAMING_SNAKE_CASE  (				)  ->				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()










def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__ )  ->				list[int]:







			'''simple docstring'''

			if length <= 0 or not isinstance(UpperCamelCase__			,			UpperCamelCase__ ):
						raise ValueError('''Length must be a positive integer.''' )
			return [n * (2 * n - 1) for n in range(UpperCamelCase__ )]


if __name__ == "__main__":
				print(hexagonal_numbers(length=5))
				print(hexagonal_numbers(length=10))



