| Bytes | Lang | Time | Link |
|---|---|---|---|
| 098 | BQN | 250308T211356Z | panadest |
| 019 | Octave | 171226T214349Z | Mr. Xcod |
| 324 | Python 2 | 171227T012457Z | Tyilo |
| 037 | R | 171226T215258Z | rturnbul |
| 028 | Python with numpy | 171227T003354Z | Tyilo |
| 002 | Julia | 171227T001840Z | Alex A. |
| 027 | SageMath | 171226T235308Z | Tyilo |
| 015 | Wolfram Language Mathematica | 171226T221151Z | notjagan |
BQN, 110 100 98 bytes
+˝∘×⎉1‿∞{1=⊢´≢𝕩?𝕩⊸÷⟜⊑⊸⋈√+˝×˜𝕩;∾˘{(q𝔽𝕨)⋈(r𝔽t)∾0𝔽⍟k𝕩}´𝕊𝔽{𝕘-𝕩𝔽t↩𝕩⍉⊸𝔽𝕘}(k↓˘𝕩)⊑q‿r←𝕊𝕩↑˘˜k←⌈2÷˜⊢´≢𝕩⊣t←@}
An answer without built-ins. Inspired by the recursive block QR algorithm from Golub and van Loan's book, algorithm 5.2.3. Uses Gram–Schmidt.
Octave, 19 bytes
@(x)[[q,r]=qr(x),r]
My first Octave answer \o/
Octave’s qr has quite a few alternatives in other languages that return both Q and R: QRDecomposition (Mathematica), matqr (PARI/GP), 128!:0 - if I recall correctly - (J), qr (R)...
Python 2, 329 324 bytes
import fractions
I=lambda v,w:sum(a*b for a,b in zip(v,w))
def f(A):
A,U=[map(fractions.Fraction,x)for x in zip(*A)],[]
for a in A:
u=a
for v in U:u=[x-y*I(v,a)/I(v,v)for x,y in zip(u,v)]
U.append(u)
Q=[[a/I(u,u)**.5 for a in u]for u in U];return zip(*Q),[[I(e,a)*(i>=j)for i,a in enumerate(A)]for j,e in enumerate(Q)]
We must use fractions to ensure correct output, see https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process#Numerical_stability
Indentation used:
- 1 space
- 1 tab
Python with numpy, 28 bytes
import numpy
numpy.linalg.qr
Julia, 2 bytes
qr
The function qr accepts a square matrix and returns a Tuple of matrices: Q and R.
SageMath, 27 bytes
lambda x:matrix(RDF,x).QR()