Exemplo n.º 1
0
  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);
    }
  }