symrcm — Compute symmetric reverse Cuthill-McKee permutations that reduce matrix bandwidth.
r = symrcm(A) returns a permutation vector for reordering symmetric or structurally symmetric matrices to reduce bandwidth. Use A(r, r) to obtain a tighter band structure, matching MATLAB-compatible ordering behavior.
Syntax
p = symrcm(A)Inputs
A- Type
Any- Required
- Yes
- Description
- Input matrix.
Returns
p- Type
NumericArray- Description
- Permutation row vector (1-based indices) from reverse Cuthill-McKee ordering.
Errors
RunMat:symrcm:InvalidInput- When
- Input shape/type cannot be interpreted as a numeric or logical square matrix.
- Message
symrcm: invalid input
RunMat:symrcm:Internal- When
- Runtime fails while constructing intermediate tensors or adjacency structures.
- Message
symrcm: internal runtime failure
Behavior and supported arrays
r = symrcm(A) returns a permutation vector for reordering square matrices to reduce bandwidth. Use A(r, r) to obtain a tighter band structure. More than one valid RCM ordering can exist, so exact permutation values need not match another implementation.
Treats the input as an undirected graph based on the structural pattern of A and A'. Any value that is not exactly zero (including NaN or Inf) creates an edge; diagonal entries are ignored.
Accepts full or sparse numeric, logical, and complex matrices. Host sparse CSC indices and native typed values build the graph without densification. Explicit numeric zeros are ignored; logical sparse pattern entries are true. Integer entries are tested for zero exactly, and complex entries are nonzero when either component is nonzero.
Requires a square matrix (scalars and the empty matrix are allowed). Inputs with more than two non-singleton dimensions raise an error, matching MATLAB matrix semantics.
Processes disconnected components independently. A degree/index-stable pseudoperipheral search chooses each component start, then Cuthill-McKee breadth-first search and reversal yield a deterministic symmetric ordering.
Returns a row vector of 1-based indices. Fully diagonal or zero matrices produce the identity permutation.
GPU residency and provider behavior
Resident matrix input is admitted through its owning acceleration provider. Explicit gpuArray is optional in RunMat; the current WGPU sym_rcm hook downloads the matrix and runs the host ordering algorithm rather than claiming a device-side RCM kernel.
RunMat first asks the active acceleration provider whether it implements the sym_rcm hook. A typed unsupported-hook result permits an owning-provider gather and host calculation. Other provider errors remain terminal and are not replayed as a host calculation.
Acceleration providers may implement the sym_rcm hook. The WGPU backend currently downloads the matrix, runs the host algorithm, and returns the permutation.
The provider specification accepts single and double precision, without broadcasting. This one-input graph operation is neither an elementwise fusion nor a reduction fusion; its runtime specification uses inline literals. No optional feature is required.
Examples
Reducing bandwidth of a near-band matrix
A = [4 1 0 0 2;
1 4 1 0 0;
0 1 4 1 0;
0 0 1 4 1;
2 0 0 1 4];
r = symrcm(A);
B = A(r, r);
bandwidth(A)
bandwidth(B)Handling disconnected components with symrcm
A = [1 1 0 0 0;
1 1 0 0 0;
0 0 1 0 1;
0 0 0 1 1;
0 0 1 1 1];
r = symrcm(A);
B = A(r, r)Using symrcm with logical adjacency matrices
adj = logical([0 1 0 0;
1 0 1 0;
0 1 0 1;
0 0 1 0]);
r = symrcm(adj)Applying symrcm to GPU matrices
G = gpuArray([0 1 0 0 2;
1 0 1 0 0;
0 1 0 1 0;
0 0 1 0 1;
2 0 0 1 0]);
r = symrcm(G);
H = gather(G(r, r))Using symrcm with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how symrcm changes the result.
Run a small symrcm example, explain the result, then change one input and compare the output.
FAQ
What class of matrices benefit from symrcm?⌄
Sparse or full square matrices whose graph bandwidth dominates solver cost, such as discretised PDE operators, circuit matrices, or graph Laplacians. Host sparse CSC input builds the symmetric graph directly without densifying.
Does symrcm modify the matrix?⌄
No. It returns a permutation vector. Apply it as A(r, r) or reorder vectors with x(r).
What happens if the matrix is not symmetric?⌄
RunMat creates an undirected edge whenever either A(i, j) or A(j, i) is nonzero. It combines their structural patterns without numerically adding the entries, so opposite values do not cancel an edge. Asymmetric inputs still produce a valid permutation.
Are diagonal entries considered?⌄
Diagonal entries are ignored when building the graph. Only off-diagonal nonzeros contribute edges.
How are NaNs or Infs handled?⌄
Any value that is not numerically equal to zero is treated as nonzero, including NaN or Inf.
Can I use symrcm on dense matrices?⌄
Yes. Full arrays use their nonzero pattern, just as sparse arrays do. A matrix with every off-diagonal entry nonzero has no bandwidth reduction available; its returned permutation need not be the identity.
How do I verify the permutation improves bandwidth?⌄
Compute bandwidth(A) and bandwidth(A(r, r)) and compare the results. Lower lower/upper bandwidth values indicate a tighter band and more efficient factorizations.
What output shape should I expect?⌄
The permutation is returned as a row vector (1 × n). The empty matrix returns the empty row vector [].
Related Linalg functions
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu
Factor
chol · decomposition · decomposition.ctranspose · decomposition.mldivide · decomposition.mrdivide · decomposition.mtimes · decomposition.rdivide · decomposition.subsref · decomposition.times · decomposition.uminus · decomposition.uplus · eig · eigs · gmres · ichol · ilu · lu · ordeig · ordqz · ordschur · pcg · qr · qz · rsf2csf · schur · svd
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Source you can read
Every RunMat function's source is public. Read exactly how symrcm is executed, line by line, in Rust.
- View the source for symrcm in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.