Search in sources :

Example 61 with NumberContext

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

the class DegenerateLUCase method testBig.

@Test
public void testBig() {
    final NumberContext tmpEvalContext = new NumberContext(7, 4);
    final BasicMatrix tmpMtrxA = RationalMatrix.FACTORY.makeZero(SimpleEquationCase.getBody().countRows(), (int) SimpleEquationCase.getBody().countColumns()).mergeColumns(SimpleEquationCase.getBody()).mergeColumns(SimpleEquationCase.getBody());
    final LU<BigDecimal> tmpBigDecomp = LU.BIG.make();
    tmpBigDecomp.decompose(BigDenseStore.FACTORY.copy(tmpMtrxA));
    // System.out.println("A: " + tmpMtrxA.enforce(tmpEvalContext));
    // System.out.println("P: " + new RationalMatrix(tmpBigDecomp.getP()).enforce(tmpEvalContext));
    // System.out.println("L: " + new RationalMatrix(tmpBigDecomp.getL()).enforce(tmpEvalContext));
    // System.out.println("PL: " + new RationalMatrix(tmpBigDecomp.getP().multiplyRight(tmpBigDecomp.getL())).enforce(tmpEvalContext));
    // System.out.println("D: " + new RationalMatrix(tmpBigDecomp.getD()).enforce(tmpEvalContext));
    // System.out.println("U: " + new RationalMatrix(tmpBigDecomp.getU()).enforce(tmpEvalContext));
    // System.out.println("DU: " + new RationalMatrix(tmpBigDecomp.getD().multiplyRight(tmpBigDecomp.getU())).enforce(tmpEvalContext));
    TestUtils.assertEquals(BigDenseStore.FACTORY.copy(tmpMtrxA), tmpBigDecomp, tmpEvalContext);
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) NumberContext(org.ojalgo.type.context.NumberContext) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 62 with NumberContext

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

the class DegenerateLUCase method testJama.

@Test
public void testJama() {
    final NumberContext tmpEvalContext = new NumberContext(7, 4);
    final BasicMatrix tmpMtrxA = RationalMatrix.FACTORY.makeZero(SimpleEquationCase.getBody().countRows(), (int) SimpleEquationCase.getBody().countColumns()).mergeColumns(SimpleEquationCase.getBody()).mergeColumns(SimpleEquationCase.getBody());
    final LU<Double> tmpDoubleDecomp = new RawLU();
    tmpDoubleDecomp.decompose(PrimitiveDenseStore.FACTORY.copy(tmpMtrxA));
    // System.out.println("A: " + tmpMtrxA.enforce(tmpEvalContext));
    // System.out.println("P: " + new PrimitiveMatrix(tmpDoubleDecomp.getP()).enforce(tmpEvalContext));
    // System.out.println("L: " + new PrimitiveMatrix(tmpDoubleDecomp.getL()).enforce(tmpEvalContext));
    // System.out.println("PL: " + new PrimitiveMatrix(tmpDoubleDecomp.getP().multiplyRight(tmpDoubleDecomp.getL())).enforce(tmpEvalContext));
    // System.out.println("D: " + new PrimitiveMatrix(tmpDoubleDecomp.getD()).enforce(tmpEvalContext));
    // System.out.println("U: " + new PrimitiveMatrix(tmpDoubleDecomp.getU()).enforce(tmpEvalContext));
    // System.out.println("DU: " + new PrimitiveMatrix(tmpDoubleDecomp.getD().multiplyRight(tmpDoubleDecomp.getU())).enforce(tmpEvalContext));
    TestUtils.assertEquals(PrimitiveDenseStore.FACTORY.copy(tmpMtrxA), tmpDoubleDecomp, tmpEvalContext);
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 63 with NumberContext

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

the class DesignCase method testWikipediaNullspace.

/**
 * http://en.wikipedia.org/wiki/Kernel_%28matrix%29
 */
@Test
public void testWikipediaNullspace() {
    final PhysicalStore<Double> tmpA = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 2, 3, 5 }, { -4, 2, 3 } });
    final QR<Double> tmpQR = QR.PRIMITIVE.make(tmpA);
    tmpQR.setFullSize(true);
    tmpQR.decompose(tmpA.transpose());
    final SingularValue<Double> tmpSVD = new SingularValueDecomposition.Primitive();
    // Supports full size
    tmpSVD.setFullSize(true);
    tmpSVD.decompose(tmpA);
    final PhysicalStore<Double> tmpNullspaceQR = tmpQR.getQ().logical().offsets(0, tmpQR.getRank()).get().copy();
    final PhysicalStore<Double> tmpNullspaceSVD = tmpSVD.getQ2().logical().offsets(0, tmpSVD.getRank()).get().copy();
    final double tmpScaleQR = PrimitiveFunction.ABS.invoke(tmpNullspaceQR.doubleValue(0));
    tmpNullspaceQR.modifyAll(PrimitiveFunction.DIVIDE.second(tmpScaleQR));
    final double tmpScaleSVD = PrimitiveFunction.ABS.invoke(tmpNullspaceSVD.doubleValue(0));
    tmpNullspaceSVD.modifyAll(PrimitiveFunction.DIVIDE.second(tmpScaleSVD));
    final PrimitiveDenseStore tmpExpected = PrimitiveDenseStore.FACTORY.columns(new double[] { -1, -26, 16 });
    final NumberContext tmpPrecision = new NumberContext(14, 8);
    TestUtils.assertEquals(tmpExpected, tmpNullspaceQR, tmpPrecision);
    TestUtils.assertEquals(tmpExpected, tmpNullspaceSVD, tmpPrecision);
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Example 64 with NumberContext

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

the class DesignCase method testWikipediaSVD.

/**
 * http://en.wikipedia.org/wiki/Singular_value_decomposition
 */
@Test
public void testWikipediaSVD() {
    final PhysicalStore<Double> tmpOriginalMatrix = 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 } });
    Array1D.PRIMITIVE64.copy(new double[] { 4.0, 3.0, PrimitiveFunction.SQRT.invoke(5.0), 0.0 });
    final SingularValue<Double> tmpOldDecomp = new SingularValueDecomposition.Primitive();
    tmpOldDecomp.decompose(tmpOriginalMatrix);
    tmpOldDecomp.getD();
    tmpOldDecomp.getQ1();
    tmpOldDecomp.getQ2();
    final SingularValue<Double> tmpNewDecomp = new RawSingularValue();
    tmpNewDecomp.decompose(tmpOriginalMatrix);
    tmpNewDecomp.getD();
    tmpNewDecomp.getQ1();
    tmpNewDecomp.getQ2();
    TestUtils.assertEquals(tmpOriginalMatrix, tmpNewDecomp, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 65 with NumberContext

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

the class EigenvalueTest method testP20061119Case.

@Test
public void testP20061119Case() {
    final PhysicalStore<Double> tmpOriginalMatrix = PrimitiveDenseStore.FACTORY.copy(P20061119Case.getProblematic());
    final ComplexNumber tmp00 = ComplexNumber.valueOf(26.14421883828456);
    final ComplexNumber tmp11 = ComplexNumber.of(2.727890580857718, 3.6223578444417908);
    final ComplexNumber tmp22 = tmp11.conjugate();
    final ComplexNumber tmp33 = ComplexNumber.ZERO;
    final ComplexNumber tmp44 = tmp33;
    final Array1D<ComplexNumber> tmpExpectedDiagonal = Array1D.COMPLEX.copy(new ComplexNumber[] { tmp00, tmp11, tmp22, tmp33, tmp44 });
    final NumberContext accuracyContext = new NumberContext(7, 6);
    MatrixStore<Double> tmpRecreatedMatrix;
    final Eigenvalue<Double> tmpDecomposition = Eigenvalue.PRIMITIVE.make(tmpOriginalMatrix);
    tmpDecomposition.decompose(tmpOriginalMatrix);
    final Array1D<ComplexNumber> tmpEigenvalues = tmpDecomposition.getEigenvalues();
    final MatrixStore<Double> tmpD = tmpDecomposition.getD();
    final MatrixStore<Double> tmpV = tmpDecomposition.getV();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Eigenvalues = {}", tmpEigenvalues);
        BasicLogger.debug("D = {}", tmpD);
        BasicLogger.debug("V = {}", tmpV);
    }
    tmpRecreatedMatrix = tmpV.multiply(tmpDecomposition.getD()).multiply(tmpV.transpose());
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Original = {}", tmpOriginalMatrix);
        BasicLogger.debug("Recreated = {}", tmpRecreatedMatrix);
    }
    TestUtils.assertEquals(tmpOriginalMatrix.multiply(tmpV), tmpV.multiply(tmpDecomposition.getD()), accuracyContext);
    tmpExpectedDiagonal.sortDescending();
    tmpEigenvalues.sortDescending();
    TestUtils.assertEquals(tmpExpectedDiagonal, tmpEigenvalues, accuracyContext);
    tmpDecomposition.computeValuesOnly(tmpOriginalMatrix);
    final Array1D<ComplexNumber> tmpEigenvaluesOnly = tmpDecomposition.getEigenvalues();
    TestUtils.assertEquals(tmpExpectedDiagonal, tmpEigenvaluesOnly, accuracyContext);
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) 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