Search in sources :

Example 21 with ComplexNumber

use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.

the class EigenvalueTest method testPaulsMathNote.

@Test
public void testPaulsMathNote() {
    final double[][] tmpData = new double[][] { { 3, -9 }, { 4, -3 } };
    final PrimitiveDenseStore tmpA = PrimitiveDenseStore.FACTORY.rows(tmpData);
    final int tmpLength = tmpData.length;
    final Array1D<ComplexNumber> tmpExpVals = Array1D.COMPLEX.makeZero(2);
    tmpExpVals.set(0, ComplexNumber.of(0.0, THREE * SQRT.invoke(THREE)));
    tmpExpVals.set(1, tmpExpVals.get(0).conjugate());
    final Array2D<ComplexNumber> tmpExpVecs = Array2D.COMPLEX.makeZero(2, 2);
    tmpExpVecs.set(0, 0, ComplexNumber.of(THREE, ZERO));
    tmpExpVecs.set(1, 0, ComplexNumber.of(ONE, -SQRT.invoke(THREE)));
    tmpExpVecs.set(0, 1, ComplexNumber.of(THREE, ZERO));
    tmpExpVecs.set(1, 1, ComplexNumber.of(ONE, SQRT.invoke(THREE)));
    final Eigenvalue<Double> tmpEvD = Eigenvalue.PRIMITIVE.make(tmpA, false);
    tmpEvD.decompose(tmpA);
    final MatrixStore<Double> tmpD = tmpEvD.getD();
    final MatrixStore<Double> tmpV = tmpEvD.getV();
    final Array1D<ComplexNumber> tmpValues = tmpEvD.getEigenvalues();
    final MatrixStore<ComplexNumber> tmpVectors = tmpEvD.getEigenvectors();
    TestUtils.assertEquals(tmpExpVals, tmpValues);
    for (int j = 0; j < tmpLength; j++) {
        final Array1D<ComplexNumber> tmpSliceColumn = tmpExpVecs.sliceColumn(0, j);
        final Access1D<ComplexNumber> tmpActual = tmpVectors.sliceColumn(0, j);
        final ComplexNumber tmpFactor = tmpActual.get(0).divide(tmpSliceColumn.get(0));
        TestUtils.assertEquals(tmpSliceColumn.get(1).multiply(tmpFactor), tmpActual.get(1));
    }
    final ComplexDenseStore tmpCmplA = ComplexDenseStore.FACTORY.copy(tmpA);
    final ComplexDenseStore tmpCmplD = ComplexDenseStore.FACTORY.copy(tmpD);
    final ComplexDenseStore tmpCmplV = ComplexDenseStore.FACTORY.copy(tmpV);
    final MatrixStore<ComplexNumber> tmpExp1 = tmpCmplA.multiply(tmpCmplV);
    final MatrixStore<ComplexNumber> tmpAct1 = tmpCmplV.multiply(tmpCmplD);
    TestUtils.assertEquals(tmpExp1, tmpAct1);
    final ComplexDenseStore tmpComplexD = ComplexDenseStore.FACTORY.makeZero(tmpLength, tmpLength);
    for (int j = 0; j < tmpLength; j++) {
        tmpComplexD.set(j, j, tmpValues.get(j));
    }
    final MatrixStore<ComplexNumber> tmpExp2 = tmpCmplA.multiply(tmpVectors);
    final MatrixStore<ComplexNumber> tmpAct2 = tmpVectors.multiply(tmpComplexD);
    TestUtils.assertEquals(tmpExp2, tmpAct2);
    tmpEvD.computeValuesOnly(tmpA);
    final Array1D<ComplexNumber> tmpEigenvaluesOnly = tmpEvD.getEigenvalues();
    TestUtils.assertEquals(tmpValues, tmpEigenvaluesOnly);
}
Also used : ComplexDenseStore(org.ojalgo.matrix.store.ComplexDenseStore) ComplexNumber(org.ojalgo.scalar.ComplexNumber) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Example 22 with ComplexNumber

use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.

the class EigenvalueTest method testProblemFoundInTheWild.

/**
 * A matrix that has been problematic for another library...
 */
@Test
public void testProblemFoundInTheWild() {
    final PrimitiveDenseStore matrix = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1, 0, 0 }, { 0.01, 0, -1 }, { 0.01, 1, 0 } });
    for (final Eigenvalue<Double> tmpEigenvalue : MatrixDecompositionTests.getEigenvaluePrimitiveGeneral()) {
        tmpEigenvalue.decompose(matrix);
        TestUtils.assertEquals(matrix, tmpEigenvalue, NumberContext.getGeneral(MathContext.DECIMAL64));
        final Array1D<ComplexNumber> tmpValues = tmpEigenvalue.getEigenvalues();
        tmpEigenvalue.computeValuesOnly(matrix);
        final Array1D<ComplexNumber> tmpEigenvaluesOnly = tmpEigenvalue.getEigenvalues();
        TestUtils.assertEquals(tmpValues, tmpEigenvaluesOnly);
    }
}
Also used : ComplexNumber(org.ojalgo.scalar.ComplexNumber) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Example 23 with ComplexNumber

use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.

the class QRTest method testHermitian.

@Test
public void testHermitian() {
    final int tmpLim = DIMENSION - 1;
    final MatrixStore<ComplexNumber> tmpOriginal = QRTest.makeHermitianMatrix();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Original", tmpOriginal);
    }
    final QR<ComplexNumber> tmpDecomposition = QR.COMPLEX.make();
    tmpDecomposition.decompose(tmpOriginal);
    final MatrixStore<ComplexNumber> tmpDecompQ = tmpDecomposition.getQ();
    final MatrixStore<ComplexNumber> tmpDecompR = tmpDecomposition.getR();
    final DecompositionStore<ComplexNumber> tmpInPlace = ComplexDenseStore.FACTORY.copy(tmpOriginal);
    final DecompositionStore<ComplexNumber> tmpNowQ = ComplexDenseStore.FACTORY.makeEye(DIMENSION, DIMENSION);
    final DecompositionStore<ComplexNumber> tmpNowR = ComplexDenseStore.FACTORY.copy(tmpOriginal);
    final DecompositionStore<ComplexNumber> tmpForwardQ = ComplexDenseStore.FACTORY.makeEye(DIMENSION, DIMENSION);
    final DecompositionStore<ComplexNumber> tmpForwardR = ComplexDenseStore.FACTORY.copy(tmpOriginal);
    final DecompositionStore<ComplexNumber> tmpReverseQ = ComplexDenseStore.FACTORY.makeEye(DIMENSION, DIMENSION);
    final Householder.Complex[] tmpHouseholders = new Householder.Complex[tmpLim];
    for (int ij = 0; ij < tmpLim; ij++) {
        final Householder.Complex tmpVector = new Householder.Complex(DIMENSION);
        if (tmpInPlace.generateApplyAndCopyHouseholderColumn(ij, ij, tmpVector)) {
            tmpInPlace.transformLeft(tmpVector, ij + 1);
            tmpNowQ.transformRight(tmpVector, 0);
            tmpNowR.transformLeft(tmpVector, ij);
        }
        tmpHouseholders[ij] = tmpVector;
    }
    for (int h = 0; h < tmpHouseholders.length; h++) {
        final Householder.Complex tmpVector = tmpHouseholders[h];
        tmpForwardQ.transformRight(tmpVector, 0);
        tmpForwardR.transformLeft(tmpVector, h);
    }
    for (int h = tmpHouseholders.length - 1; h >= 0; h--) {
        final Householder.Complex tmpVector = tmpHouseholders[h];
        tmpReverseQ.transformLeft(tmpVector, 0);
    }
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug();
        BasicLogger.debug("Decomp Q", tmpDecompQ);
        BasicLogger.debug("Now Q", tmpNowQ);
        BasicLogger.debug("Forward Q", tmpForwardQ);
        BasicLogger.debug("Reverse Q", tmpReverseQ);
        BasicLogger.debug();
        BasicLogger.debug("Decomp R", tmpDecompR);
        BasicLogger.debug("Now R", tmpNowR);
        BasicLogger.debug("Forward R", tmpForwardR);
    }
    TestUtils.assertEquals(tmpOriginal, tmpDecomposition, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompQ, tmpNowQ, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompQ, tmpForwardQ, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompQ, tmpReverseQ, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompR, tmpNowR, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompR, tmpForwardR, new NumberContext(7, 6));
}
Also used : Householder(org.ojalgo.matrix.transformation.Householder) NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) Test(org.junit.jupiter.api.Test)

Example 24 with ComplexNumber

use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.

the class SchurTest 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 });
    SchurTest.doTest(tmpOriginalMatrix, tmpExpectedDiagonal, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) Test(org.junit.jupiter.api.Test)

Example 25 with ComplexNumber

use of org.ojalgo.scalar.ComplexNumber 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)

Aggregations

ComplexNumber (org.ojalgo.scalar.ComplexNumber)53 Test (org.junit.jupiter.api.Test)20 NumberContext (org.ojalgo.type.context.NumberContext)16 DivideAndConquer (org.ojalgo.concurrent.DivideAndConquer)7 ComplexArray (org.ojalgo.array.ComplexArray)5 BigDecimal (java.math.BigDecimal)4 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)4 BasicMatrix (org.ojalgo.matrix.BasicMatrix)3 Householder (org.ojalgo.matrix.transformation.Householder)3 Tag (org.junit.jupiter.api.Tag)2 RationalMatrix (org.ojalgo.matrix.RationalMatrix)2 Solver (org.ojalgo.matrix.decomposition.MatrixDecomposition.Solver)2 ComplexDenseStore (org.ojalgo.matrix.store.ComplexDenseStore)2 Normal (org.ojalgo.random.Normal)2 Quaternion (org.ojalgo.scalar.Quaternion)2 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Uniform (org.ojalgo.random.Uniform)1