What does the det function do in MATLAB / RunMat?
det(A) returns the determinant of a real or complex square matrix A. Scalars behave like
det(1x1) = A(1,1), and the empty matrix ([]) has determinant 1, matching MATLAB's convention.
How does the det function behave in MATLAB / RunMat?
- 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 (
[]) is1.
det Function GPU Execution Behaviour
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.
Examples of using the det function in MATLAB / RunMat
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 (values rounded):
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
GPU residency in RunMat (Do I need gpuArray?)
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.
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.
See Also
inv, pinv, lu, trace, gpuArray, gather
Source & Feedback
- The full source code for the implementation of the
detfunction is available at:crates/runmat-runtime/src/builtins/math/linalg/solve/det.rs - Found a bug or behavioral difference? Please open an issue with details and a minimal repro.