void QRZ(Matrix& X, UpperTriangularMatrix& U) { REPORT Tracer et("QRZ(1)"); int n = X.Nrows(); int s = X.Ncols(); U.ReSize(s); U = 0.0; Real* xi0 = X.Store(); Real* u0 = U.Store(); Real* u; int j, k; int J = s; int i = s; while (i--) { Real* xj0 = xi0; Real* xi = xi0; k = n; if (k) for (;;) { u = u0; Real Xi = *xi; Real* xj = xj0; j = J; while(j--) *u++ += Xi * *xj++; if (!(--k)) break; xi += s; xj0 += s; } Real sum = sqrt(*u0); *u0 = sum; u = u0+1; if (sum == 0.0) Throw(SingularException(U)); int J1 = J-1; j = J1; while(j--) *u++ /= sum; xj0 = xi0; xi = xi0++; k = n; if (k) for (;;) { u = u0+1; Real Xi = *xi; Real* xj = xj0; Xi /= sum; *xj++ = Xi; j = J1; while(j--) *xj++ -= *u++ * Xi; if (!(--k)) break; xi += s; xj0 += s; } u0 += J--; } }
void updateQRZ(Matrix& X, UpperTriangularMatrix& U) { REPORT Tracer et("updateQRZ"); int n = X.Nrows(); int s = X.Ncols(); if (s != U.Ncols()) Throw(ProgramException("Incompatible dimensions",X,U)); if (n == 0 || s == 0) return; Real* xi0 = X.Store(); Real* u0 = U.Store(); Real* u; RowVector V(s); Real* v0 = V.Store(); Real* v; V = 0.0; int j, k; int J = s; int i = s; while (i--) { Real* xj0 = xi0; Real* xi = xi0; k = n; if (k) for (;;) { v = v0; Real Xi = *xi; Real* xj = xj0; j = J; while(j--) *v++ += Xi * *xj++; if (!(--k)) break; xi += s; xj0 += s; } Real r = *u0; Real sum = sqrt(*v0 + square(r)); if (sum == 0.0) { REPORT u = u0; v = v0; j = J; while(j--) { *u++ = 0.0; *v++ = 0.0; } xj0 = xi0++; k = n; if (k) for (;;) { *xj0 = 0.0; if (!(--k)) break; xj0 += s; } u0 += J--; } else { Real frs = fabs(r) + sum; Real a0 = sqrt(frs / sum); Real alpha = a0 / frs; if (r <= 0) { REPORT alpha = -alpha; *u0 = sum; } else { REPORT *u0 = -sum; } j = J - 1; v = v0 + 1; u = u0 + 1; while (j--) { *v = a0 * *u + alpha * *v; *u -= a0 * *v; ++v; ++u; } xj0 = xi0; xi = xi0++; k = n; if (k) for (;;) { v = v0 + 1; Real Xi = *xi; Real* xj = xj0; Xi *= alpha; *xj++ = Xi; j = J - 1; while(j--) *xj++ -= *v++ * Xi; if (!(--k)) break; xi += s; xj0 += s; } j = J; v = v0; while (j--) *v++ = 0.0; u0 += J--; } } }