



'''simple docstring'''

from typing import Dict, Optional, Tuple

import torch



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



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



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

		_a  :   List[str]				 =							torch.nn.functional.softmax(lowerCAmelCase_		,    dim=-1						)
		_a  :   List[Any]				 =							_calculate_expected_aligned_error(
		    alignment_confidence_breaks=lowerCAmelCase_		,    aligned_distance_error_probs=lowerCAmelCase_		,    )

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



def   __lowerCamelCase  (   lowerCAmelCase_		,    lowerCAmelCase_ = None		,    lowerCAmelCase_ = 31		,    lowerCAmelCase_ = 64		,    lowerCAmelCase_ = 1E-8		,    **lowerCAmelCase_		,    )    ->   torch.Tensor:
		if residue_weights is None:
				_a  :   Optional[int]				 =							logits.new_ones(logits.shape[-2]						)

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

		_a  :   Union[str, Any]				 =							_calculate_bin_centers(lowerCAmelCase_						)
		torch.sum(lowerCAmelCase_						)
		_a  :   str				 =							logits.shape[-2]
		_a  :   int				 =							max(lowerCAmelCase_		,    19						)

		_a  :   Optional[Any]				 =							1.24 * (clipped_n - 15) ** (1.0 / 3) - 1.8

		_a  :   Union[str, Any]				 =							torch.nn.functional.softmax(lowerCAmelCase_		,    dim=-1						)

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

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

		_a  :   Tuple				 =							per_alignment * residue_weights

		_a  :   List[Any]				 =							(weighted == torch.max(lowerCAmelCase_						)).nonzero()[0]
		return per_alignment[tuple(lowerCAmelCase_						)]









'''simple docstring'''

def   __lowerCamelCase  (   lowerCAmelCase_						)    ->   tuple[int, int]:
		# base case
		if len(lowerCAmelCase_						) == 1:
				return a[0], a[0]
		else:
				# split A into half.
				_a  :   Union[str, Any]				 =							a[: len(lowerCAmelCase_						) // 2]
				_a  :   List[Any]				 =							a[len(lowerCAmelCase_						) // 2 :]

				# 2 sub problems, 1/2 of original size.
				_a ,      _a  :   Any				 =							max_difference(lowerCAmelCase_						)
				_a ,      _a  :   Any				 =							max_difference(lowerCAmelCase_						)

				# get min of first and max of second
				# linear time
				_a  :   Union[str, Any]				 =							min(lowerCAmelCase_						)
				_a  :   Optional[int]				 =							max(lowerCAmelCase_						)

				# 3 cases, either (small1, big1),
				# (min_first, max_second), (small2, big2)
				# constant comparisons
				if biga - smalla > max_second - min_first and biga - smalla > biga - smalla:
						return smalla, biga
				elif biga - smalla > max_second - min_first:
						return smalla, biga
				else:
						return min_first, max_second


if __name__ == "__main__":
							import doctest

							doctest.testmod()




