
from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve




def 		A						(  SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE							):




			"""simple docstring"""


			UpperCAmelCase__		:Union[str, 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
			UpperCAmelCase__		:int 		=    np.stack(
			    np.meshgrid(np.arange(0     ,						image.shape[1]							)     ,						np.arange(0     ,						image.shape[0]							)							)     ,						2							)
			UpperCAmelCase__		:List[str] 		=    np.round(grid - flow							).astype(np.intaa							)

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

			UpperCAmelCase__		:Dict 		=    image[grid[:, :, 1], grid[:, :, 0]]

			# Set pixels at invalid locations to 0
			UpperCAmelCase__		:Dict 		=    0

			return warped




def 		A						(  SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE = None     ,						):




			"""simple docstring"""


			if alpha is None:
						UpperCAmelCase__		:List[Any] 		=    0.1

			# Initialize flow
			UpperCAmelCase__		:Optional[Any] 		=    np.zeros_like(__SCREAMING_SNAKE_CASE							)
			UpperCAmelCase__		:Optional[Any] 		=    np.zeros_like(__SCREAMING_SNAKE_CASE							)

			# Prepare kernels for the calculation of the derivatives and the average velocity
			UpperCAmelCase__		:Optional[int] 		=    np.array([[-1, 1], [-1, 1]]							) * 0.25
			UpperCAmelCase__		:Tuple 		=    np.array([[-1, -1], [1, 1]]							) * 0.25
			UpperCAmelCase__		:Any 		=    np.array([[1, 1], [1, 1]]							) * 0.25
			UpperCAmelCase__		:Optional[Any] 		=    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(__SCREAMING_SNAKE_CASE							):
						UpperCAmelCase__		:str 		=    warp(__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE							)
						UpperCAmelCase__		:str 		=    convolve(__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE							) + convolve(__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE							)
						UpperCAmelCase__		:Tuple 		=    convolve(__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE							) + convolve(__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE							)
						UpperCAmelCase__		:List[str] 		=    convolve(__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE							) + convolve(__SCREAMING_SNAKE_CASE     ,						-kernel_t							)

						UpperCAmelCase__		:int 		=    convolve(__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE							)
						UpperCAmelCase__		:str 		=    convolve(__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE							)

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

						UpperCAmelCase__		:Union[str, Any] 		=    avg_horizontal_velocity - derivative_x * update
						UpperCAmelCase__		:str 		=    avg_vertical_velocity - derivative_y * update

			return horizontal_flow, vertical_flow


if __name__ == "__main__":
						import doctest

						doctest.testmod()







'''simple docstring'''
import itertools

def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE = 1_0_0_0_0_0_0							):
    """simple docstring"""






    snake_case_			:     Optional[int]        =    [1]

    for i in itertools.count(len(__SCREAMING_SNAKE_CASE							)							):
        snake_case_			:     str        =    0
        for j in itertools.count(1							):
            snake_case_			:     List[str]        =    -1 if j % 2 == 0 else +1
            snake_case_			:     str        =    (j * j * 3 - j) // 2
            if index > i:
                break
            item += partitions[i - index] * sign
            item %= number
            index += j
            if index > i:
                break
            item += partitions[i - index] * sign
            item %= number

        if item == 0:
            return i
        partitions.append(__SCREAMING_SNAKE_CASE							)

    return 0


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  print(f"""{solution() = }""")




