RunMat
GitHub

det — Compute determinants of square matrices in MATLAB and RunMat.

det(A) returns the determinant of real or complex square matrix A. Scalar and empty-matrix cases (det([]) = 1) follow MATLAB and RunMat conventions.

Syntax

d = det(A)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesInput matrix.

Returns

NameTypeDescription
dNumericScalarDeterminant of A.

Errors

IdentifierWhenMessage
RunMat:det:InvalidInputInput shape/type or numeric domain is unsupported for determinant evaluation.det: invalid input
RunMat:det:InternalRuntime fails while evaluating determinant or device fallback paths.det: internal runtime failure

How det works

  • Inputs must be 2-D square matrices. RunMat rejects non-square or higher-rank inputs with the MATLAB error "det: input must be a square matrix."
  • Determinants are computed from an LU factorization with partial pivoting, ensuring numerical behavior that mirrors MATLAB. Singular matrices return 0 (within floating-point round-off).
  • Logical and integer inputs are promoted to double precision before evaluation.
  • Complex inputs are handled with full complex arithmetic. The result has complex type whenever the input is complex.
  • The determinant of the empty matrix ([]) is 1.

Does RunMat run det on the GPU?

When a GPU acceleration provider is active, RunMat first asks the provider for an LU factorization via the lu hook. Providers that implement it keep the factors on device, multiply the diagonal of U, and re-upload the scalar determinant for real inputs so downstream kernels keep running on the GPU. Complex matrices currently gather to the host for the final scalar. Providers that do not expose lu (including the current fallback backends) automatically route to the shared CPU implementation with no user intervention required.

GPU memory and residency

You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB). The fusion planner keeps producers on the GPU, det invokes the provider's lu hook when available, and real determinants are re-uploaded as device scalars so subsequent kernels keep their residency. When the provider lacks lu, RunMat transparently gathers the matrix, computes the determinant with the host fallback, and re-uploads the scalar for real inputs. Complex determinants currently return host scalars until device complex scalars land. gpuArray and gather remain available for explicit residency control and MATLAB compatibility.

Examples

Determinant Of A 2x2 Matrix

A = [4 -2; 1 3];
d = det(A)

Expected output:

d = 14

Checking Zero Determinant For Singular Matrix

B = [1 2; 2 4];
d = det(B)

Expected output:

d = 0

Determinant Of A Complex Matrix

C = [1+2i 0; 3i 4-1i];
d = det(C)

Expected output:

d = 6 + 7i

Determinant Equals Product Of Diagonal For Triangular Matrix

U = [3 2 1; 0 5 -1; 0 0 2];
d = det(U)

Expected output:

d = 30

Determinant Of An Empty Matrix

d = det([])

Expected output:

d = 1

Using det With gpuArray Data

G = gpuArray([2 0 0; 0 3 0; 1 0 4]);
d_gpu = det(G);     % stays on the GPU when a provider is active
d = gather(d_gpu)

Expected output:

d = 24

Using det with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how det changes the result.

Run a small det example, explain the result, then change one input and compare the output.

FAQ

Why does det require square matrices?

The determinant is only defined for square matrices. RunMat mirrors MATLAB by rejecting non-square inputs with "det: input must be a square matrix."

What is det([])?

The determinant of the empty matrix ([]) is 1, matching MATLAB's convention for the product of an empty diagonal.

Does det warn for nearly singular matrices?

No. RunMat returns the floating-point result of the LU-based determinant. Very small magnitudes indicate near singularity, just as in MATLAB.

How does det handle complex matrices?

Complex matrices are factorized in complex arithmetic. The result is complex; if the imaginary part happens to be zero, it is still reported through the complex number interface.

Will the result stay on the GPU?

Yes, when a GPU provider is active RunMat re-uploads the scalar determinant so that subsequent GPU code continues to operate on device-resident data. Providers without LU support fall back to the CPU path and return a host scalar.

Can det overflow or underflow?

Large determinants can overflow or underflow double precision just as in MATLAB. RunMat does not rescale the matrix; if you require scaling, consider log(det(A)) via lu or chol.

Do logical inputs work?

Yes. Logical arrays are promoted to doubles before computing the determinant.

Is the determinant computed exactly?

No. The LU factorization works in floating-point, so the result is subject to round-off. The behavior matches MATLAB's det.

Solve

cond · inv · linsolve · norm · null · pinv · rank · rcond · rref

Factor

chol · eig · lu · qr · svd

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how det is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.