API: Utilities Module (util)¶
Helper functions for common tasks.
pysymmetry.util.laplacian1d(n, h=1)
¶
Return the 1D three-point Laplacian on a line of length n (CSC sparse).
The stencil is (1, -2, 1) scaled by 1/h^2.
INPUT: - n -- positive integer, number of grid points - h -- grid spacing (default: 1)
OUTPUT: - SciPy csc_matrix of shape (n, n)
EXAMPLES::
sage: from pysymmetry.util import laplacian1d
sage: A = laplacian1d(4)
sage: A.get_shape()
(4, 4)
pysymmetry.util.laplacian2d(n)
¶
Return the 2D five-point Laplacian on an n x n grid (CSC sparse).
Uses a block-diagonal structure with off-diagonal blocks to couple vertical neighbors; diagonal blocks contain the standard 1D stencil.
INPUT: - n -- positive integer, grid side length
OUTPUT: - SciPy csc_matrix of shape \((n^2, n^2)\) representing -Δ on the grid
EXAMPLES::
sage: from pysymmetry.util import laplacian2d
sage: A = laplacian2d(3)
sage: A.get_shape()
(9, 9)
pysymmetry.util.advection_diffusion_2d(n, D=1.0, vx=1.0)
¶
Build the 2D advection-diffusion operator on an n x n grid (CSC sparse).
The operator is A = D(-Δ) - vxDx, where -Δ is the 2D five-point Laplacian and Dx is a centered first-difference in the x-direction.
INPUT: - n -- positive integer, grid side length - D -- diffusion coefficient (default: 1.0) - vx -- advection velocity in x (default: 1.0)
OUTPUT: - SciPy csc_matrix of shape (n^2, n^2)
EXAMPLES::
sage: from pysymmetry.util import advection_diffusion_2d
sage: A = advection_diffusion_2d(3)
sage: A.get_shape()
(9, 9)
pysymmetry.util.to_csr(m)
¶
Convert a Sage sparse matrix to a SciPy CSC sparse matrix.
INPUT: - m -- a Sage matrix (typically sparse) with a .dict() method of nonzeros
OUTPUT: - SciPy csc_matrix with the same nonzero pattern and values
EXAMPLES::
sage: from sage.all import matrix
sage: from pysymmetry.util import to_csr
sage: M = matrix({(0,0): 2, (1,2): -1})
sage: S = to_csr(M)
sage: S.get_shape()
(2, 3)
pysymmetry.util.to_csc(m)
¶
Convert a Sage sparse matrix to a SciPy CSC sparse matrix.
INPUT: - m -- a Sage matrix (typically sparse) with a .dict() method of nonzeros
OUTPUT: - SciPy csc_matrix with the same nonzero pattern and values
EXAMPLES::
sage: from sage.all import matrix
sage: from pysymmetry.util import to_csc
sage: M = matrix({(0,1): 3, (2,0): -5})
sage: S = to_csc(M)
sage: S.get_shape()
(3, 2)
pysymmetry.util.view(matrix, latex=True)
¶
Pretty-print a Sage matrix, optionally using LaTeX rendering.
Attempts to convert symbolic entries to radical expressions for readability. If latex=True, shows the matrix via Sage's show().
INPUT: - matrix -- a Sage matrix - latex -- bool (default: True); if True, render with show()
OUTPUT: - The displayed object (if latex=True) or the transformed matrix
EXAMPLES::
sage: from sage.all import matrix, sqrt
sage: from pysymmetry.util import view
sage: M = matrix([[1, sqrt(2)], [0, 1]])
sage: view(M, latex=False) # returns a matrix with radical_expression applied
[1 sqrt(2)]
[0 1]