





"""simple docstring"""





from math import isclose, sqrt


def      UpperCAmelCase__				(    SCREAMING_SNAKE_CASE	:		float				,		SCREAMING_SNAKE_CASE	:		float				,		SCREAMING_SNAKE_CASE	:		float     ):







      '''simple docstring'''




      lowerCAmelCase    		=			point_y / 4 / point_x
      lowerCAmelCase    		=			2 * normal_gradient / (1 + normal_gradient * normal_gradient)
      lowerCAmelCase    		=			(1 - normal_gradient * normal_gradient) / (
          1 + normal_gradient * normal_gradient
      )
      lowerCAmelCase    		=			(sa - ca * incoming_gradient) / (ca + sa * incoming_gradient)

      # to find the next point, solve the simultaeneous equations:
      # y^2 + 4x^2 = 100
      # y - b = m * (x - a)
      # ==> A x^2 + B x + C = 0
      lowerCAmelCase    		=			outgoing_gradient**2 + 4
      lowerCAmelCase    		=			2 * outgoing_gradient * (point_y - outgoing_gradient * point_x)
      lowerCAmelCase    		=			(point_y - outgoing_gradient * point_x) ** 2 - 1_00

      lowerCAmelCase    		=			(
          -linear_term - sqrt(linear_term**2 - 4 * quadratic_term * constant_term     )
      ) / (2 * quadratic_term)
      lowerCAmelCase    		=			(
          -linear_term + sqrt(linear_term**2 - 4 * quadratic_term * constant_term     )
      ) / (2 * quadratic_term)

      # two solutions, one of which is our input point
      lowerCAmelCase    		=			x_minus if isclose(SCREAMING_SNAKE_CASE				,		SCREAMING_SNAKE_CASE     ) else x_plus
      lowerCAmelCase    		=			point_y + outgoing_gradient * (next_x - point_x)

      return next_x, next_y, outgoing_gradient


def      UpperCAmelCase__				(    SCREAMING_SNAKE_CASE	:		float = 1.4				,		SCREAMING_SNAKE_CASE	:		float = -9.6     ):







      '''simple docstring'''




      lowerCAmelCase    		=			0
      lowerCAmelCase    		=			first_x_coord
      lowerCAmelCase    		=			first_y_coord
      lowerCAmelCase    		=			(10.1 - point_y) / (0.0 - point_x)

      while not (-0.01 <= point_x <= 0.01 and point_y > 0):
            lowerCAmelCase		,						lowerCAmelCase		,						lowerCAmelCase    		=			next_point(SCREAMING_SNAKE_CASE				,		SCREAMING_SNAKE_CASE				,		SCREAMING_SNAKE_CASE     )
            num_reflections += 1

      return num_reflections


if __name__ == "__main__":
       print(f'{solution() = }')













"""simple docstring"""





import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()

class 						lowercase   (   unittest.TestCase   ):





      @property
      def        _snake_case     (			self					)						->	List[str]:
            torch.manual_seed(0					)
            lowerCAmelCase    		=			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""") ,		)
            return model





      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			self.dummy_uncond_unet
            lowerCAmelCase    		=			ScoreSdeVeScheduler()

            lowerCAmelCase    		=			ScoreSdeVePipeline(unet=lowercase ,		scheduler=lowercase					)
            sde_ve.to(lowercase					)
            sde_ve.set_progress_bar_config(disable=lowercase					)

            lowerCAmelCase    		=			torch.manual_seed(0					)
            lowerCAmelCase    		=			sde_ve(num_inference_steps=2 ,		output_type="""numpy""" ,		generator=lowercase					).images

            lowerCAmelCase    		=			torch.manual_seed(0					)
            lowerCAmelCase    		=			sde_ve(num_inference_steps=2 ,		output_type="""numpy""" ,		generator=lowercase ,		return_dict=lowercase					)[
                0
            ]

            lowerCAmelCase    		=			image[0, -3:, -3:, -1]
            lowerCAmelCase    		=			image_from_tuple[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)
            lowerCAmelCase    		=			np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]					)

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







@slow
@require_torch
class 						lowercase   (   unittest.TestCase   ):





      def        _snake_case     (			self					)						->	Optional[int]:
            lowerCAmelCase    		=			"""google/ncsnpp-church-256"""
            lowerCAmelCase    		=			UNetaDModel.from_pretrained(lowercase					)

            lowerCAmelCase    		=			ScoreSdeVeScheduler.from_pretrained(lowercase					)

            lowerCAmelCase    		=			ScoreSdeVePipeline(unet=lowercase ,		scheduler=lowercase					)
            sde_ve.to(lowercase					)
            sde_ve.set_progress_bar_config(disable=lowercase					)

            lowerCAmelCase    		=			torch.manual_seed(0					)
            lowerCAmelCase    		=			sde_ve(num_inference_steps=10 ,		output_type="""numpy""" ,		generator=lowercase					).images

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

            assert image.shape == (1, 256, 256, 3)

            lowerCAmelCase    		=			np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]					)

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






