





# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce




def _a (  SCREAMING_SNAKE_CASE__      :  Optional[int]       )	->    Optional[int]:

							'''simple docstring'''






							return (torch.arange(state.num_processes       ) + 1.0 + (state.num_processes * state.process_index)).to(state.device       )




def _a (  SCREAMING_SNAKE_CASE__      :  Optional[int]       )	->    Optional[int]:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						create_tensor(SCREAMING_SNAKE_CASE__       )
							SCREAMING_SNAKE_CASE__       :      int     =						gather(SCREAMING_SNAKE_CASE__       )
							assert gathered_tensor.tolist() == list(range(1			,				state.num_processes**2 + 1       )       )




def _a (  SCREAMING_SNAKE_CASE__      :  int       )	->    int:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      List[Any]     =						[state.process_index]
							SCREAMING_SNAKE_CASE__       :      int     =						gather_object(SCREAMING_SNAKE_CASE__       )
							assert len(SCREAMING_SNAKE_CASE__       ) == state.num_processes, f'''{gathered_obj}, {len(SCREAMING_SNAKE_CASE__       )} != {state.num_processes}'''
							assert gathered_obj == list(range(state.num_processes       )       ), f'''{gathered_obj} != {list(range(state.num_processes       )       )}'''




def _a (  SCREAMING_SNAKE_CASE__      :  List[str]       )	->    List[str]:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      List[Any]     =						create_tensor(SCREAMING_SNAKE_CASE__       )
							SCREAMING_SNAKE_CASE__       :      Any     =						broadcast(SCREAMING_SNAKE_CASE__       )
							assert broadcasted_tensor.shape == torch.Size([state.num_processes]       )
							assert broadcasted_tensor.tolist() == list(range(1			,				state.num_processes + 1       )       )




def _a (  SCREAMING_SNAKE_CASE__      :  Optional[Any]       )	->    Tuple:

							'''simple docstring'''






							if state.is_main_process:
														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						torch.arange(state.num_processes + 1       ).to(state.device       )
							else:
														SCREAMING_SNAKE_CASE__       :      str     =						torch.arange(state.num_processes       ).to(state.device       )
							SCREAMING_SNAKE_CASE__       :      str     =						pad_across_processes(SCREAMING_SNAKE_CASE__       )
							assert padded_tensor.shape == torch.Size([state.num_processes + 1]       )
							if not state.is_main_process:
														assert padded_tensor.tolist() == list(range(0			,				state.num_processes       )       ) + [0]




def _a (  SCREAMING_SNAKE_CASE__      :  Optional[int]       )	->    Dict:

							'''simple docstring'''






							if state.num_processes != 2:
														return
							SCREAMING_SNAKE_CASE__       :      Tuple     =						create_tensor(SCREAMING_SNAKE_CASE__       )
							SCREAMING_SNAKE_CASE__       :      Optional[int]     =						reduce(SCREAMING_SNAKE_CASE__			,				"sum"       )
							SCREAMING_SNAKE_CASE__       :      int     =						torch.tensor([4.0, 6]       ).to(state.device       )
							assert torch.allclose(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       ), f'''{reduced_tensor} != {truth_tensor}'''




def _a (  SCREAMING_SNAKE_CASE__      :  Any       )	->    Dict:

							'''simple docstring'''






							if state.num_processes != 2:
														return
							SCREAMING_SNAKE_CASE__       :      str     =						create_tensor(SCREAMING_SNAKE_CASE__       )
							SCREAMING_SNAKE_CASE__       :      Optional[int]     =						reduce(SCREAMING_SNAKE_CASE__			,				"mean"       )
							SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						torch.tensor([2.0, 3]       ).to(state.device       )
							assert torch.allclose(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       ), f'''{reduced_tensor} != {truth_tensor}'''




def _a (  SCREAMING_SNAKE_CASE__      :  Any       )	->    Tuple:

							'''simple docstring'''






							main()




def _a (  )	->    Any:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      int     =						PartialState()
							state.print(f'''State: {state}'''       )
							state.print("testing gather"       )
							test_gather(SCREAMING_SNAKE_CASE__       )
							state.print("testing gather_object"       )
							test_gather_object(SCREAMING_SNAKE_CASE__       )
							state.print("testing broadcast"       )
							test_broadcast(SCREAMING_SNAKE_CASE__       )
							state.print("testing pad_across_processes"       )
							test_pad_across_processes(SCREAMING_SNAKE_CASE__       )
							state.print("testing reduce_sum"       )
							test_reduce_sum(SCREAMING_SNAKE_CASE__       )
							state.print("testing reduce_mean"       )
							test_reduce_mean(SCREAMING_SNAKE_CASE__       )


if __name__ == "__main__":
		main()













from sympy import diff, lambdify, symbols
from sympy.functions import *  # noqa: F403




def _a (  SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  complex			,				SCREAMING_SNAKE_CASE__      :  str = "x"			,				SCREAMING_SNAKE_CASE__      :  float = 10**-10			,				SCREAMING_SNAKE_CASE__      :  int = 1			,				)	->    complex:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						symbols(SCREAMING_SNAKE_CASE__       )
							SCREAMING_SNAKE_CASE__       :      str     =						lambdify(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       )
							SCREAMING_SNAKE_CASE__       :      int     =						lambdify(SCREAMING_SNAKE_CASE__			,				diff(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       )       )

							SCREAMING_SNAKE_CASE__       :      Any     =						starting_point

							while True:
														if diff_function(SCREAMING_SNAKE_CASE__       ) != 0:
																					SCREAMING_SNAKE_CASE__       :      Any     =						prev_guess - multiplicity * func(SCREAMING_SNAKE_CASE__       ) / diff_function(
																					    SCREAMING_SNAKE_CASE__       )
														else:
																					raise ZeroDivisionError("Could not find root"       ) from None

														# Precision is checked by comparing the difference of consecutive guesses
														if abs(next_guess - prev_guess       ) < precision:
																					return next_guess

														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						next_guess


# Let's Execute
if __name__ == "__main__":
		# Find root of trigonometric function
		# Find value of pi
		print(f"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}")

		# Find root of polynomial
		# Find fourth Root of 5
		print(f"The root of x**4 - 5 = 0 is {newton_raphson('x**4 -5', 0.4 +5J)}")

		# Find value of e
		print(
		    '''The root of log(y) - 1 = 0 is ''',
		    f"{newton_raphson('log(y) - 1', 2, variable='y')}",
		)

		# Exponential Roots
		print(
		    '''The root of exp(x) - 1 = 0 is''',
		    f"{newton_raphson('exp(x) - 1', 1_0, precision=0.005)}",
		)

		# Find root of cos(x)
		print(f"The root of cos(x) = 0 is {newton_raphson('cos(x)', 0)}")






