Ejemplo n.º 1
0
  /**
   * Compute the singular values of a matrix.
   *
   * @param A FloatMatrix of dimension m * n
   * @return A min(m, n) vector of singular values.
   */
  public static FloatMatrix SVDValues(FloatMatrix A) {
    int m = A.rows;
    int n = A.columns;
    FloatMatrix S = new FloatMatrix(min(m, n));

    int info =
        NativeBlas.sgesvd('N', 'N', m, n, A.dup().data, 0, m, S.data, 0, null, 0, 1, null, 0, 1);

    if (info > 0) {
      throw new LapackConvergenceException(
          "GESVD", info + " superdiagonals of an intermediate bidiagonal form failed to converge.");
    }

    return S;
  }
Ejemplo n.º 2
0
  /**
   * Compute a singular-value decomposition of A (sparse variant). Sparse means that the matrices U
   * and V are not square but only have as many columns (or rows) as necessary.
   *
   * @param A
   * @return A FloatMatrix[3] array of U, S, V such that A = U * diag(S) * V'
   */
  public static FloatMatrix[] sparseSVD(FloatMatrix A) {
    int m = A.rows;
    int n = A.columns;

    FloatMatrix U = new FloatMatrix(m, min(m, n));
    FloatMatrix S = new FloatMatrix(min(m, n));
    FloatMatrix V = new FloatMatrix(min(m, n), n);

    int info =
        NativeBlas.sgesvd(
            'S', 'S', m, n, A.dup().data, 0, m, S.data, 0, U.data, 0, m, V.data, 0, min(m, n));

    if (info > 0) {
      throw new LapackConvergenceException(
          "GESVD", info + " superdiagonals of an intermediate bidiagonal form failed to converge.");
    }

    return new FloatMatrix[] {U, S, V.transpose()};
  }