예제 #1
0
 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;
 }
예제 #2
0
 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;
 }
예제 #3
0
 public MyMatrix times(MyMatrix m) {
   if (numCols() != m.numRows()) {
     throw new RuntimeException("Invalid Matrix");
   }
   Matrix res = new Matrix(a.getRowDimension(), m.numCols());
   for (int i = 0; i < res.getRowDimension(); i++) {
     for (int j = 0; j < res.getColumnDimension(); j++) {
       double[] aY = new double[numCols()];
       double[] bX = new double[m.numRows()];
       for (int n = 0; n < aY.length; n++) {
         aY[n] = a.get(i, n);
       }
       for (int n = 0; n < bX.length; n++) {
         bX[n] = m.get(n, j);
       }
       MyVector u = new MyVector(aY);
       MyVector v = new MyVector(bX);
       res.set(i, j, u.dot(v));
     }
   }
   return new MyMatrix(res);
 }