


"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
 import jax.numpy as jnp

if is_tf_available():
 import tensorflow as tf

if is_torch_available():
 import torch



class 						snake_case_(				unittest.TestCase	):






     def    lowerCamelCase__   (		self     :    List[Any] ):
           lowerCAmelCase    :		Optional[int]				=      {
               '''task_specific_params''': {
                   '''summarization''': {'''length_penalty''': 1.0, '''max_length''': 1_2_8, '''min_length''': 1_2, '''num_beams''': 4},
                   '''summarization_cnn''': {'''length_penalty''': 2.0, '''max_length''': 1_4_2, '''min_length''': 5_6, '''num_beams''': 4},
                   '''summarization_xsum''': {'''length_penalty''': 1.0, '''max_length''': 6_2, '''min_length''': 1_1, '''num_beams''': 6},
               }
           }
           lowerCAmelCase    :		int				=      {
               '''task_specific_params.summarization.length_penalty''': 1.0,
               '''task_specific_params.summarization.max_length''': 1_2_8,
               '''task_specific_params.summarization.min_length''': 1_2,
               '''task_specific_params.summarization.num_beams''': 4,
               '''task_specific_params.summarization_cnn.length_penalty''': 2.0,
               '''task_specific_params.summarization_cnn.max_length''': 1_4_2,
               '''task_specific_params.summarization_cnn.min_length''': 5_6,
               '''task_specific_params.summarization_cnn.num_beams''': 4,
               '''task_specific_params.summarization_xsum.length_penalty''': 1.0,
               '''task_specific_params.summarization_xsum.max_length''': 6_2,
               '''task_specific_params.summarization_xsum.min_length''': 1_1,
               '''task_specific_params.summarization_xsum.num_beams''': 6,
           }

           self.assertEqual(flatten_dict(UpperCamelCase_ )    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Dict ):
           lowerCAmelCase    :		Dict				=      np.random.randn(3    , 4 )
           self.assertTrue(np.allclose(transpose(UpperCamelCase_ )    , x.transpose() ) )

           lowerCAmelCase    :		List[Any]				=      np.random.randn(3    , 4    , 5 )
           self.assertTrue(np.allclose(transpose(UpperCamelCase_    , axes=(1, 2, 0) )    , x.transpose((1, 2, 0) ) ) )






     @require_torch
     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase    :		Optional[int]				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		Any				=      torch.tensor(UpperCamelCase_ )
           self.assertTrue(np.allclose(transpose(UpperCamelCase_ )    , transpose(UpperCamelCase_ ).numpy() ) )

           lowerCAmelCase    :		Union[str, Any]				=      np.random.randn(3    , 4    , 5 )
           lowerCAmelCase    :		Tuple				=      torch.tensor(UpperCamelCase_ )
           self.assertTrue(np.allclose(transpose(UpperCamelCase_    , axes=(1, 2, 0) )    , transpose(UpperCamelCase_    , axes=(1, 2, 0) ).numpy() ) )






     @require_tf
     def    lowerCamelCase__   (		self     :    Tuple ):
           lowerCAmelCase    :		Any				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		str				=      tf.constant(UpperCamelCase_ )
           self.assertTrue(np.allclose(transpose(UpperCamelCase_ )    , transpose(UpperCamelCase_ ).numpy() ) )

           lowerCAmelCase    :		Optional[Any]				=      np.random.randn(3    , 4    , 5 )
           lowerCAmelCase    :		List[Any]				=      tf.constant(UpperCamelCase_ )
           self.assertTrue(np.allclose(transpose(UpperCamelCase_    , axes=(1, 2, 0) )    , transpose(UpperCamelCase_    , axes=(1, 2, 0) ).numpy() ) )






     @require_flax
     def    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		Optional[Any]				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		Union[str, Any]				=      jnp.array(UpperCamelCase_ )
           self.assertTrue(np.allclose(transpose(UpperCamelCase_ )    , np.asarray(transpose(UpperCamelCase_ ) ) ) )

           lowerCAmelCase    :		Tuple				=      np.random.randn(3    , 4    , 5 )
           lowerCAmelCase    :		Dict				=      jnp.array(UpperCamelCase_ )
           self.assertTrue(np.allclose(transpose(UpperCamelCase_    , axes=(1, 2, 0) )    , np.asarray(transpose(UpperCamelCase_    , axes=(1, 2, 0) ) ) ) )






     def    lowerCamelCase__   (		self     :    str ):
           lowerCAmelCase    :		Union[str, Any]				=      np.random.randn(3    , 4 )
           self.assertTrue(np.allclose(reshape(UpperCamelCase_    , (4, 3) )    , np.reshape(UpperCamelCase_    , (4, 3) ) ) )

           lowerCAmelCase    :		Union[str, Any]				=      np.random.randn(3    , 4    , 5 )
           self.assertTrue(np.allclose(reshape(UpperCamelCase_    , (1_2, 5) )    , np.reshape(UpperCamelCase_    , (1_2, 5) ) ) )






     @require_torch
     def    lowerCamelCase__   (		self     :    Any ):
           lowerCAmelCase    :		Optional[Any]				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		Optional[Any]				=      torch.tensor(UpperCamelCase_ )
           self.assertTrue(np.allclose(reshape(UpperCamelCase_    , (4, 3) )    , reshape(UpperCamelCase_    , (4, 3) ).numpy() ) )

           lowerCAmelCase    :		List[Any]				=      np.random.randn(3    , 4    , 5 )
           lowerCAmelCase    :		Union[str, Any]				=      torch.tensor(UpperCamelCase_ )
           self.assertTrue(np.allclose(reshape(UpperCamelCase_    , (1_2, 5) )    , reshape(UpperCamelCase_    , (1_2, 5) ).numpy() ) )






     @require_tf
     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase    :		List[Any]				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		str				=      tf.constant(UpperCamelCase_ )
           self.assertTrue(np.allclose(reshape(UpperCamelCase_    , (4, 3) )    , reshape(UpperCamelCase_    , (4, 3) ).numpy() ) )

           lowerCAmelCase    :		int				=      np.random.randn(3    , 4    , 5 )
           lowerCAmelCase    :		Tuple				=      tf.constant(UpperCamelCase_ )
           self.assertTrue(np.allclose(reshape(UpperCamelCase_    , (1_2, 5) )    , reshape(UpperCamelCase_    , (1_2, 5) ).numpy() ) )






     @require_flax
     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase    :		Dict				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		Union[str, Any]				=      jnp.array(UpperCamelCase_ )
           self.assertTrue(np.allclose(reshape(UpperCamelCase_    , (4, 3) )    , np.asarray(reshape(UpperCamelCase_    , (4, 3) ) ) ) )

           lowerCAmelCase    :		List[str]				=      np.random.randn(3    , 4    , 5 )
           lowerCAmelCase    :		int				=      jnp.array(UpperCamelCase_ )
           self.assertTrue(np.allclose(reshape(UpperCamelCase_    , (1_2, 5) )    , np.asarray(reshape(UpperCamelCase_    , (1_2, 5) ) ) ) )






     def    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		List[Any]				=      np.random.randn(1    , 3    , 4 )
           self.assertTrue(np.allclose(squeeze(UpperCamelCase_ )    , np.squeeze(UpperCamelCase_ ) ) )

           lowerCAmelCase    :		Any				=      np.random.randn(1    , 4    , 1    , 5 )
           self.assertTrue(np.allclose(squeeze(UpperCamelCase_    , axis=2 )    , np.squeeze(UpperCamelCase_    , axis=2 ) ) )






     @require_torch
     def    lowerCamelCase__   (		self     :    int ):
           lowerCAmelCase    :		Any				=      np.random.randn(1    , 3    , 4 )
           lowerCAmelCase    :		Union[str, Any]				=      torch.tensor(UpperCamelCase_ )
           self.assertTrue(np.allclose(squeeze(UpperCamelCase_ )    , squeeze(UpperCamelCase_ ).numpy() ) )

           lowerCAmelCase    :		Dict				=      np.random.randn(1    , 4    , 1    , 5 )
           lowerCAmelCase    :		Tuple				=      torch.tensor(UpperCamelCase_ )
           self.assertTrue(np.allclose(squeeze(UpperCamelCase_    , axis=2 )    , squeeze(UpperCamelCase_    , axis=2 ).numpy() ) )






     @require_tf
     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase    :		str				=      np.random.randn(1    , 3    , 4 )
           lowerCAmelCase    :		List[str]				=      tf.constant(UpperCamelCase_ )
           self.assertTrue(np.allclose(squeeze(UpperCamelCase_ )    , squeeze(UpperCamelCase_ ).numpy() ) )

           lowerCAmelCase    :		Optional[Any]				=      np.random.randn(1    , 4    , 1    , 5 )
           lowerCAmelCase    :		Optional[int]				=      tf.constant(UpperCamelCase_ )
           self.assertTrue(np.allclose(squeeze(UpperCamelCase_    , axis=2 )    , squeeze(UpperCamelCase_    , axis=2 ).numpy() ) )






     @require_flax
     def    lowerCamelCase__   (		self     :    Any ):
           lowerCAmelCase    :		int				=      np.random.randn(1    , 3    , 4 )
           lowerCAmelCase    :		str				=      jnp.array(UpperCamelCase_ )
           self.assertTrue(np.allclose(squeeze(UpperCamelCase_ )    , np.asarray(squeeze(UpperCamelCase_ ) ) ) )

           lowerCAmelCase    :		List[str]				=      np.random.randn(1    , 4    , 1    , 5 )
           lowerCAmelCase    :		str				=      jnp.array(UpperCamelCase_ )
           self.assertTrue(np.allclose(squeeze(UpperCamelCase_    , axis=2 )    , np.asarray(squeeze(UpperCamelCase_    , axis=2 ) ) ) )






     def    lowerCamelCase__   (		self     :    Tuple ):
           lowerCAmelCase    :		List[Any]				=      np.random.randn(3    , 4 )
           self.assertTrue(np.allclose(expand_dims(UpperCamelCase_    , axis=1 )    , np.expand_dims(UpperCamelCase_    , axis=1 ) ) )






     @require_torch
     def    lowerCamelCase__   (		self     :    Dict ):
           lowerCAmelCase    :		Any				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		Any				=      torch.tensor(UpperCamelCase_ )
           self.assertTrue(np.allclose(expand_dims(UpperCamelCase_    , axis=1 )    , expand_dims(UpperCamelCase_    , axis=1 ).numpy() ) )






     @require_tf
     def    lowerCamelCase__   (		self     :    Union[str, Any] ):
           lowerCAmelCase    :		Tuple				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		Optional[int]				=      tf.constant(UpperCamelCase_ )
           self.assertTrue(np.allclose(expand_dims(UpperCamelCase_    , axis=1 )    , expand_dims(UpperCamelCase_    , axis=1 ).numpy() ) )






     @require_flax
     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase    :		Dict				=      np.random.randn(3    , 4 )
           lowerCAmelCase    :		Tuple				=      jnp.array(UpperCamelCase_ )
           self.assertTrue(np.allclose(expand_dims(UpperCamelCase_    , axis=1 )    , np.asarray(expand_dims(UpperCamelCase_    , axis=1 ) ) ) )






"""simple docstring"""





from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm


def    _snake_case  (  _snake_case :	np.ndarray       ,      _snake_case :	np.ndarray	):
      return math.sqrt(sum(pow(a - b       ,      2	) for a, b in zip(_snake_case       ,      _snake_case	)	)	)


def    _snake_case  (  _snake_case :	np.ndarray       ,      _snake_case :	np.ndarray	):

      if dataset.ndim != value_array.ndim:
            lowerCAmelCase    :		List[Any]				=      (
                '''Wrong input data\'s dimensions... '''
                f'''dataset : {dataset.ndim}, value_array : {value_array.ndim}'''
            )
            raise ValueError(_snake_case	)

      try:
            if dataset.shape[1] != value_array.shape[1]:
                  lowerCAmelCase    :		Dict				=      (
                      '''Wrong input data\'s shape... '''
                      f'''dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}'''
                  )
                  raise ValueError(_snake_case	)
      except IndexError:
            if dataset.ndim != value_array.ndim:
                  raise TypeError('''Wrong shape'''	)

      if dataset.dtype != value_array.dtype:
            lowerCAmelCase    :		Optional[Any]				=      (
                '''Input data have different datatype... '''
                f'''dataset : {dataset.dtype}, value_array : {value_array.dtype}'''
            )
            raise TypeError(_snake_case	)

      lowerCAmelCase    :		str				=      []

      for value in value_array:
            lowerCAmelCase    :		int				=      euclidean(_snake_case       ,      dataset[0]	)
            lowerCAmelCase    :		Union[str, Any]				=      dataset[0].tolist()

            for dataset_value in dataset[1:]:
                  lowerCAmelCase    :		Any				=      euclidean(_snake_case       ,      _snake_case	)

                  if dist > temp_dist:
                        lowerCAmelCase    :		List[Any]				=      temp_dist
                        lowerCAmelCase    :		Tuple				=      dataset_value.tolist()

            answer.append([vector, dist]	)

      return answer


def    _snake_case  (  _snake_case :	np.ndarray       ,      _snake_case :	np.ndarray	):
      return np.dot(_snake_case       ,      _snake_case	) / (norm(_snake_case	) * norm(_snake_case	))


if __name__ == "__main__":
 import doctest

 doctest.testmod()


