






from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
A__      :     str       =					Lock()






def     a     (		lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_							):






				'''simple docstring'''
				global process_lock

				# we perform n swaps since after n swaps we know we are sorted
				# we *could* stop early if we are sorted already, but it takes as long to
				# find out we are sorted as it does to sort the list with this algorithm
				for i in range(0						,  10							):
								if (i + position) % 2 == 0 and r_send is not None:
												# send your value to your right neighbor
												process_lock.acquire()
												r_send[1].send(lowerCamelCase_							)
												process_lock.release()

												# receive your right neighbor's value
												process_lock.acquire()
												lowercase__  					=     rr_cv[0].recv()
												process_lock.release()

												# take the lower value since you are on the left
												lowercase__  					=     min(lowerCamelCase_						,  lowerCamelCase_							)
								elif (i + position) % 2 != 0 and l_send is not None:
												# send your value to your left neighbor
												process_lock.acquire()
												l_send[1].send(lowerCamelCase_							)
												process_lock.release()

												# receive your left neighbor's value
												process_lock.acquire()
												lowercase__  					=     lr_cv[0].recv()
												process_lock.release()

												# take the higher value since you are on the right
												lowercase__  					=     max(lowerCamelCase_						,  lowerCamelCase_							)
    # after all swaps are performed, send the values back to main
				result_pipe[1].send(lowerCamelCase_							)






def     a     (		lowerCamelCase_							):






				'''simple docstring'''
				lowercase__  					=     []
				lowercase__  					=     []
				# initialize the list of pipes where the values will be retrieved
				for _ in arr:
								result_pipe.append(Pipe()							)
				# creates the processes
				# the first and last process only have one neighbor so they are made outside
				# of the loop
				lowercase__  					=     Pipe()
				lowercase__  					=     Pipe()
				process_array_.append(
				    Process(
				        target=lowerCamelCase_						,  args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0])						,  )							)
				lowercase__  					=     temp_rs
				lowercase__  					=     temp_rr

				for i in range(1						,  len(lowerCamelCase_							) - 1							):
								lowercase__  					=     Pipe()
								lowercase__  					=     Pipe()
								process_array_.append(
								    Process(
								        target=lowerCamelCase_						,  args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i])						,  )							)
								lowercase__  					=     temp_rs
								lowercase__  					=     temp_rr

				process_array_.append(
				    Process(
				        target=lowerCamelCase_						,  args=(
				            len(lowerCamelCase_							) - 1,
				            arr[len(lowerCamelCase_							) - 1],
				            temp_ls,
				            None,
				            temp_lr,
				            None,
				            result_pipe[len(lowerCamelCase_							) - 1],
				        )						,  )							)

				# start the processes
				for p in process_array_:
								p.start()

				# wait for the processes to end and write their values to the list
				for p in range(0						,  len(lowerCamelCase_							)							):
								lowercase__  					=     result_pipe[p][0].recv()
								process_array_[p].join()
				return arr






def     a     (		):






				'''simple docstring'''
				lowercase__  					=     list(range(10						,  0						,  -1							)							)
				print('''Initial List'''							)
				print(*lowerCamelCase_							)
				lowercase__  					=     odd_even_transposition(lowerCamelCase_							)
				print('''Sorted List\n'''							)
				print(*lowerCamelCase_							)


if __name__ == "__main__":
							main()












import numpy as np

import datasets


A__      :     int       =					'\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n'

A__      :     Optional[int]       =					'\\n@article{de2000mahalanobis,\n  title={The mahalanobis distance},\n  author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n  journal={Chemometrics and intelligent laboratory systems},\n  volume={50},\n  number={1},\n  pages={1--18},\n  year={2000},\n  publisher={Elsevier}\n}\n'

A__      :     Optional[int]       =					'\nArgs:\n    X: List of datapoints to be compared with the `reference_distribution`.\n    reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n    mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n    >>> mahalanobis_metric = datasets.load_metric("mahalanobis")\n    >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n    >>> print(results)\n    {\'mahalanobis\': array([0.5])}\n'
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION						,_KWARGS_DESCRIPTION      )
class  _UpperCAmelCase   (			datasets.Metric      ):

				"""simple docstring"""




				def 							lowercase__  (	self		:       str    ):

								'''simple docstring'''
								return datasets.MetricInfo(
								    description=_DESCRIPTION,    citation=_CITATION,    inputs_description=_KWARGS_DESCRIPTION,    features=datasets.Features(
								        {
								            '''X''': datasets.Sequence(datasets.Value('''float''',    id='''sequence'''    ),    id='''X'''    ),
								        }    ),    )




				def 							lowercase__  (	self		:       Dict,    lowerCamelCase		:       int,    lowerCamelCase		:       Optional[int]    ):

								'''simple docstring'''
								# convert to numpy arrays
								lowercase__  					=     np.array(lowerCamelCase    )
								lowercase__  					=     np.array(lowerCamelCase    )

								# Assert that arrays are 2D
								if len(X.shape    ) != 2:
												raise ValueError('''Expected `X` to be a 2D vector'''    )
								if len(reference_distribution.shape    ) != 2:
												raise ValueError('''Expected `reference_distribution` to be a 2D vector'''    )
								if reference_distribution.shape[0] < 2:
												raise ValueError(
												    '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension'''    )

								# Get mahalanobis distance for each prediction
								lowercase__  					=     X - np.mean(lowerCamelCase    )
								lowercase__  					=     np.cov(reference_distribution.T    )
								try:
												lowercase__  					=     np.linalg.inv(lowerCamelCase    )
								except np.linalg.LinAlgError:
												lowercase__  					=     np.linalg.pinv(lowerCamelCase    )
								lowercase__  					=     np.dot(lowerCamelCase,    lowerCamelCase    )
								lowercase__  					=     np.dot(lowerCamelCase,    X_minus_mu.T    ).diagonal()

								return {"mahalanobis": mahal_dist}




