
"""simple docstring"""





from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve







def       __a	(						__lowerCamelCase,						__lowerCamelCase,						__lowerCamelCase    ):
   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_   :   Union[str, Any]     	=       np.stack(
       np.meshgrid(np.arange(0,						image.shape[1]    ),						np.arange(0,						image.shape[0]    )    ),						2    )
   UpperCAmelCase_   :   Optional[int]     	=       np.round(grid - flow    ).astype(np.intaa    )

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

   UpperCAmelCase_   :   Union[str, Any]     	=       image[grid[:, :, 1], grid[:, :, 0]]

   # Set pixels at invalid locations to 0
   UpperCAmelCase_   :   Union[str, Any]     	=       0

   return warped







def       __a	(						__lowerCamelCase,						__lowerCamelCase,						__lowerCamelCase,						__lowerCamelCase = None,						):
   if alpha is None:
      UpperCAmelCase_   :   int     	=       0.1

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

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

      UpperCAmelCase_   :   List[Any]     	=       convolve(__lowerCamelCase,						__lowerCamelCase    )
      UpperCAmelCase_   :   Union[str, Any]     	=       convolve(__lowerCamelCase,						__lowerCamelCase    )

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

      UpperCAmelCase_   :   Optional[Any]     	=       avg_horizontal_velocity - derivative_x * update
      UpperCAmelCase_   :   List[Any]     	=       avg_vertical_velocity - derivative_y * update

   return horizontal_flow, vertical_flow


if __name__ == "__main__":
    import doctest

    doctest.testmod()




"""simple docstring"""





import math
from timeit import timeit







def       __a	(						__lowerCamelCase    ):
   UpperCAmelCase_   :   Tuple     	=       0
   UpperCAmelCase_   :   Any     	=       abs(__lowerCamelCase    )
   while True:
      UpperCAmelCase_   :   Optional[int]     	=       n // 10
      digits += 1
      if n == 0:
         break
   return digits







def       __a	(						__lowerCamelCase    ):
   return 1 if n == 0 else math.floor(math.log(abs(__lowerCamelCase    ),						10    ) + 1    )







def       __a	(						__lowerCamelCase    ):
   return len(str(abs(__lowerCamelCase    )    )    )







def       __a	(						):
   from collections.abc import Callable

   def benchmark_a_function(__lowerCamelCase,						__lowerCamelCase    ) -> None:
      UpperCAmelCase_   :   Optional[Any]     	=       f"""{func.__name__}({value})"""
      UpperCAmelCase_   :   Optional[int]     	=       timeit(f"""__main__.{call}""",						setup="import __main__"    )
      print(f"""{call}: {func(__lowerCamelCase    )} -- {timing} seconds"""    )

   for value in (26_2144, 1125_8999_0684_2624, 126_7650_6002_2822_9401_4967_0320_5376):
      for func in (num_digits, num_digits_fast, num_digits_faster):
         benchmark_a_function(__lowerCamelCase,						__lowerCamelCase    )
      print()


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    benchmark()


