Search in sources :

Example 41 with NumberContext

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

the class SchurTest method testMathWorldCase.

/**
 * http://mathworld.wolfram.com/SchurDecomposition.html
 */
@Test
public void testMathWorldCase() {
    final PhysicalStore<Double> tmpOriginalMatrix = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 3, 2, 1 }, { 4, 2, 1 }, { 4, 4, 0 } });
    final double tmp00 = 3.0 + PrimitiveFunction.SQRT.invoke(13.0);
    final double tmp11 = 3.0 - PrimitiveFunction.SQRT.invoke(13.0);
    final double tmp22 = -1.0;
    final Array1D<ComplexNumber> tmpExpectedDiagonal = Array1D.COMPLEX.copy(new ComplexNumber[] { ComplexNumber.valueOf(tmp00), ComplexNumber.valueOf(tmp11), ComplexNumber.valueOf(tmp22) });
    SchurTest.doTest(tmpOriginalMatrix, tmpExpectedDiagonal, new NumberContext(7, 3));
    PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.49857, 0.76469, 0.40825 }, { 0.57405, 0.061628, -0.81650 }, { 0.64953, -0.64144, 0.40825 } });
    PrimitiveDenseStore.FACTORY.rows(new double[][] { { tmp00, 4.4907, -0.82632 }, { 0.0, tmp11, 1.0726 }, { 0.0, 0.0, tmp22 } });
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) Test(org.junit.jupiter.api.Test)

Example 42 with NumberContext

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

the class SingularValueTest method testComplexNumberVersionOfWikipediaCase.

/**
 * http://en.wikipedia.org/wiki/Singular_value_decomposition
 */
@Test
public void testComplexNumberVersionOfWikipediaCase() {
    final PhysicalStore<Double> tmpBaseMtrx = 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 Array1D<Double> tmpExpectedSingularValues = Array1D.PRIMITIVE64.copy(new double[] { 4.0, 3.0, PrimitiveFunction.SQRT.invoke(5.0), 0.0 });
    final ComplexNumber[] tmpScales = new ComplexNumber[] { ComplexNumber.makePolar(1.0, 0.0), ComplexNumber.makePolar(1.0, Math.PI / 2.0), ComplexNumber.makePolar(1.0, -Math.PI / 2.0), ComplexNumber.makePolar(1.0, Math.PI / 4.0), ComplexNumber.makePolar(1.0, (4.0 * Math.PI) / 3.0) };
    final Bidiagonal<ComplexNumber> tmpBidiagonal = Bidiagonal.COMPLEX.make();
    final SingularValue<ComplexNumber> tmpSVD = SingularValue.COMPLEX.make();
    for (final ComplexNumber tmpScale : tmpScales) {
        final PhysicalStore<ComplexNumber> tmpOriginalMtrx = ComplexDenseStore.FACTORY.transpose(tmpBaseMtrx);
        tmpOriginalMtrx.modifyAll(ComplexFunction.MULTIPLY.first(tmpScale));
        tmpBidiagonal.decompose(tmpOriginalMtrx);
        final MatrixStore<ComplexNumber> tmpReconstructed = tmpBidiagonal.reconstruct();
        if (MatrixDecompositionTests.DEBUG) {
            BasicLogger.debug();
            BasicLogger.debug("Scale = {}", tmpScale);
            BasicLogger.debug("A", tmpOriginalMtrx);
            BasicLogger.debug("Q1", tmpBidiagonal.getQ1());
            BasicLogger.debug("D", tmpBidiagonal.getD());
            BasicLogger.debug("Q2", tmpBidiagonal.getQ2());
            BasicLogger.debug("Reconstructed", tmpReconstructed);
        }
        TestUtils.assertEquals(tmpOriginalMtrx, tmpReconstructed, new NumberContext(7, 6));
    }
    for (final ComplexNumber tmpScale : tmpScales) {
        if (MatrixDecompositionTests.DEBUG) {
            BasicLogger.debug();
            BasicLogger.debug("Scale = {}", tmpScale);
        }
        final PhysicalStore<ComplexNumber> tmpOriginalMtrx = ComplexDenseStore.FACTORY.copy(tmpBaseMtrx);
        tmpOriginalMtrx.modifyAll(ComplexFunction.MULTIPLY.first(tmpScale));
        tmpBidiagonal.decompose(tmpOriginalMtrx.conjugate());
        tmpSVD.setFullSize(false);
        tmpSVD.decompose(tmpOriginalMtrx);
        final Array1D<Double> tmpActualSingularValues = tmpSVD.getSingularValues();
        if (MatrixDecompositionTests.DEBUG) {
            BasicLogger.debug("Expected = {}", tmpExpectedSingularValues);
            BasicLogger.debug("Actual = {}", tmpActualSingularValues);
        }
        TestUtils.assertEquals(tmpExpectedSingularValues, tmpActualSingularValues, new NumberContext(7, 6));
        final MatrixStore<ComplexNumber> tmpReconstructed = tmpSVD.reconstruct();
        if (MatrixDecompositionTests.DEBUG) {
            BasicLogger.debug("Original", tmpOriginalMtrx);
            BasicLogger.debug("Reconstructed", tmpReconstructed);
        }
        TestUtils.assertEquals(tmpOriginalMtrx, tmpReconstructed, new NumberContext(7, 6));
    }
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) Test(org.junit.jupiter.api.Test)

Example 43 with NumberContext

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

the class SingularValueTest method testRandomActuallyComplexCase.

@Test
public void testRandomActuallyComplexCase() {
    final PhysicalStore<ComplexNumber> tmpOriginal = MatrixUtils.makeRandomComplexStore(4, 4);
    final SingularValue<ComplexNumber> tmpDecomposition = SingularValue.COMPLEX.make();
    tmpDecomposition.decompose(tmpOriginal);
    final MatrixStore<ComplexNumber> tmpReconstructed = tmpDecomposition.reconstruct();
    if (!Access2D.equals(tmpOriginal, tmpReconstructed, new NumberContext(7, 6))) {
        BasicLogger.error("Recreation failed for: {}", tmpDecomposition.getClass().getName());
    }
    if (!SingularValue.equals(tmpOriginal, tmpDecomposition, new NumberContext(7, 6))) {
        BasicLogger.error("Decomposition not correct for: {}", tmpDecomposition.getClass().getName());
    }
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug();
        BasicLogger.debug(tmpDecomposition.toString());
        BasicLogger.debug("Original", tmpOriginal);
        BasicLogger.debug("Q1", tmpDecomposition.getQ1());
        BasicLogger.debug("D", tmpDecomposition.getD());
        BasicLogger.debug("Q2", tmpDecomposition.getQ2());
        BasicLogger.debug("Reconstructed", tmpReconstructed);
        final PhysicalStore<ComplexNumber> tmpCopy = tmpOriginal.copy();
        // tmpCopy.maxpy(ComplexNumber.NEG, tmpReconstructed);
        tmpReconstructed.axpy(-1, tmpCopy);
        BasicLogger.debug("Diff", tmpCopy);
    }
    TestUtils.assertEquals(tmpOriginal, tmpReconstructed, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpOriginal, tmpDecomposition, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) Test(org.junit.jupiter.api.Test)

Example 44 with NumberContext

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

the class TestSolveAndInvert method testSimpleEquationCase.

@Test
public void testSimpleEquationCase() {
    final MatrixStore<Double> tmpBody = PrimitiveDenseStore.FACTORY.copy(SimpleEquationCase.getBody());
    final MatrixStore<Double> tmpRHS = PrimitiveDenseStore.FACTORY.copy(SimpleEquationCase.getRHS());
    final MatrixStore<Double> tmpSolution = PrimitiveDenseStore.FACTORY.copy(SimpleEquationCase.getSolution());
    for (final MatrixDecomposition.Solver<Double> tmpDecomp : TestSolveAndInvert.getAllSquare()) {
        this.doTest(tmpDecomp, tmpBody, tmpRHS, tmpSolution, new NumberContext(7, 6));
    }
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 45 with NumberContext

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

the class TestSolveAndInvert method testInverseOfRandomCase.

@Test
public void testInverseOfRandomCase() {
    final NumberContext tmpEqualsNumberContext = new NumberContext(7, 10);
    final int tmpDim = 99;
    final PhysicalStore<Double> tmpRandom = PrimitiveDenseStore.FACTORY.copy(MatrixUtils.makeRandomComplexStore(tmpDim, tmpDim));
    final PhysicalStore<Double> tmpIdentity = PrimitiveDenseStore.FACTORY.makeEye(tmpDim, tmpDim);
    final MatrixDecomposition.Solver<Double>[] tmpAllDecomps = TestSolveAndInvert.getAllSquare();
    final LU<Double> tmpRefDecomps = new RawLU();
    tmpRefDecomps.decompose(tmpRandom);
    final MatrixStore<Double> tmpExpected = tmpRefDecomps.getInverse();
    for (final MatrixDecomposition.Solver<Double> tmpDecomp : tmpAllDecomps) {
        final String tmpName = tmpDecomp.getClass().getName();
        if (MatrixDecompositionTests.DEBUG) {
            BasicLogger.debug(tmpName);
        }
        tmpDecomp.decompose(tmpRandom);
        final MatrixStore<Double> tmpActual = tmpDecomp.getInverse();
        TestUtils.assertEquals(tmpName, tmpExpected, tmpActual, tmpEqualsNumberContext);
        TestUtils.assertEquals(tmpName, tmpIdentity, tmpActual.multiply(tmpRandom), tmpEqualsNumberContext);
        TestUtils.assertEquals(tmpName, tmpIdentity, tmpRandom.multiply(tmpActual), tmpEqualsNumberContext);
    }
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

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