@Test
  public void basicOpsTest() {
    /** TRANSPOSE */
    double[][] basicArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
    Matrix m = new Matrix(basicArray);
    Matrix t = m.transpose();
    double[][] tArray = {{1, 4, 7, 10}, {2, 5, 8, 11}, {3, 6, 9, 12}};
    assertTrue(t.equals(new Matrix(tArray)));

    double[][] oneElArr = {{4}};
    Matrix oneEl = new Matrix(oneElArr);
    assertTrue(oneEl.transpose().equals(oneEl));

    /** MULTIPLICATION */
    Matrix scaledThree = m.multiply(3);
    double[][] scaledThreeArr = {{3, 6, 9}, {12, 15, 18}, {21, 24, 27}, {30, 33, 36}};
    assertTrue(scaledThree.equals(new Matrix(scaledThreeArr)));

    double[][] oneCol = {{1}, {1}, {1}};
    Matrix prod = m.multiply(new Matrix(oneCol));
    double[][] oneColProd = {{6}, {15}, {24}, {33}};
    assertTrue(prod.equals(new Matrix(oneColProd)));

    /** TOSTRING */
    assertEquals(
        m.toString(),
        "[[ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ], [ 10.0, 11.0, 12.0 ] ]");
  }
예제 #2
0
  @Test
  public void multiplyTwoMatrix() {
    int row = 2, column = 2;
    int[] firstArrayInput = {1, 2, 3, 4};
    int[] secondArrayInput = {2, 3, 4, 5};
    int[] expectedOutput = {11, 16, 19, 28};
    Matrix firstMatrix = new Matrix(row, column);
    Matrix secondMatrix = new Matrix(row, column);
    Matrix expectedMatrix = new Matrix(row, column);

    firstMatrix.populate(firstArrayInput);
    secondMatrix.populate(secondArrayInput);
    expectedMatrix.populate(expectedOutput);
    Matrix destinationMatrix = firstMatrix.multiply(secondMatrix);

    assertArrayEquals(destinationMatrix.matrix, expectedMatrix.matrix);
  }