/** * Computes the householder vector used in QR decomposition. * * u = x / max(x) * u(0) = u(0) + |u| * u = u / u(0) * * @param x Input vector. Unmodified. * @return The found householder reflector vector */ public static CMatrixRMaj householderVector(CMatrixRMaj x) { CMatrixRMaj u = (CMatrixRMaj)x.copy(); float max = CommonOps_CDRM.elementMaxAbs(u); CommonOps_CDRM.elementDivide(u, max, 0, u); float nx = NormOps_CDRM.normF(u); Complex_F32 c = new Complex_F32(); u.get(0, 0, c); float realTau, imagTau; if (c.getMagnitude() == 0) { realTau = nx; imagTau = 0; } else { realTau = c.real / c.getMagnitude() * nx; imagTau = c.imaginary / c.getMagnitude() * nx; } u.set(0, 0, c.real + realTau, c.imaginary + imagTau); CommonOps_CDRM.elementDivide(u, u.getReal(0, 0), u.getImag(0, 0), u); return(u); }
/** * <p> * Creates a reflector from the provided vector.<br> * <br> * Q = I - γ u u<sup>T</sup><br> * γ = 2/||u||<sup>2</sup> * </p> * * @param u A vector. Not modified. * @return An orthogonal reflector. */ public static CMatrixRMaj createReflector(CMatrixRMaj u) { if (!MatrixFeatures_CDRM.isVector(u)) { throw new ArgumentException("u must be a vector"); } float norm = NormOps_CDRM.normF(u); float gamma = -2.0f / (norm * norm); CMatrixRMaj Q = CommonOps_CDRM.identity(u.getNumElements()); CommonOps_CDRM.multAddTransB(gamma, 0, u, u, Q); return(Q); }