Пример #1
0
  public void testMatrixMultiply() throws Throwable {
    double a[][] = {
      {1, 0, 2},
      {-1, 3, 1}
    };

    double b[][] = {
      {3, 1},
      {2, 1},
      {1, 0}
    };

    double c[][] = {
      {5, 1},
      {4, 2}
    };

    Matrix matrixA = new Matrix(a);
    Matrix matrixB = new Matrix(b);
    Matrix matrixC = new Matrix(c);

    Matrix result = matrixA.clone();
    result = MatrixMath.multiply(matrixA, matrixB);

    TestCase.assertTrue(result.equals(matrixC));

    double a2[][] = {
      {1, 2, 3, 4},
      {5, 6, 7, 8}
    };

    double b2[][] = {
      {1, 2, 3},
      {4, 5, 6},
      {7, 8, 9},
      {10, 11, 12}
    };

    double c2[][] = {
      {70, 80, 90},
      {158, 184, 210}
    };

    matrixA = new Matrix(a2);
    matrixB = new Matrix(b2);
    matrixC = new Matrix(c2);

    result = MatrixMath.multiply(matrixA, matrixB);
    TestCase.assertTrue(result.equals(matrixC));

    result = matrixB.clone();
    try {
      MatrixMath.multiply(matrixB, matrixA);
      TestCase.assertTrue(false);
    } catch (MatrixError e) {

    }
  }
Пример #2
0
  public void testVectorLength() throws Throwable {
    double vectorData[] = {1.0, 2.0, 3.0, 4.0};
    Matrix vector = Matrix.createRowMatrix(vectorData);
    TestCase.assertEquals(5, (int) MatrixMath.vectorLength(vector));

    Matrix nonVector = new Matrix(2, 2);
    try {
      MatrixMath.vectorLength(nonVector);
      TestCase.assertTrue(false);
    } catch (MatrixError e) {

    }
  }