





'''simple docstring'''






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





      """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												=							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												=							False
      _lowerCAmelCase												=							0
      _lowerCAmelCase												=							0
      _lowerCAmelCase												=							1e12

      while not convergence:
            # Multiple matrix by the vector.
            _lowerCAmelCase												=							np.dot(A__							,     A__  )
            # Normalize the resulting output vector.
            _lowerCAmelCase												=							w / np.linalg.norm(A__  )
            # 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(A__							,     np.dot(A__							,     A__  )  )

            # 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(A__							,     A__  )

            # Numpy implementation.

            # Get eigenvalues and eigenvectors using built-in numpy
            # eigh (eigh used for symmetric or hermetian matrices).
            _lowerCAmelCase					,							_lowerCAmelCase												=							np.linalg.eigh(A__  )
            # 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(A__  ) - np.abs(A__  )  ) <= 1e-6


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   test_power_iteration()













'''simple docstring'''






from pathlib import Path

import cva
import numpy as np
from matplotlib import pyplot as plt
def        UpperCamelCase__				(		lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase  ):





      """simple docstring"""


      _lowerCAmelCase												=							cva.getAffineTransform(lowerCAmelCase							,     lowerCAmelCase  )
      return cva.warpAffine(lowerCAmelCase							,     lowerCAmelCase							,     (rows, cols)  )


if __name__ == "__main__":
      # read original image
      A__    :       Any              =cva.imread(
          str(Path(__file__).resolve().parent.parent / '''image_data''' / '''lena.jpg''')
      )
      # turn image in gray scale value
      A__    :       Any              =cva.cvtColor(image, cva.COLOR_BGR2GRAY)
      # get image shape
      A__       ,						A__    :       Tuple              =gray_img.shape

      # set different points to rotate image
      A__    :       Optional[int]              =np.array([[50, 50], [2_00, 50], [50, 2_00]], np.floataa)
      A__    :       Tuple              =np.array([[10, 1_00], [2_00, 50], [1_00, 2_50]], np.floataa)
      A__    :       List[str]              =np.array([[50, 50], [1_50, 50], [1_20, 2_00]], np.floataa)
      A__    :       Dict              =np.array([[10, 1_00], [80, 50], [1_80, 2_50]], np.floataa)

      # add all rotated images in a list
      A__    :       List[Any]              =[
          gray_img,
          get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
          get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
          get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
      ]

      # plot different image rotations
      A__    :       Tuple              =plt.figure(1)
      A__    :       int              =['''Original''', '''Rotation 1''', '''Rotation 2''', '''Rotation 3''']
      for i, image in enumerate(images):
            plt.subplot(2, 2, i + 1), plt.imshow(image, '''gray''')
            plt.title(titles[i])
            plt.axis('''off''')
            plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95)
      plt.show()






