




'''simple docstring'''





import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()




class 			_snake_case		(     UpperCAmelCase_						,						unittest.TestCase							):
			lowerCAmelCase_					:				Optional[Any]							=   DDIMPipeline
			lowerCAmelCase_					:				str							=   UNCONDITIONAL_IMAGE_GENERATION_PARAMS
			lowerCAmelCase_					:				Any							=   PipelineTesterMixin.required_optional_params - {
			    """num_images_per_prompt""",
			    """latents""",
			    """callback""",
			    """callback_steps""",
			}
			lowerCAmelCase_					:				str							=   UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
			lowerCAmelCase_					:				Optional[Any]							=   False




			def        lowerCAmelCase__    ( self							)    ->    Tuple:

										'''simple docstring'''




										torch.manual_seed(0							)
										snake_case_      =				UNetaDModel(
										    block_out_channels=(32, 64)    ,							layers_per_block=2    ,							sample_size=32    ,							in_channels=3    ,							out_channels=3    ,							down_block_types=("DownBlock2D", "AttnDownBlock2D")    ,							up_block_types=("AttnUpBlock2D", "UpBlock2D")    ,							)
										snake_case_      =				DDIMScheduler()
										snake_case_      =				{'''unet''': unet, '''scheduler''': scheduler}
										return components




			def        lowerCAmelCase__    ( self    ,							a__    ,							a__=0							)    ->    Optional[Any]:

										'''simple docstring'''




										if str(__lowercase							).startswith("mps"							):
																	snake_case_      =				torch.manual_seed(__lowercase							)
										else:
																	snake_case_      =				torch.Generator(device=__lowercase							).manual_seed(__lowercase							)
										snake_case_      =				{
										    '''batch_size''': 1,
										    '''generator''': generator,
										    '''num_inference_steps''': 2,
										    '''output_type''': '''numpy''',
										}
										return inputs




			def        lowerCAmelCase__    ( self							)    ->    Optional[Any]:

										'''simple docstring'''




										snake_case_      =				'''cpu'''

										snake_case_      =				self.get_dummy_components()
										snake_case_      =				self.pipeline_class(**__lowercase							)
										pipe.to(__lowercase							)
										pipe.set_progress_bar_config(disable=__lowercase							)

										snake_case_      =				self.get_dummy_inputs(__lowercase							)
										snake_case_      =				pipe(**__lowercase							).images
										snake_case_      =				image[0, -3:, -3:, -1]

										self.assertEqual(image.shape    ,							(1, 32, 32, 3)							)
										snake_case_      =				np.array(
										    [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04]							)
										snake_case_      =				np.abs(image_slice.flatten() - expected_slice							).max()
										self.assertLessEqual(__lowercase    ,							1e-3							)




			def        lowerCAmelCase__    ( self							)    ->    List[Any]:

										'''simple docstring'''




										super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3							)




			def        lowerCAmelCase__    ( self							)    ->    Optional[int]:

										'''simple docstring'''




										super().test_save_load_local(expected_max_difference=3e-3							)




			def        lowerCAmelCase__    ( self							)    ->    Tuple:

										'''simple docstring'''




										super().test_save_load_optional_components(expected_max_difference=3e-3							)




			def        lowerCAmelCase__    ( self							)    ->    int:

										'''simple docstring'''




										super().test_inference_batch_single_identical(expected_max_diff=3e-3							)




@slow
@require_torch_gpu
class 			_snake_case		(     unittest.TestCase							):




			def        lowerCAmelCase__    ( self							)    ->    Any:

										'''simple docstring'''




										snake_case_      =				'''google/ddpm-cifar10-32'''

										snake_case_      =				UNetaDModel.from_pretrained(__lowercase							)
										snake_case_      =				DDIMScheduler()

										snake_case_      =				DDIMPipeline(unet=__lowercase    ,							scheduler=__lowercase							)
										ddim.to(__lowercase							)
										ddim.set_progress_bar_config(disable=__lowercase							)

										snake_case_      =				torch.manual_seed(0							)
										snake_case_      =				ddim(generator=__lowercase    ,							eta=0.0    ,							output_type="numpy"							).images

										snake_case_      =				image[0, -3:, -3:, -1]

										assert image.shape == (1, 32, 32, 3)
										snake_case_      =				np.array([0.1_7_2_3, 0.1_6_1_7, 0.1_6_0_0, 0.1_6_2_6, 0.1_4_9_7, 0.1_5_1_3, 0.1_5_0_5, 0.1_4_4_2, 0.1_4_5_3]							)

										assert np.abs(image_slice.flatten() - expected_slice							).max() < 1e-2




			def        lowerCAmelCase__    ( self							)    ->    List[str]:

										'''simple docstring'''




										snake_case_      =				'''google/ddpm-ema-bedroom-256'''

										snake_case_      =				UNetaDModel.from_pretrained(__lowercase							)
										snake_case_      =				DDIMScheduler.from_pretrained(__lowercase							)

										snake_case_      =				DDIMPipeline(unet=__lowercase    ,							scheduler=__lowercase							)
										ddpm.to(__lowercase							)
										ddpm.set_progress_bar_config(disable=__lowercase							)

										snake_case_      =				torch.manual_seed(0							)
										snake_case_      =				ddpm(generator=__lowercase    ,							output_type="numpy"							).images

										snake_case_      =				image[0, -3:, -3:, -1]

										assert image.shape == (1, 256, 256, 3)
										snake_case_      =				np.array([0.0_0_6_0, 0.0_2_0_1, 0.0_3_4_4, 0.0_0_2_4, 0.0_0_1_8, 0.0_0_0_2, 0.0_0_2_2, 0.0_0_0_0, 0.0_0_6_9]							)

										assert np.abs(image_slice.flatten() - expected_slice							).max() < 1e-2










'''simple docstring'''





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


def 					UpperCamelCase_(     snake_case							:	str							,    snake_case							:	complex							,    snake_case							:	str = "x"							,    snake_case							:	float = 1_0**-1_0							,    snake_case							:	int = 1							,    ):

							'''simple docstring'''






							snake_case_      =				symbols(snake_case					)
							snake_case_      =				lambdify(snake_case							,    snake_case					)
							snake_case_      =				lambdify(snake_case							,    diff(snake_case							,    snake_case					)					)

							snake_case_      =				starting_point

							while True:
														if diff_function(snake_case					) != 0:
																					snake_case_      =				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

														snake_case_      =				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.0_0_5)}",
		)

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




