


import string

import numpy







def  SCREAMING_SNAKE_CASE_		(    __A				:    int						,					__A				:    int       )			->	int:

       """simple docstring"""

       return b if a == 0 else greatest_common_divisor(b % a						,					__A       )
class 				SCREAMING_SNAKE_CASE__						:
      snake_case__       :  Dict       =		string.ascii_uppercase + string.digits
      # This cipher takes alphanumerics into account
      # i.e. a total of 36 characters

      # take x and return x % len(key_string)
      snake_case__       :  int       =		numpy.vectorize(lambda lowercase__    : x % 36    )

      snake_case__       :  Dict       =		numpy.vectorize(lowercase__    )
      def __init__( self       : Tuple  ,							SCREAMING_SNAKE_CASE__       : numpy.ndarray  )	->    None:
             a_      :	Optional[int]							=						self.modulus(SCREAMING_SNAKE_CASE__  )  # mod36 calc's on the encrypt key
             self.check_determinant()  # validate the determinant of the encryption key
             a_      :	List[Any]							=						encrypt_key.shape[0]
      def  SCREAMING_SNAKE_CASE							( self       : str  ,							SCREAMING_SNAKE_CASE__       : str  )	->    int:
             return self.key_string.index(SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : int  ,							SCREAMING_SNAKE_CASE__       : int  )	->    str:
             return self.key_string[round(SCREAMING_SNAKE_CASE__  )]
      def  SCREAMING_SNAKE_CASE							( self       : str  )	->    None:
             a_      :	Tuple							=						round(numpy.linalg.det(self.encrypt_key  )  )

             if det < 0:
                    a_      :	List[str]							=						det % len(self.key_string  )

             a_      :	List[Any]							=						len(self.key_string  )
             if greatest_common_divisor(SCREAMING_SNAKE_CASE__  ,							len(self.key_string  )  ) != 1:
                    a_      :	Tuple							=						(
                        F"""determinant modular {req_l} of encryption key({det}) """
                        F"""is not co prime w.r.t {req_l}.\nTry another key."""
                    )
                    raise ValueError(SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : int  ,							SCREAMING_SNAKE_CASE__       : str  )	->    str:
             a_      :	Tuple							=						[char for char in text.upper() if char in self.key_string]

             a_      :	Any							=						chars[-1]
             while len(SCREAMING_SNAKE_CASE__  ) % self.break_key != 0:
                    chars.append(SCREAMING_SNAKE_CASE__  )

             return "".join(SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Any  ,							SCREAMING_SNAKE_CASE__       : str  )	->    str:
             a_      :	str							=						self.process_text(text.upper()  )
             a_      :	Optional[int]							=						''

             for i in range(0  ,							len(SCREAMING_SNAKE_CASE__  ) - self.break_key + 1  ,							self.break_key  ):
                    a_      :	Tuple							=						text[i : i + self.break_key]
                    a_      :	Dict							=						[self.replace_letters(SCREAMING_SNAKE_CASE__  ) for char in batch]
                    a_      :	List[str]							=						numpy.array([vec]  ).T
                    a_      :	Optional[Any]							=						self.modulus(self.encrypt_key.dot(SCREAMING_SNAKE_CASE__  )  ).T.tolist()[
                        0
                    ]
                    a_      :	Union[str, Any]							=						''.join(
                        self.replace_digits(SCREAMING_SNAKE_CASE__  ) for num in batch_encrypted  )
                    encrypted += encrypted_batch

             return encrypted
      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  )	->    numpy.ndarray:
             a_      :	Optional[Any]							=						round(numpy.linalg.det(self.encrypt_key  )  )

             if det < 0:
                    a_      :	List[str]							=						det % len(self.key_string  )
             a_      :	Optional[Any]							=						None
             for i in range(len(self.key_string  )  ):
                    if (det * i) % len(self.key_string  ) == 1:
                           a_      :	List[Any]							=						i
                           break

             a_      :	Dict							=						(
                 det_inv
                 * numpy.linalg.det(self.encrypt_key  )
                 * numpy.linalg.inv(self.encrypt_key  )
             )

             return self.to_int(self.modulus(SCREAMING_SNAKE_CASE__  )  )

      def  SCREAMING_SNAKE_CASE							( self       : List[str]  ,							SCREAMING_SNAKE_CASE__       : str  )	->    str:
             a_      :	Any							=						self.make_decrypt_key()
             a_      :	Dict							=						self.process_text(text.upper()  )
             a_      :	List[str]							=						''

             for i in range(0  ,							len(SCREAMING_SNAKE_CASE__  ) - self.break_key + 1  ,							self.break_key  ):
                    a_      :	Optional[Any]							=						text[i : i + self.break_key]
                    a_      :	Dict							=						[self.replace_letters(SCREAMING_SNAKE_CASE__  ) for char in batch]
                    a_      :	Union[str, Any]							=						numpy.array([vec]  ).T
                    a_      :	Optional[Any]							=						self.modulus(decrypt_key.dot(SCREAMING_SNAKE_CASE__  )  ).T.tolist()[0]
                    a_      :	List[str]							=						''.join(
                        self.replace_digits(SCREAMING_SNAKE_CASE__  ) for num in batch_decrypted  )
                    decrypted += decrypted_batch

             return decrypted







def  SCREAMING_SNAKE_CASE_		(    )			->	None:

       """simple docstring"""

       a_      :	Any							=						int(input('Enter the order of the encryption key: '       )       )
       a_      :	Optional[int]							=						[]

       print('Enter each row of the encryption key with space separated integers'       )
       for _ in range(__A       ):
              a_      :	Dict							=						[int(__A       ) for x in input().split()]
              hill_matrix.append(__A       )

       a_      :	List[str]							=						HillCipher(numpy.array(__A       )       )

       print('Would you like to encrypt or decrypt some text? (1 or 2)'       )
       a_      :	Tuple							=						input('\n1. Encrypt\n2. Decrypt\n'       )
       if option == "1":
              a_      :	Union[str, Any]							=						input('What text would you like to encrypt?: '       )
              print('Your encrypted text is:'       )
              print(hc.encrypt(__A       )       )
       elif option == "2":
              a_      :	Any							=						input('What text would you like to decrypt?: '       )
              print('Your decrypted text is:'       )
              print(hc.decrypt(__A       )       )


if __name__ == "__main__":
   import doctest

   doctest.testmod()

   main()






import unittest

import numpy as np







def  SCREAMING_SNAKE_CASE_		(    __A				:    np.ndarray						,					__A				:    np.ndarray						,					__A				:    np.ndarray						,					__A				:    np.ndarray | None = None						,					)			->	np.ndarray:

       """simple docstring"""

       a_      :	List[str]							=						np.shape(__A       )
       a_      :	Any							=						np.shape(__A       )
       a_      :	List[Any]							=						np.shape(__A       )

       if shape_a[0] != shape_b[0]:
              a_      :	List[Any]							=						(
                  'Expected the same number of rows for A and B. '
                  F"""Instead found A of size {shape_a} and B of size {shape_b}"""
              )
              raise ValueError(__A       )

       if shape_b[1] != shape_c[1]:
              a_      :	int							=						(
                  'Expected the same number of columns for B and C. '
                  F"""Instead found B of size {shape_b} and C of size {shape_c}"""
              )
              raise ValueError(__A       )

       a_      :	Any							=						pseudo_inv
       if a_inv is None:
              try:
                     a_      :	List[str]							=						np.linalg.inv(__A       )
              except np.linalg.LinAlgError:
                     raise ValueError(
                         'Input matrix A is not invertible. Cannot compute Schur complement.'       )

       return mat_c - mat_b.T @ a_inv @ mat_b
class 				SCREAMING_SNAKE_CASE__						(       unittest.TestCase    ):
      def  SCREAMING_SNAKE_CASE							( self       : Tuple  )	->    None:
             a_      :	str							=						np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]  )
             a_      :	Any							=						np.array([[0, 3], [3, 0], [2, 3]]  )
             a_      :	List[str]							=						np.array([[2, 1], [6, 3]]  )

             a_      :	int							=						schur_complement(SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  )

             a_      :	Optional[Any]							=						np.block([[a, b], [b.T, c]]  )

             a_      :	str							=						np.linalg.det(SCREAMING_SNAKE_CASE__  )
             a_      :	List[Any]							=						np.linalg.det(SCREAMING_SNAKE_CASE__  )
             a_      :	str							=						np.linalg.det(SCREAMING_SNAKE_CASE__  )

             self.assertAlmostEqual(SCREAMING_SNAKE_CASE__  ,							det_a * det_s  )
      def  SCREAMING_SNAKE_CASE							( self       : Optional[Any]  )	->    None:
             a_      :	Optional[int]							=						np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]  )
             a_      :	Dict							=						np.array([[0, 3], [3, 0], [2, 3]]  )
             a_      :	str							=						np.array([[2, 1], [6, 3]]  )

             with self.assertRaises(SCREAMING_SNAKE_CASE__  ):
                    schur_complement(SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  )

      def  SCREAMING_SNAKE_CASE							( self       : List[Any]  )	->    None:
             a_      :	Tuple							=						np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]  )
             a_      :	Dict							=						np.array([[0, 3], [3, 0], [2, 3]]  )
             a_      :	List[str]							=						np.array([[2, 1, 3], [6, 3, 5]]  )

             with self.assertRaises(SCREAMING_SNAKE_CASE__  ):
                    schur_complement(SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  )


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   unittest.main()


