





"""simple docstring"""




import unittest
from dataclasses import dataclass
from typing import List, Union

import numpy as np
import PIL.Image

from diffusers.utils.outputs import BaseOutput

@dataclass
class 			_UpperCAmelCase  (       UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_				:			Union[List[PIL.Image.Image], np.ndarray]


class 			_UpperCAmelCase  (       unittest.TestCase		):



      '''simple docstring'''

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Union[str, Any]    						=  CustomOutput(images=np.random.rand(1 ,				3 ,				4 ,				4  )  )

        # check every way of getting the attribute
        assert isinstance(outputs.images ,				np.ndarray  )
        assert outputs.images.shape == (1, 3, 4, 4)
        assert isinstance(outputs['images'] ,				np.ndarray  )
        assert outputs["images"].shape == (1, 3, 4, 4)
        assert isinstance(outputs[0] ,				np.ndarray  )
        assert outputs[0].shape == (1, 3, 4, 4)

        # test with a non-tensor attribute
        A_  :	Dict    						=  CustomOutput(images=[PIL.Image.new('RGB' ,				(4, 4)  )]  )

        # check every way of getting the attribute
        assert isinstance(outputs.images ,				snake_case_  )
        assert isinstance(outputs.images[0] ,				PIL.Image.Image  )
        assert isinstance(outputs['images'] ,				snake_case_  )
        assert isinstance(outputs['images'][0] ,				PIL.Image.Image  )
        assert isinstance(outputs[0] ,				snake_case_  )
        assert isinstance(outputs[0][0] ,				PIL.Image.Image  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Optional[int]    						=  CustomOutput({'images': np.random.rand(1 ,				3 ,				4 ,				4  )}  )

        # check every way of getting the attribute
        assert isinstance(outputs.images ,				np.ndarray  )
        assert outputs.images.shape == (1, 3, 4, 4)
        assert isinstance(outputs['images'] ,				np.ndarray  )
        assert outputs["images"].shape == (1, 3, 4, 4)
        assert isinstance(outputs[0] ,				np.ndarray  )
        assert outputs[0].shape == (1, 3, 4, 4)

        # test with a non-tensor attribute
        A_  :	Optional[int]    						=  CustomOutput({'images': [PIL.Image.new('RGB' ,				(4, 4)  )]}  )

        # check every way of getting the attribute
        assert isinstance(outputs.images ,				snake_case_  )
        assert isinstance(outputs.images[0] ,				PIL.Image.Image  )
        assert isinstance(outputs['images'] ,				snake_case_  )
        assert isinstance(outputs['images'][0] ,				PIL.Image.Image  )
        assert isinstance(outputs[0] ,				snake_case_  )
        assert isinstance(outputs[0][0] ,				PIL.Image.Image  )






"""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}}}")