






'''simple docstring'''
from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				torch.Tensor:






					"""simple docstring"""







					if not inplace:
										a_									=   ma + ma
					else:
										ma += ma

					return ma

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				torch.Tensor:






					"""simple docstring"""







					a_									=   -1 * len(_UpperCAmelCase )
					a_									=   list(range(len(tensor.shape[:zero_index] ) ) )
					return tensor.permute(first_inds + [zero_index + i for i in inds] )

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				torch.Tensor:






					"""simple docstring"""







					return t.reshape(t.shape[:-no_dims] + (-1,) )

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase = 1E-4 )				->				torch.Tensor:






					"""simple docstring"""







					a_									=   mask.expand(*value.shape )
					return torch.sum(mask * value					,  dim=_UpperCAmelCase ) / (eps + torch.sum(_UpperCAmelCase					,  dim=_UpperCAmelCase ))

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase = 2.3_1_2_5					,  _UpperCAmelCase = 2_1.6_8_7_5					,  _UpperCAmelCase = 6_4 )				->				torch.Tensor:






					"""simple docstring"""







					a_									=   torch.linspace(_UpperCAmelCase					,  _UpperCAmelCase					,  no_bins - 1					,  device=pts.device )
					a_									=   torch.sqrt(torch.sum((pts.unsqueeze(-2 ) - pts.unsqueeze(-3 )) ** 2					,  dim=-1 ) )
					return torch.bucketize(_UpperCAmelCase					,  _UpperCAmelCase )

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				dict:






					"""simple docstring"""







					a_									=   dicts[0]
					a_									=   {}
					for k, v in first.items():
										a_									=   [d[k] for d in dicts]
										if isinstance(_UpperCAmelCase					,  _UpperCAmelCase ):
															a_									=   dict_multimap(_UpperCAmelCase					,  _UpperCAmelCase )
										else:
															a_									=   fn(_UpperCAmelCase )

					return new_dict

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				torch.Tensor:






					"""simple docstring"""







					a_									=   v_bins.view(((1,) * len(x.shape )) + (len(_UpperCAmelCase ),) )
					a_									=   x[..., None] - reshaped_bins
					a_									=   torch.argmin(torch.abs(_UpperCAmelCase )					,  dim=-1 )
					return nn.functional.one_hot(_UpperCAmelCase					,  num_classes=len(_UpperCAmelCase ) ).float()

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase = 0					,  _UpperCAmelCase = 0 )				->				torch.Tensor:






					"""simple docstring"""







					a_									=   []
					for i, s in enumerate(data.shape[:no_batch_dims] ):
										a_									=   torch.arange(_UpperCAmelCase )
										a_									=   r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape ) - i - 1))) )
										ranges.append(_UpperCAmelCase )

					a_									=   [slice(_UpperCAmelCase ) for _ in range(len(data.shape ) - no_batch_dims )]
					a_									=   inds
					ranges.extend(_UpperCAmelCase )
					# Matt note: Editing this to get around the behaviour of using a list as an array index changing
					# in recent Numpy versions
					return data[tuple(_UpperCAmelCase )]


__lowerCAmelCase							=TypeVar("T")

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				Dict[Any, Union[dict, list, tuple, Any]]:






					"""simple docstring"""







					a_									=   {}
					for k, v in dic.items():
										if isinstance(_UpperCAmelCase					,  _UpperCAmelCase ):
															a_									=   dict_map(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
										else:
															a_									=   tree_map(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )

					return new_dict

@overload
def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				Any:






					"""simple docstring"""







					...

@overload
def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				dict:






					"""simple docstring"""







					...

@overload
def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list:






					"""simple docstring"""







					...

@overload
def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				tuple:






					"""simple docstring"""







					...

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				List[Any]:






					"""simple docstring"""







					if isinstance(_UpperCAmelCase					,  _UpperCAmelCase ):
										return dict_map(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					elif isinstance(_UpperCAmelCase					,  _UpperCAmelCase ):
										return [tree_map(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase ) for x in tree]
					elif isinstance(_UpperCAmelCase					,  _UpperCAmelCase ):
										return tuple(tree_map(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase ) for x in tree )
					elif isinstance(_UpperCAmelCase					,  _UpperCAmelCase ):
										return fn(_UpperCAmelCase )
					else:
										print(type(_UpperCAmelCase ) )
										raise ValueError('Not supported' )





__lowerCAmelCase							=partial(tree_map, leaf_type=torch.Tensor)









'''simple docstring'''
def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list[int]:






					"""simple docstring"""







					if hue < 0 or hue > 3_6_0:
										raise Exception('hue should be between 0 and 360' )

					if saturation < 0 or saturation > 1:
										raise Exception('saturation should be between 0 and 1' )

					if value < 0 or value > 1:
										raise Exception('value should be between 0 and 1' )

					a_									=   value * saturation
					a_									=   hue / 6_0
					a_									=   chroma * (1 - abs(hue_section % 2 - 1 ))
					a_									=   value - chroma

					if hue_section >= 0 and hue_section <= 1:
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (match_value) )
					elif hue_section > 1 and hue_section <= 2:
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (match_value) )
					elif hue_section > 2 and hue_section <= 3:
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
					elif hue_section > 3 and hue_section <= 4:
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
					elif hue_section > 4 and hue_section <= 5:
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
					else:
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )

					return [red, green, blue]

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list[float]:






					"""simple docstring"""







					if red < 0 or red > 2_5_5:
										raise Exception('red should be between 0 and 255' )

					if green < 0 or green > 2_5_5:
										raise Exception('green should be between 0 and 255' )

					if blue < 0 or blue > 2_5_5:
										raise Exception('blue should be between 0 and 255' )

					a_									=   red / 2_5_5
					a_									=   green / 2_5_5
					a_									=   blue / 2_5_5
					a_									=   max(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					a_									=   value - min(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					a_									=   0 if value == 0 else chroma / value

					if chroma == 0:
										a_									=   0.0
					elif value == float_red:
										a_									=   6_0 * (0 + (float_green - float_blue) / chroma)
					elif value == float_green:
										a_									=   6_0 * (2 + (float_blue - float_red) / chroma)
					else:
										a_									=   6_0 * (4 + (float_red - float_green) / chroma)

					a_									=   (hue + 3_6_0) % 3_6_0

					return [hue, saturation, value]





def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				bool:






					"""simple docstring"""







					a_									=   abs(hsv_a[0] - hsv_a[0] ) < 0.2
					a_									=   abs(hsv_a[1] - hsv_a[1] ) < 0.0_0_2
					a_									=   abs(hsv_a[2] - hsv_a[2] ) < 0.0_0_2

					return check_hue and check_saturation and check_value

