Package org.jamesii.core.math

Examples of org.jamesii.core.math.Matrix


    }
  }

  /** Tests the {@link Matrix#isSquare()} method. */
  public void testIsSquare() {
    assertTrue(new Matrix(20).isSquare());
    assertTrue(new Matrix(1, 1).isSquare());
    assertTrue(new Matrix(5, 5).isSquare());
    assertTrue(new Matrix(8).isSquare());

    assertFalse(new Matrix(1, 2).isSquare());
    assertFalse(new Matrix(15, 14).isSquare());

    double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
    assertFalse(new Matrix(data).isSquare());

    data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 8, 7, 6 } };
    assertTrue(new Matrix(data).isSquare());
  }
View Full Code Here


  /** Tests the {@link Matrix#multMat(double)} method. */
  public void testMultMat() {
    double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
    double[][] result = new double[][] { { 1.5, 3, 4.5 }, { 6, 7.5, 9 } };

    Matrix m = new Matrix(data);
    m.multMat(1.5);

    for (int c = 0; c < m.getColumns(); c++) {
      for (int r = 0; r < m.getRows(); r++) {
        assertEquals(result[r][c], m.getElement(r, c));
      }
    }
  }
View Full Code Here

    }
  }

  /** Tests the {@link Matrix#setDiagonal(double)} method. */
  public void testSetDiagonal() {
    Matrix m;

    for (int i = 1; i < 30; i++) {
      m = new Matrix(i, i);
      m.setDiagonal(42.42);
      for (int c = 0; c < m.getColumns(); c++) {
        for (int r = 0; r < m.getRows(); r++) {
          assertEquals(r == c ? 42.42 : 0.0, m.getElement(r, c));
        }
      }
    }

    // TODO: test non-square matrices here
View Full Code Here

    // TODO: test non-square matrices here
  }

  /** Tests the {@link Matrix#sub(Matrix)} method. */
  public void testSub() {
    Matrix m1 =
        new Matrix(new double[][] { { 7, 2, 6 }, { 2, 8, 2 }, { 2, 3, 1 } });
    Matrix m2 =
        new Matrix(new double[][] { { 2, 3, 5 }, { 1, -4, 2 }, { -2, 1, 7 } });
    double[][] result =
        new double[][] { { 5, -1, 1 }, { 1, 12, 0 }, { 4, 2, -6 } };

    m1.sub(m2);

View Full Code Here

   * Tests whether the {@link Matrix#sub(Matrix)} method throws the correct
   * exceptions when encountering invalid arguments.
   */
  public void testSubExceptions() {
    try {
      new Matrix(1, 2).sub(new Matrix(2));
      fail();
    } catch (MatrixException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    try {
      new Matrix(2).sub(new Matrix(2, 3));
      fail();
    } catch (MatrixException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
View Full Code Here

  public void testSwapRows() {
    double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    double[][] result =
        new double[][] { { 7, 8, 9 }, { 4, 5, 6 }, { 1, 2, 3 } };

    Matrix m = new Matrix(data);
    m.swapRows(0, 2);

    for (int c = 0; c < m.getColumns(); c++) {
      for (int r = 0; r < m.getRows(); r++) {
        assertEquals(result[r][c], m.getElement(r, c));
      }
    }
  }
View Full Code Here

   */
  public void testSwapRowsExceptions() {
    // TODO: should this really be ArrayIndexOutOfBoundsExceptions here?

    try {
      new Matrix(5).swapRows(-1, 2);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    try {
      new Matrix(5).swapRows(3, -2);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
View Full Code Here

  }

  /** Tests the {@link Matrix#transpose()} method. */
  public void testTranspose() {
    double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
    Matrix m1 = new Matrix(data);

    Matrix m2 = m1.transpose();

    for (int c = 0; c < m2.getColumns(); c++) {
      for (int r = 0; r < m2.getRows(); r++) {
        assertEquals(data[c][r], m2.getElement(r, c));
      }
    }
  }
View Full Code Here

      throw new RuntimeException(
          "Input and response variables must be set before regression can be calculated!");
    }
    if (getB() == null) {
      // Calculate coefficients.
      Matrix xT = getX().transpose();
      setB(xT.mult(getX()).solve());
      // Save this result for the calculation of the standard error for each
      // coefficient.
      setStandardErrorMatrix(getB().copy());
      setB(getB().mult(xT).mult(getY()));
    }
View Full Code Here

  }

  @Override
  public void setInputVariables(List<List<Double>> input) {
    reset();
    setX(new Matrix(input.get(0).size(), input.size()));
    for (int row = 0; row < getX().getRows(); row++) {
      for (int col = 0; col < getX().getColumns(); col++) {
        double val = input.get(col).get(row);
        getX().setElement(row, col, val);
      }
View Full Code Here

TOP

Related Classes of org.jamesii.core.math.Matrix

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.