





"""simple docstring"""




from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload




class 			_a						:






				def __init__(			self		:   int,			lowerCAmelCase__		:   Collection[float] | None = None							)     ->				None:




										'''simple docstring'''



										if components is None:
																_UpperCamelCase      : Tuple         =   []
										_UpperCamelCase      : Optional[int]         =   list(lowerCAmelCase__							)






				def __len__(			self		:   List[Any]							)     ->				int:




										'''simple docstring'''



										return len(self.__components							)






				def __str__(			self		:   Dict							)     ->				str:




										'''simple docstring'''



										return "(" + ",".join(map(lowerCAmelCase__,			self.__components							)							) + ")"






				def __add__(			self		:   int,			lowerCAmelCase__		:   Vector							)     ->				Vector:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   len(self							)
										if size == len(lowerCAmelCase__							):
																_UpperCamelCase      : str         =   [self.__components[i] + other.component(lowerCAmelCase__							) for i in range(lowerCAmelCase__							)]
																return Vector(lowerCAmelCase__							)
										else:
																raise Exception('''must have the same size'''							)






				def __sub__(			self		:   Optional[Any],			lowerCAmelCase__		:   Vector							)     ->				Vector:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   len(self							)
										if size == len(lowerCAmelCase__							):
																_UpperCamelCase      : List[Any]         =   [self.__components[i] - other.component(lowerCAmelCase__							) for i in range(lowerCAmelCase__							)]
																return Vector(lowerCAmelCase__							)
										else:  # error case
																raise Exception('''must have the same size'''							)






				@overload
				def __mul__(			self		:   Optional[int],			lowerCAmelCase__		:   float							)     ->				Vector:




										'''simple docstring'''



										...






				@overload
				def __mul__(			self		:   int,			lowerCAmelCase__		:   Vector							)     ->				float:




										'''simple docstring'''



										...






				def __mul__(			self		:   int,			lowerCAmelCase__		:   float | Vector							)     ->				float | Vector:




										'''simple docstring'''



										if isinstance(lowerCAmelCase__,			(float, int)							):
																_UpperCamelCase      : Tuple         =   [c * other for c in self.__components]
																return Vector(lowerCAmelCase__							)
										elif isinstance(lowerCAmelCase__,			lowerCAmelCase__							) and len(self							) == len(lowerCAmelCase__							):
																_UpperCamelCase      : Tuple         =   len(self							)
																_UpperCamelCase      : Any         =   [self.__components[i] * other.component(lowerCAmelCase__							) for i in range(lowerCAmelCase__							)]
																return sum(lowerCAmelCase__							)
										else:  # error case
																raise Exception('''invalid operand!'''							)






				def     snake_case     (			self		:   List[str]							)     ->				Vector:




										'''simple docstring'''



										return Vector(self.__components							)






				def     snake_case     (			self		:   Any,			lowerCAmelCase__		:   int							)     ->				float:




										'''simple docstring'''



										if isinstance(lowerCAmelCase__,			lowerCAmelCase__							) and -len(self.__components							) <= i < len(self.__components							):
																return self.__components[i]
										else:
																raise Exception('''index out of range'''							)






				def     snake_case     (			self		:   Optional[Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   float							)     ->				None:




										'''simple docstring'''



										assert -len(self.__components							) <= pos < len(self.__components							)
										_UpperCamelCase      : List[str]         =   value






				def     snake_case     (			self		:   str							)     ->				float:




										'''simple docstring'''



										if len(self.__components							) == 0:
																raise Exception('''Vector is empty'''							)
										_UpperCamelCase      : Optional[int]         =   [c**2 for c in self.__components]
										return math.sqrt(sum(lowerCAmelCase__							)							)






				def     snake_case     (			self		:   Optional[Any],			lowerCAmelCase__		:   Vector,			lowerCAmelCase__		:   bool = False							)     ->				float:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self * other
										_UpperCamelCase      : Union[str, Any]         =   self.euclidean_length() * other.euclidean_length()
										if deg:
																return math.degrees(math.acos(num / den							)							)
										else:
																return math.acos(num / den							)





def 	a_    (							_lowercase       ):
						assert isinstance(_lowercase	,						_lowercase       )
						return Vector([0] * dimension       )





def 	a_    (							_lowercase	,						_lowercase       ):
						assert isinstance(_lowercase	,						_lowercase       ) and (isinstance(_lowercase	,						_lowercase       ))
						_UpperCamelCase      : Tuple         =   [0] * dimension
						_UpperCamelCase      : List[Any]         =   1
						return Vector(_lowercase       )





def 	a_    (							_lowercase	,						_lowercase	,						_lowercase       ):
						assert (
						    isinstance(_lowercase	,						_lowercase       )
						    and isinstance(_lowercase	,						_lowercase       )
						    and (isinstance(_lowercase	,						(int, float)       ))
						)
						return x * scalar + y





def 	a_    (							_lowercase	,						_lowercase	,						_lowercase       ):
						random.seed(_lowercase       )
						_UpperCamelCase      : int         =   [random.randint(_lowercase	,						_lowercase       ) for _ in range(_lowercase       )]
						return Vector(_lowercase       )




class 			_a						:






				def __init__(			self		:   Dict,			lowerCAmelCase__		:   list[list[float]],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int							)     ->				None:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   matrix
										_UpperCamelCase      : List[str]         =   w
										_UpperCamelCase      : Dict         =   h






				def __str__(			self		:   Optional[int]							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   ''''''
										for i in range(self.__height							):
																ans += "|"
																for j in range(self.__width							):
																						if j < self.__width - 1:
																												ans += str(self.__matrix[i][j]							) + ","
																						else:
																												ans += str(self.__matrix[i][j]							) + "|\n"
										return ans






				def __add__(			self		:   Any,			lowerCAmelCase__		:   Matrix							)     ->				Matrix:




										'''simple docstring'''



										if self.__width == other.width() and self.__height == other.height():
																_UpperCamelCase      : List[str]         =   []
																for i in range(self.__height							):
																						_UpperCamelCase      : int         =   [
																						    self.__matrix[i][j] + other.component(lowerCAmelCase__,			lowerCAmelCase__							)
																						    for j in range(self.__width							)
																						]
																						matrix.append(lowerCAmelCase__							)
																return Matrix(lowerCAmelCase__,			self.__width,			self.__height							)
										else:
																raise Exception('''matrix must have the same dimension!'''							)






				def __sub__(			self		:   Union[str, Any],			lowerCAmelCase__		:   Matrix							)     ->				Matrix:




										'''simple docstring'''



										if self.__width == other.width() and self.__height == other.height():
																_UpperCamelCase      : int         =   []
																for i in range(self.__height							):
																						_UpperCamelCase      : int         =   [
																						    self.__matrix[i][j] - other.component(lowerCAmelCase__,			lowerCAmelCase__							)
																						    for j in range(self.__width							)
																						]
																						matrix.append(lowerCAmelCase__							)
																return Matrix(lowerCAmelCase__,			self.__width,			self.__height							)
										else:
																raise Exception('''matrices must have the same dimension!'''							)






				@overload
				def __mul__(			self		:   Any,			lowerCAmelCase__		:   float							)     ->				Matrix:




										'''simple docstring'''



										...






				@overload
				def __mul__(			self		:   str,			lowerCAmelCase__		:   Vector							)     ->				Vector:




										'''simple docstring'''



										...






				def __mul__(			self		:   List[Any],			lowerCAmelCase__		:   float | Vector							)     ->				Vector | Matrix:




										'''simple docstring'''



										if isinstance(lowerCAmelCase__,			lowerCAmelCase__							):  # matrix-vector
																if len(lowerCAmelCase__							) == self.__width:
																						_UpperCamelCase      : List[Any]         =   zero_vector(self.__height							)
																						for i in range(self.__height							):
																												_UpperCamelCase      : Dict         =   [
																												    self.__matrix[i][j] * other.component(lowerCAmelCase__							)
																												    for j in range(self.__width							)
																												]
																												ans.change_component(lowerCAmelCase__,			sum(lowerCAmelCase__							)							)
																						return ans
																else:
																						raise Exception(
																						    '''vector must have the same size as the '''
																						    '''number of columns of the matrix!'''							)
										elif isinstance(lowerCAmelCase__,			(int, float)							):  # matrix-scalar
																_UpperCamelCase      : int         =   [
																    [self.__matrix[i][j] * other for j in range(self.__width							)]
																    for i in range(self.__height							)
																]
																return Matrix(lowerCAmelCase__,			self.__width,			self.__height							)
										return None






				def     snake_case     (			self		:   Union[str, Any]							)     ->				int:




										'''simple docstring'''



										return self.__height






				def     snake_case     (			self		:   List[str]							)     ->				int:




										'''simple docstring'''



										return self.__width






				def     snake_case     (			self		:   Union[str, Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int							)     ->				float:




										'''simple docstring'''



										if 0 <= x < self.__height and 0 <= y < self.__width:
																return self.__matrix[x][y]
										else:
																raise Exception('''change_component: indices out of bounds'''							)






				def     snake_case     (			self		:   Any,			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   float							)     ->				None:




										'''simple docstring'''



										if 0 <= x < self.__height and 0 <= y < self.__width:
																_UpperCamelCase      : str         =   value
										else:
																raise Exception('''change_component: indices out of bounds'''							)






				def     snake_case     (			self		:   List[Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int							)     ->				float:




										'''simple docstring'''



										if self.__height != self.__width:
																raise Exception('''Matrix is not square'''							)
										_UpperCamelCase      : Dict         =   self.__matrix[:x] + self.__matrix[x + 1 :]
										for i in range(len(lowerCAmelCase__							)							):
																_UpperCamelCase      : Optional[int]         =   minor[i][:y] + minor[i][y + 1 :]
										return Matrix(lowerCAmelCase__,			self.__width - 1,			self.__height - 1							).determinant()






				def     snake_case     (			self		:   Union[str, Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int							)     ->				float:




										'''simple docstring'''



										if self.__height != self.__width:
																raise Exception('''Matrix is not square'''							)
										if 0 <= x < self.__height and 0 <= y < self.__width:
																return (-1) ** (x + y) * self.minor(lowerCAmelCase__,			lowerCAmelCase__							)
										else:
																raise Exception('''Indices out of bounds'''							)






				def     snake_case     (			self		:   Optional[Any]							)     ->				float:




										'''simple docstring'''



										if self.__height != self.__width:
																raise Exception('''Matrix is not square'''							)
										if self.__height < 1:
																raise Exception('''Matrix has no element'''							)
										elif self.__height == 1:
																return self.__matrix[0][0]
										elif self.__height == 2:
																return (
																    self.__matrix[0][0] * self.__matrix[1][1]
																    - self.__matrix[0][1] * self.__matrix[1][0]
																)
										else:
																_UpperCamelCase      : Any         =   [
																    self.__matrix[0][y] * self.cofactor(0,			lowerCAmelCase__							) for y in range(self.__width							)
																]
																return sum(lowerCAmelCase__							)





def 	a_    (							_lowercase       ):
						_UpperCamelCase      : list[list[float]]         =   [[0] * n for _ in range(_lowercase       )]
						return Matrix(_lowercase	,						_lowercase	,						_lowercase       )






def 	a_    (							_lowercase	,						_lowercase	,						_lowercase	,						_lowercase       ):
						random.seed(_lowercase       )
						_UpperCamelCase      : list[list[float]]         =   [
						    [random.randint(_lowercase	,						_lowercase       ) for _ in range(_lowercase       )] for _ in range(_lowercase       )
						]
						return Matrix(_lowercase	,						_lowercase	,						_lowercase       )










"""simple docstring"""




import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
						from PIL import Image

						from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor




@require_vision
class 			_a						(				unittest.TestCase    ):






				def     snake_case     (			self		:   Tuple							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : int         =   tempfile.mkdtemp()

										_UpperCamelCase      : List[str]         =   [
										    '''[UNK]''',
										    '''[CLS]''',
										    '''[SEP]''',
										    '''[PAD]''',
										    '''[MASK]''',
										    '''的''',
										    '''价''',
										    '''格''',
										    '''是''',
										    '''15''',
										    '''便''',
										    '''alex''',
										    '''##andra''',
										    '''，''',
										    '''。''',
										    '''-''',
										    '''t''',
										    '''shirt''',
										]
										_UpperCamelCase      : Optional[Any]         =   os.path.join(self.tmpdirname,			VOCAB_FILES_NAMES['''vocab_file''']							)
										with open(self.vocab_file,			'''w''',			encoding='''utf-8'''							) as vocab_writer:
																vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]							)							)

										_UpperCamelCase      : Dict         =   {
										    '''do_resize''': True,
										    '''size''': {'''height''': 2_2_4, '''width''': 2_2_4},
										    '''do_center_crop''': True,
										    '''crop_size''': {'''height''': 1_8, '''width''': 1_8},
										    '''do_normalize''': True,
										    '''image_mean''': [0.48_145_466, 0.4_578_275, 0.40_821_073],
										    '''image_std''': [0.26_862_954, 0.26_130_258, 0.27_577_711],
										    '''do_convert_rgb''': True,
										}
										_UpperCamelCase      : Optional[Any]         =   os.path.join(self.tmpdirname,			lowerCAmelCase__							)
										with open(self.image_processor_file,			'''w''',			encoding='''utf-8'''							) as fp:
																json.dump(lowerCAmelCase__,			lowerCAmelCase__							)






				def     snake_case     (			self		:   str,			**lowerCAmelCase__		:   List[Any]							)     ->				Optional[int]:




										'''simple docstring'''



										return BertTokenizer.from_pretrained(self.tmpdirname,			**lowerCAmelCase__							)






				def     snake_case     (			self		:   Union[str, Any],			**lowerCAmelCase__		:   Tuple							)     ->				str:




										'''simple docstring'''



										return BertTokenizerFast.from_pretrained(self.tmpdirname,			**lowerCAmelCase__							)






				def     snake_case     (			self		:   Any,			**lowerCAmelCase__		:   Optional[int]							)     ->				Optional[Any]:




										'''simple docstring'''



										return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname,			**lowerCAmelCase__							)






				def     snake_case     (			self		:   str							)     ->				Optional[int]:




										'''simple docstring'''



										shutil.rmtree(self.tmpdirname							)






				def     snake_case     (			self		:   Any							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   [np.random.randint(2_5_5,			size=(3, 3_0, 4_0_0),			dtype=np.uinta							)]

										_UpperCamelCase      : List[Any]         =   [Image.fromarray(np.moveaxis(lowerCAmelCase__,			0,			-1							)							) for x in image_inputs]

										return image_inputs






				def     snake_case     (			self		:   str							)     ->				Any:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self.get_tokenizer()
										_UpperCamelCase      : int         =   self.get_rust_tokenizer()
										_UpperCamelCase      : int         =   self.get_image_processor()

										_UpperCamelCase      : Tuple         =   ChineseCLIPProcessor(tokenizer=lowerCAmelCase__,			image_processor=lowerCAmelCase__							)
										processor_slow.save_pretrained(self.tmpdirname							)
										_UpperCamelCase      : List[Any]         =   ChineseCLIPProcessor.from_pretrained(self.tmpdirname,			use_fast=lowerCAmelCase__							)

										_UpperCamelCase      : List[Any]         =   ChineseCLIPProcessor(tokenizer=lowerCAmelCase__,			image_processor=lowerCAmelCase__							)
										processor_fast.save_pretrained(self.tmpdirname							)
										_UpperCamelCase      : List[Any]         =   ChineseCLIPProcessor.from_pretrained(self.tmpdirname							)

										self.assertEqual(processor_slow.tokenizer.get_vocab(),			tokenizer_slow.get_vocab()							)
										self.assertEqual(processor_fast.tokenizer.get_vocab(),			tokenizer_fast.get_vocab()							)
										self.assertEqual(tokenizer_slow.get_vocab(),			tokenizer_fast.get_vocab()							)
										self.assertIsInstance(processor_slow.tokenizer,			lowerCAmelCase__							)
										self.assertIsInstance(processor_fast.tokenizer,			lowerCAmelCase__							)

										self.assertEqual(processor_slow.image_processor.to_json_string(),			image_processor.to_json_string()							)
										self.assertEqual(processor_fast.image_processor.to_json_string(),			image_processor.to_json_string()							)
										self.assertIsInstance(processor_slow.image_processor,			lowerCAmelCase__							)
										self.assertIsInstance(processor_fast.image_processor,			lowerCAmelCase__							)






				def     snake_case     (			self		:   int							)     ->				Tuple:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   ChineseCLIPProcessor(tokenizer=self.get_tokenizer(),			image_processor=self.get_image_processor()							)
										processor.save_pretrained(self.tmpdirname							)

										_UpperCamelCase      : Dict         =   self.get_tokenizer(cls_token='''(CLS)''',			sep_token='''(SEP)'''							)
										_UpperCamelCase      : List[str]         =   self.get_image_processor(do_normalize=lowerCAmelCase__							)

										_UpperCamelCase      : Optional[Any]         =   ChineseCLIPProcessor.from_pretrained(
										    self.tmpdirname,			cls_token='''(CLS)''',			sep_token='''(SEP)''',			do_normalize=lowerCAmelCase__							)

										self.assertEqual(processor.tokenizer.get_vocab(),			tokenizer_add_kwargs.get_vocab()							)
										self.assertIsInstance(processor.tokenizer,			lowerCAmelCase__							)

										self.assertEqual(processor.image_processor.to_json_string(),			image_processor_add_kwargs.to_json_string()							)
										self.assertIsInstance(processor.image_processor,			lowerCAmelCase__							)






				def     snake_case     (			self		:   Optional[int]							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.get_image_processor()
										_UpperCamelCase      : str         =   self.get_tokenizer()

										_UpperCamelCase      : Optional[Any]         =   ChineseCLIPProcessor(tokenizer=lowerCAmelCase__,			image_processor=lowerCAmelCase__							)

										_UpperCamelCase      : List[str]         =   self.prepare_image_inputs()

										_UpperCamelCase      : Any         =   image_processor(lowerCAmelCase__,			return_tensors='''np'''							)
										_UpperCamelCase      : Any         =   processor(images=lowerCAmelCase__,			return_tensors='''np'''							)

										for key in input_feat_extract.keys():
																self.assertAlmostEqual(input_feat_extract[key].sum(),			input_processor[key].sum(),			delta=1e-2							)






				def     snake_case     (			self		:   Optional[Any]							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Tuple         =   self.get_image_processor()
										_UpperCamelCase      : Optional[Any]         =   self.get_tokenizer()

										_UpperCamelCase      : Any         =   ChineseCLIPProcessor(tokenizer=lowerCAmelCase__,			image_processor=lowerCAmelCase__							)

										_UpperCamelCase      : Tuple         =   '''Alexandra，T-shirt的价格是15便士。'''

										_UpperCamelCase      : List[str]         =   processor(text=lowerCAmelCase__							)

										_UpperCamelCase      : Any         =   tokenizer(lowerCAmelCase__							)

										for key in encoded_tok.keys():
																self.assertListEqual(encoded_tok[key],			encoded_processor[key]							)






				def     snake_case     (			self		:   Dict							)     ->				Tuple:




										'''simple docstring'''



										_UpperCamelCase      : Tuple         =   self.get_image_processor()
										_UpperCamelCase      : Optional[Any]         =   self.get_tokenizer()

										_UpperCamelCase      : Dict         =   ChineseCLIPProcessor(tokenizer=lowerCAmelCase__,			image_processor=lowerCAmelCase__							)

										_UpperCamelCase      : Any         =   '''Alexandra，T-shirt的价格是15便士。'''
										_UpperCamelCase      : Union[str, Any]         =   self.prepare_image_inputs()

										_UpperCamelCase      : str         =   processor(text=lowerCAmelCase__,			images=lowerCAmelCase__							)

										self.assertListEqual(list(inputs.keys()							),			['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values''']							)

										# test if it raises when no input is passed
										with pytest.raises(lowerCAmelCase__							):
																processor()






				def     snake_case     (			self		:   Optional[Any]							)     ->				Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase      : int         =   self.get_image_processor()
										_UpperCamelCase      : int         =   self.get_tokenizer()

										_UpperCamelCase      : Optional[Any]         =   ChineseCLIPProcessor(tokenizer=lowerCAmelCase__,			image_processor=lowerCAmelCase__							)

										_UpperCamelCase      : List[Any]         =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

										_UpperCamelCase      : List[Any]         =   processor.batch_decode(lowerCAmelCase__							)
										_UpperCamelCase      : Dict         =   tokenizer.batch_decode(lowerCAmelCase__							)

										self.assertListEqual(lowerCAmelCase__,			lowerCAmelCase__							)






				def     snake_case     (			self		:   Union[str, Any]							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self.get_image_processor()
										_UpperCamelCase      : Optional[int]         =   self.get_tokenizer()

										_UpperCamelCase      : Optional[Any]         =   ChineseCLIPProcessor(tokenizer=lowerCAmelCase__,			image_processor=lowerCAmelCase__							)

										_UpperCamelCase      : Any         =   '''Alexandra，T-shirt的价格是15便士。'''
										_UpperCamelCase      : int         =   self.prepare_image_inputs()

										_UpperCamelCase      : Dict         =   processor(text=lowerCAmelCase__,			images=lowerCAmelCase__							)

										self.assertListEqual(list(inputs.keys()							),			processor.model_input_names							)



