public MyMatrix[] qRHouseHolder() { if (numCols() > numRows()) { throw new RuntimeException("Invalid Matrix"); } ArrayList<MyMatrix> hHolder = new ArrayList<MyMatrix>(); MyMatrix temp = new MyMatrix(numRows(), numCols()); for (int i = 0; i < numRows(); i++) { for (int j = 0; j < numCols(); j++) { temp.set(i, j, this.get(i, j)); } } for (int j = 0; j < numCols(); j++) { if (!zeroesBelow(j, j)) { MyVector aVec = new MyVector(temp.getCol(j)); for (int i = 0; i < j; i++) { aVec.set(i, 0); } MyVector uVec = aVec; uVec.set(j, aVec.get(j) + aVec.norm()); uVec = uVec.normalize(); hHolder.add(temp.identity().minus(((uVec.toMyMatrix().times(uVec.transpose()).scale(2))))); temp = hHolder.get(j).times(temp); } } MyMatrix[] qR = new MyMatrix[2]; MyMatrix hhTemp = this.identity(); for (int i = 0; i < hHolder.size(); i++) { hhTemp = hHolder.get(i).times(hhTemp); } qR[0] = hhTemp.transpose(); // q qR[1] = temp; // r return qR; }
public MyMatrix append(MyVector vec) { if (vec.numRows() != this.numRows()) { throw new RuntimeException("Invalid Vector"); } MyMatrix temp = new MyMatrix(this.numRows(), this.numCols() + 1); for (int i = 0; i < temp.numRows(); i++) { for (int j = 0; j < temp.numCols(); j++) { if (j == temp.numCols() - 1) { temp.set(i, j, vec.get(i)); } else { temp.set(i, j, this.get(i, j)); } } } return temp; }