Search in sources :

Example 11 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class BidiagonalTest method doTestCorrect.

private void doTestCorrect(final PhysicalStore<Double> aMatrix) {
    final BidiagonalDecomposition<Double> tmpDecomposition = (BidiagonalDecomposition<Double>) Bidiagonal.PRIMITIVE.make();
    tmpDecomposition.decompose(aMatrix);
    if (!Bidiagonal.equals(aMatrix, tmpDecomposition, new NumberContext(7, 6))) {
        this.doPrint(tmpDecomposition, aMatrix);
        TestUtils.fail("Not equals, easy!");
    }
    if (!Bidiagonal.equals(aMatrix, tmpDecomposition, new NumberContext(7, 6))) {
        this.doPrint(tmpDecomposition, aMatrix);
        TestUtils.fail("Not equals, hard!");
    }
    final MatrixStore<Double> tmpReconstructed = Bidiagonal.reconstruct(tmpDecomposition);
    if (!Access2D.equals(aMatrix, tmpReconstructed, new NumberContext(7, 6))) {
        this.doPrint(tmpDecomposition, aMatrix);
        TestUtils.fail("Failed to reconstruct!");
    }
    TestUtils.assertEquals(aMatrix, tmpDecomposition, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext)

Example 12 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class StandardType method percent.

public static NumberContext percent(final Locale locale) {
    final NumberContext retVal = NumberContext.getPercent(locale);
    retVal.format(new BigDecimal("0.5000"));
    return retVal;
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) BigDecimal(java.math.BigDecimal)

Example 13 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class ApproximationCase method testFirstOrderApproximation.

@Test
public void testFirstOrderApproximation() {
    final int tmpArity = 9;
    final PhysicalStore<Double> tmpLinear = PrimitiveDenseStore.FACTORY.makeFilled(tmpArity, 1, new Uniform(-1, 2));
    final PhysicalStore<Double> tmpPoint = PrimitiveDenseStore.FACTORY.makeFilled(tmpArity, 1, new Uniform(-10, 20));
    final LinearFunction<Double> tmpOrgFunc = LinearFunction.makePrimitive(tmpLinear);
    final FirstOrderApproximation<Double> tmpApprFunc = tmpOrgFunc.toFirstOrderApproximation(tmpPoint);
    final PhysicalStore<Double> tmpX = PrimitiveDenseStore.FACTORY.makeFilled(tmpArity, 1, new Uniform(-10, 20));
    TestUtils.assertEquals(tmpOrgFunc.invoke(tmpX), tmpApprFunc.invoke(tmpX), new NumberContext(7, 14));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Uniform(org.ojalgo.random.Uniform) Test(org.junit.jupiter.api.Test)

Example 14 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class ConjugateGradientSolver method resolve.

public double resolve(final List<Equation> equations, final PhysicalStore<Double> solution) {
    final int tmpCountRows = equations.size();
    double tmpNormErr = POSITIVE_INFINITY;
    double tmpNormRHS = ONE;
    final PrimitiveDenseStore tmpResidual = this.residual(solution);
    final PrimitiveDenseStore tmpDirection = this.direction(solution);
    final PrimitiveDenseStore tmpPreconditioned = this.preconditioned(solution);
    final PrimitiveDenseStore tmpVector = this.vector(solution);
    double tmpStepLength;
    double tmpGradientCorrectionFactor;
    double zr0 = 1;
    double zr1 = 1;
    double pAp0 = 0;
    for (int r = 0; r < tmpCountRows; r++) {
        final Equation tmpRow = equations.get(r);
        double tmpVal = tmpRow.getRHS();
        tmpNormRHS = PrimitiveFunction.HYPOT.invoke(tmpNormRHS, tmpVal);
        tmpVal -= tmpRow.dot(solution);
        tmpResidual.set(tmpRow.index, tmpVal);
        // precondition
        tmpPreconditioned.set(tmpRow.index, tmpVal / tmpRow.getPivot());
    }
    // tmpPreconditioned.supplyNonZerosTo(tmpDirection);
    tmpDirection.fillMatching(tmpPreconditioned);
    int tmpIterations = 0;
    final int tmpLimit = this.getIterationsLimit();
    final NumberContext tmpCntxt = this.getAccuracyContext();
    // zr1 = tmpPreconditioned.transpose().multiply(tmpResidual).doubleValue(0L);
    zr1 = tmpPreconditioned.dot(tmpResidual);
    do {
        zr0 = zr1;
        for (int i = 0; i < tmpCountRows; i++) {
            final Equation tmpRow = equations.get(i);
            final double tmpVal = tmpRow.dot(tmpDirection);
            tmpVector.set(tmpRow.index, tmpVal);
        }
        // pAp0 = tmpVector.multiplyLeft(tmpDirection.transpose()).get().doubleValue(0L);
        pAp0 = tmpDirection.dot(tmpVector);
        tmpStepLength = zr0 / pAp0;
        if (!Double.isNaN(tmpStepLength)) {
            // solution.maxpy(tmpStepLength, tmpDirection);
            tmpDirection.axpy(tmpStepLength, solution);
            // tmpResidual.maxpy(-tmpStepLength, tmpVector);
            tmpVector.axpy(-tmpStepLength, tmpResidual);
        }
        tmpNormErr = ZERO;
        for (int r = 0; r < tmpCountRows; r++) {
            final Equation tmpRow = equations.get(r);
            final double tmpValue = tmpResidual.doubleValue(tmpRow.index);
            tmpNormErr = PrimitiveFunction.HYPOT.invoke(tmpNormErr, tmpValue);
            tmpPreconditioned.set(tmpRow.index, tmpValue / tmpRow.getPivot());
        }
        zr1 = tmpPreconditioned.dot(tmpResidual);
        tmpGradientCorrectionFactor = zr1 / zr0;
        tmpDirection.modifyAll(PrimitiveFunction.MULTIPLY.second(tmpGradientCorrectionFactor));
        tmpDirection.modifyMatching(PrimitiveFunction.ADD, tmpPreconditioned);
        tmpIterations++;
        if (this.isDebugPrinterSet()) {
            this.debug(tmpIterations, solution);
        }
    } while ((tmpIterations < tmpLimit) && !Double.isNaN(tmpNormErr) && !tmpCntxt.isSmall(tmpNormRHS, tmpNormErr));
    return tmpNormErr / tmpNormRHS;
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 15 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class GaussSeidelSolver method resolve.

public double resolve(final List<Equation> equations, final PhysicalStore<Double> solution) {
    double tmpNormErr = POSITIVE_INFINITY;
    double tmpNormRHS = ZERO;
    final int tmpCountRows = equations.size();
    for (int r = 0; r < tmpCountRows; r++) {
        tmpNormRHS = PrimitiveFunction.HYPOT.invoke(tmpNormRHS, equations.get(r).getRHS());
    }
    int tmpIterations = 0;
    final int tmpLimit = this.getIterationsLimit();
    final NumberContext tmpCntxt = this.getAccuracyContext();
    final double tmpRelaxationFactor = this.getRelaxationFactor();
    do {
        tmpNormErr = ZERO;
        for (int r = 0; r < tmpCountRows; r++) {
            tmpNormErr = PrimitiveFunction.HYPOT.invoke(tmpNormErr, equations.get(r).adjust(solution, tmpRelaxationFactor));
        }
        tmpIterations++;
        if (this.isDebugPrinterSet()) {
            this.debug(tmpIterations, solution);
        }
    } while ((tmpIterations < tmpLimit) && !tmpCntxt.isSmall(tmpNormRHS, tmpNormErr));
    return tmpNormErr / tmpNormRHS;
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext)

Aggregations

NumberContext (org.ojalgo.type.context.NumberContext)91 Test (org.junit.jupiter.api.Test)63 ComplexNumber (org.ojalgo.scalar.ComplexNumber)16 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)15 BigDecimal (java.math.BigDecimal)14 BasicMatrix (org.ojalgo.matrix.BasicMatrix)13 BeforeEach (org.junit.jupiter.api.BeforeEach)12 Result (org.ojalgo.optimisation.Optimisation.Result)12 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)9 Expression (org.ojalgo.optimisation.Expression)8 Variable (org.ojalgo.optimisation.Variable)8 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)6 Optimisation (org.ojalgo.optimisation.Optimisation)6 Uniform (org.ojalgo.random.Uniform)4 BigArray (org.ojalgo.array.BigArray)3 SimultaneousPrimitive (org.ojalgo.matrix.decomposition.HermitianEvD.SimultaneousPrimitive)3 MatrixStore (org.ojalgo.matrix.store.MatrixStore)3 Tag (org.junit.jupiter.api.Tag)2 RationalMatrix (org.ojalgo.matrix.RationalMatrix)2 Solver (org.ojalgo.matrix.decomposition.MatrixDecomposition.Solver)2