(grid, dtype=float, format=None, type='FD')
return stencil_grid(stencil, grid, format=format)
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.

    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.]])
Return a sparse matrix for the N-dimensional Poisson problem.
Return a sparse matrix for the N - dimensional Poisson problem .
def poisson(grid, dtype=float, format=None, type='FD'):
    """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.

    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.]])

    """
    grid = tuple(grid)

    N = len(grid)  # grid dimension

    if N < 1 or min(grid) < 1:
        raise ValueError(f'Invalid grid shape: {grid}')

    # create N-dimension Laplacian stencil
    if type == 'FD':
        stencil = np.zeros((3,) * N, dtype=dtype)
        for i in range(N):
            stencil[(1,)*i + (0,) + (1,)*(N-i-1)] = -1
            stencil[(1,)*i + (2,) + (1,)*(N-i-1)] = -1
        stencil[(1,)*N] = 2*N

    if type == 'FE':
        stencil = -np.ones((3,) * N, dtype=dtype)
        stencil[(1,)*N] = 3**N - 1

    return stencil_grid(stencil, grid, format=format)
def poisson ( grid , dtype = float , format = None , type = 'FD' ) : grid = tuple ( grid ) N = len ( grid ) # grid dimension if N < 1 or min ( grid ) < 1 : raise ValueError ( f'Invalid grid shape: {grid}' ) # create N-dimension Laplacian stencil if type == 'FD' : stencil = np . zeros ( ( 3 , ) * N , dtype = dtype ) for i in range ( N ) : stencil [ ( 1 , ) * i + ( 0 , ) + ( 1 , ) * ( N - i - 1 ) ] = - 1 stencil [ ( 1 , ) * i + ( 2 , ) + ( 1 , ) * ( N - i - 1 ) ] = - 1 stencil [ ( 1 , ) * N ] = 2 * N if type == 'FE' : stencil = - np . ones ( ( 3 , ) * N , dtype = dtype ) stencil [ ( 1 , ) * N ] = 3 ** N - 1 return stencil_grid ( stencil , grid , format = format )