




import numpy as np

def  UpperCAmelCase_							(				__UpperCAmelCase						:     np.ndarray      ,      __UpperCAmelCase						:     np.ndarray      ,      __UpperCAmelCase						:     float = 1E-12      ,      __UpperCAmelCase						:     int = 1_00      ,      ) ->			tuple[float, np.ndarray]:
      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							)
      SCREAMING_SNAKE_CASE_     						=      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.

      SCREAMING_SNAKE_CASE_     						=      False
      SCREAMING_SNAKE_CASE_     						=      0
      SCREAMING_SNAKE_CASE_     						=      0
      SCREAMING_SNAKE_CASE_     						=      1E12

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

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

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

            SCREAMING_SNAKE_CASE_     						=      lambda_

      if is_complex:
            SCREAMING_SNAKE_CASE_     						=      np.real(lambda_							)

      return lambda_, vector

def  UpperCAmelCase_							(				) ->			None:
      SCREAMING_SNAKE_CASE_     						=      np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]							)
      SCREAMING_SNAKE_CASE_     						=      np.array([41, 4, 20]							)
      SCREAMING_SNAKE_CASE_     						=      real_input_matrix.astype(np.complexaaa							)
      SCREAMING_SNAKE_CASE_     						=      np.triu(1J * complex_input_matrix      ,      1							)
      complex_input_matrix += imag_matrix
      complex_input_matrix += -1 * imag_matrix.T
      SCREAMING_SNAKE_CASE_     						=      np.array([41, 4, 20]							).astype(np.complexaaa							)

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

            # Our implementation.
            SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_     						=      power_iteration(__UpperCAmelCase      ,      __UpperCAmelCase							)

            # Numpy implementation.

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

def  UpperCAmelCase_							(				) ->			None:
      assert nand_gate(0      ,      0							) == 1
      assert nand_gate(0      ,      1							) == 1
      assert nand_gate(1      ,      0							) == 1
      assert nand_gate(1      ,      1							) == 0


if __name__ == "__main__":
   print(nand_gate(0, 0))
   print(nand_gate(0, 1))
   print(nand_gate(1, 0))
   print(nand_gate(1, 1))