


'''simple docstring'''




import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)

class 		UpperCAmelCase_      (  unittest.TestCase		):





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  None:
        lowerCAmelCase     =			Vector([1, 2, 3]    )
        self.assertEqual(x.component(0    )    ,      1    )
        self.assertEqual(x.component(2    )    ,      3    )
        lowerCAmelCase     =			Vector()





     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  None:
        lowerCAmelCase     =			Vector([0, 0, 0, 0, 0, 1]    )
        self.assertEqual(str(UpperCAmelCase__    )    ,      '(0,0,0,0,0,1)'    )





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  None:
        lowerCAmelCase     =			Vector([1, 2, 3, 4]    )
        self.assertEqual(len(UpperCAmelCase__    )    ,      4    )





     def 							__UpperCAmelCase      ( self       :      Optional[int]    )				->  None:
        lowerCAmelCase     =			Vector([1, 2]    )
        lowerCAmelCase     =			Vector([1, 2, 3, 4, 5]    )
        lowerCAmelCase     =			Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]    )
        lowerCAmelCase     =			Vector([1, -1, 1, -1, 2, -3, 4, -5]    )
        self.assertAlmostEqual(x.euclidean_length()    ,      2.236    ,      3    )
        self.assertAlmostEqual(y.euclidean_length()    ,      7.416    ,      3    )
        self.assertEqual(z.euclidean_length()    ,      0    )
        self.assertAlmostEqual(w.euclidean_length()    ,      7.616    ,      3    )





     def 							__UpperCAmelCase      ( self       :      int    )				->  None:
        lowerCAmelCase     =			Vector([1, 2, 3]    )
        lowerCAmelCase     =			Vector([1, 1, 1]    )
        self.assertEqual((x + y).component(0    )    ,      2    )
        self.assertEqual((x + y).component(1    )    ,      3    )
        self.assertEqual((x + y).component(2    )    ,      4    )





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    )				->  None:
        lowerCAmelCase     =			Vector([1, 2, 3]    )
        lowerCAmelCase     =			Vector([1, 1, 1]    )
        self.assertEqual((x - y).component(0    )    ,      0    )
        self.assertEqual((x - y).component(1    )    ,      1    )
        self.assertEqual((x - y).component(2    )    ,      2    )





     def 							__UpperCAmelCase      ( self       :      str    )				->  None:
        lowerCAmelCase     =			Vector([1, 2, 3]    )
        lowerCAmelCase     =			Vector([2, -1, 4]    )  # for test of dot product
        lowerCAmelCase     =			Vector([1, -2, -1]    )
        self.assertEqual(str(x * 3.0    )    ,      '(3.0,6.0,9.0)'    )
        self.assertEqual((a * b)    ,      0    )





     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  None:
        self.assertEqual(str(zero_vector(1_0    )    ).count('0'    )    ,      1_0    )





     def 							__UpperCAmelCase      ( self       :      Tuple    )				->  None:
        self.assertEqual(str(unit_basis_vector(3    ,      1    )    )    ,      '(0,1,0)'    )





     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  None:
        lowerCAmelCase     =			Vector([1, 2, 3]    )
        lowerCAmelCase     =			Vector([1, 0, 1]    )
        self.assertEqual(str(axpy(2    ,      UpperCAmelCase__    ,      UpperCAmelCase__    )    )    ,      '(3,4,7)'    )





     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  None:
        lowerCAmelCase     =			Vector([1, 0, 0, 0, 0, 0]    )
        lowerCAmelCase     =			x.copy()
        self.assertEqual(str(UpperCAmelCase__    )    ,      str(UpperCAmelCase__    )    )





     def 							__UpperCAmelCase      ( self       :      str    )				->  None:
        lowerCAmelCase     =			Vector([1, 0, 0]    )
        x.change_component(0    ,      0    )
        x.change_component(1    ,      1    )
        self.assertEqual(str(UpperCAmelCase__    )    ,      '(0,1,0)'    )





     def 							__UpperCAmelCase      ( self       :      str    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]    ,      3    ,      3    )
        self.assertEqual('|1,2,3|\n|2,4,5|\n|6,7,8|\n'    ,      str(UpperCAmelCase__    )    )





     def 							__UpperCAmelCase      ( self       :      Tuple    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]    ,      3    ,      3    )
        lowerCAmelCase     =			[[-3, -1_4, -1_0], [-5, -1_0, -5], [-2, -1, 0]]
        for x in range(a.height()    ):
           for y in range(a.width()    ):
              self.assertEqual(minors[x][y]    ,      a.minor(UpperCAmelCase__    ,      UpperCAmelCase__    )    )





     def 							__UpperCAmelCase      ( self       :      List[Any]    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]    ,      3    ,      3    )
        lowerCAmelCase     =			[[-3, 1_4, -1_0], [5, -1_0, 5], [-2, 1, 0]]
        for x in range(a.height()    ):
           for y in range(a.width()    ):
              self.assertEqual(cofactors[x][y]    ,      a.cofactor(UpperCAmelCase__    ,      UpperCAmelCase__    )    )





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]    ,      3    ,      3    )
        self.assertEqual(-5    ,      a.determinant()    )





     def 							__UpperCAmelCase      ( self       :      int    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]    ,      3    ,      3    )
        lowerCAmelCase     =			Vector([1, 2, 3]    )
        self.assertEqual('(14,32,50)'    ,      str(a * x    )    )
        self.assertEqual('|2,4,6|\n|8,10,12|\n|14,16,18|\n'    ,      str(a * 2    )    )





     def 							__UpperCAmelCase      ( self       :      Optional[int]    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]    ,      3    ,      3    )
        a.change_component(0    ,      2    ,      5    )
        self.assertEqual('|1,2,5|\n|2,4,5|\n|6,7,8|\n'    ,      str(UpperCAmelCase__    )    )





     def 							__UpperCAmelCase      ( self       :      int    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]    ,      3    ,      3    )
        self.assertEqual(7    ,      a.component(2    ,      1    )    ,      0.01    )





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]    ,      3    ,      3    )
        lowerCAmelCase     =			Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 1_0]]    ,      3    ,      3    )
        self.assertEqual('|2,4,10|\n|4,8,10|\n|12,14,18|\n'    ,      str(a + b    )    )





     def 							__UpperCAmelCase      ( self       :      str    )				->  None:
        lowerCAmelCase     =			Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]    ,      3    ,      3    )
        lowerCAmelCase     =			Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 1_0]]    ,      3    ,      3    )
        self.assertEqual('|0,0,-4|\n|0,0,0|\n|0,0,-2|\n'    ,      str(a - b    )    )





     def 							__UpperCAmelCase      ( self       :      Tuple    )				->  None:
        self.assertEqual(
            '|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n'    ,      str(square_zero_matrix(5    )    )    ,      )


if __name__ == "__main__":
       unittest.main()









'''simple docstring'''




import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


__snake_case    						=random.Random()

if is_torch_available():
       import torch





def 	a_					( lowerCamelCase	:     Dict	, lowerCamelCase	:     Dict=1.0	, lowerCamelCase	:     List[Any]=None	, lowerCamelCase	:     Union[str, Any]=None				):
   if rng is None:
      lowerCAmelCase     =			global_rng

   lowerCAmelCase     =			[]
   for batch_idx in range(shape[0]				):
      values.append([]				)
      for _ in range(shape[1]				):
         values[-1].append(rng.random() * scale				)

   return values

class 		UpperCAmelCase_      (  unittest.TestCase		):





     def __init__( self       :      List[Any]    ,      UpperCAmelCase__       :      Optional[Any]    ,      UpperCAmelCase__       :      List[str]=7    ,      UpperCAmelCase__       :      int=4_0_0    ,      UpperCAmelCase__       :      int=2_0_0_0    ,      UpperCAmelCase__       :      List[str]=1    ,      UpperCAmelCase__       :      Tuple=0.0    ,      UpperCAmelCase__       :      Tuple=1_6_0_0_0    ,      UpperCAmelCase__       :      Tuple=True    ,      UpperCAmelCase__       :      Union[str, Any]=True    ,      )				->  Any:
        lowerCAmelCase     =			parent
        lowerCAmelCase     =			batch_size
        lowerCAmelCase     =			min_seq_length
        lowerCAmelCase     =			max_seq_length
        lowerCAmelCase     =			(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        lowerCAmelCase     =			feature_size
        lowerCAmelCase     =			padding_value
        lowerCAmelCase     =			sampling_rate
        lowerCAmelCase     =			return_attention_mask
        lowerCAmelCase     =			do_normalize





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    )				->  List[str]:
        return {
            "feature_size": self.feature_size,
            "padding_value": self.padding_value,
            "sampling_rate": self.sampling_rate,
            "return_attention_mask": self.return_attention_mask,
            "do_normalize": self.do_normalize,
        }





     def 							__UpperCAmelCase      ( self       :      str    ,      UpperCAmelCase__       :      Union[str, Any]=False    ,      UpperCAmelCase__       :      Union[str, Any]=False    )				->  Optional[Any]:
        def _flatten(UpperCAmelCase__       :      int    ):
           return list(itertools.chain(*UpperCAmelCase__    )    )

        if equal_length:
           lowerCAmelCase     =			floats_list((self.batch_size, self.max_seq_length)    )
        else:
           # make sure that inputs increase in size
           lowerCAmelCase     =			[
               _flatten(floats_list((x, self.feature_size)    )    )
               for x in range(self.min_seq_length    ,      self.max_seq_length    ,      self.seq_length_diff    )
           ]

        if numpify:
           lowerCAmelCase     =			[np.asarray(UpperCAmelCase__    ) for x in speech_inputs]

        return speech_inputs




@require_torch
@require_torchaudio
class 		UpperCAmelCase_      (  __lowercase       ,	unittest.TestCase		):
     lowerCamelCase				:	Dict		=     ASTFeatureExtractor





     def 							__UpperCAmelCase      ( self       :      str    )				->  Optional[int]:
        lowerCAmelCase     =			ASTFeatureExtractionTester(self    )





     def 							__UpperCAmelCase      ( self       :      Optional[int]    )				->  Optional[int]:
        # Tests that all call wrap to encode_plus and batch_encode_plus
        lowerCAmelCase     =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
        # create three inputs of length 800, 1000, and 1200
        lowerCAmelCase     =			[floats_list((1, x)    )[0] for x in range(8_0_0    ,      1_4_0_0    ,      2_0_0    )]
        lowerCAmelCase     =			[np.asarray(UpperCAmelCase__    ) for speech_input in speech_inputs]

        # Test not batched input
        lowerCAmelCase     =			feat_extract(speech_inputs[0]    ,      return_tensors='np'    ).input_values
        lowerCAmelCase     =			feat_extract(np_speech_inputs[0]    ,      return_tensors='np'    ).input_values
        self.assertTrue(np.allclose(UpperCAmelCase__    ,      UpperCAmelCase__    ,      atol=1E-3    )    )

        # Test batched
        lowerCAmelCase     =			feat_extract(UpperCAmelCase__    ,      padding=UpperCAmelCase__    ,      return_tensors='np'    ).input_values
        lowerCAmelCase     =			feat_extract(UpperCAmelCase__    ,      padding=UpperCAmelCase__    ,      return_tensors='np'    ).input_values
        for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__    ,      UpperCAmelCase__    ):
           self.assertTrue(np.allclose(UpperCAmelCase__    ,      UpperCAmelCase__    ,      atol=1E-3    )    )

        # Test 2-D numpy arrays are batched.
        lowerCAmelCase     =			[floats_list((1, x)    )[0] for x in (8_0_0, 8_0_0, 8_0_0)]
        lowerCAmelCase     =			np.asarray(UpperCAmelCase__    )
        lowerCAmelCase     =			feat_extract(UpperCAmelCase__    ,      return_tensors='np'    ).input_values
        lowerCAmelCase     =			feat_extract(UpperCAmelCase__    ,      return_tensors='np'    ).input_values
        for enc_seq_a, enc_seq_a in zip(UpperCAmelCase__    ,      UpperCAmelCase__    ):
           self.assertTrue(np.allclose(UpperCAmelCase__    ,      UpperCAmelCase__    ,      atol=1E-3    )    )





     @require_torch
     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  Optional[int]:
        import torch

        lowerCAmelCase     =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
        lowerCAmelCase     =			np.random.rand(1_0_0    ).astype(np.floataa    )
        lowerCAmelCase     =			np_speech_inputs.tolist()

        for inputs in [py_speech_inputs, np_speech_inputs]:
           lowerCAmelCase     =			feature_extractor.pad([{'input_values': inputs}]    ,      return_tensors='np'    )
           self.assertTrue(np_processed.input_values.dtype == np.floataa    )
           lowerCAmelCase     =			feature_extractor.pad([{'input_values': inputs}]    ,      return_tensors='pt'    )
           self.assertTrue(pt_processed.input_values.dtype == torch.floataa    )





     def 							__UpperCAmelCase      ( self       :      int    ,      UpperCAmelCase__       :      str    )				->  Tuple:
        from datasets import load_dataset

        lowerCAmelCase     =			load_dataset('hf-internal-testing/librispeech_asr_dummy'    ,      'clean'    ,      split='validation'    )
        # automatic decoding with librispeech
        lowerCAmelCase     =			ds.sort('id'    ).select(range(UpperCAmelCase__    )    )[:num_samples]['audio']

        return [x["array"] for x in speech_samples]





     @require_torch
     def 							__UpperCAmelCase      ( self       :      str    )				->  Optional[Any]:
        # fmt: off
        lowerCAmelCase     =			torch.tensor(
            [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
             -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
             -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
             -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]    )
        # fmt: on

        lowerCAmelCase     =			self._load_datasamples(1    )
        lowerCAmelCase     =			ASTFeatureExtractor()
        lowerCAmelCase     =			feature_extractor(UpperCAmelCase__    ,      return_tensors='pt'    ).input_values
        self.assertEquals(input_values.shape    ,      (1, 1_0_2_4, 1_2_8)    )
        self.assertTrue(torch.allclose(input_values[0, 0, :3_0]    ,      UpperCAmelCase__    ,      atol=1E-4    )    )





