






"""simple docstring"""





from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve






def 							A(    snake_case_		,  snake_case_		,  snake_case_						):
       """simple docstring"""





       lowercase__:							List[Any]         =     np.stack((horizontal_flow, vertical_flow)		,  2						)

       # Create a grid of all pixel coordinates and subtract the flow to get the
       # target pixels coordinates
       lowercase__:							Union[str, Any]         =     np.stack(
           np.meshgrid(np.arange(0		,  image.shape[1]						)		,  np.arange(0		,  image.shape[0]						)						)		,  2						)
       lowercase__:							Dict         =     np.round(grid - flow						).astype(np.intaa						)

       # Find the locations outside of the original image
       lowercase__:							Optional[int]         =     (grid < 0) | (grid >= np.array([image.shape[1], image.shape[0]]						))
       lowercase__:							List[Any]         =     0

       lowercase__:							int         =     image[grid[:, :, 1], grid[:, :, 0]]

       # Set pixels at invalid locations to 0
       lowercase__:							Tuple         =     0

       return warped






def 							A(    snake_case_		,  snake_case_		,  snake_case_		,  snake_case_ = None		,  ):
       """simple docstring"""





       if alpha is None:
              lowercase__:							Optional[int]         =     0.1

       # Initialize flow
       lowercase__:							Union[str, Any]         =     np.zeros_like(snake_case_						)
       lowercase__:							List[Any]         =     np.zeros_like(snake_case_						)

       # Prepare kernels for the calculation of the derivatives and the average velocity
       lowercase__:							int         =     np.array([[-1, 1], [-1, 1]]						) * 0.25
       lowercase__:							Tuple         =     np.array([[-1, -1], [1, 1]]						) * 0.25
       lowercase__:							Dict         =     np.array([[1, 1], [1, 1]]						) * 0.25
       lowercase__:							List[str]         =     np.array(
           [[1 / 12, 1 / 6, 1 / 12], [1 / 6, 0, 1 / 6], [1 / 12, 1 / 6, 1 / 12]]						)

       # Iteratively refine the flow
       for _ in range(snake_case_						):
              lowercase__:							Any         =     warp(snake_case_		,  snake_case_		,  snake_case_						)
              lowercase__:							Dict         =     convolve(snake_case_		,  snake_case_						) + convolve(snake_case_		,  snake_case_						)
              lowercase__:							Any         =     convolve(snake_case_		,  snake_case_						) + convolve(snake_case_		,  snake_case_						)
              lowercase__:							Dict         =     convolve(snake_case_		,  snake_case_						) + convolve(snake_case_		,  -kernel_t						)

              lowercase__:							str         =     convolve(snake_case_		,  snake_case_						)
              lowercase__:							List[Any]         =     convolve(snake_case_		,  snake_case_						)

              # This updates the flow as proposed in the paper (Step 12)
              lowercase__:							Optional[Any]         =     (
                  derivative_x * avg_horizontal_velocity
                  + derivative_y * avg_vertical_velocity
                  + derivative_t
              )
              lowercase__:							Any         =     update / (alpha**2 + derivative_x**2 + derivative_y**2)

              lowercase__:							Optional[int]         =     avg_horizontal_velocity - derivative_x * update
              lowercase__:							List[str]         =     avg_vertical_velocity - derivative_y * update

       return horizontal_flow, vertical_flow


if __name__ == "__main__":
  import doctest

  doctest.testmod()









import timeit


class 					A		:







    """simple docstring"""





    def __init__( self					:			Optional[int],lowercase_					:			Union[str, Any]						)->       List[str]:





        '''simple docstring'''







        if isinstance(lowercase_,lowercase_						):  # Initializes a matrix identical to the one provided.
            A__        =  arg
            A__        =  len(lowercase_						)
        else:  # Initializes a square matrix of the given size and set values to zero.
            A__        =  arg
            A__        =  [[0 for _ in range(self.n						)] for _ in range(self.n						)]


    def __mul__( self					:			Union[str, Any],lowercase_					:			List[Any]						)->       List[str]:





        '''simple docstring'''







        A__        =  Matrix(self.n						)
        for i in range(self.n						):
            for j in range(self.n						):
                for k in range(self.n						):
                    matrix.t[i][j] += self.t[i][k] * b.t[k][j]
        return matrix




def 		_snake_case(       SCREAMING_SNAKE_CASE__     :      List[Any]       ,     SCREAMING_SNAKE_CASE__     :      Optional[Any] )				-> Union[str, Any]:





    '''simple docstring'''






    A__        =  Matrix([[1, 0], [0, 1]] )
    while b > 0:
        if b & 1:
            matrix *= a
        a *= a
        b >>= 1
    return matrix
def 		_snake_case(       SCREAMING_SNAKE_CASE__     :      Optional[int]       ,     SCREAMING_SNAKE_CASE__     :      str       ,     SCREAMING_SNAKE_CASE__     :      Any )				-> List[str]:





    '''simple docstring'''






    if n == 1:
        return fa
    elif n == 2:
        return fa
    A__        =  Matrix([[1, 1], [1, 0]] )
    A__        =  modular_exponentiation(SCREAMING_SNAKE_CASE__       ,     n - 2 )
    return fa * matrix.t[0][0] + fa * matrix.t[0][1]
def 		_snake_case(       SCREAMING_SNAKE_CASE__     :      Dict       ,     SCREAMING_SNAKE_CASE__     :      Tuple       ,     SCREAMING_SNAKE_CASE__     :      Tuple )				-> Dict:





    '''simple docstring'''






    if n == 1:
        return fa
    elif n == 2:
        return fa

    A__        =  fa
    A__        =  fa
    n -= 2

    while n > 0:
        A__ ,		A__        =  fn_a + fn_a, fn_a
        n -= 1

    return fn_a
def 		_snake_case(       )				-> Optional[Any]:





    '''simple docstring'''






    A__        =  '\nfrom random import randint\nfrom __main__ import fibonacci_with_matrix_exponentiation\n'
    A__        =  'fibonacci_with_matrix_exponentiation(randint(1,70000), 1, 1)'
    A__        =  timeit.timeit(setup=SCREAMING_SNAKE_CASE__       ,     stmt=SCREAMING_SNAKE_CASE__       ,     number=100 )
    print('With matrix exponentiation the average execution time is '       ,     exec_time / 100 )
    return exec_time
def 		_snake_case(       )				-> Optional[int]:





    '''simple docstring'''






    A__        =  '\nfrom random import randint\nfrom __main__ import simple_fibonacci\n'
    A__        =  'simple_fibonacci(randint(1,70000), 1, 1)'
    A__        =  timeit.timeit(setup=SCREAMING_SNAKE_CASE__       ,     stmt=SCREAMING_SNAKE_CASE__       ,     number=100 )
    print(
        'Without matrix exponentiation the average execution time is '       ,     exec_time / 100 )
    return exec_time
def 		_snake_case(       )				-> Union[str, Any]:





    '''simple docstring'''






    matrix_exponentiation_time()
    simple_fibonacci_time()


if __name__ == "__main__":
     main()

