Ejemplo n.º 1
0
 /**
  * Frobenius norm
  *
  * @return sqrt of sum of squares of all elements.
  */
 public double normF() {
   double f = 0;
   for (int i = 0; i < m; i++) {
     for (int j = 0; j < n; j++) {
       f = Maths.hypot(f, A[i][j]);
     }
   }
   return f;
 }
Ejemplo n.º 2
0
  /**
   * QR Decomposition, computed by Householder reflections.
   *
   * @param A Rectangular matrix
   * @return Structure to access R and the Householder vectors and compute Q.
   */
  public QRDecomposition(Matrix A) {
    // Initialize.
    QR = A.getArrayCopy();
    m = A.getRowDimension();
    n = A.getColumnDimension();
    Rdiag = new double[n];

    // Main loop.
    for (int k = 0; k < n; k++) {
      // Compute 2-norm of k-th column without under/overflow.
      double nrm = 0;
      for (int i = k; i < m; i++) {
        nrm = Maths.hypot(nrm, QR[i][k]);
      }

      if (nrm != 0.0) {
        // Form k-th Householder vector.
        if (QR[k][k] < 0) {
          nrm = -nrm;
        }
        for (int i = k; i < m; i++) {
          QR[i][k] /= nrm;
        }
        QR[k][k] += 1.0;

        // Apply transformation to remaining columns.
        for (int j = k + 1; j < n; j++) {
          double s = 0.0;
          for (int i = k; i < m; i++) {
            s += QR[i][k] * QR[i][j];
          }
          s = -s / QR[k][k];
          for (int i = k; i < m; i++) {
            QR[i][j] += s * QR[i][k];
          }
        }
      }
      Rdiag[k] = -nrm;
    }
  }