Search in sources :

Example 6 with Uniform

use of org.ojalgo.random.Uniform in project ojAlgo by optimatika.

the class DesignCase method testSolveIdentity.

@Test
public void testSolveIdentity() {
    final Access2D<?> tmpIdentity = MatrixStore.PRIMITIVE.makeIdentity(9).get();
    final Access2D<?> tmpRandom = PrimitiveDenseStore.FACTORY.makeFilled(9, 1, new Uniform());
    final List<MatrixDecomposition<Double>> tmpAllDecomps = MatrixDecompositionTests.getAllPrimitive();
    for (final MatrixDecomposition<Double> tmpDecomp : tmpAllDecomps) {
        if (tmpDecomp instanceof SolverTask) {
            final SolverTask<Double> tmpSolverTask = (SolverTask<Double>) tmpDecomp;
            try {
                TestUtils.assertEquals(tmpDecomp.getClass().toString(), tmpRandom, tmpSolverTask.solve(tmpIdentity, tmpRandom));
            } catch (final RecoverableCondition xcptn) {
                TestUtils.fail(tmpDecomp.getClass().toString() + " " + xcptn.getMessage());
            }
        }
    }
}
Also used : RecoverableCondition(org.ojalgo.RecoverableCondition) SolverTask(org.ojalgo.matrix.task.SolverTask) Uniform(org.ojalgo.random.Uniform) Test(org.junit.jupiter.api.Test)

Example 7 with Uniform

use of org.ojalgo.random.Uniform in project ojAlgo by optimatika.

the class ExtremeElementsCase method doTestSolve.

static void doTestSolve(final boolean large) {
    for (int precision = 1; precision <= 16; precision++) {
        final NumberContext tmpContext = NumberContext.getGeneral(precision, Integer.MIN_VALUE);
        for (int dim = 1; dim <= 10; dim++) {
            // exp = 308 could potentially create numbers that are 2E308 which is larger than Double.MAX_VALUE
            for (int exp = 0; exp < 308; exp++) {
                final double tmpScale = PrimitiveFunction.POWER.invoke(PrimitiveMath.TEN, large ? exp : -exp);
                final PrimitiveDenseStore tmpBody = MatrixUtils.makeSPD(dim);
                final PrimitiveDenseStore tmpRHS = PrimitiveDenseStore.FACTORY.makeFilled(dim, 1, new Uniform());
                if (DEBUG) {
                    BasicLogger.debug("Scale exp={} => factor={} and context={}", exp, tmpScale, tmpContext);
                    BasicLogger.debug("Body (unscaled) {}", tmpBody.toString());
                    BasicLogger.debug("RHS (unscaled) {}", tmpRHS.toString());
                }
                final UnaryFunction<Double> tmpModifier = PrimitiveFunction.MULTIPLY.second(tmpScale);
                tmpBody.modifyAll(tmpModifier);
                tmpRHS.modifyAll(tmpModifier);
                ExtremeElementsCase.performSolveTest(tmpBody, tmpRHS, SolverTask.PRIMITIVE.make(tmpBody, tmpRHS), tmpContext);
                final List<MatrixDecomposition<Double>> tmpAllDecomps = MatrixDecompositionTests.getAllPrimitive();
                for (final MatrixDecomposition<Double> tmpDecomp : tmpAllDecomps) {
                    if (DEBUG) {
                        BasicLogger.debug("{} at dim={} for scale={}", tmpDecomp.getClass(), dim, tmpScale);
                    }
                    if (tmpDecomp instanceof MatrixDecomposition.Solver) {
                        ExtremeElementsCase.performSolveTest(tmpBody, tmpRHS, (SolverTask<Double>) tmpDecomp, tmpContext);
                    }
                }
            }
        }
    }
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Uniform(org.ojalgo.random.Uniform) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 8 with Uniform

use of org.ojalgo.random.Uniform in project ojAlgo by optimatika.

the class ArrayTests method doTestRandomSetAndGetBack.

static void doTestRandomSetAndGetBack(final BasicArray<Double> array, final long expectedCount) {
    TestUtils.assertEquals(expectedCount, array.count());
    final Uniform tmpUniform = new Uniform();
    final Map<Long, Double> pairs = new HashMap<>();
    for (int i = 0; i < 100; i++) {
        final long tmpIndex = Uniform.randomInteger(expectedCount);
        final double tmpValue = tmpUniform.doubleValue();
        array.set(tmpIndex, tmpValue);
        TestUtils.assertEquals(tmpValue, array.doubleValue(tmpIndex));
        pairs.put(tmpIndex, tmpValue);
    }
    for (final Entry<Long, Double> pair : pairs.entrySet()) {
        TestUtils.assertEquals(pair.getValue().doubleValue(), array.doubleValue(pair.getKey().longValue()));
    }
}
Also used : HashMap(java.util.HashMap) Uniform(org.ojalgo.random.Uniform)

Example 9 with Uniform

use of org.ojalgo.random.Uniform in project ojAlgo by optimatika.

the class DirectCleanupTest method main.

public static void main(final String[] args) {
    DenseArray<Double> tmpOrg = BufferArray.DIRECT64.makeZero(SIZE);
    tmpOrg.fillAll(new Uniform());
    while (true) {
        final DenseArray<Double> tmpCopy = BufferArray.DIRECT64.makeZero(SIZE);
        final long start = System.nanoTime();
        tmpCopy.fillMatching(tmpOrg);
        final long stop = System.nanoTime();
        tmpOrg = tmpCopy;
        BasicLogger.debug("Copied {} times. Last copy took {}ms", COUNTER.incrementAndGet(), ((stop - start) / 1_000_000));
        try {
            TimeUnit.SECONDS.sleep(1);
            System.gc();
        } catch (final InterruptedException exception) {
            exception.printStackTrace();
        }
    }
}
Also used : Uniform(org.ojalgo.random.Uniform)

Example 10 with Uniform

use of org.ojalgo.random.Uniform in project ojAlgo by optimatika.

the class ApproximationCase method testSecondOrderApproximation.

@Test
public void testSecondOrderApproximation() {
    final int tmpArity = 9;
    final PhysicalStore<Double> tmpQuadratic = PrimitiveDenseStore.FACTORY.makeFilled(tmpArity, tmpArity, new Uniform());
    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 CompoundFunction<Double> tmpOrgFunc = CompoundFunction.makePrimitive(tmpQuadratic, tmpLinear);
    final SecondOrderApproximation<Double> tmpApprFunc = tmpOrgFunc.toSecondOrderApproximation(tmpPoint);
    final PhysicalStore<Double> tmpX = PrimitiveDenseStore.FACTORY.makeFilled(tmpArity, 1, new Uniform(-10, 20));
    TestUtils.assertEquals(tmpOrgFunc.invoke(tmpX), tmpApprFunc.invoke(tmpX), PrimitiveFunction.SQRT.invoke(1E-14 / PrimitiveMath.THREE));
}
Also used : Uniform(org.ojalgo.random.Uniform) Test(org.junit.jupiter.api.Test)

Aggregations

Uniform (org.ojalgo.random.Uniform)14 Test (org.junit.jupiter.api.Test)8 NumberContext (org.ojalgo.type.context.NumberContext)4 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Tag (org.junit.jupiter.api.Tag)1 RecoverableCondition (org.ojalgo.RecoverableCondition)1 BasicMatrix (org.ojalgo.matrix.BasicMatrix)1 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)1 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)1 SolverTask (org.ojalgo.matrix.task.SolverTask)1 ComplexNumber (org.ojalgo.scalar.ComplexNumber)1