QR factorization of a complex matrix

727 Views Asked by At

I am looking for an algorithm for factorizing $n \times n$ matrices of complex elements $a + bi$. My implementation of the Graham-Schmidt process follows Wikipedia and always works in the case of $b = 0$ and fails otherwise. I defined the inner product for complex vectors as per Wikipedia.

EDIT: Examples and code

QR factorization of $\begin{bmatrix}3i&2-2i\\4+7i&-1+5i\end{bmatrix}$ produces $Q=\begin{bmatrix}0.35i&0.17-0.46i\\0.46+0.81i&-0.66+0.57i\end{bmatrix}$ and $R=\begin{bmatrix}-4.88+6.51i&-3.84+2.21i\\-5.27-1.8i&-2.77-5.13i\end{bmatrix}$, which not only isn't MATLAB's result, it's not even correct - they don't multiply to produce A.

public Double innerProd(Matrix v, Matrix w){
    Double sum = 0.0;
    for (int i = 0; i < v.numRows(); i++){
        sum += v.getElement(i,0)*w.getElement(i,0).conjugate();
    }
    return sum;
}

public Matrix proj(Matrix u, Matrix a){
    return u.scalarMult(innerProd(u,a)/innerProd(u,u));
}

public Matrix[] QR(Matrix X){
    ArrayList<Matrix> U = new ArrayList();
    ArrayList<Matrix> E = new ArrayList();
    ArrayList<Matrix> A = new ArrayList();
    for (int j = 0; j < X.numCols; j++) {
        Matrix a = X.getCol(j);
        Matrix u = a.duplicate();
        for (int k = 0; k < j; k++) {
            u = u.subtract((proj(U.get(k), a);
        }
        U.add(u);
        E.add(u.scalarDivide(u.mag()));
        int z = 0;
        do {
            a = a.add(E.get(z).scalarMult(innerProd(E.get(z), a)));
            z++;
        } while (z < j);
        A.add(a);
    }
    Matrix Q = new Matrix(X.numRows, X.numCols);
    for (int j = 0; j < X.numCols; j++){
        for (int i = 0; i < X.numRows; i++)
            Q.setElement(E.get(j).getElement(i, 0), i, j);
    }
    return new Matrix[]{Q, Q.transpose().mult(this)};
}