



'''simple docstring'''

from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve







def     __UpperCamelCase					(				_lowercase,					_lowercase,					_lowercase       )			->      np.ndarray:
	_lowercase       :							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       :							Dict  	=		np.stack(
	    np.meshgrid(np.arange(0,					image.shape[1]       ),					np.arange(0,					image.shape[0]       )       ),					2       )
	_lowercase       :							Union[str, Any]  	=		np.round(grid - flow       ).astype(np.intaa       )

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

	_lowercase       :							Tuple  	=		image[grid[:, :, 1], grid[:, :, 0]]

	# Set pixels at invalid locations to 0
	_lowercase       :							List[str]  	=		0

	return warped







def     __UpperCamelCase					(				_lowercase,					_lowercase,					_lowercase,					_lowercase = None,					)			->      tuple[np.ndarray, np.ndarray]:
	if alpha is None:
		_lowercase       :							Any  	=		0.1

	# Initialize flow
	_lowercase       :							str  	=		np.zeros_like(_lowercase       )
	_lowercase       :							List[Any]  	=		np.zeros_like(_lowercase       )

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

		_lowercase       :							Optional[int]  	=		convolve(_lowercase,					_lowercase       )
		_lowercase       :							str  	=		convolve(_lowercase,					_lowercase       )

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

		_lowercase       :							Dict  	=		avg_horizontal_velocity - derivative_x * update
		_lowercase       :							str  	=		avg_vertical_velocity - derivative_y * update

	return horizontal_flow, vertical_flow


if __name__ == "__main__":
		import doctest

		doctest.testmod()





'''simple docstring'''

def     __UpperCamelCase					(				_lowercase = 1000       )			->      int:
	_lowercase       :							Dict  	=		0
	_lowercase       :							int  	=		0
	while 1:
		num += 3
		if num >= n:
			break
		total += num
		num += 2
		if num >= n:
			break
		total += num
		num += 1
		if num >= n:
			break
		total += num
		num += 3
		if num >= n:
			break
		total += num
		num += 1
		if num >= n:
			break
		total += num
		num += 2
		if num >= n:
			break
		total += num
		num += 3
		if num >= n:
			break
		total += num
	return total


if __name__ == "__main__":
		print(F'''{solution() = }''')
