RunMat
GitHub

qr — Compute QR factorizations in MATLAB and RunMat.

qr(A) computes orthogonal/unitary-upper-triangular factorizations of matrix A. Full, economy, and pivoted output forms follow MATLAB semantics.

Syntax

R = qr(A)
R = qr(A, option)
R = qr(A, option1, option2)
[Q, R] = qr(A)
[Q, R] = qr(A, option)
[Q, R] = qr(A, option1, option2)
All supported qr forms
R = qr(A)
R = qr(A, option)
R = qr(A, option1, option2)
[Q, R] = qr(A)
[Q, R] = qr(A, option)
[Q, R] = qr(A, option1, option2)
[Q, R, P] = qr(A)
[Q, R, P] = qr(A, option)
[Q, R, P] = qr(A, option1, option2)

Inputs

NameTypeRequiredDefaultDescription
ANumericArrayYesInput matrix to factorize.
optionStringScalarYesOption token (`0`, `econ`, `matrix`, or `vector`).
option1StringScalarYesFirst option token (`0`, `econ`, `matrix`, or `vector`).
option2StringScalarYesSecond option token (`0`, `econ`, `matrix`, or `vector`).

Returns

NameTypeDescription
RNumericArrayUpper triangular (or trapezoidal) factor.
QNumericArrayOrthogonal/unitary factor.
PNumericArrayPermutation matrix or vector based on pivot mode.

Returned values from qr depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:qr:InvalidArgumentOptions are invalid or output count exceeds supported forms.qr currently supports at most three outputs
RunMat:qr:InvalidInputInput is non-numeric or has rank greater than two.qr: expected a numeric 2-D input matrix
RunMat:qr:InternalQR runtime fails to materialize output values.qr: internal runtime failure

How qr works

  • Single output: R = qr(A) returns the upper-triangular (or upper-trapezoidal) factor. Column pivoting is applied implicitly for numerical stability.
  • Two outputs: [Q, R] = qr(A) yields the orthonormal/unitary factor Q and the upper-triangular R such that Q * R = A * E, where E is the column permutation matrix selected during factorization. When you omit E, the columns of R already appear in that permuted order.
  • Three outputs: [Q, R, E] = qr(A) additionally returns the column permutation so that A * E = Q * R. Use the 'vector' option to receive a pivot vector instead of a permutation matrix.
  • Economy size: qr(A, 0) or qr(A, 'econ') returns the reduced-size factors, mirroring MATLAB’s behaviour: when m ≥ n, Q is m × n and R is n × n. For wide matrices (m < n) the economy form equals the full form.
  • Logical inputs are promoted to double precision. Scalars and vectors are treated as 1-D or 2-D dense arrays.

Does RunMat run qr on the GPU?

When the active acceleration provider exposes the dedicated QR hook (the bundled WGPU backend does), RunMat stages the input through the provider, performs the factorisation via the runtime implementation, and re-uploads all outputs so they remain resident as gpuTensor handles.

If no provider hook is present, RunMat gathers the input to the host, performs the CPU factorisation, and leaves the outputs on the host until an explicit gpuArray request.

Examples

Computing the full QR factorization of a square matrix

A = [12 -51 4; 6 167 -68; -4 24 -41];
[Q, R] = qr(A)

Obtaining orthonormal Q and upper-triangular R for a tall matrix

A = [1 2; 3 4; 5 6];
[Q, R] = qr(A)

Working with column pivoting when requesting three outputs

A = [1 1 0; 1 0 1; 0 1 1];
[Q, R, E] = qr(A)

Using economy-size QR to reduce memory footprint

A = randn(1000, 20);
[Q, R] = qr(A, 0)

Receiving the permutation vector instead of a matrix

A = magic(4);
[Q, R, p] = qr(A, 'vector')

Running QR on a gpuArray input (with automatic fallback)

G = gpuArray(rand(256, 64));
[Q, R] = qr(G, 'econ');
class(Q)

Using qr with coding agents

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

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

FAQ

Does qr(A) always perform column pivoting?

Yes. MATLAB switched to pivoted QR for the single-output form many releases ago; RunMat mirrors that behaviour for improved numerical stability.

How do I request the reduced (economy) factors?

Pass 0, '0', or 'econ' as the second argument. For example, [Q,R] = qr(A,0) or [Q,R] = qr(A,'econ').

How can I obtain the permutation vector instead of a matrix?

Add the option 'vector', e.g., [Q,R,p] = qr(A,'vector'). When you also request economy size, use [Q,R,p] = qr(A,'econ','vector').

Are complex matrices supported?

Yes. Inputs of type complex double produce complex orthonormal factors using complex Householder reflectors.

What precision do the returned factors use?

The builtin always produces double-precision (or complex double) outputs, matching MATLAB’s dense QR behaviour.

Can I call qr on logical arrays?

Yes. Logical inputs are converted to double precision before factorisation.

What happens if I pass more than two option arguments?

The builtin raises a MATLAB-compatible error (qr: too many option arguments). Only one size option and one permutation option are accepted.

Do gpuArray outputs stay on the device?

Yes when an acceleration provider exposes the QR hook; the bundled WGPU backend does so by staging through the host implementation and re-uploading the factors. Otherwise RunMat gathers the data to the host, factors it there, and uploads the results back automatically.

How can I verify the factorisation?

Check that Q'*Q is (approximately) the identity matrix and that Q*R equals A*E. For vector permutations, A(:,p) reproduces the pivoted columns.

Factor

chol · eig · lu · svd

Solve

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

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how qr 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.