





from typing import Dict, Optional, Tuple

import torch






def 				_lowerCAmelCase			(	_a :       torch.Tensor      )						->      torch.Tensor:
     lowerCAmelCase_       :							List[str]			 =		boundaries[1] - boundaries[0]
     lowerCAmelCase_       :							List[Any]			 =		boundaries + step / 2
     lowerCAmelCase_       :							Tuple			 =		torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1      )]       , dim=0      )
     return bin_centers






def 				_lowerCAmelCase			(	_a :       torch.Tensor       , _a :       torch.Tensor       , )						->      Tuple[torch.Tensor, torch.Tensor]:
     lowerCAmelCase_       :							Optional[int]			 =		_calculate_bin_centers(UpperCAmelCase_      )
     return (
         torch.sum(aligned_distance_error_probs * bin_centers       , dim=-1      ),
         bin_centers[-1],
     )






def 				_lowerCAmelCase			(	_a :       torch.Tensor       , _a :       int = 31       , _a :       int = 64       , **_a :       List[Any]       , )						->      Dict[str, torch.Tensor]:
     lowerCAmelCase_       :							Union[str, Any]			 =		torch.linspace(0       , UpperCAmelCase_       , steps=(no_bins - 1)       , device=logits.device      )

     lowerCAmelCase_       :							Union[str, Any]			 =		torch.nn.functional.softmax(UpperCAmelCase_       , dim=-1      )
     lowerCAmelCase_ ,	lowerCAmelCase_       :							Optional[int]			 =		_calculate_expected_aligned_error(
         alignment_confidence_breaks=UpperCAmelCase_       , aligned_distance_error_probs=UpperCAmelCase_       , )

     return {
         "aligned_confidence_probs": aligned_confidence_probs,
         "predicted_aligned_error": predicted_aligned_error,
         "max_predicted_aligned_error": max_predicted_aligned_error,
     }







def 				_lowerCAmelCase			(	_a :       torch.Tensor       , _a :       Optional[torch.Tensor] = None       , _a :       int = 31       , _a :       int = 64       , _a :       float = 1E-8       , **_a :       Any       , )						->      torch.Tensor:




     if residue_weights is None:
          lowerCAmelCase_       :							Any			 =		logits.new_ones(logits.shape[-2]      )

     lowerCAmelCase_       :							List[str]			 =		torch.linspace(0       , UpperCAmelCase_       , steps=(no_bins - 1)       , device=logits.device      )

     lowerCAmelCase_       :							int			 =		_calculate_bin_centers(UpperCAmelCase_      )
     torch.sum(UpperCAmelCase_      )
     lowerCAmelCase_       :							Dict			 =		logits.shape[-2]
     lowerCAmelCase_       :							Optional[int]			 =		max(UpperCAmelCase_       , 19      )

     lowerCAmelCase_       :							Tuple			 =		1.24 * (clipped_n - 15) ** (1.0 / 3) - 1.8

     lowerCAmelCase_       :							int			 =		torch.nn.functional.softmax(UpperCAmelCase_       , dim=-1      )

     lowerCAmelCase_       :							List[str]			 =		1.0 / (1 + (bin_centers**2) / (da**2))
     lowerCAmelCase_       :							Any			 =		torch.sum(probs * tm_per_bin       , dim=-1      )

     lowerCAmelCase_       :							List[Any]			 =		residue_weights / (eps + residue_weights.sum())
     lowerCAmelCase_       :							Tuple			 =		torch.sum(predicted_tm_term * normed_residue_mask       , dim=-1      )

     lowerCAmelCase_       :							Any			 =		per_alignment * residue_weights

     lowerCAmelCase_       :							Union[str, Any]			 =		(weighted == torch.max(UpperCAmelCase_      )).nonzero()[0]
     return per_alignment[tuple(UpperCAmelCase_      )]






import math
from timeit import timeit





def 							lowerCamelCase    (							UpperCAmelCase_	:      int      )->			int:

       """simple docstring"""




       a			   =0
       a			   =abs(UpperCAmelCase_      )
       while True:
              a			   =n // 10
              digits += 1
              if n == 0:
                     break
       return digits





def 							lowerCamelCase    (							UpperCAmelCase_	:      int      )->			int:

       """simple docstring"""




       return 1 if n == 0 else math.floor(math.log(abs(UpperCAmelCase_      )   ,       10      ) + 1      )





def 							lowerCamelCase    (							UpperCAmelCase_	:      int      )->			int:

       """simple docstring"""




       return len(str(abs(UpperCAmelCase_      )      )      )





def 							lowerCamelCase    (							)->			None:

       """simple docstring"""




       from collections.abc import Callable

       def benchmark_a_function(UpperCAmelCase_	:      Callable   ,       UpperCAmelCase_	:      int      ) -> None:
              a			   =F'''{func.__name__}({value})'''
              a			   =timeit(F'''__main__.{call}'''   ,       setup="""import __main__"""      )
              print(F'''{call}: {func(UpperCAmelCase_      )} -- {timing} seconds'''      )

       for value in (26_2144, 1125_8999_0684_2624, 126_7650_6002_2822_9401_4967_0320_5376):
              for func in (num_digits, num_digits_fast, num_digits_faster):
                     benchmark_a_function(UpperCAmelCase_   ,       UpperCAmelCase_      )
              print()


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    benchmark()
