






'''simple docstring'''
# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

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


if TYPE_CHECKING:
 import torch


class        _UpperCAmelCase						(		TensorFormatter[Mapping, 'torch.Tensor', Mapping]			):




    def __init__(			self  :       Union[str, Any]  ,     a  :       int=None  ,     **a  :       str			):






       '''simple docstring'''






       super().__init__(features=a			)
       lowercase_	:    List[str]           =     torch_tensor_kwargs
       import torch  # noqa import torch at initialization




    def 				lowerCAmelCase__				(			self  :       str  ,     a  :       Dict			):






       '''simple docstring'''






       import torch

       if isinstance(a  ,     a			) and column:
          if all(
              isinstance(a  ,     torch.Tensor			) and x.shape == column[0].shape and x.dtype == column[0].dtype
              for x in column			):
             return torch.stack(a			)
       return column




    def 				lowerCAmelCase__				(			self  :       Optional[Any]  ,     a  :       Union[str, Any]			):






       '''simple docstring'''






       import torch

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

       lowercase_	:    Optional[int]           =     {}

       if isinstance(a  ,     (np.number, np.ndarray)			) and np.issubdtype(value.dtype  ,     np.integer			):
          lowercase_	:    List[Any]           =     {"dtype": torch.intaa}
       elif isinstance(a  ,     (np.number, np.ndarray)			) and np.issubdtype(value.dtype  ,     np.floating			):
          lowercase_	:    List[Any]           =     {"dtype": torch.floataa}
       elif config.PIL_AVAILABLE and "PIL" in sys.modules:
          import PIL.Image

          if isinstance(a  ,     PIL.Image.Image			):
             lowercase_	:    Dict           =     np.asarray(a			)
       return torch.tensor(a  ,     **{**default_dtype, **self.torch_tensor_kwargs}			)




    def 				lowerCAmelCase__				(			self  :       Optional[int]  ,     a  :       Dict			):






       '''simple docstring'''






       import torch

       # support for torch, tf, jax etc.
       if hasattr(a  ,     "__array__"			) and not isinstance(a  ,     torch.Tensor			):
          lowercase_	:    Tuple           =     data_struct.__array__()
       # support for nested types like struct of list of struct
       if isinstance(a  ,     np.ndarray			):
          if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
             return self._consolidate([self.recursive_tensorize(a			) for substruct in data_struct]			)
       elif isinstance(a  ,     (list, tuple)			):
          return self._consolidate([self.recursive_tensorize(a			) for substruct in data_struct]			)
       return self._tensorize(a			)




    def 				lowerCAmelCase__				(			self  :       Optional[int]  ,     a  :       dict			):






       '''simple docstring'''






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




    def 				lowerCAmelCase__				(			self  :       Tuple  ,     a  :       pa.Table			):






       '''simple docstring'''






       lowercase_	:    Optional[Any]           =     self.numpy_arrow_extractor().extract_row(a			)
       lowercase_	:    Union[str, Any]           =     self.python_features_decoder.decode_row(a			)
       return self.recursive_tensorize(a			)




    def 				lowerCAmelCase__				(			self  :       str  ,     a  :       pa.Table			):






       '''simple docstring'''






       lowercase_	:    List[str]           =     self.numpy_arrow_extractor().extract_column(a			)
       lowercase_	:    Any           =     self.python_features_decoder.decode_column(a  ,     pa_table.column_names[0]			)
       lowercase_	:    Tuple           =     self.recursive_tensorize(a			)
       lowercase_	:    List[Any]           =     self._consolidate(a			)
       return column





    def 				lowerCAmelCase__				(			self  :       int  ,     a  :       pa.Table			):






       '''simple docstring'''






       lowercase_	:    Any           =     self.numpy_arrow_extractor().extract_batch(a			)
       lowercase_	:    List[Any]           =     self.python_features_decoder.decode_batch(a			)
       lowercase_	:    str           =     self.recursive_tensorize(a			)
       for column_name in batch:
          lowercase_	:    str           =     self._consolidate(batch[column_name]			)
       return batch












'''simple docstring'''
def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   def update_area_of_max_square(_UpperCamelCase						,		_UpperCamelCase							) -> int:
      # BASE CASE
      if row >= rows or col >= cols:
         return 0

      lowercase_	:    List[str]           =     update_area_of_max_square(_UpperCamelCase						,		col + 1							)
      lowercase_	:    List[Any]           =     update_area_of_max_square(row + 1						,		col + 1							)
      lowercase_	:    Tuple           =     update_area_of_max_square(row + 1						,		_UpperCamelCase							)

      if mat[row][col]:
         lowercase_	:    Optional[int]           =     1 + min([right, diagonal, down]							)
         lowercase_	:    Any           =     max(largest_square_area[0]						,		_UpperCamelCase							)
         return sub_problem_sol
      else:
         return 0

   lowercase_	:    int           =     [0]
   update_area_of_max_square(0						,		0							)
   return largest_square_area[0]




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   def update_area_of_max_square_using_dp_array(
       _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							) -> int:
      if row >= rows or col >= cols:
         return 0
      if dp_array[row][col] != -1:
         return dp_array[row][col]

      lowercase_	:    Dict           =     update_area_of_max_square_using_dp_array(_UpperCamelCase						,		col + 1						,		_UpperCamelCase							)
      lowercase_	:    str           =     update_area_of_max_square_using_dp_array(row + 1						,		col + 1						,		_UpperCamelCase							)
      lowercase_	:    Optional[Any]           =     update_area_of_max_square_using_dp_array(row + 1						,		_UpperCamelCase						,		_UpperCamelCase							)

      if mat[row][col]:
         lowercase_	:    Tuple           =     1 + min([right, diagonal, down]							)
         lowercase_	:    int           =     max(largest_square_area[0]						,		_UpperCamelCase							)
         lowercase_	:    Dict           =     sub_problem_sol
         return sub_problem_sol
      else:
         return 0

   lowercase_	:    Any           =     [0]
   lowercase_	:    Optional[int]           =     [[-1] * cols for _ in range(_UpperCamelCase							)]
   update_area_of_max_square_using_dp_array(0						,		0						,		_UpperCamelCase							)

   return largest_square_area[0]




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   lowercase_	:    str           =     [[0] * (cols + 1) for _ in range(rows + 1							)]
   lowercase_	:    List[Any]           =     0
   for row in range(rows - 1						,		-1						,		-1							):
      for col in range(cols - 1						,		-1						,		-1							):
         lowercase_	:    Tuple           =     dp_array[row][col + 1]
         lowercase_	:    List[str]           =     dp_array[row + 1][col + 1]
         lowercase_	:    List[Any]           =     dp_array[row + 1][col]

         if mat[row][col] == 1:
            lowercase_	:    Any           =     1 + min(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)
            lowercase_	:    Any           =     max(dp_array[row][col]						,		_UpperCamelCase							)
         else:
            lowercase_	:    int           =     0

   return largest_square_area




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   lowercase_	:    Optional[int]           =     [0] * (cols + 1)
   lowercase_	:    Union[str, Any]           =     [0] * (cols + 1)
   lowercase_	:    int           =     0
   for row in range(rows - 1						,		-1						,		-1							):
      for col in range(cols - 1						,		-1						,		-1							):
         lowercase_	:    Dict           =     current_row[col + 1]
         lowercase_	:    List[Any]           =     next_row[col + 1]
         lowercase_	:    Tuple           =     next_row[col]

         if mat[row][col] == 1:
            lowercase_	:    Dict           =     1 + min(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)
            lowercase_	:    int           =     max(current_row[col]						,		_UpperCamelCase							)
         else:
            lowercase_	:    Tuple           =     0
      lowercase_	:    Optional[Any]           =     current_row

   return largest_square_area


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))




