


'''simple docstring'''

import warnings
from collections.abc import Callable
from typing import Any

import numpy
from matplotlib import pyplot

lowerCamelCase							:       Optional[Any]  						=     0.2_5 + 0.0j
lowerCamelCase							:       Optional[int]  						=     -0.4 + 0.6j
lowerCamelCase							:       Any  						=     -0.1 + 0.651j
lowerCamelCase							:       Optional[Any]  						=     -2.0
lowerCamelCase							:       Optional[int]  						=     56
lowerCamelCase							:       Optional[Any]  						=     2.0
lowerCamelCase							:       Optional[int]  						=     666

def _SCREAMING_SNAKE_CASE				(A  ,			A	)	->	numpy.ndarray:






							"""simple docstring"""







							return numpy.exp(A	) + c_parameter

def _SCREAMING_SNAKE_CASE				(A  ,			A	)	->	numpy.ndarray:






							"""simple docstring"""







							return z_values * z_values + c_parameter

def _SCREAMING_SNAKE_CASE				(A  ,			A	)	->	numpy.ndarray:






							"""simple docstring"""







							lowercase__				      =			numpy.linspace(-window_size  ,			A  ,			A	)
							lowercase__				      =			x.reshape((nb_pixels, 1)	)
							lowercase__				      =			numpy.linspace(-window_size  ,			A  ,			A	)
							lowercase__				      =			y.reshape((1, nb_pixels)	)
							return x + 1.0J * y

def _SCREAMING_SNAKE_CASE				(A  ,			A  ,			A  ,			A  ,			A = None  ,			)	->	numpy.ndarray:






							"""simple docstring"""







							lowercase__				      =			z_a.astype('''complex64'''	)
							for _ in range(A	):
														lowercase__				      =			eval_function(A  ,			A	)
														if infinity is not None:
																					numpy.nan_to_num(A  ,			copy=A  ,			nan=A	)
																					lowercase__				      =			infinity
							return z_n

def _SCREAMING_SNAKE_CASE				(A  ,			A  ,			A  ,			A  ,			)	->	None:






							"""simple docstring"""







							lowercase__				      =			(abs(A	)).transpose()
							lowercase__				      =			abs_z_final[::-1, :]
							pyplot.matshow(abs_z_final < escape_radius	)
							pyplot.title(f"Julia set of ${function_label}$, $c={function_params}$"	)
							pyplot.show()

def _SCREAMING_SNAKE_CASE				()	->	None:






							"""simple docstring"""







							warnings.filterwarnings(
							    '''ignore'''  ,			category=A  ,			message='''overflow encountered in multiply'''	)
							warnings.filterwarnings(
							    '''ignore'''  ,			category=A  ,			message='''invalid value encountered in multiply'''  ,			)
							warnings.filterwarnings(
							    '''ignore'''  ,			category=A  ,			message='''overflow encountered in absolute'''	)
							warnings.filterwarnings(
							    '''ignore'''  ,			category=A  ,			message='''overflow encountered in exp'''	)


if __name__ == "__main__":
	lowerCamelCase							:       Optional[int]  						=     prepare_grid(window_size, nb_pixels)

	ignore_overflow_warnings()  # See file header for explanations

	lowerCamelCase							:       List[str]  						=     24
	lowerCamelCase							:       Dict  						=     2 * abs(c_cauliflower) + 1
	lowerCamelCase							:       List[Any]  						=     iterate_function(
	    eval_quadratic_polynomial,
	    c_cauliflower,
	    nb_iterations,
	    z_a,
	    infinity=1.1 * escape_radius,
	)
	show_results('z^2+c', c_cauliflower, escape_radius, z_final)

	lowerCamelCase							:       Union[str, Any]  						=     64
	lowerCamelCase							:       Union[str, Any]  						=     2 * abs(c_polynomial_a) + 1
	lowerCamelCase							:       str  						=     iterate_function(
	    eval_quadratic_polynomial,
	    c_polynomial_a,
	    nb_iterations,
	    z_a,
	    infinity=1.1 * escape_radius,
	)
	show_results('z^2+c', c_polynomial_a, escape_radius, z_final)

	lowerCamelCase							:       Optional[Any]  						=     161
	lowerCamelCase							:       Any  						=     2 * abs(c_polynomial_a) + 1
	lowerCamelCase							:       List[str]  						=     iterate_function(
	    eval_quadratic_polynomial,
	    c_polynomial_a,
	    nb_iterations,
	    z_a,
	    infinity=1.1 * escape_radius,
	)
	show_results('z^2+c', c_polynomial_a, escape_radius, z_final)

	lowerCamelCase							:       Union[str, Any]  						=     12
	lowerCamelCase							:       Tuple  						=     10_000.0
	lowerCamelCase							:       List[Any]  						=     iterate_function(
	    eval_exponential,
	    c_exponential,
	    nb_iterations,
	    z_a + 2,
	    infinity=1.0e10,
	)
	show_results('e^z+c', c_exponential, escape_radius, z_final)








'''simple docstring'''

import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
 import torch_xla.core.xla_model as xm

def _SCREAMING_SNAKE_CASE				(A  ,			A = False	)	->	Tuple:






       """simple docstring"""







       if device_specific:
              seed += AcceleratorState().process_index
       random.seed(A	)
       np.random.seed(A	)
       torch.manual_seed(A	)
       if is_xpu_available():
              torch.xpu.manual_seed_all(A	)
       elif is_npu_available():
              torch.npu.manual_seed_all(A	)
       else:
              torch.cuda.manual_seed_all(A	)
       # ^^ safe to call this function even if cuda is not available
       if is_tpu_available():
              xm.set_rng_state(A	)

def _SCREAMING_SNAKE_CASE				(A = None  ,			A = None	)	->	Tuple:






       """simple docstring"""







       if rng_type == RNGType.TORCH:
              lowercase__				      =			torch.get_rng_state()
       elif rng_type == RNGType.CUDA:
              lowercase__				      =			torch.cuda.get_rng_state()
       elif rng_type == RNGType.XLA:
              assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
              lowercase__				      =			torch.tensor(xm.get_rng_state()	)
       elif rng_type == RNGType.NPU:
              assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
              lowercase__				      =			torch.npu.get_rng_state()
       elif rng_type == RNGType.XPU:
              assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
              lowercase__				      =			torch.xpu.get_rng_state()
       elif rng_type == RNGType.GENERATOR:
              assert generator is not None, "Need a generator to synchronize its seed."
              lowercase__				      =			generator.get_state()

       # Broadcast the rng state from device 0 to other devices
       lowercase__				      =			AcceleratorState()
       if state.distributed_type == DistributedType.TPU:
              lowercase__				      =			rng_state.to(xm.xla_device()	)
              xm.collective_broadcast([rng_state]	)
              xm.mark_step()
              lowercase__				      =			rng_state.cpu()
       elif (
           state.distributed_type in CUDA_DISTRIBUTED_TYPES
           or state.distributed_type == DistributedType.MULTI_NPU
           or state.distributed_type == DistributedType.MULTI_XPU
       ):
              lowercase__				      =			rng_state.to(state.device	)
              torch.distributed.broadcast(A  ,			0	)
              lowercase__				      =			rng_state.cpu()
       elif state.distributed_type == DistributedType.MULTI_CPU:
              torch.distributed.broadcast(A  ,			0	)

       # Set the broadcast rng state
       if rng_type == RNGType.TORCH:
              torch.set_rng_state(A	)
       elif rng_type == RNGType.CUDA:
              torch.cuda.set_rng_state(A	)
       elif rng_type == RNGType.NPU:
              torch.npu.set_rng_state(A	)
       elif rng_type == RNGType.XPU:
              torch.xpu.set_rng_state(A	)
       elif rng_type == RNGType.XLA:
              xm.set_rng_state(rng_state.item()	)
       elif rng_type == RNGType.GENERATOR:
              generator.set_state(A	)

def _SCREAMING_SNAKE_CASE				(A  ,			A = None	)	->	int:






       """simple docstring"""







       for rng_type in rng_types:
              synchronize_rng_state(RNGType(A	)  ,			generator=A	)




