Package de.jungblut.math.dense

Examples of de.jungblut.math.dense.DenseDoubleMatrix


   */
  private DoubleMatrix generateDistanceMatrix(DistanceMeasurer measurer,
      List<DoubleVector> pointList) {

    final int n = pointList.size();
    DenseDoubleMatrix matrix = new DenseDoubleMatrix(n, n);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        final double distance = measurer.measureDistance(pointList.get(i),
            pointList.get(j));
        matrix.set(i, j, distance);
      }
    }

    return matrix;
  }
View Full Code Here


   * @return the normalized matrix (0 mean and stddev of 1) as well as the mean
   *         and the stddev.
   */
  public static Tuple3<DoubleMatrix, DoubleVector, DoubleVector> meanNormalizeColumns(
      DoubleMatrix x) {
    DenseDoubleMatrix toReturn = new DenseDoubleMatrix(x.getRowCount(),
        x.getColumnCount());
    final int length = x.getColumnCount();
    DoubleVector meanVector = new DenseDoubleVector(length);
    DoubleVector stddevVector = new DenseDoubleVector(length);
    for (int col = 0; col < length; col++) {
      DoubleVector column = x.getColumnVector(col);
      double mean = column.sum() / column.getLength();
      meanVector.set(col, mean);
      double var = column.subtract(mean).pow(2).sum() / column.getLength();
      stddevVector.set(col, Math.sqrt(var));
    }

    for (int col = 0; col < length; col++) {
      DoubleVector column = x.getColumnVector(col)
          .subtract(meanVector.get(col)).divide(stddevVector.get(col));
      toReturn.setColumn(col, column.toArray());
    }

    return new Tuple3<>(toReturn, meanVector, stddevVector);
  }
View Full Code Here

   */
  public static DenseDoubleMatrix createPolynomials(DenseDoubleMatrix seed,
      int num) {
    if (num == 1)
      return seed;
    DenseDoubleMatrix m = new DenseDoubleMatrix(seed.getRowCount(),
        seed.getColumnCount() * num);
    int index = 0;
    for (int c = 0; c < m.getColumnCount(); c += num) {
      double[] column = seed.getColumn(index++);
      m.setColumn(c, column);
      for (int i = 2; i < num + 1; i++) {
        DoubleVector pow = new DenseDoubleVector(column).pow(i);
        m.setColumn(c + i - 1, pow.toArray());
      }
    }
    return m;
  }
View Full Code Here

  /**
   * @return a log'd matrix that was guarded against edge cases of the
   *         logarithm.
   */
  public static DoubleMatrix logMatrix(DoubleMatrix input) {
    DenseDoubleMatrix log = new DenseDoubleMatrix(input.getRowCount(),
        input.getColumnCount());
    for (int row = 0; row < log.getRowCount(); row++) {
      for (int col = 0; col < log.getColumnCount(); col++) {
        double d = input.get(row, col);
        log.set(row, col, guardLogarithm(d));
      }
    }
    return log;
  }
View Full Code Here

   * @param toMax the upper bound of the target interval.
   * @return the new matrix with scaled values.
   */
  public static DoubleMatrix minMaxScale(DoubleMatrix input, double fromMin,
      double fromMax, double toMin, double toMax) {
    DoubleMatrix newOne = new DenseDoubleMatrix(input.getRowCount(),
        input.getColumnCount());
    double[][] array = input.toArray();
    for (int row = 0; row < newOne.getRowCount(); row++) {
      for (int col = 0; col < newOne.getColumnCount(); col++) {
        newOne.set(row, col,
            minMaxScale(array[row][col], fromMin, fromMax, toMin, toMax));
      }
    }
    return newOne;
  }
View Full Code Here

        { 3.768227750561635, -1.861672501324946 },
        { -1.7606548237507884, 2.5544868606627005 },
        { -0.9148771784733722, -1.9820382601667268 },
        { 3.936150254656125, 1.2253565233931112 } };

    assertEquals(0, thetaMat.subtract(new DenseDoubleMatrix(result)).sum(),
        1e-4);

  }
View Full Code Here

public class MathUtilsTest {

  @Test
  public void testMeanNormalizeRows() {
    DoubleMatrix mat = new DenseDoubleMatrix(new double[][] { { 2, 5 },
        { 5, 1 }, { 7, 25 } });
    Tuple<DoubleMatrix, DoubleVector> normal = MathUtils.meanNormalizeRows(mat);
    DoubleVector mean = normal.getSecond();
    assertSmallDiff(mean, new DenseDoubleVector(new double[] { 3.5d, 3d, 16d }));
    DoubleMatrix meanNormalizedMatrix = normal.getFirst();
    DoubleMatrix matNormal = new DenseDoubleMatrix(new double[][] {
        { -1.5, 1.5 }, { 2, -2 }, { -9, 9 } });
    for (int i = 0; i < 3; i++) {
      assertSmallDiff(meanNormalizedMatrix.getRowVector(i),
          matNormal.getRowVector(i));
    }
  }
View Full Code Here

    DoubleVector res = new DenseDoubleVector(new double[] { 0.4, 0.6 });

    DoubleVector minMaxScale = MathUtils.minMaxScale(in, 0, 100, 0, 1);
    assertSmallDiff(res, minMaxScale);

    DoubleMatrix mat = new DenseDoubleMatrix(new double[][] { { 40, 60 },
        { 2, 25 } });
    DoubleMatrix resMat = new DenseDoubleMatrix(new double[][] { { 0.4, 0.6 },
        { 0.02, 0.25 } });

    DoubleMatrix minMaxScaleMat = MathUtils.minMaxScale(mat, 0, 100, 0, 1);
    assertSmallDiff(resMat, minMaxScaleMat);
View Full Code Here

  }

  @Test
  public void testLogMatrix() {
    DoubleMatrix y = new DenseDoubleMatrix(
        new double[][] { { 0d, 1d, 0.5d, Double.NaN, 0.2d,
            Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY } });
    DoubleMatrix mat = MathUtils.logMatrix(y);
    assertEquals(-10d, mat.get(0, 0), 1e-4);
    assertEquals(0d, mat.get(0, 1), 1e-4);
View Full Code Here

    assertEquals(0d, mat.get(0, 6), 1e-4);
  }

  @Test
  public void testFeatureNormalize() {
    DoubleMatrix mat = new DenseDoubleMatrix(new double[][] { { 2, 5 },
        { 5, 1 }, { 7, 25 } });
    Tuple3<DoubleMatrix, DoubleVector, DoubleVector> normal = MathUtils
        .meanNormalizeColumns(mat);
    DoubleVector mean = normal.getSecond();
    assertSmallDiff(mean, new DenseDoubleVector(new double[] { 14d / 3d,
        31d / 3d }));
    DoubleVector stddev = normal.getThird();
    assertSmallDiff(stddev, new DenseDoubleVector(new double[] { 2.0548046,
        10.498677 }));
    DoubleMatrix meanNormalizedMatrix = normal.getFirst();
    DoubleMatrix matNormal = new DenseDoubleMatrix(new double[][] {
        { -1.2977713, -0.508 }, { 0.162221421, -0.889 }, { 1.135549, 1.397 } });

    for (int i = 0; i < 3; i++) {
      assertSmallDiff(meanNormalizedMatrix.getRowVector(i),
          matNormal.getRowVector(i));
    }
  }
View Full Code Here

TOP

Related Classes of de.jungblut.math.dense.DenseDoubleMatrix

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.