View all functions

CategoryMath: Linalg & Solve
GPUYes
BLAS/LAPACK

What does the cond function do in MATLAB / RunMat?

k = cond(A) returns the condition number of matrix A, measuring how sensitive solutions to linear systems are to small perturbations in A or in the right-hand side. By default, cond computes the 2-norm condition number using the ratio of the largest to smallest singular values.

How does the cond function behave in MATLAB / RunMat?

  • cond(A) and cond(A, 2) use the singular values of A, so rectangular matrices are supported.
  • cond(A, 1), cond(A, Inf), and cond(A, 'fro') require a square, invertible matrix and use the definition norm(A, p) * norm(inv(A), p) with MATLAB's norm semantics.
  • Scalars behave like 1x1 matrices. Non-zero scalars have condition number 1, while cond(0) = Inf.
  • Empty matrices return 0, matching MATLAB's convention.
  • Singular or rank-deficient matrices return Inf.
  • Complex inputs are handled in full complex arithmetic.

cond function GPU execution behaviour

When the input already lives on a GPU, RunMat first looks for an acceleration provider that exposes the custom cond hook registered below. Current providers gather the matrix to host memory, reuse the shared CPU implementation, and then re-upload the scalar so downstream GPU computations preserve residency. This mirrors MATLAB semantics while keeping the user-facing API uniform.

Examples of using the cond function in MATLAB / RunMat

Condition number of the identity matrix

A = eye(3);
k = cond(A);

Expected output:

k = 1

Diagnosing an ill-conditioned diagonal matrix

D = diag([1, 1e-8]);
k = cond(D);

Expected output:

k = 1.0e+8

Condition number of a rectangular matrix (2-norm)

A = [1 0; 0 1; 1 1];
k = cond(A, 2);

Expected output:

k = 1.7321

Using a different norm specification

A = [4 -1; 2 3];
k1 = cond(A, 1);
kInf = cond(A, Inf);

Expected output (rounded):

k1   = 2.1429
kInf = 2.1429

Complex-valued matrices

A = [1+2i 0; 3i 4-1i];
k = cond(A);

Expected output (rounded):

k = 3.0327

Empty inputs and GPU residency

G = gpuArray([]);      % Empty 0x0 matrix on the GPU
k = cond(G);           % Returns 0 and keeps residency when possible
result = gather(k);

Expected output:

result = 0

GPU residency in RunMat (Do I need gpuArray?)

Manual calls to gpuArray are rarely necessary. When matrices already reside on the device, RunMat attempts to execute cond via the active provider. If no native implementation exists, the runtime gathers the matrix, computes the condition number with the shared CPU path, and uploads the scalar result back to the GPU. This preserves compatibility with MATLAB while keeping the workflow simple.

FAQ

What does a large condition number mean?

Large condition numbers (>> 1) indicate that small perturbations in the input can produce large changes in the solution of a linear system involving A. Values close to 1 indicate a well- conditioned matrix.

Why does cond return Inf for singular matrices?

Singular matrices have at least one zero singular value (or an undefined inverse), so the condition number is mathematically infinite. RunMat mirrors MATLAB and returns Inf in these cases.

Does cond support rectangular matrices?

Yes for the default 2-norm: cond(A) uses singular values and accepts any two-dimensional matrix. Norms 1, Inf, and 'fro' require a square, invertible matrix because they are defined using the matrix inverse.

How does cond handle empty matrices?

All norm choices return 0 for empty matrices (0x0), matching MATLAB's behaviour.

Will calling cond move my data off the GPU?

Only when the active provider lacks a dedicated implementation. In that case RunMat gathers the data, computes the scalar on the host, and uploads it back so subsequent GPU operations still see a device-resident value.

See Also

rcond, inv, pinv, linsolve, gpuArray, gather

Source & Feedback

  • Implementation: crates/runmat-runtime/src/builtins/math/linalg/solve/cond.rs
  • Found a behavioural difference? Open an issue with a minimal reproduction.