

from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve



def   UpperCamelCase(   lowercase_     ,   lowercase_     ,   lowercase_				)      ->							np.ndarray:





  '''simple docstring'''





  snake_case_					= np.stack((horizontal_flow, vertical_flow)     ,   2				)

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

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

  snake_case_					= image[grid[:, :, 1], grid[:, :, 0]]

  # Set pixels at invalid locations to 0
  snake_case_					= 0

  return warped



def   UpperCamelCase(   lowercase_     ,   lowercase_     ,   lowercase_     ,   lowercase_ = None     ,   )      ->							tuple[np.ndarray, np.ndarray]:





  '''simple docstring'''





  if alpha is None:
    snake_case_					= 0.1

  # Initialize flow
  snake_case_					= np.zeros_like(lowercase_				)
  snake_case_					= np.zeros_like(lowercase_				)

  # Prepare kernels for the calculation of the derivatives and the average velocity
  snake_case_					= np.array([[-1, 1], [-1, 1]]				) * 0.25
  snake_case_					= np.array([[-1, -1], [1, 1]]				) * 0.25
  snake_case_					= np.array([[1, 1], [1, 1]]				) * 0.25
  snake_case_					= 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(lowercase_				):
    snake_case_					= warp(lowercase_     ,   lowercase_     ,   lowercase_				)
    snake_case_					= convolve(lowercase_     ,   lowercase_				) + convolve(lowercase_     ,   lowercase_				)
    snake_case_					= convolve(lowercase_     ,   lowercase_				) + convolve(lowercase_     ,   lowercase_				)
    snake_case_					= convolve(lowercase_     ,   lowercase_				) + convolve(lowercase_     ,   -kernel_t				)

    snake_case_					= convolve(lowercase_     ,   lowercase_				)
    snake_case_					= convolve(lowercase_     ,   lowercase_				)

    # This updates the flow as proposed in the paper (Step 12)
    snake_case_					= (
        derivative_x * avg_horizontal_velocity
        + derivative_y * avg_vertical_velocity
        + derivative_t
    )
    snake_case_					= update / (alpha**2 + derivative_x**2 + derivative_y**2)

    snake_case_					= avg_horizontal_velocity - derivative_x * update
    snake_case_					= avg_vertical_velocity - derivative_y * update

  return horizontal_flow, vertical_flow


if __name__ == "__main__":
    import doctest

    doctest.testmod()


def   UpperCamelCase(   lowercase_ = 100     ,   lowercase_ = 100				)      ->							int:





  '''simple docstring'''





  return max(
      sum(int(lowercase_				) for x in str(base**power				)				)
      for base in range(lowercase_				)
      for power in range(lowercase_				)				)


# Tests
if __name__ == "__main__":
    import doctest

    doctest.testmod()