





'''simple docstring'''




import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
  import jax.numpy as jnp

if is_tf_available():
  import tensorflow as tf

if is_torch_available():
  import torch


class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       {
              'task_specific_params': {
                  'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4},
                  'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4},
                  'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6},
              }
          }
          __A						: Optional[Any]      =       {
              'task_specific_params.summarization.length_penalty': 1.0,
              'task_specific_params.summarization.max_length': 128,
              'task_specific_params.summarization.min_length': 12,
              'task_specific_params.summarization.num_beams': 4,
              'task_specific_params.summarization_cnn.length_penalty': 2.0,
              'task_specific_params.summarization_cnn.max_length': 142,
              'task_specific_params.summarization_cnn.min_length': 56,
              'task_specific_params.summarization_cnn.num_beams': 4,
              'task_specific_params.summarization_xsum.length_penalty': 1.0,
              'task_specific_params.summarization_xsum.max_length': 62,
              'task_specific_params.summarization_xsum.min_length': 11,
              'task_specific_params.summarization_xsum.num_beams': 6,
          }

          self.assertEqual(flatten_dict(_UpperCAmelCase)						,	_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: str      =       np.random.randn(3						,	4)
          self.assertTrue(np.allclose(transpose(_UpperCAmelCase)						,	x.transpose()))

          __A						: Union[str, Any]      =       np.random.randn(3						,	4						,	5)
          self.assertTrue(np.allclose(transpose(_UpperCAmelCase						,	axes=(1, 2, 0))						,	x.transpose((1, 2, 0))))


     @require_torch
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       np.random.randn(3						,	4)
          __A						: Optional[Any]      =       torch.tensor(_UpperCAmelCase)
          self.assertTrue(np.allclose(transpose(_UpperCAmelCase)						,	transpose(_UpperCAmelCase).numpy()))

          __A						: str      =       np.random.randn(3						,	4						,	5)
          __A						: Tuple      =       torch.tensor(_UpperCAmelCase)
          self.assertTrue(np.allclose(transpose(_UpperCAmelCase						,	axes=(1, 2, 0))						,	transpose(_UpperCAmelCase						,	axes=(1, 2, 0)).numpy()))


     @require_tf
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Union[str, Any]      =       np.random.randn(3						,	4)
          __A						: Optional[Any]      =       tf.constant(_UpperCAmelCase)
          self.assertTrue(np.allclose(transpose(_UpperCAmelCase)						,	transpose(_UpperCAmelCase).numpy()))

          __A						: Tuple      =       np.random.randn(3						,	4						,	5)
          __A						: List[str]      =       tf.constant(_UpperCAmelCase)
          self.assertTrue(np.allclose(transpose(_UpperCAmelCase						,	axes=(1, 2, 0))						,	transpose(_UpperCAmelCase						,	axes=(1, 2, 0)).numpy()))


     @require_flax
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Union[str, Any]      =       np.random.randn(3						,	4)
          __A						: Union[str, Any]      =       jnp.array(_UpperCAmelCase)
          self.assertTrue(np.allclose(transpose(_UpperCAmelCase)						,	np.asarray(transpose(_UpperCAmelCase))))

          __A						: Any      =       np.random.randn(3						,	4						,	5)
          __A						: List[Any]      =       jnp.array(_UpperCAmelCase)
          self.assertTrue(np.allclose(transpose(_UpperCAmelCase						,	axes=(1, 2, 0))						,	np.asarray(transpose(_UpperCAmelCase						,	axes=(1, 2, 0)))))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Dict      =       np.random.randn(3						,	4)
          self.assertTrue(np.allclose(reshape(_UpperCAmelCase						,	(4, 3))						,	np.reshape(_UpperCAmelCase						,	(4, 3))))

          __A						: int      =       np.random.randn(3						,	4						,	5)
          self.assertTrue(np.allclose(reshape(_UpperCAmelCase						,	(12, 5))						,	np.reshape(_UpperCAmelCase						,	(12, 5))))


     @require_torch
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       np.random.randn(3						,	4)
          __A						: List[Any]      =       torch.tensor(_UpperCAmelCase)
          self.assertTrue(np.allclose(reshape(_UpperCAmelCase						,	(4, 3))						,	reshape(_UpperCAmelCase						,	(4, 3)).numpy()))

          __A						: Dict      =       np.random.randn(3						,	4						,	5)
          __A						: Optional[int]      =       torch.tensor(_UpperCAmelCase)
          self.assertTrue(np.allclose(reshape(_UpperCAmelCase						,	(12, 5))						,	reshape(_UpperCAmelCase						,	(12, 5)).numpy()))


     @require_tf
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Tuple      =       np.random.randn(3						,	4)
          __A						: str      =       tf.constant(_UpperCAmelCase)
          self.assertTrue(np.allclose(reshape(_UpperCAmelCase						,	(4, 3))						,	reshape(_UpperCAmelCase						,	(4, 3)).numpy()))

          __A						: Optional[Any]      =       np.random.randn(3						,	4						,	5)
          __A						: List[str]      =       tf.constant(_UpperCAmelCase)
          self.assertTrue(np.allclose(reshape(_UpperCAmelCase						,	(12, 5))						,	reshape(_UpperCAmelCase						,	(12, 5)).numpy()))


     @require_flax
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Dict      =       np.random.randn(3						,	4)
          __A						: List[str]      =       jnp.array(_UpperCAmelCase)
          self.assertTrue(np.allclose(reshape(_UpperCAmelCase						,	(4, 3))						,	np.asarray(reshape(_UpperCAmelCase						,	(4, 3)))))

          __A						: Any      =       np.random.randn(3						,	4						,	5)
          __A						: List[Any]      =       jnp.array(_UpperCAmelCase)
          self.assertTrue(np.allclose(reshape(_UpperCAmelCase						,	(12, 5))						,	np.asarray(reshape(_UpperCAmelCase						,	(12, 5)))))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[str]      =       np.random.randn(1						,	3						,	4)
          self.assertTrue(np.allclose(squeeze(_UpperCAmelCase)						,	np.squeeze(_UpperCAmelCase)))

          __A						: Optional[Any]      =       np.random.randn(1						,	4						,	1						,	5)
          self.assertTrue(np.allclose(squeeze(_UpperCAmelCase						,	axis=2)						,	np.squeeze(_UpperCAmelCase						,	axis=2)))


     @require_torch
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[int]      =       np.random.randn(1						,	3						,	4)
          __A						: int      =       torch.tensor(_UpperCAmelCase)
          self.assertTrue(np.allclose(squeeze(_UpperCAmelCase)						,	squeeze(_UpperCAmelCase).numpy()))

          __A						: Optional[int]      =       np.random.randn(1						,	4						,	1						,	5)
          __A						: Union[str, Any]      =       torch.tensor(_UpperCAmelCase)
          self.assertTrue(np.allclose(squeeze(_UpperCAmelCase						,	axis=2)						,	squeeze(_UpperCAmelCase						,	axis=2).numpy()))


     @require_tf
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       np.random.randn(1						,	3						,	4)
          __A						: Dict      =       tf.constant(_UpperCAmelCase)
          self.assertTrue(np.allclose(squeeze(_UpperCAmelCase)						,	squeeze(_UpperCAmelCase).numpy()))

          __A						: Tuple      =       np.random.randn(1						,	4						,	1						,	5)
          __A						: Tuple      =       tf.constant(_UpperCAmelCase)
          self.assertTrue(np.allclose(squeeze(_UpperCAmelCase						,	axis=2)						,	squeeze(_UpperCAmelCase						,	axis=2).numpy()))


     @require_flax
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[str]      =       np.random.randn(1						,	3						,	4)
          __A						: Any      =       jnp.array(_UpperCAmelCase)
          self.assertTrue(np.allclose(squeeze(_UpperCAmelCase)						,	np.asarray(squeeze(_UpperCAmelCase))))

          __A						: List[Any]      =       np.random.randn(1						,	4						,	1						,	5)
          __A						: int      =       jnp.array(_UpperCAmelCase)
          self.assertTrue(np.allclose(squeeze(_UpperCAmelCase						,	axis=2)						,	np.asarray(squeeze(_UpperCAmelCase						,	axis=2))))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       np.random.randn(3						,	4)
          self.assertTrue(np.allclose(expand_dims(_UpperCAmelCase						,	axis=1)						,	np.expand_dims(_UpperCAmelCase						,	axis=1)))


     @require_torch
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       np.random.randn(3						,	4)
          __A						: Union[str, Any]      =       torch.tensor(_UpperCAmelCase)
          self.assertTrue(np.allclose(expand_dims(_UpperCAmelCase						,	axis=1)						,	expand_dims(_UpperCAmelCase						,	axis=1).numpy()))


     @require_tf
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       np.random.randn(3						,	4)
          __A						: str      =       tf.constant(_UpperCAmelCase)
          self.assertTrue(np.allclose(expand_dims(_UpperCAmelCase						,	axis=1)						,	expand_dims(_UpperCAmelCase						,	axis=1).numpy()))


     @require_flax
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[Any]      =       np.random.randn(3						,	4)
          __A						: List[Any]      =       jnp.array(_UpperCAmelCase)
          self.assertTrue(np.allclose(expand_dims(_UpperCAmelCase						,	axis=1)						,	np.asarray(expand_dims(_UpperCAmelCase						,	axis=1))))






'''simple docstring'''




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






def        _lowerCAmelCase			(  __snake_case  : str ,							__snake_case  : complex ,							__snake_case  : str = "x" ,							__snake_case  : float = 10**-10 ,							__snake_case  : int = 1 ,							)     ->     complex:
     __A						: int      =       symbols(__snake_case			)
     __A						: Tuple      =       lambdify(__snake_case ,							__snake_case			)
     __A						: Any      =       lambdify(__snake_case ,							diff(__snake_case ,							__snake_case			)			)

     __A						: str      =       starting_point

     while True:
          if diff_function(__snake_case			) != 0:
               __A						: Optional[Any]      =       prev_guess - multiplicity * func(__snake_case			) / diff_function(
                   __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

          __A						: Dict      =       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", 10, precision=0.005)}""",
  )

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