
import sys
from collections.abc import Mapping

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter






class 							SCREAMING_SNAKE_CASE__     (				TensorFormatter[Mapping, np.ndarray, Mapping]     ):

							"""simple docstring"""






							def __init__(	self  ,						SCREAMING_SNAKE_CASE_=None  ,						**SCREAMING_SNAKE_CASE_      )-> Any:





													'''simple docstring'''






													super().__init__(features=SCREAMING_SNAKE_CASE_      )
													__UpperCamelCase  	=      np_array_kwargs



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> Optional[int]:





													'''simple docstring'''






													if isinstance(SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_      ):
																			if column and all(
																			    isinstance(SCREAMING_SNAKE_CASE_  ,						np.ndarray      ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column      ):
																									return np.stack(SCREAMING_SNAKE_CASE_      )
																			else:
																									# don't use np.array(column, dtype=object)
																									# since it fails in certain cases
																									# see https://stackoverflow.com/q/51005699
																									__UpperCamelCase  	=      np.empty(len(SCREAMING_SNAKE_CASE_      )  ,						dtype=SCREAMING_SNAKE_CASE_      )
																									__UpperCamelCase  	=      column
																									return out
													return column



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> Optional[Any]:





													'''simple docstring'''






													if isinstance(SCREAMING_SNAKE_CASE_  ,						(str, bytes, type(SCREAMING_SNAKE_CASE_      ))      ):
																			return value
													elif isinstance(SCREAMING_SNAKE_CASE_  ,						(np.character, np.ndarray)      ) and np.issubdtype(value.dtype  ,						np.character      ):
																			return value
													elif isinstance(SCREAMING_SNAKE_CASE_  ,						np.number      ):
																			return value

													__UpperCamelCase  	=      {}

													if isinstance(SCREAMING_SNAKE_CASE_  ,						np.ndarray      ) and np.issubdtype(value.dtype  ,						np.integer      ):
																			__UpperCamelCase  	=      {'''dtype''': np.intaa}
													elif isinstance(SCREAMING_SNAKE_CASE_  ,						np.ndarray      ) and np.issubdtype(value.dtype  ,						np.floating      ):
																			__UpperCamelCase  	=      {'''dtype''': np.floataa}
													elif config.PIL_AVAILABLE and "PIL" in sys.modules:
																			import PIL.Image

																			if isinstance(SCREAMING_SNAKE_CASE_  ,						PIL.Image.Image      ):
																									return np.asarray(SCREAMING_SNAKE_CASE_  ,						**self.np_array_kwargs      )

													return np.asarray(SCREAMING_SNAKE_CASE_  ,						**{**default_dtype, **self.np_array_kwargs}      )



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> Dict:





													'''simple docstring'''






													if config.TORCH_AVAILABLE and "torch" in sys.modules:
																			import torch

																			if isinstance(SCREAMING_SNAKE_CASE_  ,						torch.Tensor      ):
																									return self._tensorize(data_struct.detach().cpu().numpy()[()]      )
													if hasattr(SCREAMING_SNAKE_CASE_  ,						'''__array__'''      ) and not isinstance(SCREAMING_SNAKE_CASE_  ,						(np.ndarray, np.character, np.number)      ):
																			__UpperCamelCase  	=      data_struct.__array__()
													# support for nested types like struct of list of struct
													if isinstance(SCREAMING_SNAKE_CASE_  ,						np.ndarray      ):
																			if data_struct.dtype == object:
																									return self._consolidate([self.recursive_tensorize(SCREAMING_SNAKE_CASE_      ) for substruct in data_struct]      )
													if isinstance(SCREAMING_SNAKE_CASE_  ,						(list, tuple)      ):
																			return self._consolidate([self.recursive_tensorize(SCREAMING_SNAKE_CASE_      ) for substruct in data_struct]      )
													return self._tensorize(SCREAMING_SNAKE_CASE_      )



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> int:





													'''simple docstring'''






													return map_nested(self._recursive_tensorize  ,						SCREAMING_SNAKE_CASE_  ,						map_list=SCREAMING_SNAKE_CASE_      )



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> Mapping:





													'''simple docstring'''






													__UpperCamelCase  	=      self.numpy_arrow_extractor().extract_row(SCREAMING_SNAKE_CASE_      )
													__UpperCamelCase  	=      self.python_features_decoder.decode_row(SCREAMING_SNAKE_CASE_      )
													return self.recursive_tensorize(SCREAMING_SNAKE_CASE_      )



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> np.ndarray:





													'''simple docstring'''






													__UpperCamelCase  	=      self.numpy_arrow_extractor().extract_column(SCREAMING_SNAKE_CASE_      )
													__UpperCamelCase  	=      self.python_features_decoder.decode_column(SCREAMING_SNAKE_CASE_  ,						pa_table.column_names[0]      )
													__UpperCamelCase  	=      self.recursive_tensorize(SCREAMING_SNAKE_CASE_      )
													__UpperCamelCase  	=      self._consolidate(SCREAMING_SNAKE_CASE_      )
													return column



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> Mapping:





													'''simple docstring'''






													__UpperCamelCase  	=      self.numpy_arrow_extractor().extract_batch(SCREAMING_SNAKE_CASE_      )
													__UpperCamelCase  	=      self.python_features_decoder.decode_batch(SCREAMING_SNAKE_CASE_      )
													__UpperCamelCase  	=      self.recursive_tensorize(SCREAMING_SNAKE_CASE_      )
													for column_name in batch:
																			__UpperCamelCase  	=      self._consolidate(batch[column_name]      )
													return batch










"""simple docstring"""

from __future__ import annotations

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	assert (
	    c % greatest_common_divisor(_UpperCAmelCase	,   _UpperCAmelCase						) == 0
	)  # greatest_common_divisor(a,b) function implemented below
	((A_) ,			(A_) ,			(A_))	:					List[Any]						 =		extended_gcd(_UpperCAmelCase	,   _UpperCAmelCase						)  # extended_gcd(a,b) function implemented below
	A_	:					int						 =		c / d
	return (r * x, r * y)

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase = 2						):




	"""simple docstring"""






	((A_) ,			(A_))	:					Any						 =		diophantine(_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase						)  # Initial value
	A_	:					Union[str, Any]						 =		greatest_common_divisor(_UpperCAmelCase	,   _UpperCAmelCase						)
	A_	:					List[str]						 =		a // d
	A_	:					str						 =		b // d

	for i in range(_UpperCAmelCase						):
		A_	:					int						 =		xa + i * q
		A_	:					str						 =		ya - i * p
		print(_UpperCAmelCase	,   _UpperCAmelCase						)

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	if a < b:
		A_ ,			A_	:					Tuple						 =		b, a

	while a % b != 0:
		A_ ,			A_	:					int						 =		b, a % b

	return b

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	assert a >= 0 and b >= 0

	if b == 0:
		A_ ,			A_ ,			A_	:					str						 =		a, 1, 0
	else:
		((A_) ,			(A_) ,			(A_))	:					List[str]						 =		extended_gcd(_UpperCAmelCase	,   a % b						)
		A_	:					Optional[int]						 =		q
		A_	:					Tuple						 =		p - q * (a // b)

	assert a % d == 0 and b % d == 0
	assert d == a * x + b * y

	return (d, x, y)


if __name__ == "__main__":
		from doctest import testmod

		testmod(name='diophantine', verbose=True)
		testmod(name='diophantine_all_soln', verbose=True)
		testmod(name='extended_gcd', verbose=True)
		testmod(name='greatest_common_divisor', verbose=True)





