Search in sources :

Example 21 with NumberContext

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

the class LinearProblems method testMath286.

@Test
public void testMath286() {
    final Variable tmpX1 = new Variable("X1").weight(TENTH.multiply(EIGHT)).lower(TEN);
    final Variable tmpX2 = new Variable("X2").weight(TENTH.multiply(TWO)).lower(ZERO);
    final Variable tmpX3 = new Variable("X3").weight(TENTH.multiply(SEVEN)).lower(EIGHT);
    final Variable tmpX4 = new Variable("X4").weight(TENTH.multiply(THREE)).lower(ZERO);
    final Variable tmpX5 = new Variable("X5").weight(TENTH.multiply(SIX)).lower(FIVE);
    final Variable tmpX6 = new Variable("X6").weight(TENTH.multiply(FOUR)).lower(ZERO);
    final Variable[] tmpFullVars = new Variable[] { tmpX1.copy(), tmpX2.copy(), tmpX3.copy(), tmpX4.copy(), tmpX5.copy(), tmpX6.copy() };
    final Variable[] tmpOddVars = new Variable[] { tmpX1.copy(), tmpX3.copy(), tmpX5.copy() };
    final Variable[] tmpEvenVars = new Variable[] { tmpX2.copy(), tmpX4.copy(), tmpX6.copy() };
    final ExpressionsBasedModel tmpFullModel = new ExpressionsBasedModel(tmpFullVars);
    // tmpFullModel.setMaximisation();
    final ExpressionsBasedModel tmpOddModel = new ExpressionsBasedModel(tmpOddVars);
    // tmpOddModel.setMaximisation();
    final ExpressionsBasedModel tmpEvenModel = new ExpressionsBasedModel(tmpEvenVars);
    // tmpEvenModel.setMaximisation();
    // tmpFullModel.options.debug(LinearSolver.class);
    // tmpOddModel.options.debug(LinearSolver.class);
    // tmpEvenModel.options.debug(LinearSolver.class);
    final BigDecimal tmpRHS = new BigDecimal("23.0");
    final int tmpLength = tmpFullModel.countVariables();
    final Expression retVal = tmpFullModel.addExpression("C1");
    for (int i = 0; i < tmpLength; i++) {
        retVal.set(i, new BigDecimal[] { ONE, ZERO, ONE, ZERO, ONE, ZERO }[i]);
    }
    final Expression tmpAddWeightExpression = retVal;
    tmpAddWeightExpression.level(tmpRHS);
    final int tmpLength1 = tmpOddModel.countVariables();
    final Expression retVal1 = tmpOddModel.addExpression("C1");
    for (int i = 0; i < tmpLength1; i++) {
        retVal1.set(i, new BigDecimal[] { ONE, ONE, ONE }[i]);
    }
    final Expression tmpAddWeightExpression2 = retVal1;
    tmpAddWeightExpression2.level(tmpRHS);
    final int tmpLength2 = tmpFullModel.countVariables();
    final Expression retVal2 = tmpFullModel.addExpression("C2");
    for (int i = 0; i < tmpLength2; i++) {
        retVal2.set(i, new BigDecimal[] { ZERO, ONE, ZERO, ONE, ZERO, ONE }[i]);
    }
    final Expression tmpAddWeightExpression3 = retVal2;
    tmpAddWeightExpression3.level(tmpRHS);
    final int tmpLength3 = tmpEvenModel.countVariables();
    final Expression retVal3 = tmpEvenModel.addExpression("C2");
    for (int i = 0; i < tmpLength3; i++) {
        retVal3.set(i, new BigDecimal[] { ONE, ONE, ONE }[i]);
    }
    final Expression tmpAddWeightExpression4 = retVal3;
    tmpAddWeightExpression4.level(tmpRHS);
    final Expression tmpFullObjective = tmpFullModel.objective();
    final Expression tmpOddObjective = tmpOddModel.objective();
    final Expression tmpEvenObjective = tmpEvenModel.objective();
    // A valid solution of 25.8 can be produced with:
    // X1=10, X2=0, X3=8, X4=0, X5=5, X6=23
    final BigDecimal tmpClaimedValue = new BigDecimal("25.8");
    final Builder<PrimitiveMatrix> tmpBuilder = PrimitiveMatrix.FACTORY.getBuilder(6, 1);
    tmpBuilder.set(0, 0, 10);
    tmpBuilder.set(2, 0, 8);
    tmpBuilder.set(4, 0, 5);
    tmpBuilder.set(5, 0, 23);
    final BasicMatrix tmpFullSolution = tmpBuilder.build();
    final BasicMatrix tmpOddSolution = tmpFullSolution.selectRows(0, 2, 4);
    final BasicMatrix tmpEvenSolution = tmpFullSolution.selectRows(1, 3, 5);
    TestUtils.assertEquals("Claimed solution not valid!", true, tmpFullModel.validate(BigDenseStore.FACTORY.copy(tmpFullSolution), new NumberContext(7, 6)));
    final Double tmpActualValue = tmpFullObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(tmpFullSolution));
    // final BigDecimal tmpActualValue = TypeUtils.toBigDecimal(tmpObjectiveValue);
    // JUnitUtils.assertEquals("Claimed objective value wrong!", 0, tmpClaimedValue.compareTo(tmpActualValue));
    TestUtils.assertEquals(tmpClaimedValue, tmpActualValue, new NumberContext(7, 6));
    // Start validating ojAlgo results
    final Optimisation.Result tmpEvenResult = tmpEvenModel.maximise();
    final Optimisation.Result tmpOddResult = tmpOddModel.maximise();
    final Optimisation.Result tmpFullResult = tmpFullModel.maximise();
    TestUtils.assertEquals(true, tmpEvenModel.validate(tmpEvenResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(true, tmpOddModel.validate(tmpOddResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(true, tmpFullModel.validate(tmpFullResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(tmpEvenSolution, RationalMatrix.FACTORY.columns(tmpEvenResult).selectRows(0, 1, 2), new NumberContext(7, 6));
    TestUtils.assertEquals(tmpOddSolution, RationalMatrix.FACTORY.columns(tmpOddResult).selectRows(0, 1, 2), new NumberContext(7, 6));
    TestUtils.assertEquals(tmpFullSolution, RationalMatrix.FACTORY.columns(tmpFullResult).selectRows(0, 1, 2, 3, 4, 5), new NumberContext(7, 6));
    final BigDecimal tmpEvenValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpEvenObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpEvenResult).selectRows(0, 1, 2)))));
    final BigDecimal tmpOddValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpOddObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpOddResult).selectRows(0, 1, 2)))));
    final BigDecimal tmpFullValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpFullObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpFullResult).selectRows(0, 1, 2, 3, 4, 5)))));
    TestUtils.assertEquals(0, tmpFullValue.compareTo(tmpEvenValue.add(tmpOddValue)));
    TestUtils.assertEquals(0, tmpClaimedValue.compareTo(tmpFullValue));
}
Also used : Variable(org.ojalgo.optimisation.Variable) Optimisation(org.ojalgo.optimisation.Optimisation) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) BasicMatrix(org.ojalgo.matrix.BasicMatrix) Expression(org.ojalgo.optimisation.Expression) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 22 with NumberContext

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

the class DecompositionProblems method testP20091012fixed.

/**
 * Fat matrices were not QR-decomposed correctly ("R" was not created correctly).
 */
@Test
public void testP20091012fixed() {
    final PhysicalStore<Double> tmpA = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.5686711899234411, 5.857030526629094, 2.1798778832593637, 1.4901137152515287, 5.640993583029061 }, { 4.890945865607895, 4.2576454398997265, 1.0251822439318778, 0.8623492557631138, 5.7457253685285545 }, { 1.6397137349990025, 0.6795594856277076, 4.7101325736711095, 2.0823473021899517, 2.2159317240940233 } });
    final QR<Double> tmpQR = QR.PRIMITIVE.make(tmpA);
    tmpQR.decompose(tmpA);
    TestUtils.assertEquals(tmpA, tmpQR, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 23 with NumberContext

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

the class DecompositionProblems method testP20100512b.

@Test
public void testP20100512b() {
    final PhysicalStore<Double> tmpA = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.2845, 0.3597, 0.9544 }, { 0.3597, 0.6887, 0.0782 }, { 0.9544, 0.0782, 0.1140 } });
    final Eigenvalue<Double> tmpPrimitive = Eigenvalue.PRIMITIVE.make();
    tmpPrimitive.decompose(tmpA);
    TestUtils.assertEquals(tmpA, tmpPrimitive, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 24 with NumberContext

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

the class DecompositionProblems method testP20090923.

/**
 * http://en.wikipedia.org/wiki/Singular_value_decomposition There is no problem...
 */
@Test
public void testP20090923() {
    final PhysicalStore<Double> tmpA = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 0.0, 0.0, 0.0, 2.0 }, { 0.0, 0.0, 3.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 4.0, 0.0, 0.0, 0.0 } });
    final SingularValue<Double> tmpSVD = SingularValue.PRIMITIVE.make();
    tmpSVD.decompose(tmpA);
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("D", tmpSVD.getD(), new NumberContext(7, 6));
        BasicLogger.debug("Q1", tmpSVD.getQ1(), new NumberContext(7, 6));
        BasicLogger.debug("Q2", tmpSVD.getQ2(), new NumberContext(7, 6));
    }
    TestUtils.assertEquals(tmpA, tmpSVD, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 25 with NumberContext

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

the class DecompositionProblems method testP20111213square.

/**
 * A user reported problems solving complex valued (overdetermined) equation systemes.
 */
@Test
@Tag("unstable")
public void testP20111213square() {
    final int tmpDim = Uniform.randomInteger(2, 6);
    final PhysicalStore<ComplexNumber> tmpSquare = MatrixUtils.makeRandomComplexStore(tmpDim, tmpDim);
    final MatrixStore<ComplexNumber> tmpHermitian = tmpSquare.conjugate().multiply(tmpSquare);
    final PhysicalStore<ComplexNumber> tmpExpected = ComplexDenseStore.FACTORY.makeEye(tmpDim, tmpDim);
    MatrixStore<ComplexNumber> tmpActual;
    @SuppressWarnings("unchecked") final MatrixDecomposition<ComplexNumber>[] tmpCmplxDecomps = new MatrixDecomposition[] { Bidiagonal.COMPLEX.make(), Cholesky.COMPLEX.make(), Eigenvalue.COMPLEX.make(MatrixDecomposition.TYPICAL, true), /*
                              * , HessenbergDecomposition. makeComplex()
                              */
    LU.COMPLEX.make(), QR.COMPLEX.make(), SingularValue.COMPLEX.make() /*
                                              * , TridiagonalDecomposition . makeComplex ( )
                                              */
    };
    for (final MatrixDecomposition<ComplexNumber> tmpDecomposition : tmpCmplxDecomps) {
        tmpDecomposition.decompose(tmpHermitian);
        if (MatrixDecompositionTests.DEBUG) {
            BasicLogger.debug(tmpDecomposition.toString());
            BasicLogger.debug("Original", tmpHermitian);
            BasicLogger.debug("Recretaed", tmpDecomposition.reconstruct());
        }
        TestUtils.assertEquals("Recreation: " + tmpDecomposition.toString(), tmpHermitian, tmpDecomposition.reconstruct(), new NumberContext(8, 5));
        if ((tmpDecomposition instanceof MatrixDecomposition.Solver<?>) && ((Solver) tmpDecomposition).isSolvable()) {
            tmpActual = ((Solver) tmpDecomposition).getSolution(tmpHermitian);
            if (MatrixDecompositionTests.DEBUG) {
                BasicLogger.debug("Actual", tmpActual);
            }
            TestUtils.assertEquals("Solving: " + tmpDecomposition.toString(), tmpExpected, tmpActual, new NumberContext(7, 6));
        }
    }
}
Also used : Solver(org.ojalgo.matrix.decomposition.MatrixDecomposition.Solver) NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

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