예제 #1
0
  public static void jacobianPrint(
      FunctionNtoM func,
      FunctionNtoMxN jacobian,
      double param[],
      double tol,
      double differenceScale) {
    NumericalJacobianForward numerical = new NumericalJacobianForward(func, differenceScale);

    DenseMatrix64F found = new DenseMatrix64F(func.getNumOfOutputsM(), func.getNumOfInputsN());
    DenseMatrix64F expected = new DenseMatrix64F(func.getNumOfOutputsM(), func.getNumOfInputsN());

    jacobian.process(param, found.data);
    numerical.process(param, expected.data);

    System.out.println("FOUND:");
    found.print();
    System.out.println("-----------------------------");
    System.out.println("Numerical");
    expected.print();

    System.out.println("-----------------------------");
    System.out.println("Large Differences");
    for (int y = 0; y < found.numRows; y++) {
      for (int x = 0; x < found.numCols; x++) {
        double diff = Math.abs(found.get(y, x) - expected.get(y, x));
        if (diff > tol) {
          //					double e = expected.get(y,x);
          //					double f = found.get(y,x);
          System.out.print("1");
        } else System.out.print("0");
      }
      System.out.println();
    }
  }
  /**
   * Multiplies the two sub-matrices together. Checks to see if the same result is found when
   * multiplied using the normal algorithm versus the submatrix one.
   */
  private static void checkMult_submatrix(
      Method func,
      int operationType,
      boolean transA,
      boolean transB,
      D1Submatrix64F A,
      D1Submatrix64F B) {
    if (A.col0 % BLOCK_LENGTH != 0 || A.row0 % BLOCK_LENGTH != 0)
      throw new IllegalArgumentException("Submatrix A is not block aligned");
    if (B.col0 % BLOCK_LENGTH != 0 || B.row0 % BLOCK_LENGTH != 0)
      throw new IllegalArgumentException("Submatrix B is not block aligned");

    BlockMatrix64F origA = BlockMatrixOps.createRandom(numRows, numCols, -1, 1, rand, BLOCK_LENGTH);
    BlockMatrix64F origB = BlockMatrixOps.createRandom(numCols, numRows, -1, 1, rand, BLOCK_LENGTH);

    A.original = origA;
    B.original = origB;
    int w = B.col1 - B.col0;
    int h = A.row1 - A.row0;

    // offset it to make the test harder
    // randomize to see if its set or adding
    BlockMatrix64F subC =
        BlockMatrixOps.createRandom(BLOCK_LENGTH + h, BLOCK_LENGTH + w, -1, 1, rand, BLOCK_LENGTH);
    D1Submatrix64F C =
        new D1Submatrix64F(subC, BLOCK_LENGTH, subC.numRows, BLOCK_LENGTH, subC.numCols);

    DenseMatrix64F rmC = multByExtract(operationType, A, B, C);

    if (transA) {
      origA = BlockMatrixOps.transpose(origA, null);
      transposeSub(A);
      A.original = origA;
    }

    if (transB) {
      origB = BlockMatrixOps.transpose(origB, null);
      transposeSub(B);
      B.original = origB;
    }

    try {
      func.invoke(null, BLOCK_LENGTH, A, B, C);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
      throw new RuntimeException(e);
    }

    for (int i = C.row0; i < C.row1; i++) {
      for (int j = C.col0; j < C.col1; j++) {
        //                System.out.println(i+" "+j);
        double diff = Math.abs(subC.get(i, j) - rmC.get(i - C.row0, j - C.col0));
        //                System.out.println(subC.get(i,j)+" "+rmC.get(i-C.row0,j-C.col0));
        if (diff >= 1e-12) {
          subC.print();
          rmC.print();
          System.out.println(func.getName());
          System.out.println("transA    " + transA);
          System.out.println("transB    " + transB);
          System.out.println("type      " + operationType);
          fail("Error too large");
        }
      }
    }
  }