Extract the Q matrix
Extract the R matrix
Reconstruct the original matrix given a QR decomposition
Reconstruct the original matrix given a QR decomposition
Solve the least squares problem: \min ||A * X - B|| Using the QR factorization: \A = Q * R computed by qrDecomp.
Matrix in witch the elements on and above the diagonal of the array contain the min(M, N) x N upper trapezoidal matrix 'R' (R is upper triangular if m >= n). The elements below the diagonal, with the array tau, represent the orthogonal matrix 'Q' as product of min(m, n).
The scalar factors of the elementary reflectors
import mir.ndslice; auto A = [ 1, 1, 0, 1, 0, 1, 0, 1, 1 ] .sliced(3, 3) .as!double.slice; auto Q_test = [ -0.7071068, 0.4082483, -0.5773503, -0.7071068, -0.4082483, 0.5773503, 0, 0.8164966, 0.5773503] .sliced(3, 3) .as!double.slice; auto R_test = [ -1.414214, -0.7071068, -0.7071068, 0, 1.2247449, 0.4082483, 0, 0, 1.1547005] .sliced(3, 3) .as!double.slice; auto val = qrDecomp(A); //saving these values to doublecheck they don't change later auto val_matrix = val.matrix.slice; auto val_tau = val.tau.slice; import mir.math.common: approxEqual; import mir.ndslice : equal; auto r = val.R; assert(equal!approxEqual(val.R, R_test)); auto q = val.Q; assert(equal!approxEqual(val.Q, Q_test)); //double-checking values do not change assert(equal!approxEqual(val_matrix, val.matrix)); assert(equal!approxEqual(val_tau, val.tau)); auto a = val.reconstruct; assert(equal!approxEqual(A, a));