Beispiel #1
0
  public void testMinMax() {

    TestUtils.assertEquals(Math.min(2, -78), FunctionUtils.min(2, -78));
    TestUtils.assertEquals(Math.max(2, -78), FunctionUtils.max(2, -78));

    TestUtils.assertEquals(67, FunctionUtils.max(new int[] {67}));
    TestUtils.assertEquals(67, FunctionUtils.min(new int[] {67}));

    TestUtils.assertEquals(FunctionUtils.max(67, -76), FunctionUtils.max(new int[] {67, -76}));
    TestUtils.assertEquals(FunctionUtils.min(67, -76), FunctionUtils.min(new int[] {67, -76}));

    TestUtils.assertEquals(
        FunctionUtils.max(0, 67, -76), FunctionUtils.max(new int[] {0, 67, -76}));
    TestUtils.assertEquals(
        FunctionUtils.min(0, 67, -76), FunctionUtils.min(new int[] {0, 67, -76}));

    TestUtils.assertEquals(
        FunctionUtils.max(0, 67, -76, 80), FunctionUtils.max(new int[] {0, 67, -76, 80}));
    TestUtils.assertEquals(
        FunctionUtils.min(0, 67, -76, -80), FunctionUtils.min(new int[] {0, 67, -76, -80}));

    TestUtils.assertEquals(
        FunctionUtils.max(80, 0, 67, -76), FunctionUtils.max(new int[] {80, 0, 67, -76}));
    TestUtils.assertEquals(
        FunctionUtils.min(-80, 0, 67, -76), FunctionUtils.min(new int[] {-80, 0, 67, -76}));

    TestUtils.assertEquals(80, FunctionUtils.max(new int[] {0, 67, -76, 80}));
    TestUtils.assertEquals(-80, FunctionUtils.min(new int[] {0, 67, -76, -80}));

    TestUtils.assertEquals(80, FunctionUtils.max(new int[] {80, 0, 67, -76}));
    TestUtils.assertEquals(-80, FunctionUtils.min(new int[] {-80, 0, 67, -76}));
  }
  private QuadraticSolver buildIterationSolver(final boolean addSmallDiagonal) {

    MatrixStore<Double> tmpQ = this.getQ();
    final MatrixStore<Double> tmpC = this.getC();

    if (addSmallDiagonal) {

      final PhysicalStore<Double> tmpCopyQ = tmpQ.copy();

      final double tmpLargest = tmpCopyQ.aggregateAll(Aggregator.LARGEST);
      final double tmpRelativelySmall = MACHINE_DOUBLE_ERROR * tmpLargest;
      final double tmpPracticalLimit = MACHINE_DOUBLE_ERROR + IS_ZERO;
      final double tmpSmallToAdd = Math.max(tmpRelativelySmall, tmpPracticalLimit);

      final UnaryFunction<Double> tmpFunc = ADD.second(tmpSmallToAdd);

      tmpCopyQ.modifyDiagonal(0, 0, tmpFunc);
      tmpQ = tmpCopyQ;
    }

    if (this.hasEqualityConstraints()) {

      final MatrixStore<Double> tmpAE = this.getAE();
      final MatrixStore<Double> tmpBE = this.getBE();

      final int tmpZeroSize = tmpAE.getRowDim();

      final MatrixStore<Double> tmpUpperLeftAE = tmpQ;
      final MatrixStore<Double> tmpUpperRightAE = tmpAE.builder().transpose().build();
      final MatrixStore<Double> tmpLowerLefAE = tmpAE;
      final MatrixStore<Double> tmpLowerRightAE = ZeroStore.makePrimitive(tmpZeroSize, tmpZeroSize);

      final MatrixStore<Double> tmpSubAE =
          new AboveBelowStore<Double>(
              new LeftRightStore<Double>(tmpUpperLeftAE, tmpUpperRightAE),
              new LeftRightStore<Double>(tmpLowerLefAE, tmpLowerRightAE));

      final MatrixStore<Double> tmpUpperBE = tmpC;
      final MatrixStore<Double> tmpLowerBE = tmpBE;

      final MatrixStore<Double> tmpSubBE = new AboveBelowStore<Double>(tmpUpperBE, tmpLowerBE);

      return new Builder().equalities(tmpSubAE, tmpSubBE).build(options);

    } else {

      return new Builder().equalities(tmpQ, tmpC).build(options);
    }
  }
  public static void tred2nr(
      final double[] z, final double[] d, final double[] e, final boolean yesvecs) {

    final int n = d.length;
    int l;
    final int tmpRowDim = n;

    double scale;
    double h;
    double hh;
    double g;
    double f;

    for (int i = n - 1; i > 0; i--) {

      l = i - 1;

      scale = PrimitiveMath.ZERO;
      h = PrimitiveMath.ZERO;

      if (l > 0) {

        for (int k = 0; k < i; k++) {
          scale += Math.abs(z[i + (k * tmpRowDim)]);
        }

        if (scale == PrimitiveMath.ZERO) {
          e[i] = z[i + (l * tmpRowDim)];
        } else {
          for (int k = 0; k < i; k++) {
            z[i + (k * tmpRowDim)] /= scale;
            h += z[i + (k * tmpRowDim)] * z[i + (k * tmpRowDim)];
          }
          f = z[i + (l * tmpRowDim)];
          g = (f >= PrimitiveMath.ZERO) ? -Math.sqrt(h) : Math.sqrt(h);
          e[i] = scale * g;
          h -= f * g;
          z[i + (l * tmpRowDim)] = f - g;
          f = PrimitiveMath.ZERO;
          for (int j = 0; j < i; j++) {
            if (yesvecs) {
              z[j + (i * tmpRowDim)] = z[i + (j * tmpRowDim)] / h;
            }
            g = PrimitiveMath.ZERO;
            for (int k = 0; k < (j + 1); k++) {
              g += z[j + (k * tmpRowDim)] * z[i + (k * tmpRowDim)];
            }
            for (int k = j + 1; k < i; k++) {
              g += z[k + (j * tmpRowDim)] * z[i + (k * tmpRowDim)];
            }
            e[j] = g / h;
            f += e[j] * z[i + (j * tmpRowDim)];
          }
          hh = f / (h + h);
          for (int j = 0; j < i; j++) {
            f = z[i + (j * tmpRowDim)];
            e[j] = g = e[j] - (hh * f);
            for (int k = 0; k < (j + 1); k++) {
              z[j + (k * tmpRowDim)] -= ((f * e[k]) + (g * z[i + (k * tmpRowDim)]));
            }
          }
        }
      } else {
        e[i] = z[i + (l * tmpRowDim)];
      }
      d[i] = h;
    }
    if (yesvecs) {
      d[0] = PrimitiveMath.ZERO;
    }
    e[0] = PrimitiveMath.ZERO;
    for (int i = 0; i < n; i++) {
      if (yesvecs) {
        if (d[i] != PrimitiveMath.ZERO) {
          for (int j = 0; j < i; j++) {
            g = PrimitiveMath.ZERO;
            for (int k = 0; k < i; k++) {
              g += z[i + (k * tmpRowDim)] * z[k + (j * tmpRowDim)];
            }
            for (int k = 0; k < i; k++) {
              z[k + (j * tmpRowDim)] -= g * z[k + (i * tmpRowDim)];
            }
          }
        }
        d[i] = z[i + (i * tmpRowDim)];
        z[i + (i * tmpRowDim)] = PrimitiveMath.ONE;
        for (int j = 0; j < i; j++) {
          z[i + (j * tmpRowDim)] = PrimitiveMath.ZERO;
          z[j + (i * tmpRowDim)] = PrimitiveMath.ZERO;
        }
      } else {
        d[i] = z[i + (i * tmpRowDim)];
      }
    }
  }
  public static void tred2j(
      final double[] z, final double[] d, final double[] e, final boolean yesvecs) {

    /*
    Symmetric Householder reduction to tridiagonal form.
    The original version of this code was taken from JAMA.
    That code is in turn derived from the Algol procedures tred2
    by Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
    Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
    Fortran subroutine in EISPACK.
    tred2 is also described in Numerical Recipes. Parameters and
    variables are names are choosen to match what is used there.

    z is the original matrix [A] that will be overwritten with [Q]
    d will hold the main diagonal of the tridiagonal result
    e will hold the off (super and sub) diagonals of the tridiagonal result
    */

    final int n = d.length;

    double scale;
    double h;
    double f;
    double g;
    double hh;

    final int tmpRowDim = n;
    final int tmpLast = n - 1;

    // Copy the last column (same as the last row) of z to d
    // The last row/column is the first to be worked on in the main loop
    for (int i = 0; i < n; i++) {
      d[i] = z[i + (tmpRowDim * tmpLast)];
    }

    // Householder reduction to tridiagonal form.
    for (int i = tmpLast; i > 0; i--) { // row index of target householder point

      final int l = i - 1; // col index of target householder point

      h = scale = PrimitiveMath.ZERO;

      // Calc the norm of the row/col to zero out
      for (int k = 0; k < i; k++) {
        scale += Math.abs(d[k]);
      }

      if (scale == PrimitiveMath.ZERO) {
        // Skip generation, already zero
        e[i] = d[l];
        for (int j = 0; j < i; j++) {
          d[j] = z[l + (tmpRowDim * j)];
          z[i + (tmpRowDim * j)] = PrimitiveMath.ZERO; // Are both needed?
          z[j + (tmpRowDim * i)] = PrimitiveMath.ZERO; // Could cause cache-misses
        }

      } else {
        // Generate Householder vector.

        for (int k = 0; k < i; k++) {
          d[k] /= scale;
          h += d[k] * d[k]; // can be optimised, too many array read/write ops
        }
        f = d[l];
        g = Math.sqrt(h);
        if (f > 0) {
          g = -g;
        }
        e[i] = scale * g;
        h -= f * g;
        d[l] = f - g;
        for (int j = 0; j < i; j++) {
          e[j] = PrimitiveMath.ZERO;
        }

        // Apply similarity transformation to remaining columns.
        // Remaing refers to all columns "before" the target col
        for (int j = 0; j < i; j++) {
          f = d[j];
          z[j + (tmpRowDim * i)] = f;
          g = e[j] + (z[j + (tmpRowDim * j)] * f);
          for (int k = j + 1; k <= l; k++) {
            g += z[k + (tmpRowDim * j)] * d[k]; // access the same element in z twice
            e[k] += z[k + (tmpRowDim * j)] * f;
          }
          e[j] = g;
        }
        f = PrimitiveMath.ZERO;
        for (int j = 0; j < i; j++) {
          e[j] /= h;
          f += e[j] * d[j];
        }
        hh = f / (h + h);
        for (int j = 0; j < i; j++) {
          e[j] -= hh * d[j];
        }
        for (int j = 0; j < i; j++) {
          f = d[j];
          g = e[j];
          for (int k = j; k <= l; k++) {
            z[k + (tmpRowDim * j)] -= ((f * e[k]) + (g * d[k]));
          }
          d[j] = z[l + (tmpRowDim * j)];
          z[i + (tmpRowDim * j)] = PrimitiveMath.ZERO;
        }
      }
      d[i] = h;
    }

    // Accumulate transformations.
    if (yesvecs) {

      for (int i = 0; i < tmpLast; i++) {

        final int l = i + 1;

        z[tmpLast + (tmpRowDim * i)] = z[i + (tmpRowDim * i)];
        z[i + (tmpRowDim * i)] = PrimitiveMath.ONE;
        h = d[l];
        if (h != PrimitiveMath.ZERO) {
          for (int k = 0; k <= i; k++) {
            d[k] = z[k + (tmpRowDim * l)] / h;
          }
          for (int j = 0; j <= i; j++) {
            g = PrimitiveMath.ZERO;
            for (int k = 0; k <= i; k++) {
              g += z[k + (tmpRowDim * l)] * z[k + (tmpRowDim * j)];
            }
            for (int k = 0; k <= i; k++) {
              z[k + (tmpRowDim * j)] -= g * d[k];
            }
          }
        }
        for (int k = 0; k <= i; k++) {
          z[k + (tmpRowDim * l)] = PrimitiveMath.ZERO;
        }
      }
      for (int j = 0; j < n; j++) {
        d[j] = z[tmpLast + (tmpRowDim * j)];
        z[tmpLast + (tmpRowDim * j)] = PrimitiveMath.ZERO;
      }
      z[tmpLast + (tmpRowDim * tmpLast)] = PrimitiveMath.ONE;

      e[0] = PrimitiveMath.ZERO;
    }
  }
Beispiel #5
0
 public double getProbability(final int aVal) {
   return RandomUtils.subsets(myCount, aVal)
       * Math.pow(myProbability, aVal)
       * Math.pow(ONE - myProbability, myCount - aVal);
 }