

'''simple docstring'''






import numpy as np
def        UpperCamelCase__				(		lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase = 1e-12							,     lowerCAmelCase = 1_00							,     ):





      """simple docstring"""


      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  )
      _lowerCAmelCase												=							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.

      _lowerCAmelCase												=							False
      _lowerCAmelCase												=							0
      _lowerCAmelCase												=							0
      _lowerCAmelCase												=							1e12

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

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

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

            _lowerCAmelCase												=							lambda_

      if is_complex:
            _lowerCAmelCase												=							np.real(lambda_  )

      return lambda_, vector
def        UpperCamelCase__				(		):





      """simple docstring"""


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

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

            # Our implementation.
            _lowerCAmelCase					,							_lowerCAmelCase												=							power_iteration(lowerCAmelCase							,     lowerCAmelCase  )

            # Numpy implementation.

            # Get eigenvalues and eigenvectors using built-in numpy
            # eigh (eigh used for symmetric or hermetian matrices).
            _lowerCAmelCase					,							_lowerCAmelCase												=							np.linalg.eigh(lowerCAmelCase  )
            # Last eigenvalue is the maximum one.
            _lowerCAmelCase												=							eigen_values[-1]
            # Last column in this matrix is eigenvector corresponding to largest eigenvalue.
            _lowerCAmelCase												=							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()













'''simple docstring'''






# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

A__    :       List[str]              =TypeVar('''T''')




class 			UpperCAmelCase					(      Generic[T]       ):




      def __init__(      self      :			Tuple      ,      __snake_case      :			bool = True  )    ->				None:
            _lowerCAmelCase												=							{}  # dictionary of lists
            _lowerCAmelCase												=							directed




      def     lowercase__   (      self      :			Union[str, Any]      ,      __snake_case      :			T      ,      __snake_case      :			T  )    ->				GraphAdjacencyList[T]:

            if not self.directed:  # For undirected graphs
                  # if both source vertex and destination vertex are both present in the
                  # adjacency list, add destination vertex to source vertex list of adjacent
                  # vertices and add source vertex to destination vertex list of adjacent
                  # vertices.
                  if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                        self.adj_list[source_vertex].append(__snake_case  )
                        self.adj_list[destination_vertex].append(__snake_case  )
                  # if only source vertex is present in adjacency list, add destination vertex
                  # to source vertex list of adjacent vertices, then create a new vertex with
                  # destination vertex as key and assign a list containing the source vertex
                  # as it's first adjacent vertex.
                  elif source_vertex in self.adj_list:
                        self.adj_list[source_vertex].append(__snake_case  )
                        _lowerCAmelCase												=							[source_vertex]
                  # if only destination vertex is present in adjacency list, add source vertex
                  # to destination vertex list of adjacent vertices, then create a new vertex
                  # with source vertex as key and assign a list containing the source vertex
                  # as it's first adjacent vertex.
                  elif destination_vertex in self.adj_list:
                        self.adj_list[destination_vertex].append(__snake_case  )
                        _lowerCAmelCase												=							[destination_vertex]
                  # if both source vertex and destination vertex are not present in adjacency
                  # list, create a new vertex with source vertex as key and assign a list
                  # containing the destination vertex as it's first adjacent vertex also
                  # create a new vertex with destination vertex as key and assign a list
                  # containing the source vertex as it's first adjacent vertex.
                  else:
                        _lowerCAmelCase												=							[destination_vertex]
                        _lowerCAmelCase												=							[source_vertex]
            else:  # For directed graphs
                  # if both source vertex and destination vertex are present in adjacency
                  # list, add destination vertex to source vertex list of adjacent vertices.
                  if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                        self.adj_list[source_vertex].append(__snake_case  )
                  # if only source vertex is present in adjacency list, add destination
                  # vertex to source vertex list of adjacent vertices and create a new vertex
                  # with destination vertex as key, which has no adjacent vertex
                  elif source_vertex in self.adj_list:
                        self.adj_list[source_vertex].append(__snake_case  )
                        _lowerCAmelCase												=							[]
                  # if only destination vertex is present in adjacency list, create a new
                  # vertex with source vertex as key and assign a list containing destination
                  # vertex as first adjacent vertex
                  elif destination_vertex in self.adj_list:
                        _lowerCAmelCase												=							[destination_vertex]
                  # if both source vertex and destination vertex are not present in adjacency
                  # list, create a new vertex with source vertex as key and a list containing
                  # destination vertex as it's first adjacent vertex. Then create a new vertex
                  # with destination vertex as key, which has no adjacent vertex
                  else:
                        _lowerCAmelCase												=							[destination_vertex]
                        _lowerCAmelCase												=							[]

            return self




      def __repr__(      self      :			int  )    ->				str:
            return pformat(self.adj_list  )






