
"""simple docstring"""




from __future__ import annotations



def 				a__			(					lowerCAmelCase						:							str							,     lowerCAmelCase						:							dict[str, str]		):






     '''simple docstring'''
     UpperCAmelCase__				:			str							=    '', '', ''
     UpperCAmelCase__				:			Optional[int]							=    []

     for character in message_part:
          tmp.append(character_to_number[character]		)

     for each in tmp:
          one += each[0]
          two += each[1]
          three += each[2]

     return one + two + three



def 				a__			(					lowerCAmelCase						:							str							,     lowerCAmelCase						:							dict[str, str]		):






     '''simple docstring'''
     UpperCAmelCase__				:			str							=    '', ''
     UpperCAmelCase__				:			int							=    []

     for character in message_part:
          this_part += character_to_number[character]

     for digit in this_part:
          tmp += digit
          if len(lowerCAmelCase__		) == len(lowerCAmelCase__		):
               result.append(lowerCAmelCase__		)
               UpperCAmelCase__				:			str							=    ''

     return result[0], result[1], result[2]



def 				a__			(					lowerCAmelCase						:							str							,     lowerCAmelCase						:							str		):






     '''simple docstring'''
     UpperCAmelCase__				:			Dict							=    alphabet.replace(" "							,     ""		).upper()
     UpperCAmelCase__				:			int							=    message.replace(" "							,     ""		).upper()

     # Check length and characters
     if len(lowerCAmelCase__		) != 27:
          raise KeyError("Length of alphabet has to be 27."		)
     for each in message:
          if each not in alphabet:
               raise ValueError("Each message character has to be included in alphabet!"		)

    # Generate dictionares
     UpperCAmelCase__				:			int							=    (
         '111',
         '112',
         '113',
         '121',
         '122',
         '123',
         '131',
         '132',
         '133',
         '211',
         '212',
         '213',
         '221',
         '222',
         '223',
         '231',
         '232',
         '233',
         '311',
         '312',
         '313',
         '321',
         '322',
         '323',
         '331',
         '332',
         '333',
     )
     UpperCAmelCase__				:			List[str]							=    {}
     UpperCAmelCase__				:			str							=    {}
     for letter, number in zip(lowerCAmelCase__							,     lowerCAmelCase__		):
          UpperCAmelCase__				:			str							=    number
          UpperCAmelCase__				:			Optional[int]							=    letter

     return message, alphabet, character_to_number, number_to_character



def 				a__			(					lowerCAmelCase						:							str							,     lowerCAmelCase						:							str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."							,     lowerCAmelCase						:							int = 5		):






     '''simple docstring'''
     UpperCAmelCase__				:			Optional[Any]							=    __prepare(
         lowerCAmelCase__							,     lowerCAmelCase__		)
     UpperCAmelCase__				:			Tuple							=    '', ''

     for i in range(0							,     len(lowerCAmelCase__		) + 1							,     lowerCAmelCase__		):
          encrypted_numeric += __encrypt_part(
              message[i : i + period]							,     lowerCAmelCase__		)

     for i in range(0							,     len(lowerCAmelCase__		)							,     3		):
          encrypted += number_to_character[encrypted_numeric[i : i + 3]]

     return encrypted



def 				a__			(					lowerCAmelCase						:							str							,     lowerCAmelCase						:							str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."							,     lowerCAmelCase						:							int = 5		):






     '''simple docstring'''
     UpperCAmelCase__				:			List[Any]							=    __prepare(
         lowerCAmelCase__							,     lowerCAmelCase__		)
     UpperCAmelCase__				:			Tuple							=    []
     UpperCAmelCase__				:			int							=    ''

     for i in range(0							,     len(lowerCAmelCase__		) + 1							,     lowerCAmelCase__		):
          UpperCAmelCase__				:			str							=    __decrypt_part(message[i : i + period]							,     lowerCAmelCase__		)

          for j in range(0							,     len(lowerCAmelCase__		)		):
               decrypted_numeric.append(a[j] + b[j] + c[j]		)

     for each in decrypted_numeric:
          decrypted += number_to_character[each]

     return decrypted


if __name__ == "__main__":
    A__   :						List[Any] 							=     """DEFEND THE EAST WALL OF THE CASTLE."""
    A__   :						Any 							=     encrypt_message(msg, """EPSDUCVWYM.ZLKXNBTFGORIJHAQ""")
    A__   :						Any 							=     decrypt_message(encrypted, """EPSDUCVWYM.ZLKXNBTFGORIJHAQ""")
    print(f"""Encrypted: {encrypted}\nDecrypted: {decrypted}""")











from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve



def        _lowerCamelCase(      lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray   ):





				'''simple docstring'''







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

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

				SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= image[grid[:, :, 1], grid[:, :, 0]]

				# Set pixels at invalid locations to 0
				SCREAMING_SNAKE_CASE_ :						Optional[int]    				= 0

				return warped



def        _lowerCamelCase(      lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		SupportsIndex     ,     lowerCAmelCase__					:		float | None = None     ,     ):





				'''simple docstring'''







				if alpha is None:
								SCREAMING_SNAKE_CASE_ :						Any    				= 0.1

				# Initialize flow
				SCREAMING_SNAKE_CASE_ :						List[Any]    				= np.zeros_like(lowerCAmelCase__   )
				SCREAMING_SNAKE_CASE_ :						Any    				= np.zeros_like(lowerCAmelCase__   )

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

								SCREAMING_SNAKE_CASE_ :						List[str]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )

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

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

				return horizontal_flow, vertical_flow


if __name__ == "__main__":
				import doctest

				doctest.testmod()