qr — QR factorization with optional column pivoting and economy-size outputs.
qr(A) computes an orthogonal (or unitary)–upper-triangular factorization of a real or complex matrix A. It matches MATLAB’s dense semantics, supporting full-size, economy-size, and column-pivoted variants.
How qr works in RunMat
- 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 factorQand the upper-triangularRsuch thatQ * R = A * E, whereEis the column permutation matrix selected during factorization. When you omitE, the columns ofRalready appear in that permuted order. - Three outputs:
[Q, R, E] = qr(A)additionally returns the column permutation so thatA * E = Q * R. Use the'vector'option to receive a pivot vector instead of a permutation matrix. - Economy size:
qr(A, 0)orqr(A, 'econ')returns the reduced-size factors, mirroring MATLAB’s behaviour: whenm ≥ n,Qism × nandRisn × 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.
How qr runs 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)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.
Related functions to explore
These functions work well alongside qr. Each page has runnable examples you can try in the browser.
lu, chol, svd, det, gpuArray, gather, eig
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how qr works, line by line, in Rust.
- View qr.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.