



import numpy as np

def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__ = 1E-12   ,   UpperCAmelCase__ = 100   ,   ):


      """simple docstring"""



      assert np.shape(lowerCAmelCase_ )[0] == np.shape(lowerCAmelCase_ )[1]
      # Ensure proper dimensionality.
      assert np.shape(lowerCAmelCase_ )[0] == np.shape(lowerCAmelCase_ )[0]
      # Ensure inputs are either both complex or both real
      assert np.iscomplexobj(lowerCAmelCase_ ) == np.iscomplexobj(lowerCAmelCase_ )
      lowerCamelCase								=		np.iscomplexobj(lowerCAmelCase_ )
      if is_complex:
            # Ensure complex input_matrix is Hermitian
            assert np.array_equal(lowerCAmelCase_   ,   input_matrix.conj().T )

      # Set convergence to False. Will define convergence when we exceed max_iterations
      # or when we have small changes from one iteration to next.

      lowerCamelCase								=		False
      lowerCamelCase								=		0
      lowerCamelCase								=		0
      lowerCamelCase								=		1E12

      while not convergence:
            # Multiple matrix by the vector.
            lowerCamelCase								=		np.dot(lowerCAmelCase_   ,   lowerCAmelCase_ )
            # Normalize the resulting output vector.
            lowerCamelCase								=		w / np.linalg.norm(lowerCAmelCase_ )
            # Find rayleigh quotient
            # (faster than usual b/c we know vector is normalized already)
            lowerCamelCase								=		vector.conj().T if is_complex else vector.T
            lowerCamelCase								=		np.dot(lowerCAmelCase_   ,   np.dot(lowerCAmelCase_   ,   lowerCAmelCase_ ) )

            # Check convergence.
            lowerCamelCase								=		np.abs(lambda_ - lambda_previous ) / lambda_
            iterations += 1

            if error <= error_tol or iterations >= max_iterations:
                  lowerCamelCase								=		True

            lowerCamelCase								=		lambda_

      if is_complex:
            lowerCamelCase								=		np.real(lambda_ )

      return lambda_, vector

def     __lowercase(	):


      """simple docstring"""



      lowerCamelCase								=		np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]] )
      lowerCamelCase								=		np.array([41, 4, 20] )
      lowerCamelCase								=		real_input_matrix.astype(np.complexaaa )
      lowerCamelCase								=		np.triu(1j * complex_input_matrix   ,   1 )
      complex_input_matrix += imag_matrix
      complex_input_matrix += -1 * imag_matrix.T
      lowerCamelCase								=		np.array([41, 4, 20] ).astype(np.complexaaa )

      for problem_type in ["real", "complex"]:
            if problem_type == "real":
                  lowerCamelCase								=		real_input_matrix
                  lowerCamelCase								=		real_vector
            elif problem_type == "complex":
                  lowerCamelCase								=		complex_input_matrix
                  lowerCamelCase								=		complex_vector

            # Our implementation.
            lowerCamelCase								=		power_iteration(lowerCAmelCase_   ,   lowerCAmelCase_ )

            # Numpy implementation.

            # Get eigenvalues and eigenvectors using built-in numpy
            # eigh (eigh used for symmetric or hermetian matrices).
            lowerCamelCase								=		np.linalg.eigh(lowerCAmelCase_ )
            # Last eigenvalue is the maximum one.
            lowerCamelCase								=		eigen_values[-1]
            # Last column in this matrix is eigenvector corresponding to largest eigenvalue.
            lowerCamelCase								=		eigen_vectors[:, -1]

            # Check our implementation and numpy gives close answers.
            assert np.abs(eigen_value - eigen_value_max ) <= 1E-6
            # Take absolute values element wise of each eigenvector.
            # as they are only unique to a minus sign.
            assert np.linalg.norm(np.abs(lowerCAmelCase_ ) - np.abs(lowerCAmelCase_ ) ) <= 1E-6


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     test_power_iteration()




import gc
import unittest

from diffusers import FlaxStableDiffusionInpaintPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
     import jax
     import jax.numpy as jnp
     from flax.jax_utils import replicate
     from flax.training.common_utils import shard





@slow
@require_flax
class    lowerCamelCase__  (						unittest.TestCase):
     """simple docstring"""



     def 						_a      (self    ):






           '''simple docstring'''



           super().tearDown()
           gc.collect()







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
               "/sd2-inpaint/init_image.png"    )
           lowerCamelCase								=		load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"    )

           lowerCamelCase								=		"xvjiarui/stable-diffusion-2-inpainting"
           lowerCamelCase			,							lowerCamelCase								=		FlaxStableDiffusionInpaintPipeline.from_pretrained(__a				,			safety_checker=__a    )

           lowerCamelCase								=		"Face of a yellow cat, high resolution, sitting on a park bench"

           lowerCamelCase								=		jax.random.PRNGKey(0    )
           lowerCamelCase								=		50

           lowerCamelCase								=		jax.device_count()
           lowerCamelCase								=		num_samples * [prompt]
           lowerCamelCase								=		num_samples * [init_image]
           lowerCamelCase								=		num_samples * [mask_image]
           lowerCamelCase			,							lowerCamelCase			,							lowerCamelCase								=		pipeline.prepare_inputs(__a				,			__a				,			__a    )

           # shard inputs and rng
           lowerCamelCase								=		replicate(__a    )
           lowerCamelCase								=		jax.random.split(__a				,			jax.device_count()    )
           lowerCamelCase								=		shard(__a    )
           lowerCamelCase								=		shard(__a    )
           lowerCamelCase								=		shard(__a    )

           lowerCamelCase								=		pipeline(
               __a				,			__a				,			__a				,			__a				,			__a				,			__a				,			jit=__a    )

           lowerCamelCase								=		output.images.reshape(__a				,			5_12				,			5_12				,			3    )

           lowerCamelCase								=		images[0, 2_53:2_56, 2_53:2_56, -1]

           lowerCamelCase								=		jnp.asarray(jax.device_get(image_slice.flatten()    )    )
           lowerCamelCase								=		jnp.array(
               [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084]    )
           print(F"""output_slice: {output_slice}"""    )
           assert jnp.abs(output_slice - expected_slice    ).max() < 1E-2