示例#1
0
 /**
  * @param A
  * @param B
  * @return
  */
 public static Matrix mmul(final Matrix A, final Matrix B) {
   try {
     if (A.n != B.m) throw new Matrix.WrongDimensionsException();
   } catch (Matrix.WrongDimensionsException e) {
     e.printStackTrace();
     System.exit(1);
   }
   float[] result = new float[A.m * B.n];
   Level3.sgemm(
       BlasOrder.ROW_MAJOR,
       BlasTrans.NO_TRANS,
       BlasTrans.NO_TRANS,
       A.m,
       B.n,
       A.n,
       1f,
       A.getData(),
       A.ld,
       B.getData(),
       B.ld,
       1f,
       result,
       B.ld);
   return new Matrix(result, A.m, B.n);
 }
示例#2
0
 /**
  * @param A
  * @param B
  * @return
  */
 public static Matrix vstack(Matrix A, Matrix B) {
   int aLen = A.m * A.n;
   int bLen = B.m * B.n;
   float[] C = new float[aLen + bLen];
   System.arraycopy(A.getData(), 0, C, 0, aLen);
   System.arraycopy(B.getData(), 0, C, aLen, bLen);
   return new Matrix(C, A.m + B.m, Math.max(A.n, B.n));
 }
示例#3
0
 /**
  * @param A
  * @param B
  * @return
  */
 public static Matrix add(final Matrix A, final Matrix B) {
   try {
     if (A.m == B.m && A.n == B.n) {
       throw new Matrix.WrongDimensionsException();
     }
   } catch (Matrix.WrongDimensionsException e) {
   }
   float[] cData = add(A.getData(), B.getData());
   return new Matrix(cData, A.m, A.n);
 }
示例#4
0
 /** Matrix Ops */
 public static Matrix transpose(final Matrix M) {
   if (M.n == 1 || M.m == 1) {
     return new Matrix(M.getData(), M.n, M.m);
   } else {
     try {
       return new Matrix(transpose(M.getData(), M.ld), M.n, M.m);
     } catch (Exception e) {
       e.printStackTrace();
       return M;
     }
   }
 }
示例#5
0
  /**
   * @param A
   * @param B
   * @return
   */
  public static float dot(Matrix A, Matrix B) {
    int aLen = A.m * A.n;
    int bLen = B.m * B.n;
    try {
      if (bLen != aLen) throw new Matrix.WrongDimensionsException();
    } catch (Matrix.WrongDimensionsException e) {
      e.printStackTrace();
      System.exit(1);
    }
    float[] dotP = new float[1];

    Level1.sdot(aLen, dotP, A.getData(), B.getData());
    return dotP[0];
  }
示例#6
0
 /**
  * @param M
  * @return
  */
 public static Matrix tanh(Matrix M) {
   float[] a = M.getData();
   for (int i = 0; i < a.length; i++) {
     a[i] = (float) Math.tanh(a[i]);
   }
   return new Matrix(a, M.m, M.n);
 }
示例#7
0
 /**
  * @param A
  * @param B
  * @return
  */
 public static Matrix hstack(Matrix A, Matrix B) {
   float[] C = new float[A.getData().length + B.getData().length];
   return transpose(vstack(transpose(A), transpose(B)));
 }
示例#8
0
 /**
  * @param M
  * @param alpha
  * @return
  */
 public static Matrix scale(Matrix M, float alpha) {
   float[] scaled = M.getData().clone();
   Level1.sscal(scaled.length, alpha, scaled);
   return new Matrix(scaled, M.m, M.n);
 }