Package org.jamesii.core.math

Examples of org.jamesii.core.math.Matrix


   *          the covariance matrix of the assets
   * @return the mean and variance
   */
  public static Pair<Double, Double> calcMeanAndVariance(double[] weights,
      Double[] avgPerf, Double[][] covMat) {
    Matrix w = new Matrix(weights);
    Matrix s = new Matrix(covMat);
    Matrix r = new Matrix(avgPerf);
    return new Pair<>(r.transpose().mult(w).getElement(0, 0), w
        .transpose().mult(s.mult(w)).getElement(0, 0));
  }
View Full Code Here


public class TestMatrix extends TestCase {

  /** Tests the {@link Matrix#Matrix(int)} constructor. */
  public void testIntConstructor() {
    // should create a square identity matrix
    Matrix m;

    for (int i = 1; i < 30; i++) {
      m = new Matrix(i);

      assertEquals(i, m.getRows());
      assertEquals(i, m.getColumns());

      for (int c = 0; c < m.getColumns(); c++) {
        for (int r = 0; r < m.getRows(); r++) {
          assertEquals(r == c ? 1.0 : 0.0, m.getElement(r, c));
        }
      }
    }
  }
View Full Code Here

    }
  }

  /** Tests the {@link Matrix#Matrix(int, int)} constructor. */
  public void testIntIntConstructor() {
    Matrix m;

    for (int i = 1; i < 30; i++) {
      for (int j = 1; j < 30; j++) {
        m = new Matrix(i, j);

        assertEquals(i, m.getRows());
        assertEquals(j, m.getColumns());

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

  public void testDoubleArrayConstructor() {
    double[][] data = new double[][] { { 1., 2., 3. }, { 4., 5., 6. } };
    Double[][] dData =
        new Double[][] { { data[0][0], data[0][1], data[0][2] },
            { data[1][0], data[1][1], data[1][2] } };
    checkConstructedDoubleArrayMatrix(data, new Matrix(data));
    checkConstructedDoubleArrayMatrix(data, new Matrix(dData));
  }
View Full Code Here

  /** Tests the {@link Matrix#Matrix(double[])} constructor. */
  public void testOneDimDoubleArrayConstructor() {
    double[] data = new double[] { 1., 2., 3. };
    Double[] dData = new Double[] { data[0], data[1], data[2] };
    checkConstructedOneDimDoubleArrayMatrix(data, new Matrix(data));
    checkConstructedOneDimDoubleArrayMatrix(data, new Matrix(dData));
  }
View Full Code Here

  public void testAdd() {
    double[][] data1 = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
    double[][] data2 = new double[][] { { 8, 7, 6 }, { -4, 23, -8 } };
    double[][] results = new double[][] { { 9, 9, 9 }, { 0, 28, -2 } };

    Matrix m = new Matrix(data1);
    m.add(new Matrix(data2));
    for (int c = 0; c < m.getColumns(); c++) {
      for (int r = 0; r < m.getRows(); r++) {
        assertEquals(results[r][c], m.getElement(r, c));
      }
    }
  }
View Full Code Here

   */
  public void testAddExceptions() {
    double[][] data1 = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
    double[][] data2 = new double[][] { { 8, 7, 6 }, { 4, 3, 8 }, { 1, 2, 3 } };

    Matrix m1 = new Matrix(data1);
    Matrix m2 = new Matrix(data2);

    try {
      m1.add(m2);
    } catch (MatrixException e) {
      assertTrue(true);
View Full Code Here

  public void testAddCols() {
    double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
    double[][] results1 = new double[][] { { 3, 2, 3 }, { 9, 5, 6 } };
    double[][] results2 = new double[][] { { 3, 8, 3 }, { 9, 17, 6 } };

    Matrix m = new Matrix(data);
    m.addCols(0, 1);
    for (int c = 0; c < m.getColumns(); c++) {
      for (int r = 0; r < m.getRows(); r++) {
        assertEquals(results1[r][c], m.getElement(r, c));
      }
    }

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

   * exceptions when encountering invalid arguments.
   */
  public void testAddColsExceptions() {
    // TODO: Should the exception being thrown here really be an
    // ArrayIndexOutOfBoundsException?
    Matrix m = new Matrix(3);

    // test addCols(int, int)

    try {
      m.addCols(1, 3);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    try {
      m.addCols(5, 2);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    try {
      m.addCols(-1, 1);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    try {
      m.addCols(1, -3);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    // test addCols(int, int, double)

    try {
      m.addCols(1, 3, 1);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    try {
      m.addCols(5, 2, 2);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    try {
      m.addCols(-1, 1, 3);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
    }

    try {
      m.addCols(1, -3, 4);
      fail();
    } catch (ArrayIndexOutOfBoundsException e) {
      assertTrue(true);
    } catch (Throwable e) {
      fail();
View Full Code Here

    double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    double[][] res1 = new double[][] { { 5, 7, 9 }, { 4, 5, 6 }, { 7, 8, 9 } };
    double[][] res2 =
        new double[][] { { 5, 7, 9 }, { 18, 21, 24 }, { 7, 8, 9 } };

    Matrix m = new Matrix(data);
    m.addRows(0, 1);
    for (int c = 0; c < m.getColumns(); c++) {
      for (int r = 0; r < m.getRows(); r++) {
        assertEquals(res1[r][c], m.getElement(r, c));
      }
    }

    m.addRows(1, 2, 2);
    for (int c = 0; c < m.getColumns(); c++) {
      for (int r = 0; r < m.getRows(); r++) {
        assertEquals(res2[r][c], m.getElement(r, c));
      }
    }

  }
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.