





"""simple docstring"""




from __future__ import annotations





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ) ->    List[str]:






  """simple docstring"""





  assert (
      c % greatest_common_divisor(_UpperCAmelCase			, _UpperCAmelCase ) == 0
  )  # greatest_common_divisor(a,b) function implemented below
  (A_)  :	Dict    						=  extended_gcd(_UpperCAmelCase			, _UpperCAmelCase )  # extended_gcd(a,b) function implemented below
  A_  :	List[str]    						=  c / d
  return (r * x, r * y)





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase = 2 ) ->    List[Any]:






  """simple docstring"""





  (A_)  :	List[Any]    						=  diophantine(_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase )  # Initial value
  A_  :	Any    						=  greatest_common_divisor(_UpperCAmelCase			, _UpperCAmelCase )
  A_  :	Union[str, Any]    						=  a // d
  A_  :	Tuple    						=  b // d

  for i in range(_UpperCAmelCase ):
    A_  :	Optional[int]    						=  xa + i * q
    A_  :	List[Any]    						=  ya - i * p
    print(_UpperCAmelCase			, _UpperCAmelCase )





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ) ->    Tuple:






  """simple docstring"""





  if a < b:
    A_  :	int    						=  b, a

  while a % b != 0:
    A_  :	List[Any]    						=  b, a % b

  return b





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ) ->    int:






  """simple docstring"""





  assert a >= 0 and b >= 0

  if b == 0:
    A_  :	Tuple    						=  a, 1, 0
  else:
    (A_)  :	List[Any]    						=  extended_gcd(_UpperCAmelCase			, a % b )
    A_  :	str    						=  q
    A_  :	Optional[Any]    						=  p - q * (a // b)

  assert a % d == 0 and b % d == 0
  assert d == a * x + b * y

  return (d, x, y)


if __name__ == "__main__":
   from doctest import testmod

   testmod(name='diophantine', verbose=True)
   testmod(name='diophantine_all_soln', verbose=True)
   testmod(name='extended_gcd', verbose=True)
   testmod(name='greatest_common_divisor', verbose=True)






"""simple docstring"""




import math as m





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase=0.001 ):






  """simple docstring"""





  return (f(a + h ) - f(a - h )) / (2 * h)





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase=0			, _UpperCAmelCase=100			, _UpperCAmelCase=0.0_001			, _UpperCAmelCase=1E-6			, _UpperCAmelCase=False ):






  """simple docstring"""





  A_  :	Optional[int]    						=  xa  # set the initial guess
  A_  :	Any    						=  [a]
  A_  :	str    						=  abs(f(_UpperCAmelCase ) )
  A_  :	str    						=  lambda _UpperCAmelCase : calc_derivative(_UpperCAmelCase			, _UpperCAmelCase			, h=_UpperCAmelCase )  # noqa: E731  Derivative of f(x)
  for _ in range(_UpperCAmelCase ):
    if fa(_UpperCAmelCase ) == 0:
      raise ValueError('No converging solution found' )
    A_  :	Union[str, Any]    						=  a - f(_UpperCAmelCase ) / fa(_UpperCAmelCase )  # Calculate the next estimate
    if logsteps:
      steps.append(_UpperCAmelCase )
    if error < maxerror:
      break
  else:
    raise ValueError('Iteration limit reached, no converging solution found' )
  if logsteps:
    # If logstep is true, then log intermediate steps
    return a, error, steps
  return a, error


if __name__ == "__main__":
   from matplotlib import pyplot as plt

   lowerCamelCase_					:						str      = lambda x: m.tanh(x) ** 2 - m.exp(3 * x)  # noqa: E731
   lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_					:						int      = newton_raphson(
       f, xa=10, maxiter=10_00, step=1E-6, logsteps=True
   )
   plt.plot([abs(f(x)) for x in steps])
   plt.xlabel('step')
   plt.ylabel('error')
   plt.show()
   print(F"solution = {{{solution:f}}}, error = {{{error:f}}}")