





'''simple docstring'''




import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available







def _lowerCAmelCase     (      *_lowerCAmelCase    )->     Any:




 if not isinstance(__a       ,    __a    ):
  __UpperCAmelCase					=   list(__a    )
 for i in range(len(__a    )    ):
  __UpperCAmelCase					=   None
 gc.collect()
 if is_xpu_available():
  torch.xpu.empty_cache()
 elif is_npu_available():
  torch.npu.empty_cache()
 else:
  torch.cuda.empty_cache()
 return objects







def _lowerCAmelCase     (      _lowerCAmelCase    )->     Optional[Any]:
 __UpperCAmelCase					=   [
     'CUDA out of memory.',  # CUDA OOM
     'cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.',  # CUDNN SNAFU
     'DefaultCPUAllocator: can\'t allocate memory',  # CPU OOM
 ]
 if isinstance(__a       ,    __a    ) and len(exception.args    ) == 1:
  return any(err in exception.args[0] for err in _statements    )
 return False







def _lowerCAmelCase     (      _lowerCAmelCase = None       ,    _lowerCAmelCase = 1_28    )->     Tuple:




 if function is None:
  return functools.partial(__a       ,    starting_batch_size=__a    )

 __UpperCAmelCase					=   starting_batch_size

 def decorator(*_lowerCAmelCase       ,    **_lowerCAmelCase    ):
  nonlocal batch_size
  gc.collect()
  if is_xpu_available():
   torch.xpu.empty_cache()
  elif is_npu_available():
   torch.npu.empty_cache()
  else:
   torch.cuda.empty_cache()
  __UpperCAmelCase					=   list(inspect.signature(__a    ).parameters.keys()    )
  # Guard against user error
  if len(__a    ) < (len(__a    ) + 1):
   __UpperCAmelCase					=   ', '.join([F'{arg}={value}' for arg, value in zip(params[1:]       ,    args[1:]    )]    )
   raise TypeError(
       F'Batch size was passed into `{function.__name__}` as the first argument when called.'
       F'Remove this as the decorator already does so: `{function.__name__}({arg_str})`'    )
  while True:
   if batch_size == 0:
    raise RuntimeError('No executable batch size found, reached zero.'    )
   try:
    return function(__a       ,    *__a       ,    **__a    )
   except Exception as e:
    if should_reduce_batch_size(__a    ):
     gc.collect()
     if is_xpu_available():
      torch.xpu.empty_cache()
     elif is_npu_available():
      torch.npu.empty_cache()
     else:
      torch.cuda.empty_cache()
     batch_size //= 2
    else:
     raise

 return decorator












'''simple docstring'''




from __future__ import annotations

from collections.abc import Sequence
from typing import Literal







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     str | Literal[False]:
 __UpperCAmelCase					=   list(_lowerCAmelCase    )
 __UpperCAmelCase					=   list(_lowerCAmelCase    )
 __UpperCAmelCase					=   0
 for i in range(len(_lowerCAmelCase    )    ):
  if lista[i] != lista[i]:
   count += 1
   __UpperCAmelCase					=   '_'
 if count > 1:
  return False
 else:
  return "".join(_lowerCAmelCase    )







def _lowerCAmelCase     (      _lowerCAmelCase    )->     list[str]:
 __UpperCAmelCase					=   []
 while True:
  __UpperCAmelCase					=   ['$'] * len(_lowerCAmelCase    )
  __UpperCAmelCase					=   []
  for i in range(len(_lowerCAmelCase    )    ):
   for j in range(i + 1       ,    len(_lowerCAmelCase    )    ):
    __UpperCAmelCase					=   compare_string(binary[i]       ,    binary[j]    )
    if k is False:
     __UpperCAmelCase					=   '*'
     __UpperCAmelCase					=   '*'
     temp.append('X'    )
  for i in range(len(_lowerCAmelCase    )    ):
   if checka[i] == "$":
    pi.append(binary[i]    )
  if len(_lowerCAmelCase    ) == 0:
   return pi
  __UpperCAmelCase					=   list(set(_lowerCAmelCase    )    )







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     list[str]:
 __UpperCAmelCase					=   []
 for minterm in minterms:
  __UpperCAmelCase					=   ''
  for _ in range(_lowerCAmelCase    ):
   __UpperCAmelCase					=   str(minterm % 2    ) + string
   minterm //= 2
  temp.append(_lowerCAmelCase    )
 return temp







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )->     bool:
 __UpperCAmelCase					=   list(_lowerCAmelCase    )
 __UpperCAmelCase					=   list(_lowerCAmelCase    )
 __UpperCAmelCase					=   0
 for i in range(len(_lowerCAmelCase    )    ):
  if lista[i] != lista[i]:
   count_n += 1
 return count_n == count







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     list[str]:
 __UpperCAmelCase					=   []
 __UpperCAmelCase					=   [0] * len(_lowerCAmelCase    )
 for i in range(len(chart[0]    )    ):
  __UpperCAmelCase					=   0
  __UpperCAmelCase					=   -1
  for j in range(len(_lowerCAmelCase    )    ):
   if chart[j][i] == 1:
    count += 1
    __UpperCAmelCase					=   j
  if count == 1:
   __UpperCAmelCase					=   1
 for i in range(len(_lowerCAmelCase    )    ):
  if select[i] == 1:
   for j in range(len(chart[0]    )    ):
    if chart[i][j] == 1:
     for k in range(len(_lowerCAmelCase    )    ):
      __UpperCAmelCase					=   0
   temp.append(prime_implicants[i]    )
 while True:
  __UpperCAmelCase					=   0
  __UpperCAmelCase					=   -1
  __UpperCAmelCase					=   0
  for i in range(len(_lowerCAmelCase    )    ):
   __UpperCAmelCase					=   chart[i].count(1    )
   if count_n > max_n:
    __UpperCAmelCase					=   count_n
    __UpperCAmelCase					=   i

  if max_n == 0:
   return temp

  temp.append(prime_implicants[rem]    )

  for i in range(len(chart[0]    )    ):
   if chart[rem][i] == 1:
    for j in range(len(_lowerCAmelCase    )    ):
     __UpperCAmelCase					=   0







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     list[list[int]]:
 __UpperCAmelCase					=   [[0 for x in range(len(_lowerCAmelCase    )    )] for x in range(len(_lowerCAmelCase    )    )]
 for i in range(len(_lowerCAmelCase    )    ):
  __UpperCAmelCase					=   prime_implicants[i].count('_'    )
  for j in range(len(_lowerCAmelCase    )    ):
   if is_for_table(prime_implicants[i]       ,    binary[j]       ,    _lowerCAmelCase    ):
    __UpperCAmelCase					=   1

 return chart







def _lowerCAmelCase     (      )->     None:
 __UpperCAmelCase					=   int(input('Enter the no. of variables\n'    )    )
 __UpperCAmelCase					=   [
     float(_lowerCAmelCase    )
     for x in input(
         'Enter the decimal representation of Minterms \'Spaces Separated\'\n'    ).split()
 ]
 __UpperCAmelCase					=   decimal_to_binary(_lowerCAmelCase       ,    _lowerCAmelCase    )

 __UpperCAmelCase					=   check(_lowerCAmelCase    )
 print('Prime Implicants are:'    )
 print(_lowerCAmelCase    )
 __UpperCAmelCase					=   prime_implicant_chart(_lowerCAmelCase       ,    _lowerCAmelCase    )

 __UpperCAmelCase					=   selection(_lowerCAmelCase       ,    _lowerCAmelCase    )
 print('Essential Prime Implicants are:'    )
 print(_lowerCAmelCase    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    main()





