Search in sources :

Example 26 with ComplexNumber

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

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

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

the class SuperimposedMatrixElementCase method setUp.

@BeforeEach
public void setUp() {
    final int tmpRowDim = Uniform.randomInteger(1, 9);
    final int tmpColDim = Uniform.randomInteger(1, 9);
    final BasicMatrix tmpBase = NonPhysicalTest.makeRandomMatrix(tmpRowDim, tmpColDim);
    final int tmpRowIndex = Uniform.randomInteger(tmpRowDim);
    final int tmpColumnIndex = Uniform.randomInteger(tmpColDim);
    final BigDecimal tmpElement = BigMath.PI;
    final MatrixStore<BigDecimal> aBase = BigDenseStore.FACTORY.copy(tmpBase);
    // myBigStore = new SuperimposedMatrixStore<BigDecimal>(BigDenseStore.FACTORY.copyMatrix(tmpBase), tmpRowIndex, tmpColumnIndex, tmpElement);
    // myComplexStore = new SuperimposedMatrixStore<ComplexNumber>(ComplexDenseStore.FACTORY.copyMatrix(tmpBase), tmpRowIndex, tmpColumnIndex, ComplexNumber.makeReal(tmpElement.doubleValue()));
    // myPrimitiveStore = new SuperimposedMatrixStore<Double>(PrimitiveDenseStore.FACTORY.copyMatrix(tmpBase), tmpRowIndex, tmpColumnIndex, tmpElement.doubleValue());
    myBigStore = new SuperimposedStore<>(aBase, tmpRowIndex, tmpColumnIndex, new SingleStore<>(aBase.physical(), tmpElement));
    final MatrixStore<ComplexNumber> aBase1 = ComplexDenseStore.FACTORY.copy(tmpBase);
    myComplexStore = new SuperimposedStore<>(aBase1, tmpRowIndex, tmpColumnIndex, new SingleStore<>(aBase1.physical(), ComplexNumber.valueOf(tmpElement.doubleValue())));
    final MatrixStore<Double> aBase2 = PrimitiveDenseStore.FACTORY.copy(tmpBase);
    myPrimitiveStore = new SuperimposedStore<>(aBase2, tmpRowIndex, tmpColumnIndex, new SingleStore<>(aBase2.physical(), tmpElement.doubleValue()));
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) ComplexNumber(org.ojalgo.scalar.ComplexNumber) BigDecimal(java.math.BigDecimal) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 29 with ComplexNumber

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

the class ComplexDenseStore method transformRight.

public void transformRight(final Householder<ComplexNumber> transformation, final int firstRow) {
    final Householder.Complex tmpTransf = ComplexDenseStore.cast(transformation);
    final ComplexNumber[] tmpData = data;
    final int tmpRowDim = myRowDim;
    final int tmpColDim = myColDim;
    if ((tmpRowDim - firstRow) > HouseholderRight.THRESHOLD) {
        final DivideAndConquer tmpConquerer = new DivideAndConquer() {

            @Override
            public void conquer(final int aFirst, final int aLimit) {
                HouseholderRight.invoke(tmpData, aFirst, aLimit, tmpColDim, tmpTransf);
            }
        };
        tmpConquerer.invoke(firstRow, tmpRowDim, HouseholderRight.THRESHOLD);
    } else {
        HouseholderRight.invoke(tmpData, firstRow, tmpRowDim, tmpColDim, tmpTransf);
    }
}
Also used : DivideAndConquer(org.ojalgo.concurrent.DivideAndConquer) Householder(org.ojalgo.matrix.transformation.Householder) ComplexNumber(org.ojalgo.scalar.ComplexNumber)

Example 30 with ComplexNumber

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

the class ComplexDenseStore method transformLeft.

public void transformLeft(final Householder<ComplexNumber> transformation, final int firstColumn) {
    final Householder.Complex tmpTransf = ComplexDenseStore.cast(transformation);
    final ComplexNumber[] tmpData = data;
    final int tmpRowDim = myRowDim;
    final int tmpColDim = myColDim;
    if ((tmpColDim - firstColumn) > HouseholderLeft.THRESHOLD) {
        final DivideAndConquer tmpConquerer = new DivideAndConquer() {

            @Override
            public void conquer(final int aFirst, final int aLimit) {
                HouseholderLeft.invoke(tmpData, tmpRowDim, aFirst, aLimit, tmpTransf);
            }
        };
        tmpConquerer.invoke(firstColumn, tmpColDim, HouseholderLeft.THRESHOLD);
    } else {
        HouseholderLeft.invoke(tmpData, tmpRowDim, firstColumn, tmpColDim, tmpTransf);
    }
}
Also used : DivideAndConquer(org.ojalgo.concurrent.DivideAndConquer) Householder(org.ojalgo.matrix.transformation.Householder) ComplexNumber(org.ojalgo.scalar.ComplexNumber)

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