Search in sources :

Example 46 with NumberContext

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

the class TridiagonalizeCase method doTheTest.

private void doTheTest(final MatrixStore<Double> aMtrxA, final MatrixStore<Double> aMtrxD) {
    final Tridiagonal<Double> tmpDecomp = Tridiagonal.PRIMITIVE.make();
    // final Tridiagonal<Double> tmpDecomp = new TridiagonalAltDecomp();
    tmpDecomp.decompose(aMtrxA);
    TestUtils.assertEquals(aMtrxD, tmpDecomp.getD(), new NumberContext(7, 6));
    TestUtils.assertEquals(aMtrxA, tmpDecomp, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext)

Example 47 with NumberContext

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

the class RandomNumberTest method testERF.

/**
 * Tests that the error function implementation returns correct confidence intervals for +/- 6 standard
 * deviations. They are all correct to at least 10 decimal places.
 */
@Test
public void testERF() {
    double tmpStdDevCount;
    double tmpConfidence;
    double tmpError = new NumberContext(7, 12).epsilon();
    tmpStdDevCount = ZERO;
    tmpConfidence = ZERO;
    TestUtils.assertEquals(tmpConfidence, RandomUtils.erf(tmpStdDevCount / SQRT_TWO), tmpError);
    TestUtils.assertEquals(-tmpConfidence, RandomUtils.erf(-tmpStdDevCount / SQRT_TWO), tmpError);
    tmpStdDevCount = ONE;
    tmpConfidence = 0.682689492137;
    TestUtils.assertEquals(tmpConfidence, RandomUtils.erf(tmpStdDevCount / SQRT_TWO), tmpError);
    TestUtils.assertEquals(-tmpConfidence, RandomUtils.erf(-tmpStdDevCount / SQRT_TWO), tmpError);
    tmpStdDevCount = TWO;
    tmpConfidence = 0.954499736104;
    TestUtils.assertEquals(tmpConfidence, RandomUtils.erf(tmpStdDevCount / SQRT_TWO), tmpError);
    TestUtils.assertEquals(-tmpConfidence, RandomUtils.erf(-tmpStdDevCount / SQRT_TWO), tmpError);
    tmpStdDevCount = THREE;
    tmpConfidence = 0.997300203937;
    TestUtils.assertEquals(tmpConfidence, RandomUtils.erf(tmpStdDevCount / SQRT_TWO), tmpError);
    TestUtils.assertEquals(-tmpConfidence, RandomUtils.erf(-tmpStdDevCount / SQRT_TWO), tmpError);
    tmpStdDevCount = FOUR;
    tmpConfidence = 0.999936657516;
    TestUtils.assertEquals(tmpConfidence, RandomUtils.erf(tmpStdDevCount / SQRT_TWO), tmpError);
    TestUtils.assertEquals(-tmpConfidence, RandomUtils.erf(-tmpStdDevCount / SQRT_TWO), tmpError);
    tmpStdDevCount = FIVE;
    tmpConfidence = 0.999999426697;
    TestUtils.assertEquals(tmpConfidence, RandomUtils.erf(tmpStdDevCount / SQRT_TWO), tmpError);
    TestUtils.assertEquals(-tmpConfidence, RandomUtils.erf(-tmpStdDevCount / SQRT_TWO), tmpError);
    tmpError = new NumberContext(7, 8).epsilon();
    tmpStdDevCount = SIX;
    tmpConfidence = 0.999999998027;
    TestUtils.assertEquals(tmpConfidence, RandomUtils.erf(tmpStdDevCount / SQRT_TWO), tmpError);
    TestUtils.assertEquals(-tmpConfidence, RandomUtils.erf(-tmpStdDevCount / SQRT_TWO), tmpError);
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 48 with NumberContext

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

the class RandomNumberTest method testERFI.

@Test
public void testERFI() {
    double tmpConfidenceLevel;
    double tmpExpected;
    final NumberContext tmpNewScale = new NumberContext(2, 5);
    tmpConfidenceLevel = 0.80;
    tmpExpected = 1.28155;
    TestUtils.assertEquals(tmpExpected, SQRT_TWO * RandomUtils.erfi(tmpConfidenceLevel), tmpNewScale);
    tmpConfidenceLevel = 0.90;
    tmpExpected = 1.64485;
    TestUtils.assertEquals(tmpExpected, SQRT_TWO * RandomUtils.erfi(tmpConfidenceLevel), tmpNewScale);
    tmpConfidenceLevel = 0.95;
    tmpExpected = 1.95996;
    TestUtils.assertEquals(tmpExpected, SQRT_TWO * RandomUtils.erfi(tmpConfidenceLevel), tmpNewScale);
    tmpConfidenceLevel = 0.98;
    tmpExpected = 2.32635;
    TestUtils.assertEquals(tmpExpected, SQRT_TWO * RandomUtils.erfi(tmpConfidenceLevel), tmpNewScale);
    tmpConfidenceLevel = 0.99;
    tmpExpected = 2.57583;
    TestUtils.assertEquals(tmpExpected, SQRT_TWO * RandomUtils.erfi(tmpConfidenceLevel), tmpNewScale);
    tmpConfidenceLevel = 0.995;
    tmpExpected = 2.80703;
    TestUtils.assertEquals(tmpExpected, SQRT_TWO * RandomUtils.erfi(tmpConfidenceLevel), tmpNewScale);
    tmpConfidenceLevel = 0.998;
    tmpExpected = 3.09023;
    TestUtils.assertEquals(tmpExpected, SQRT_TWO * RandomUtils.erfi(tmpConfidenceLevel), tmpNewScale);
    tmpConfidenceLevel = 0.999;
    tmpExpected = 3.29052;
    TestUtils.assertEquals(tmpExpected, SQRT_TWO * RandomUtils.erfi(tmpConfidenceLevel), tmpNewScale);
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 49 with NumberContext

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

the class RandomNumberTest method testGammaFunction.

@Test
public void testGammaFunction() {
    final double tmpEps = 0.000005;
    // From a table of values 1.0 <= x <= 2.0
    TestUtils.assertEquals(ONE, RandomUtils.gamma(1.0), 1E-14 / THREE);
    TestUtils.assertEquals(0.95135, RandomUtils.gamma(1.10), tmpEps);
    TestUtils.assertEquals(0.91817, RandomUtils.gamma(1.20), tmpEps);
    TestUtils.assertEquals(0.89747, RandomUtils.gamma(1.30), tmpEps);
    TestUtils.assertEquals(0.88726, RandomUtils.gamma(1.40), tmpEps);
    TestUtils.assertEquals(0.88623, RandomUtils.gamma(1.50), tmpEps);
    TestUtils.assertEquals(0.89352, RandomUtils.gamma(1.60), tmpEps);
    TestUtils.assertEquals(0.90864, RandomUtils.gamma(1.70), tmpEps);
    TestUtils.assertEquals(0.93138, RandomUtils.gamma(1.80), tmpEps);
    TestUtils.assertEquals(0.96177, RandomUtils.gamma(1.90), tmpEps);
    TestUtils.assertEquals(ONE, RandomUtils.gamma(2.0), 1E-14 / THREE);
    // Values larger than 2.0 and smaller than 1.0
    TestUtils.assertEquals("π", RandomUtils.gamma(PI), (PI - ONE) * (PI - TWO) * RandomUtils.gamma(PI - TWO), 1E-14 / THREE);
    TestUtils.assertEquals("0.5", RandomUtils.gamma(HALF), RandomUtils.gamma(HALF + ONE) / HALF, 1E-14 / THREE);
    TestUtils.assertEquals("0.25", RandomUtils.gamma(QUARTER), RandomUtils.gamma(QUARTER + ONE) / QUARTER, 1E-14 / THREE);
    TestUtils.assertEquals("0.1", RandomUtils.gamma(TENTH), RandomUtils.gamma(TENTH + ONE) / TENTH, tmpEps);
    TestUtils.assertEquals("0.01", RandomUtils.gamma(HUNDREDTH), RandomUtils.gamma(HUNDREDTH + ONE) / HUNDREDTH, tmpEps);
    TestUtils.assertEquals("0.001", RandomUtils.gamma(THOUSANDTH), RandomUtils.gamma(THOUSANDTH + ONE) / THOUSANDTH, tmpEps);
    // Should align with n! for positve integers
    for (int n = 0; n < 10; n++) {
        TestUtils.assertEquals("n!:" + n, RandomUtils.factorial(n), RandomUtils.gamma(n + ONE), tmpEps);
    }
    // Negative values
    TestUtils.assertEquals("-0.5", RandomUtils.gamma(-0.5), RandomUtils.gamma(HALF) / (-0.5), tmpEps);
    TestUtils.assertEquals("-1.5", RandomUtils.gamma(-1.5), RandomUtils.gamma(HALF) / (-1.5 * -0.5), tmpEps);
    TestUtils.assertEquals("-2.5", RandomUtils.gamma(-2.5), RandomUtils.gamma(HALF) / (-2.5 * -1.5 * -0.5), tmpEps);
    TestUtils.assertEquals("-3.5", RandomUtils.gamma(-3.5), RandomUtils.gamma(HALF) / (-3.5 * -2.5 * -1.5 * -0.5), tmpEps);
    TestUtils.assertEquals("-4.5", RandomUtils.gamma(-4.5), RandomUtils.gamma(HALF) / (-4.5 * -3.5 * -2.5 * -1.5 * -0.5), tmpEps);
    // Should be undefined for 0, -1, -2, -3...
    for (int n = 0; n < 10; n++) {
        TestUtils.assertTrue("-" + n, Double.isNaN(RandomUtils.gamma(NEG * n)));
    }
    final NumberContext tmpEval = new NumberContext(10, 10);
    // Positive half integer
    for (int n = 0; n < 10; n++) {
        TestUtils.assertEquals(n + ".5", (SQRT_PI * RandomUtils.factorial(2 * n)) / (PrimitiveFunction.POW.invoke(FOUR, n) * RandomUtils.factorial(n)), RandomUtils.gamma(n + HALF), tmpEval);
    }
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 50 with NumberContext

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

the class ExpressionsBasedModel method validate.

public boolean validate(final Access1D<BigDecimal> solution) {
    final NumberContext context = options.feasibility;
    final Printer appender = (options.logger_detailed && (options.logger_appender != null)) ? options.logger_appender : BasicLogger.NULL;
    return this.validate(solution, context, appender);
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Printer(org.ojalgo.netio.BasicLogger.Printer)

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