Search in sources :

Example 71 with NumberContext

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

the class HessenbergTest method doTestCorrect.

private void doTestCorrect(final PhysicalStore<Double> aMatrix) {
    final HessenbergDecomposition<Double> tmpDecomposition = (HessenbergDecomposition<Double>) Hessenberg.PRIMITIVE.make();
    tmpDecomposition.decompose(aMatrix);
    if (!Hessenberg.equals(aMatrix, tmpDecomposition, new NumberContext(7, 6))) {
        this.doPrint(tmpDecomposition, aMatrix);
        TestUtils.fail("Not equals!");
    }
    final MatrixStore<Double> tmpReconstructed = Hessenberg.reconstruct(tmpDecomposition);
    if (!Access2D.equals(aMatrix, tmpReconstructed, new NumberContext(7, 6))) {
        this.doPrint(tmpDecomposition, aMatrix);
        TestUtils.fail("Failed to reconstruct!");
    }
    if (!Access2D.equals(tmpDecomposition.getQ(), tmpDecomposition.doQ(this.makeEye((int) aMatrix.countRows(), (int) Math.min(aMatrix.countRows(), aMatrix.countColumns()))), new NumberContext(7, 6))) {
        this.doPrint(tmpDecomposition, aMatrix);
        TestUtils.fail("get and do Q are different!");
    }
    TestUtils.assertEquals(aMatrix, tmpDecomposition, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext)

Example 72 with NumberContext

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

the class LUTest method testP20061119Case.

@Test
public void testP20061119Case() {
    final RationalMatrix tmpProblematic = P20061119Case.getProblematic();
    final LU<BigDecimal> tmpBig = LU.BIG.make();
    tmpBig.decompose(BigDenseStore.FACTORY.copy(tmpProblematic));
    final LU<ComplexNumber> tmpComplex = LU.COMPLEX.make();
    tmpComplex.decompose(ComplexDenseStore.FACTORY.copy(tmpProblematic));
    final LU<Double> tmpPrimitive = LU.PRIMITIVE.make();
    tmpPrimitive.decompose(PrimitiveDenseStore.FACTORY.copy(tmpProblematic));
    final LU<Double> tmpJama = new RawLU();
    tmpJama.decompose(PrimitiveDenseStore.FACTORY.copy(tmpProblematic));
    final NumberContext tmpPrintContext = NumberContext.getGeneral(20);
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Big L", tmpBig.getL(), tmpPrintContext);
        BasicLogger.debug("Complex L", tmpComplex.getL(), tmpPrintContext);
        BasicLogger.debug("Primitive L", tmpPrimitive.getL(), tmpPrintContext);
        BasicLogger.debug("Jama L", tmpJama.getL(), tmpPrintContext);
    }
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Big U", tmpBig.getU(), tmpPrintContext);
        BasicLogger.debug("Complex U", tmpComplex.getU(), tmpPrintContext);
        BasicLogger.debug("Primitive U", tmpPrimitive.getU(), tmpPrintContext);
        BasicLogger.debug("Jama U", tmpJama.getU(), tmpPrintContext);
    }
    final SingularValue<Double> tmpSVD = new RawSingularValue();
    tmpSVD.decompose(PrimitiveDenseStore.FACTORY.copy(tmpProblematic));
    TestUtils.assertEquals("LU.rank SVD vs Big", tmpSVD.getRank(), tmpBig.getRank());
    TestUtils.assertEquals("LU.rank SVD vs Complex", tmpSVD.getRank(), tmpComplex.getRank());
    TestUtils.assertEquals("LU.rank SVD vs Primitive", tmpSVD.getRank(), tmpPrimitive.getRank());
    TestUtils.assertEquals("LU.rank SVD vs Jama", tmpSVD.getRank(), tmpJama.getRank());
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) RationalMatrix(org.ojalgo.matrix.RationalMatrix) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 73 with NumberContext

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

the class MarketShareCase method testRedundant.

private void testRedundant(final String constraint) {
    final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
    final Expression tmpExpression = tmpModel.getExpression(constraint);
    if (DEBUG) {
        BasicLogger.debug("Fix count: {}", tmpExpression.getLinearKeySet().size());
    }
    for (final IntIndex tmpIndex : tmpExpression.getLinearKeySet()) {
        final Variable tmpVariable = tmpModel.getVariable(tmpIndex.index);
        final String tmpName = tmpVariable.getName();
        tmpVariable.level(SOLUTION.get(tmpName));
    }
    final Result tmpResult = tmpModel.minimise();
    final NumberContext tmpContext = new NumberContext(8, 13);
    TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), tmpContext);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), tmpContext);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) IntIndex(org.ojalgo.access.Structure1D.IntIndex) Result(org.ojalgo.optimisation.Optimisation.Result)

Example 74 with NumberContext

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

the class QRTest method testP20030422Case.

@Test
public void testP20030422Case() {
    final RationalMatrix tmpOriginal = P20030422Case.getProblematic();
    final QR<BigDecimal> tmpBigDecomp = QR.BIG.make();
    final QR<ComplexNumber> tmpComplexDecomp = QR.COMPLEX.make();
    final QR<Double> tmpPrimitiveDecomp = QR.PRIMITIVE.make();
    tmpBigDecomp.decompose(BigDenseStore.FACTORY.copy(tmpOriginal));
    tmpComplexDecomp.decompose(ComplexDenseStore.FACTORY.copy(tmpOriginal));
    tmpPrimitiveDecomp.decompose(PrimitiveDenseStore.FACTORY.copy(tmpOriginal));
    final MatrixStore<BigDecimal> tmpBigQ = tmpBigDecomp.getQ();
    final MatrixStore<ComplexNumber> tmpComplexQ = tmpComplexDecomp.getQ();
    final MatrixStore<Double> tmpPrimitiveQ = tmpPrimitiveDecomp.getQ();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Big Q", tmpBigQ);
        BasicLogger.debug("Complex Q", tmpComplexQ);
        BasicLogger.debug("Primitive Q", tmpPrimitiveQ);
    }
    final MatrixStore<BigDecimal> tmpBigR = tmpBigDecomp.getR();
    final MatrixStore<ComplexNumber> tmpComplexR = tmpComplexDecomp.getR();
    final MatrixStore<Double> tmpPrimitiveR = tmpPrimitiveDecomp.getR();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Big R", tmpBigR);
        BasicLogger.debug("Complex R", tmpComplexR);
        BasicLogger.debug("Primitive R", tmpPrimitiveR);
    }
    TestUtils.assertEquals(BigDenseStore.FACTORY.copy(tmpOriginal), tmpBigDecomp, new NumberContext(7, 14));
    TestUtils.assertEquals(ComplexDenseStore.FACTORY.copy(tmpOriginal), tmpComplexDecomp, new NumberContext(7, 14));
    TestUtils.assertEquals(PrimitiveDenseStore.FACTORY.copy(tmpOriginal), tmpPrimitiveDecomp, new NumberContext(7, 14));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) RationalMatrix(org.ojalgo.matrix.RationalMatrix) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 75 with NumberContext

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

the class SVDbyEvD method doTest.

private void doTest(final PhysicalStore<Double> matrixA, final Array1D<Double> singularValues) {
    final MatrixStore<Double> tmpTranspA = matrixA.transpose();
    final MatrixStore<Double> tmpLeftA = matrixA.multiply(tmpTranspA);
    final MatrixStore<Double> tmpRightA = tmpTranspA.multiply(matrixA);
    final Eigenvalue<Double> tmpEigenvalue = Eigenvalue.PRIMITIVE.make(true);
    tmpEigenvalue.decompose(tmpLeftA);
    final MatrixStore<Double> tmpLeftD = tmpEigenvalue.getD();
    final MatrixStore<Double> tmpLeftV = tmpEigenvalue.getV();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Left D", tmpLeftD, new NumberContext(7, 6));
        BasicLogger.debug("Left V", tmpLeftV, new NumberContext(7, 6));
    }
    // Check that the eigenvalue decomposition of the "left" matrix is correct
    TestUtils.assertEquals(tmpLeftA, tmpEigenvalue, new NumberContext(7, 6));
    tmpEigenvalue.decompose(tmpRightA);
    final MatrixStore<Double> tmpRightD = tmpEigenvalue.getD();
    final MatrixStore<Double> tmpRightV = tmpEigenvalue.getV();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Right D", tmpRightD, new NumberContext(7, 6));
        BasicLogger.debug("Right V", tmpRightV, new NumberContext(7, 6));
    }
    // Check that the eigenvalue decomposition of the "right" matrix is correct
    TestUtils.assertEquals(tmpRightA, tmpEigenvalue, new NumberContext(7, 6));
    // Check that the, left and right, singular values are correct
    for (int ij = 0; ij < singularValues.length; ij++) {
        final double tmpExpected = singularValues.doubleValue(ij);
        final double tmpLeftSqrt = PrimitiveFunction.SQRT.invoke(PrimitiveFunction.ABS.invoke(tmpLeftD.doubleValue(ij, ij)));
        final double tmpRightSqrt = PrimitiveFunction.SQRT.invoke(PrimitiveFunction.ABS.invoke(tmpRightD.doubleValue(ij, ij)));
        TestUtils.assertEquals("Left " + ij, tmpExpected, tmpLeftSqrt, new NumberContext(7, 6));
        TestUtils.assertEquals("Right " + ij, tmpExpected, tmpRightSqrt, new NumberContext(7, 6));
    }
    // So far...
    final SingularValue<Double> tmpExperimental = SingularValue.PRIMITIVE.make();
    tmpExperimental.decompose(matrixA);
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug();
        BasicLogger.debug("Experimental  S: {}.", tmpExperimental.getSingularValues());
        BasicLogger.debug("D", tmpExperimental.getD(), new NumberContext(7, 6));
        BasicLogger.debug("Q1", tmpExperimental.getQ1(), new NumberContext(7, 6));
        BasicLogger.debug("Q2", tmpExperimental.getQ2(), new NumberContext(7, 6));
    }
    TestUtils.assertEquals(matrixA, tmpExperimental, new NumberContext(7, 6));
}
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