
    _-Ph                     &    d Z ddlZddlmZ ddZdS )z-Construct sparse matrix from a local stencil.    N)sparsec                     t          j        | |          } t          |          }t          j        | j                  dz  dk                                    st          d          t          |          t          j        |           k    rt          d          t          |          dk     rt          d          t          j	        |          }| dk    
                                }t          j        |t                    }t          j        dgt          t          |                    z             dd	         }t          d
 |                                 D                       }t#          || j                  D ]\  }	}
|	|
dz  z  }	t#          |t          |                    D ]\  }}|||z  z  }| | dk                                 |                              ||          }t          j        |          j        }t#          ||          D ]\  }}|                    |          }t-          |          D ]\  }}	|	dk    rHt/          d          gt          |          z  }
t/          d|	          |
|<   t          |
          }
d||
<   S|	dk     rGt/          d          gt          |          z  }
t/          |	d          |
|<   t          |
          }
d||
<   t1          |          |k     }|                                s||         }||         }t          t          j        |                    t          |          k    rt          j        |          }t          j        t          |          |j        d         f|j                  }t#          ||          D ].\  }}t          j        ||          }||ddfxx         |z  cc<   /|}|}t9          j        ||f||f                              |          S )ak  Construct a sparse matrix form a local matrix stencil.

    Parameters
    ----------
    S : ndarray
        matrix stencil stored in N-d array
    grid : tuple
        tuple containing the N grid dimensions
    dtype :
        data type of the result
    format : string
        sparse matrix format to return, e.g. "csr", "coo", etc.

    Returns
    -------
    A : sparse matrix
        Sparse matrix which represents the operator given by applying
        stencil S at each vertex of a regular grid with given dimensions.

    Notes
    -----
    The grid vertices are enumerated as arange(prod(grid)).reshape(grid).
    This implies that the last grid dimension cycles fastest, while the
    first dimension cycles slowest.  For example, if grid=(2,3) then the
    grid vertices are ordered as (0,0), (0,1), (0,2), (1,0), (1,1), (1,2).

    This coincides with the ordering used by the NumPy functions
    ndenumerate() and mgrid().

    Examples
    --------
    >>> from pyamg.gallery import stencil_grid
    >>> stencil = [-1,2,-1]  # 1D Poisson stencil
    >>> grid = (5,)          # 1D grid with 5 vertices
    >>> A = stencil_grid(stencil, grid, dtype=float, format='csr')
    >>> A.toarray()
    array([[ 2., -1.,  0.,  0.,  0.],
           [-1.,  2., -1.,  0.,  0.],
           [ 0., -1.,  2., -1.,  0.],
           [ 0.,  0., -1.,  2., -1.],
           [ 0.,  0.,  0., -1.,  2.]])

    >>> stencil = [[0,-1,0],[-1,4,-1],[0,-1,0]] # 2D Poisson stencil
    >>> grid = (3,3)                            # 2D grid with shape 3x3
    >>> A = stencil_grid(stencil, grid, dtype=float, format='csr')
    >>> A.toarray()
    array([[ 4., -1.,  0., -1.,  0.,  0.,  0.,  0.,  0.],
           [-1.,  4., -1.,  0., -1.,  0.,  0.,  0.,  0.],
           [ 0., -1.,  4.,  0.,  0., -1.,  0.,  0.,  0.],
           [-1.,  0.,  0.,  4., -1.,  0., -1.,  0.,  0.],
           [ 0., -1.,  0., -1.,  4., -1.,  0., -1.,  0.],
           [ 0.,  0., -1.,  0., -1.,  4.,  0.,  0., -1.],
           [ 0.,  0.,  0., -1.,  0.,  0.,  4., -1.,  0.],
           [ 0.,  0.,  0.,  0., -1.,  0., -1.,  4., -1.],
           [ 0.,  0.,  0.,  0.,  0., -1.,  0., -1.,  4.]])

    )dtype      z"all stencil dimensions must be oddzOstencil dimension must equal number of grid                          dimensionsz grid dimensions must be positiver   Nc              3   >   K   | ]}|                                 V  d S )N)copy).0is     U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/gallery/stencil.py	<genexpr>zstencil_grid.<locals>.<genexpr>W   s*      22AFFHH222222    )shape)npasarraytupler   all
ValueErrorlenndimminprodsumzerosintcumprodlistreversednonzeroziprepeatreshapevstackT	enumeratesliceabsuniquer   searchsortedr   
dia_matrixasformat)Sgridr   formatN_vN_sdiagsstridesindicesr   sstridecoordsdataindexdiagnmask	new_diagsnew_datadiadats                         r   stencil_gridrA      s   t 	
1E"""A;;DJqw!#q(--// ?=>>>
4yyBGAJJ & ' ' 	' 4yy1}};<<<
'$--C6,,..C HS$$$E j!tHTNN33344SbS9G22aiikk22222GGQW%%  1	Q!V gx'8'899 ! !& Q!V9C  ((c22Di  "G 7D))  t||D!!e$$ 
	 
	DAq1uu4[[MCII-Q{{!!HHQQ4[[M#d))+Q~~!!HHQ
	 u::D88:: dDz 29UE

**Ie$$	8S^^TZ];"&*. . . E4(( 	" 	"HC	3//AQTNNNc!NNNNdE]$':/ / //7x/?/?@r   )NN)__doc__numpyr   scipyr   rA    r   r   <module>rF      sU    3 3          @ @ @ @ @ @r   