Beispiel #1
0
  /**
   * Generalized Least Squares via *GELSD.
   *
   * <p>Note that B must be padded to contain the solution matrix. This occurs when A has fewer rows
   * than columns.
   *
   * <p>For example: in A * X = B, A is (m,n), X is (n,k) and B is (m,k). Now if m < n, since B is
   * overwritten to contain the solution (in classical LAPACK style), B needs to be padded to be an
   * (n,k) matrix.
   *
   * <p>Likewise, if m > n, the solution consists only of the first n rows of B.
   *
   * @param A an (m,n) matrix
   * @param B an (max(m,n), k) matrix (well, at least)
   */
  public static void gelsd(FloatMatrix A, FloatMatrix B) {
    int m = A.rows;
    int n = A.columns;
    int nrhs = B.columns;
    int minmn = min(m, n);
    int maxmn = max(m, n);

    if (B.rows < maxmn) {
      throw new SizeException("Result matrix B must be padded to contain the solution matrix X!");
    }

    int smlsiz = NativeBlas.ilaenv(9, "DGELSD", "", m, n, nrhs, 0);
    int nlvl = max(0, (int) log2(minmn / (smlsiz + 1)) + 1);

    //      System.err.printf("GELSD\n");
    //      System.err.printf("m = %d, n = %d, nrhs = %d\n", m, n, nrhs);
    //      System.err.printf("smlsiz = %d, nlvl = %d\n", smlsiz, nlvl);
    //      System.err.printf("iwork size = %d\n", 3 * minmn * nlvl + 11 * minmn);

    int[] iwork = new int[3 * minmn * nlvl + 11 * minmn];
    float[] s = new float[minmn];
    int[] rank = new int[1];
    int info =
        NativeBlas.sgelsd(m, n, nrhs, A.data, 0, m, B.data, 0, B.rows, s, 0, -1, rank, 0, iwork, 0);
    if (info == 0) {
      return;
    } else if (info < 0) {
      throw new LapackArgumentException("DGESD", -info);
    } else if (info > 0) {
      throw new LapackConvergenceException(
          "DGESD",
          info + " off-diagonal elements of an intermediat bidiagonal form did not converge to 0.");
    }
  }
Beispiel #2
0
 /** Compute c <- a*b + beta * c (general matrix matrix multiplication) */
 public static FloatMatrix gemm(
     float alpha, FloatMatrix a, FloatMatrix b, float beta, FloatMatrix c) {
   NativeBlas.sgemm(
       'N', 'N', c.rows, c.columns, a.columns, alpha, a.data, 0, a.rows, b.data, 0, b.rows, beta,
       c.data, 0, c.rows);
   return c;
 }
Beispiel #3
0
 /** Compute c <- a*b + beta * c (general matrix matrix multiplication) */
 public static DoubleMatrix gemm(
     double alpha, DoubleMatrix a, DoubleMatrix b, double beta, DoubleMatrix c) {
   NativeBlas.dgemm(
       'N', 'N', c.rows, c.columns, a.columns, alpha, a.data, 0, a.rows, b.data, 0, b.rows, beta,
       c.data, 0, c.rows);
   return c;
 }
Beispiel #4
0
  /** Compute y <- alpha*op(a)*x + beta * y (general matrix vector multiplication) */
  public static FloatMatrix gemv(
      float alpha, FloatMatrix a, FloatMatrix x, float beta, FloatMatrix y) {
    if (false) {
      NativeBlas.sgemv(
          'N', a.rows, a.columns, alpha, a.data, 0, a.rows, x.data, 0, 1, beta, y.data, 0, 1);
    } else {
      if (beta == 0.0f) {
        for (int i = 0; i < y.length; i++) y.data[i] = 0.0f;

        for (int j = 0; j < a.columns; j++) {
          float xj = x.get(j);
          if (xj != 0.0f) {
            for (int i = 0; i < a.rows; i++) y.data[i] += a.get(i, j) * xj;
          }
        }
      } else {
        for (int j = 0; j < a.columns; j++) {
          float byj = beta * y.data[j];
          float xj = x.get(j);
          for (int i = 0; i < a.rows; i++) y.data[j] = a.get(i, j) * xj + byj;
        }
      }
    }
    return y;
  }
Beispiel #5
0
  public static int syevr(
      char jobz,
      char range,
      char uplo,
      FloatMatrix a,
      float vl,
      float vu,
      int il,
      int iu,
      float abstol,
      FloatMatrix w,
      FloatMatrix z,
      int[] isuppz) {
    int n = a.rows;
    int[] m = new int[1];

    int info =
        NativeBlas.ssyevr(
            jobz, range, uplo, n, a.data, 0, a.rows, vl, vu, il, iu, abstol, m, 0, w.data, 0,
            z.data, 0, z.rows, isuppz, 0);

    checkInfo("SYEVR", info);

    return info;
  }
Beispiel #6
0
 public static void ormqr(char side, char trans, FloatMatrix A, FloatMatrix tau, FloatMatrix C) {
   int k = tau.length;
   int info =
       NativeBlas.sormqr(
           side, trans, C.rows, C.columns, k, A.data, 0, A.rows, tau.data, 0, C.data, 0, C.rows);
   checkInfo("ORMQR", info);
 }
Beispiel #7
0
  /** Compute y <- alpha*op(a)*x + beta * y (general matrix vector multiplication) */
  public static DoubleMatrix gemv(
      double alpha, DoubleMatrix a, DoubleMatrix x, double beta, DoubleMatrix y) {
    if (false) {
      NativeBlas.dgemv(
          'N', a.rows, a.columns, alpha, a.data, 0, a.rows, x.data, 0, 1, beta, y.data, 0, 1);
    } else {
      if (beta == 0.0) {
        for (int i = 0; i < y.length; i++) y.data[i] = 0.0;

        for (int j = 0; j < a.columns; j++) {
          double xj = x.get(j);
          if (xj != 0.0) {
            for (int i = 0; i < a.rows; i++) y.data[i] += a.get(i, j) * xj;
          }
        }
      } else {
        for (int j = 0; j < a.columns; j++) {
          double byj = beta * y.data[j];
          double xj = x.get(j);
          for (int i = 0; i < a.rows; i++) y.data[j] = a.get(i, j) * xj + byj;
        }
      }
    }
    return y;
  }
Beispiel #8
0
  public static int syevd(char jobz, char uplo, FloatMatrix A, FloatMatrix w) {
    int n = A.rows;

    int info = NativeBlas.ssyevd(jobz, uplo, n, A.data, 0, A.rows, w.data, 0);

    if (info > 0) throw new LapackConvergenceException("SYEVD", "Not all eigenvalues converged.");

    return info;
  }
Beispiel #9
0
 public static void posv(char uplo, FloatMatrix A, FloatMatrix B) {
   int n = A.rows;
   int nrhs = B.columns;
   int info = NativeBlas.sposv(uplo, n, nrhs, A.data, 0, A.rows, B.data, 0, B.rows);
   checkInfo("DPOSV", info);
   if (info > 0)
     throw new LapackArgumentException(
         "DPOSV", "Leading minor of order i of A is not positive definite.");
 }
Beispiel #10
0
  /** ************************************************************************* LAPACK */
  public static FloatMatrix gesv(FloatMatrix a, int[] ipiv, FloatMatrix b) {
    int info = NativeBlas.sgesv(a.rows, b.columns, a.data, 0, a.rows, ipiv, 0, b.data, 0, b.rows);
    checkInfo("DGESV", info);

    if (info > 0)
      throw new LapackException(
          "DGESV", "Linear equation cannot be solved because the matrix was singular.");

    return b;
  }
Beispiel #11
0
  public static int syev(char jobz, char uplo, FloatMatrix a, FloatMatrix w) {
    int info = NativeBlas.ssyev(jobz, uplo, a.rows, a.data, 0, a.rows, w.data, 0);

    if (info > 0)
      throw new LapackConvergenceException(
          "SYEV",
          "Eigenvalues could not be computed " + info + " off-diagonal elements did not converge");

    return info;
  }
Beispiel #12
0
 public static ComplexFloatMatrix gemm(
     ComplexFloat alpha,
     ComplexFloatMatrix a,
     ComplexFloatMatrix b,
     ComplexFloat beta,
     ComplexFloatMatrix c) {
   NativeBlas.cgemm(
       'N', 'N', c.rows, c.columns, a.columns, alpha, a.data, 0, a.rows, b.data, 0, b.rows, beta,
       c.data, 0, c.rows);
   return c;
 }
Beispiel #13
0
  public static FloatMatrix sysv(char uplo, FloatMatrix a, int[] ipiv, FloatMatrix b) {
    int info =
        NativeBlas.ssysv(uplo, a.rows, b.columns, a.data, 0, a.rows, ipiv, 0, b.data, 0, b.rows);
    checkInfo("SYSV", info);

    if (info > 0)
      throw new LapackSingularityException(
          "SYV", "Linear equation cannot be solved because the matrix was singular.");

    return b;
  }
Beispiel #14
0
 public static int geev(
     char jobvl,
     char jobvr,
     FloatMatrix A,
     FloatMatrix WR,
     FloatMatrix WI,
     FloatMatrix VL,
     FloatMatrix VR) {
   int info =
       NativeBlas.sgeev(
           jobvl, jobvr, A.rows, A.data, 0, A.rows, WR.data, 0, WI.data, 0, VL.data, 0, VL.rows,
           VR.data, 0, VR.rows);
   if (info > 0)
     throw new LapackConvergenceException(
         "DGEEV", "First " + info + " eigenvalues have not converged.");
   return info;
 }
Beispiel #15
0
 public static int sygvd(
     int itype, char jobz, char uplo, FloatMatrix A, FloatMatrix B, FloatMatrix W) {
   int info =
       NativeBlas.ssygvd(
           itype, jobz, uplo, A.rows, A.data, 0, A.rows, B.data, 0, B.rows, W.data, 0);
   if (info == 0) return 0;
   else {
     if (info < 0) throw new LapackArgumentException("DSYGVD", -info);
     if (info <= A.rows && jobz == 'N')
       throw new LapackConvergenceException(
           "DSYGVD", info + " off-diagonal elements did not converge to 0.");
     if (info <= A.rows && jobz == 'V')
       throw new LapackException(
           "DSYGVD",
           "Failed to compute an eigenvalue while working on a sub-matrix  " + info + ".");
     else
       throw new LapackException(
           "DSYGVD",
           "The leading minor of order " + (info - A.rows) + " of B is not positive definite.");
   }
 }
Beispiel #16
0
 public static int sygvx(
     int itype,
     char jobz,
     char range,
     char uplo,
     FloatMatrix A,
     FloatMatrix B,
     float vl,
     float vu,
     int il,
     int iu,
     float abstol,
     int[] m,
     FloatMatrix W,
     FloatMatrix Z) {
   int[] iwork = new int[1];
   int[] ifail = new int[1];
   int info =
       NativeBlas.ssygvx(
           itype, jobz, range, uplo, A.rows, A.data, 0, A.rows, B.data, 0, B.rows, vl, vu, il, iu,
           abstol, m, 0, W.data, 0, Z.data, 0, Z.rows, iwork, 0, ifail, 0);
   if (info == 0) {
     return 0;
   } else {
     if (info < 0) {
       throw new LapackArgumentException("DSYGVX", -info);
     }
     if (info <= A.rows) {
       throw new LapackConvergenceException("DSYGVX", info + " eigenvectors failed to converge");
     } else {
       throw new LapackException(
           "DSYGVX",
           "The leading minor order " + (info - A.rows) + " of B is not postivie definite.");
     }
   }
 }
Beispiel #17
0
  public static int syevx(
      char jobz,
      char range,
      char uplo,
      FloatMatrix a,
      float vl,
      float vu,
      int il,
      int iu,
      float abstol,
      FloatMatrix w,
      FloatMatrix z) {
    int n = a.rows;
    int[] iwork = new int[5 * n];
    int[] ifail = new int[n];
    int[] m = new int[1];
    int info;

    info =
        NativeBlas.ssyevx(
            jobz, range, uplo, n, a.data, 0, a.rows, vl, vu, il, iu, abstol, m, 0, w.data, 0,
            z.data, 0, z.rows, iwork, 0, ifail, 0);

    if (info > 0) {
      StringBuilder msg = new StringBuilder();
      msg.append("Not all eigenvalues converged. Non-converging eigenvalues were: ");
      for (int i = 0; i < info; i++) {
        if (i > 0) msg.append(", ");
        msg.append(ifail[i]);
      }
      msg.append(".");
      throw new LapackConvergenceException("SYEVX", msg.toString());
    }

    return info;
  }
Beispiel #18
0
 public static void geqrf(FloatMatrix A, FloatMatrix tau) {
   int info = NativeBlas.sgeqrf(A.rows, A.columns, A.data, 0, A.rows, tau.data, 0);
   checkInfo("GEQRF", info);
 }
Beispiel #19
0
 public static ComplexFloatMatrix axpy(
     ComplexFloat da, ComplexFloatMatrix dx, ComplexFloatMatrix dy) {
   NativeBlas.caxpy(dx.length, da, dx.data, 0, 1, dy.data, 0, 1);
   return dy;
 }
Beispiel #20
0
 public static ComplexDoubleMatrix copy(ComplexDoubleMatrix x, ComplexDoubleMatrix y) {
   NativeBlas.zcopy(x.length, x.data, 0, 1, y.data, 0, 1);
   return y;
 }
Beispiel #21
0
 /** Compute x <- alpha * x (scale a matrix) */
 public static DoubleMatrix scal(double alpha, DoubleMatrix x) {
   NativeBlas.dscal(x.length, alpha, x.data, 0, 1);
   return x;
 }
Beispiel #22
0
 /** Compute || x ||_2 (2-norm) */
 public static float nrm2(FloatMatrix x) {
   return NativeBlas.snrm2(x.length, x.data, 0, 1);
 }
Beispiel #23
0
 public static float asum(ComplexFloatMatrix x) {
   return NativeBlas.scasum(x.length, x.data, 0, 1);
 }
Beispiel #24
0
 public static ComplexDoubleMatrix scal(ComplexDouble alpha, ComplexDoubleMatrix x) {
   NativeBlas.zscal(x.length, alpha, x.data, 0, 1);
   return x;
 }
Beispiel #25
0
 /**
  * Compute index of element with largest absolute value (complex version).
  *
  * @param x matrix
  * @return index of element with largest absolute value.
  */
 public static int iamax(ComplexFloatMatrix x) {
   return NativeBlas.icamax(x.length, x.data, 0, 1) - 1;
 }
Beispiel #26
0
 /** Compute A <- alpha * x * y^T + A (general rank-1 update) */
 public static FloatMatrix ger(float alpha, FloatMatrix x, FloatMatrix y, FloatMatrix a) {
   NativeBlas.sger(a.rows, a.columns, alpha, x.data, 0, 1, y.data, 0, 1, a.data, 0, a.rows);
   return a;
 }
Beispiel #27
0
 public static ComplexFloatMatrix copy(ComplexFloatMatrix x, ComplexFloatMatrix y) {
   NativeBlas.ccopy(x.length, x.data, 0, 1, y.data, 0, 1);
   return y;
 }
Beispiel #28
0
 /** Compute x^T * y (dot product) */
 public static ComplexFloat dotu(ComplexFloatMatrix x, ComplexFloatMatrix y) {
   return NativeBlas.cdotu(x.length, x.data, 0, 1, y.data, 0, 1);
 }
Beispiel #29
0
 public static ComplexDoubleMatrix axpy(
     ComplexDouble da, ComplexDoubleMatrix dx, ComplexDoubleMatrix dy) {
   NativeBlas.zaxpy(dx.length, da, dx.data, 0, 1, dy.data, 0, 1);
   return dy;
 }
Beispiel #30
0
 /** Compute A <- alpha * x * y^H + A (general rank-1 update) */
 public static ComplexFloatMatrix gerc(
     ComplexFloat alpha, ComplexFloatMatrix x, ComplexFloatMatrix y, ComplexFloatMatrix a) {
   NativeBlas.cgerc(a.rows, a.columns, alpha, x.data, 0, 1, y.data, 0, 1, a.data, 0, a.rows);
   return a;
 }