
    _-Ph,                     <    d Z ddlZddlZddlmZ eddfdZd
d	Z	dS )z'Discretizations of the Poisson problem.    N   )stencil_gridFDc                    t          |           } t          |           }|dk     st          |           dk     rt          d|            |dk    rdt	          j        d|z  |          }t          |          D ]0}d|d|z  dz   d||z
  dz
  z  z   <   d|d|z  d	z   d||z
  dz
  z  z   <   1d
|z  |d|z  <   |dk    r(t	          j        d|z  |           }d|z  dz
  |d|z  <   t          || |          S )a  Return a sparse matrix for the N-dimensional Poisson problem.

    The matrix represents a finite Difference approximation to the
    Poisson problem on a regular n-dimensional grid with unit grid
    spacing and Dirichlet boundary conditions.

    The last dimension is iterated over first: z, then y, then x.
    This should be used with np.mgrid() or np.ndenumerate.

    Parameters
    ----------
    grid : tuple of integers
        grid dimensions e.g. (100,100)

    Notes
    -----
    The matrix is symmetric and positive definite (SPD).

    Examples
    --------
    >>> from pyamg.gallery import poisson
    >>> # 4 nodes in one dimension
    >>> poisson((4,)).toarray()
    array([[ 2., -1.,  0.,  0.],
           [-1.,  2., -1.,  0.],
           [ 0., -1.,  2., -1.],
           [ 0.,  0., -1.,  2.]])

    >>> # rectangular two dimensional grid
    >>> poisson((2,3)).toarray()
    array([[ 4., -1.,  0., -1.,  0.,  0.],
           [-1.,  4., -1.,  0., -1.,  0.],
           [ 0., -1.,  4.,  0.,  0., -1.],
           [-1.,  0.,  0.,  4., -1.,  0.],
           [ 0., -1.,  0., -1.,  4., -1.],
           [ 0.,  0., -1.,  0., -1.,  4.]])
    r   zInvalid grid shape: r   )   )dtype)r   )r   )   r
   FEr   )format)	tuplelenmin
ValueErrornpzerosrangeonesr   )gridr   r   typeNstencilis          W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/gallery/laplacian.pypoissonr   
   s)   L ;;DD		A1uuD		A666777 t||(4!85111q 	7 	7A46GDFTMD!A#a%L0146GDFTMD!A#a%L011A#Qt||74!851111Q$(Qf5555          ?皙?c           	      	   | }t          ||fdt                    }dt          j        z  |z  t          j                            ||z            z  }dt          j        z  |z  t          j                            ||z            z  }t          |j                  D ]}|j        |         }|j	        |         }	t          j
        ||	z
            }
t          ||	          }||	k    rd}nd}|
dk    r(dt          j        |||         z            z  |j        |<   |
|k    r(dt          j        |||         z            z  |j        |<   dt          j        z  |z  t          j                            ||z            z  }dt          j        z  |z  t          j                            ||z            z  }g }g }g }g }t          d|          D ]J}|                    |           |                    |||z  z   |z
             |                    |           Kt          ||z  |z
  ||z            D ]J}|                    |           |                    |||z  z
  |z              |                    |           Kt          d||z  dz
  |          D ]G}|                    |           |                    ||z   dz
             |                    d           Ht          |dz
  ||z  |          D ]G}|                    |           |                    ||z
  dz              |                    d           Ht          t!          ||                    D ]\  }\  }}	||         }
t          ||	          }||	k    rd}nd}|
dk    r3|                    dt          j        |||         z            z             |
|k    r3|                    dt          j        |||         z            z             t          j        |j        t          j        |          f          }t          j        |j        t          j        |          f          }t          j        |j	        t          j        |          f          }t&          j                            |||ff||z  ||z  f                                          }d|d	z  z  |z  S )
a  Construct a Gauge Laplacian from Quantum Chromodynamics for regular 2D grids.

    Note that this function is not written efficiently, but should be
    fine for N x N grids where N is in the low hundreds.

    Parameters
    ----------
    npts : int
        number of pts in x and y directions

    spacing : float
        grid spacing between points

    beta : float
        temperature
        Note that if beta=0, then we get the typical 5pt Laplacian stencil

    Returns
    -------
    A : csr matrix
        A is Hermitian positive definite for beta > 0.0
        A is Symmetric semi-definite for beta = 0.0

    Examples
    --------
    >>> from pyamg.gallery import gauge_laplacian
    >>> A = gauge_laplacian(10)

    References
    ----------
    .. [1] MacLachlan, S. and Oosterlee, C.,
       "Algebraic Multigrid Solvers for Complex-Valued Matrices",
       Vol. 30, SIAM J. Sci. Comp, 2008

    coo)r   r   y               @g      r   r   r   )shaper
   )r   complexr   pirandomrandnr   nnzrowcolabsr   expdataappend	enumerateziphstackarrayspsparse
coo_matrixtocsr)nptsspacingbetar   Aalpha_xalpha_yr   rcdiffindexsnew_rnew_cnew_datanew_diffr+   r'   r(   s                       r   gauge_laplacianrD   F   s~   L 	AAuG444A 25 4'")//!A#*>*>>G25 4'")//!A#*>*>>G 15\\ : :E!HE!Hva!e}}Aq		q55AAA199rva'%.&8999AF1I199rva'%.&8999AF1I 25 4'")//!A#*>*>>G25 4'")//!A#*>*>>GEEHH1a[[  QQ1Wq[!!!1Q37AaC    QQ1Wq[!!!1ac!eQ  QQUQY1Q3!Q  QQUQYs5%0011 ? ?	6Aq{Aq		q55AAA199OOD26!gen*<#=#==>>>199OOD26!gen*<#=#==>>> 9afbhx00122D
)QUBHUOO,
-
-C
)QUBHUOO,
-
-C
	dS#J/!QqSzBBHHJJA
NAr   )r   r   )
__doc__numpyr   scipyr1   r   r   floatr   rD    r   r   <module>rJ      sy    - -         ! ! ! ! ! ! d 96 96 96 96xp p p p p pr   