



'''simple docstring'''



import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import ChineseCLIPImageProcessor






class 					UpperCAmelCase__	(		unittest.TestCase ):



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase=7			,				UpperCamelCase=3			,				UpperCamelCase=18			,				UpperCamelCase=30			,				UpperCamelCase=400			,				UpperCamelCase=True			,				UpperCamelCase=None			,				UpperCamelCase=True			,				UpperCamelCase=None			,				UpperCamelCase=True			,				UpperCamelCase=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]			,				UpperCamelCase=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]			,				UpperCamelCase=True			,				)     ->			List[Any]:
						__lowerCAmelCase												=  size if size is not None else {"height": 224, "width": 224}
						__lowerCAmelCase												=  crop_size if crop_size is not None else {"height": 18, "width": 18}
						__lowerCAmelCase												=  parent
						__lowerCAmelCase												=  batch_size
						__lowerCAmelCase												=  num_channels
						__lowerCAmelCase												=  image_size
						__lowerCAmelCase												=  min_resolution
						__lowerCAmelCase												=  max_resolution
						__lowerCAmelCase												=  do_resize
						__lowerCAmelCase												=  size
						__lowerCAmelCase												=  do_center_crop
						__lowerCAmelCase												=  crop_size
						__lowerCAmelCase												=  do_normalize
						__lowerCAmelCase												=  image_mean
						__lowerCAmelCase												=  image_std
						__lowerCAmelCase												=  do_convert_rgb



					def        UpperCAmelCase_ (    self     )     ->			Any:
						return {
						    "do_resize": self.do_resize,
						    "size": self.size,
						    "do_center_crop": self.do_center_crop,
						    "crop_size": self.crop_size,
						    "do_normalize": self.do_normalize,
						    "image_mean": self.image_mean,
						    "image_std": self.image_std,
						    "do_convert_rgb": self.do_convert_rgb,
						}



					def        UpperCAmelCase_ (    self			,				UpperCamelCase=False			,				UpperCamelCase=False			,				UpperCamelCase=False     )     ->			Union[str, Any]:
						assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

						if equal_resolution:
							__lowerCAmelCase												=  []
							for i in range(self.batch_size     ):
								image_inputs.append(
								    np.random.randint(
								        255			,				size=(self.num_channels, self.max_resolution, self.max_resolution)			,				dtype=np.uinta     )     )
						else:
							__lowerCAmelCase												=  []
							for i in range(self.batch_size     ):
								__lowerCAmelCase							,       __lowerCAmelCase												=  np.random.choice(np.arange(self.min_resolution			,				self.max_resolution     )			,				2     )
								image_inputs.append(np.random.randint(255			,				size=(self.num_channels, width, height)			,				dtype=np.uinta     )     )

						if not numpify and not torchify:
							# PIL expects the channel dimension as last dimension
							__lowerCAmelCase												=  [Image.fromarray(np.moveaxis(UpperCamelCase			,				0			,				-1     )     ) for x in image_inputs]

						if torchify:
							__lowerCAmelCase												=  [torch.from_numpy(UpperCamelCase     ) for x in image_inputs]

						return image_inputs






@require_torch
@require_vision
class 					UpperCAmelCase__	(		UpperCamelCase__							,       unittest.TestCase ):
					a      :     Any        =							ChineseCLIPImageProcessor if is_vision_available() else None



					def        UpperCAmelCase_ (    self     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  ChineseCLIPImageProcessingTester(self			,				do_center_crop=UpperCamelCase     )



					@property
					def        UpperCAmelCase_ (    self     )     ->			List[Any]:
						return self.image_processor_tester.prepare_image_processor_dict()



					def        UpperCAmelCase_ (    self     )     ->			Any:
						__lowerCAmelCase												=  self.image_processing_class(**self.image_processor_dict     )
						self.assertTrue(hasattr(UpperCamelCase			,				"do_resize"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"size"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"do_center_crop"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"center_crop"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"do_normalize"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"image_mean"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"image_std"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"do_convert_rgb"     )     )



					def        UpperCAmelCase_ (    self     )     ->			Optional[int]:
						__lowerCAmelCase												=  self.image_processing_class.from_dict(self.image_processor_dict     )
						self.assertEqual(image_processor.size			,				{"height": 224, "width": 224}     )
						self.assertEqual(image_processor.crop_size			,				{"height": 18, "width": 18}     )

						__lowerCAmelCase												=  self.image_processing_class.from_dict(self.image_processor_dict			,				size=42			,				crop_size=84     )
						self.assertEqual(image_processor.size			,				{"shortest_edge": 42}     )
						self.assertEqual(image_processor.crop_size			,				{"height": 84, "width": 84}     )



					def        UpperCAmelCase_ (    self     )     ->			Optional[int]:
						pass



					def        UpperCAmelCase_ (    self     )     ->			Any:
						# Initialize image_processing
						__lowerCAmelCase												=  self.image_processing_class(**self.image_processor_dict     )
						# create random PIL images
						__lowerCAmelCase												=  self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase     )
						for image in image_inputs:
							self.assertIsInstance(UpperCamelCase			,				Image.Image     )

						# Test not batched input
						__lowerCAmelCase												=  image_processing(image_inputs[0]			,				return_tensors="pt"     ).pixel_values
						self.assertEqual(
						    encoded_images.shape			,				(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )			,				)

						# Test batched
						__lowerCAmelCase												=  image_processing(UpperCamelCase			,				return_tensors="pt"     ).pixel_values
						self.assertEqual(
						    encoded_images.shape			,				(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )			,				)



					def        UpperCAmelCase_ (    self     )     ->			List[str]:
						# Initialize image_processing
						__lowerCAmelCase												=  self.image_processing_class(**self.image_processor_dict     )
						# create random numpy tensors
						__lowerCAmelCase												=  self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase			,				numpify=UpperCamelCase     )
						for image in image_inputs:
							self.assertIsInstance(UpperCamelCase			,				np.ndarray     )

						# Test not batched input
						__lowerCAmelCase												=  image_processing(image_inputs[0]			,				return_tensors="pt"     ).pixel_values
						self.assertEqual(
						    encoded_images.shape			,				(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )			,				)

						# Test batched
						__lowerCAmelCase												=  image_processing(UpperCamelCase			,				return_tensors="pt"     ).pixel_values
						self.assertEqual(
						    encoded_images.shape			,				(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )			,				)



					def        UpperCAmelCase_ (    self     )     ->			Optional[Any]:
						# Initialize image_processing
						__lowerCAmelCase												=  self.image_processing_class(**self.image_processor_dict     )
						# create random PyTorch tensors
						__lowerCAmelCase												=  self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase			,				torchify=UpperCamelCase     )
						for image in image_inputs:
							self.assertIsInstance(UpperCamelCase			,				torch.Tensor     )

						# Test not batched input
						__lowerCAmelCase												=  image_processing(image_inputs[0]			,				return_tensors="pt"     ).pixel_values
						self.assertEqual(
						    encoded_images.shape			,				(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )			,				)

						# Test batched
						__lowerCAmelCase												=  image_processing(UpperCamelCase			,				return_tensors="pt"     ).pixel_values
						self.assertEqual(
						    encoded_images.shape			,				(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )			,				)






@require_torch
@require_vision
class 					UpperCAmelCase__	(		UpperCamelCase__							,       unittest.TestCase ):
					a      :     int        =							ChineseCLIPImageProcessor if is_vision_available() else None



					def        UpperCAmelCase_ (    self     )     ->			Optional[Any]:
						__lowerCAmelCase												=  ChineseCLIPImageProcessingTester(self			,				num_channels=4			,				do_center_crop=UpperCamelCase     )
						__lowerCAmelCase												=  3



					@property
					def        UpperCAmelCase_ (    self     )     ->			str:
						return self.image_processor_tester.prepare_image_processor_dict()



					def        UpperCAmelCase_ (    self     )     ->			Optional[Any]:
						__lowerCAmelCase												=  self.image_processing_class(**self.image_processor_dict     )
						self.assertTrue(hasattr(UpperCamelCase			,				"do_resize"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"size"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"do_center_crop"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"center_crop"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"do_normalize"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"image_mean"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"image_std"     )     )
						self.assertTrue(hasattr(UpperCamelCase			,				"do_convert_rgb"     )     )



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						pass



					def        UpperCAmelCase_ (    self     )     ->			str:
						# Initialize image_processing
						__lowerCAmelCase												=  self.image_processing_class(**self.image_processor_dict     )
						# create random PIL images
						__lowerCAmelCase												=  self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase     )
						for image in image_inputs:
							self.assertIsInstance(UpperCamelCase			,				Image.Image     )

						# Test not batched input
						__lowerCAmelCase												=  image_processing(image_inputs[0]			,				return_tensors="pt"     ).pixel_values
						self.assertEqual(
						    encoded_images.shape			,				(
						        1,
						        self.expected_encoded_image_num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )			,				)

						# Test batched
						__lowerCAmelCase												=  image_processing(UpperCamelCase			,				return_tensors="pt"     ).pixel_values
						self.assertEqual(
						    encoded_images.shape			,				(
						        self.image_processor_tester.batch_size,
						        self.expected_encoded_image_num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )			,				)




'''simple docstring'''



from __future__ import annotations

import math



def   __lowerCAmelCase					(							lowerCamelCase   :						list				,		lowerCamelCase   :						list					):






	'''simple docstring'''




	if len(lowerCamelCase					) != 2 or len(a[0]					) != 2 or len(lowerCamelCase					) != 2 or len(b[0]					) != 2:
		raise Exception("Matrices are not 2x2"					)
	__lowerCAmelCase												=  [
	    [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]],
	    [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]],
	]
	return new_matrix



def   __lowerCAmelCase					(							lowerCamelCase   :						list				,		lowerCamelCase   :						list					):






	'''simple docstring'''




	return [
	    [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row]					)					)]
	    for row in range(len(lowerCamelCase					)					)
	]



def   __lowerCAmelCase					(							lowerCamelCase   :						list				,		lowerCamelCase   :						list					):






	'''simple docstring'''




	return [
	    [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row]					)					)]
	    for row in range(len(lowerCamelCase					)					)
	]



def   __lowerCAmelCase					(							lowerCamelCase   :						list					):






	'''simple docstring'''




	if len(lowerCamelCase					) % 2 != 0 or len(a[0]					) % 2 != 0:
		raise Exception("Odd matrices are not supported!"					)

	__lowerCAmelCase												=  len(lowerCamelCase					)
	__lowerCAmelCase												=  matrix_length // 2

	__lowerCAmelCase												=  [[a[i][j] for j in range(lowerCamelCase				,		lowerCamelCase					)] for i in range(lowerCamelCase					)]
	__lowerCAmelCase												=  [
	    [a[i][j] for j in range(lowerCamelCase				,		lowerCamelCase					)] for i in range(lowerCamelCase				,		lowerCamelCase					)
	]

	__lowerCAmelCase												=  [[a[i][j] for j in range(lowerCamelCase					)] for i in range(lowerCamelCase					)]
	__lowerCAmelCase												=  [[a[i][j] for j in range(lowerCamelCase					)] for i in range(lowerCamelCase				,		lowerCamelCase					)]

	return top_left, top_right, bot_left, bot_right



def   __lowerCAmelCase					(							lowerCamelCase   :						list					):






	'''simple docstring'''




	return len(lowerCamelCase					), len(matrix[0]					)



def   __lowerCAmelCase					(							lowerCamelCase   :						list					):






	'''simple docstring'''




	print("\n".join(str(lowerCamelCase					) for line in matrix					)					)



def   __lowerCAmelCase					(							lowerCamelCase   :						list				,		lowerCamelCase   :						list					):






	'''simple docstring'''




	if matrix_dimensions(lowerCamelCase					) == (2, 2):
		return default_matrix_multiplication(lowerCamelCase				,		lowerCamelCase					)

	__lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase												=  split_matrix(lowerCamelCase					)
	__lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase												=  split_matrix(lowerCamelCase					)

	__lowerCAmelCase												=  actual_strassen(lowerCamelCase				,		matrix_subtraction(lowerCamelCase				,		lowerCamelCase					)					)
	__lowerCAmelCase												=  actual_strassen(matrix_addition(lowerCamelCase				,		lowerCamelCase					)				,		lowerCamelCase					)
	__lowerCAmelCase												=  actual_strassen(matrix_addition(lowerCamelCase				,		lowerCamelCase					)				,		lowerCamelCase					)
	__lowerCAmelCase												=  actual_strassen(lowerCamelCase				,		matrix_subtraction(lowerCamelCase				,		lowerCamelCase					)					)
	__lowerCAmelCase												=  actual_strassen(matrix_addition(lowerCamelCase				,		lowerCamelCase					)				,		matrix_addition(lowerCamelCase				,		lowerCamelCase					)					)
	__lowerCAmelCase												=  actual_strassen(matrix_subtraction(lowerCamelCase				,		lowerCamelCase					)				,		matrix_addition(lowerCamelCase				,		lowerCamelCase					)					)
	__lowerCAmelCase												=  actual_strassen(matrix_subtraction(lowerCamelCase				,		lowerCamelCase					)				,		matrix_addition(lowerCamelCase				,		lowerCamelCase					)					)

	__lowerCAmelCase												=  matrix_addition(matrix_subtraction(matrix_addition(lowerCamelCase				,		lowerCamelCase					)				,		lowerCamelCase					)				,		lowerCamelCase					)
	__lowerCAmelCase												=  matrix_addition(lowerCamelCase				,		lowerCamelCase					)
	__lowerCAmelCase												=  matrix_addition(lowerCamelCase				,		lowerCamelCase					)
	__lowerCAmelCase												=  matrix_subtraction(matrix_subtraction(matrix_addition(lowerCamelCase				,		lowerCamelCase					)				,		lowerCamelCase					)				,		lowerCamelCase					)

	# construct the new matrix from our 4 quadrants
	__lowerCAmelCase												=  []
	for i in range(len(lowerCamelCase					)					):
		new_matrix.append(top_left[i] + top_right[i]					)
	for i in range(len(lowerCamelCase					)					):
		new_matrix.append(bot_left[i] + bot_right[i]					)
	return new_matrix



def   __lowerCAmelCase					(							lowerCamelCase   :						list				,		lowerCamelCase   :						list					):






	'''simple docstring'''




	if matrix_dimensions(lowerCamelCase					)[1] != matrix_dimensions(lowerCamelCase					)[0]:
		__lowerCAmelCase												=  (
		    "Unable to multiply these matrices, please check the dimensions.\n"
		    f'''Matrix A: {matrixa}\n'''
		    f'''Matrix B: {matrixa}'''
		)
		raise Exception(lowerCamelCase					)
	__lowerCAmelCase												=  matrix_dimensions(lowerCamelCase					)
	__lowerCAmelCase												=  matrix_dimensions(lowerCamelCase					)

	if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]:
		return [matrixa, matrixa]

	__lowerCAmelCase												=  max(*lowerCamelCase				,		*lowerCamelCase					)
	__lowerCAmelCase												=  int(math.pow(2				,		math.ceil(math.loga(lowerCamelCase					)					)					)					)
	__lowerCAmelCase												=  matrixa
	__lowerCAmelCase												=  matrixa

	# Adding zeros to the matrices so that the arrays dimensions are the same and also
	# power of 2
	for i in range(0				,		lowerCamelCase					):
		if i < dimensiona[0]:
			for _ in range(dimensiona[1]				,		lowerCamelCase					):
				new_matrixa[i].append(0					)
		else:
			new_matrixa.append([0] * maxim					)
		if i < dimensiona[0]:
			for _ in range(dimensiona[1]				,		lowerCamelCase					):
				new_matrixa[i].append(0					)
		else:
			new_matrixa.append([0] * maxim					)

	__lowerCAmelCase												=  actual_strassen(lowerCamelCase				,		lowerCamelCase					)

	# Removing the additional zeros
	for i in range(0				,		lowerCamelCase					):
		if i < dimensiona[0]:
			for _ in range(dimensiona[1]				,		lowerCamelCase					):
				final_matrix[i].pop()
		else:
			final_matrix.pop()
	return final_matrix


if __name__ == "__main__":
	lowerCAmelCase	:		Tuple				=   [
	    [2, 3, 4, 5],
	    [6, 4, 3, 1],
	    [2, 3, 6, 7],
	    [3, 1, 2, 4],
	    [2, 3, 4, 5],
	    [6, 4, 3, 1],
	    [2, 3, 6, 7],
	    [3, 1, 2, 4],
	    [2, 3, 4, 5],
	    [6, 2, 3, 1],
	]
	lowerCAmelCase	:		Any				=   [[0, 2, 1, 1], [1_6, 2, 3, 3], [2, 2, 7, 7], [1_3, 1_1, 2_2, 4]]
	print(strassen(matrixa, matrixa))