Search in sources :

Example 11 with ComplexNumber

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

the class GenerateApplyAndCopyHouseholderColumn method invoke.

public static boolean invoke(final ComplexNumber[] data, final int structure, final int row, final int col, final Householder.Complex destination) {
    final int tmpColBase = col * structure;
    final ComplexNumber[] tmpVector = destination.vector;
    destination.first = row;
    double tmpNormInf = PrimitiveMath.ZERO;
    for (int i = row; i < structure; i++) {
        tmpNormInf = PrimitiveFunction.MAX.invoke(tmpNormInf, (tmpVector[i] = data[i + tmpColBase]).norm());
    }
    boolean retVal = tmpNormInf != PrimitiveMath.ZERO;
    ComplexNumber tmpVal;
    double tmpNorm2 = PrimitiveMath.ZERO;
    if (retVal) {
        for (int i = row + 1; i < structure; i++) {
            tmpVal = tmpVector[i].divide(tmpNormInf);
            tmpNorm2 += tmpVal.norm() * tmpVal.norm();
            tmpVector[i] = tmpVal;
        }
        retVal = !PrimitiveScalar.isSmall(PrimitiveMath.ONE, tmpNorm2);
    }
    if (retVal) {
        ComplexNumber tmpScale = tmpVector[row].divide(tmpNormInf);
        tmpNorm2 += tmpScale.norm() * tmpScale.norm();
        tmpNorm2 = PrimitiveFunction.SQRT.invoke(tmpNorm2);
        data[row + tmpColBase] = ComplexNumber.makePolar(tmpNorm2 * tmpNormInf, tmpScale.phase());
        tmpScale = tmpScale.subtract(ComplexNumber.makePolar(tmpNorm2, tmpScale.phase()));
        tmpVector[row] = ComplexNumber.ONE;
        for (int i = row + 1; i < structure; i++) {
            data[i + tmpColBase] = tmpVector[i] = ComplexFunction.DIVIDE.invoke(tmpVector[i], tmpScale);
        }
        destination.beta = ComplexNumber.valueOf(tmpScale.norm() / tmpNorm2);
    }
    return retVal;
}
Also used : ComplexNumber(org.ojalgo.scalar.ComplexNumber)

Example 12 with ComplexNumber

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

the class Rotation method rotationsC.

static Rotation<ComplexNumber>[] rotationsC(final PhysicalStore<ComplexNumber> matrix, final int low, final int high, final Rotation<ComplexNumber>[] results) {
    final ComplexNumber a00 = matrix.get(low, low);
    final ComplexNumber a01 = matrix.get(low, high);
    final ComplexNumber a10 = matrix.get(high, low);
    final ComplexNumber a11 = matrix.get(high, high);
    final ComplexNumber x = a00.add(a11);
    final ComplexNumber y = a10.subtract(a01);
    // tan, cot or something temporary
    ComplexNumber t;
    // Symmetrise - Givens
    // cos Givens
    final ComplexNumber cg;
    // sin Givens
    final ComplexNumber sg;
    if (ComplexNumber.isSmall(PrimitiveMath.ONE, y)) {
        cg = x.signum();
        sg = ComplexNumber.ZERO;
    } else if (ComplexNumber.isSmall(PrimitiveMath.ONE, x)) {
        sg = y.signum();
        cg = ComplexNumber.ZERO;
    } else if (y.compareTo(x) == 1) {
        // cot
        t = x.divide(y);
        sg = y.signum().divide(ComplexFunction.SQRT1PX2.invoke(t));
        cg = sg.multiply(t);
    } else {
        // tan
        t = y.divide(x);
        cg = x.signum().divide(ComplexFunction.SQRT1PX2.invoke(t));
        sg = cg.multiply(t);
    }
    final ComplexNumber b00 = cg.multiply(a00).add(sg.multiply(a10));
    final ComplexNumber b11 = cg.multiply(a11).subtract(sg.multiply(a01));
    // b01 + b10
    final ComplexNumber b2 = cg.multiply(a01.add(a10)).add(sg.multiply(a11.subtract(a00)));
    t = b11.subtract(b00).divide(b2);
    t = t.signum().divide(ComplexFunction.SQRT1PX2.invoke(t).add(t.norm()));
    // Annihilate - Jacobi
    // Cos Jacobi
    final ComplexNumber cj = ComplexFunction.SQRT1PX2.invoke(t).invert();
    // Sin Jacobi
    final ComplexNumber sj = cj.multiply(t);
    // Jacobi
    results[1] = new Rotation.Complex(low, high, cj, sj);
    // Givens - Jacobi
    results[0] = new Rotation.Complex(low, high, cj.multiply(cg).add(sj.multiply(sg)), cj.multiply(sg).subtract(sj.multiply(cg)));
    return results;
}
Also used : ComplexNumber(org.ojalgo.scalar.ComplexNumber)

Example 13 with ComplexNumber

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

the class Eigenvalue method getEigenvalues.

/**
 * @param realParts An array that will receive the real parts of the eigenvalues
 * @param imaginaryParts An optional array that, if present, will receive the imaginary parts of the
 *        eigenvalues
 */
default void getEigenvalues(final double[] realParts, final Optional<double[]> imaginaryParts) {
    ProgrammingError.throwIfNull(realParts, imaginaryParts);
    final Array1D<ComplexNumber> values = this.getEigenvalues();
    final int length = realParts.length;
    if (imaginaryParts.isPresent()) {
        final double[] imagParts = imaginaryParts.get();
        for (int i = 0; i < length; i++) {
            final ComplexNumber value = values.get(i);
            realParts[i] = value.getReal();
            imagParts[i] = value.getImaginary();
        }
    } else {
        for (int i = 0; i < length; i++) {
            realParts[i] = values.doubleValue(i);
        }
    }
}
Also used : ComplexNumber(org.ojalgo.scalar.ComplexNumber)

Example 14 with ComplexNumber

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

the class NewGeneralEvD method doGeneral.

@Override
protected boolean doGeneral(final Collectable<N, ? super PhysicalStore<N>> matrix, final boolean eigenvaluesOnly) {
    final int tmpDiagDim = (int) matrix.countRows();
    // final DecompositionStore<N> tmpMtrxA = this.copy(matrix.get());
    final DecompositionStore<N> tmpMtrxA = this.makeZero(tmpDiagDim, tmpDiagDim);
    matrix.supplyTo(tmpMtrxA);
    final DecompositionStore<N> tmpV = this.makeEye(tmpDiagDim, tmpDiagDim);
    final Array1D<ComplexNumber> tmpEigenvalues = tmpMtrxA.computeInPlaceSchur(tmpV, true);
    this.setV(tmpV);
    this.setEigenvalues(tmpEigenvalues);
    final PhysicalStore<N> tmpD = this.makeZero(tmpDiagDim, tmpDiagDim);
    ComplexNumber tmpValue;
    double tmpImaginary;
    for (int ij = 0; ij < tmpDiagDim; ij++) {
        tmpValue = tmpEigenvalues.get(ij);
        tmpD.set(ij, ij, tmpValue.doubleValue());
        tmpImaginary = tmpValue.i;
        if (tmpImaginary > PrimitiveMath.ZERO) {
            tmpD.set(ij, ij + 1, tmpImaginary);
        } else if (tmpImaginary < PrimitiveMath.ZERO) {
            tmpD.set(ij, ij - 1, tmpImaginary);
        }
    }
    this.setD(tmpD);
    // BasicLogger.logDebug("Eigenvalues: {}", tmpEigenvalues);
    // BasicLogger.logDebug("D", tmpD);
    // BasicLogger.logDebug("THIS", tmpMtrxA);
    tmpEigenvalues.sortDescending();
    return this.computed(true);
}
Also used : ComplexNumber(org.ojalgo.scalar.ComplexNumber)

Example 15 with ComplexNumber

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

the class MatrixUtils method copyComplexRealAndImaginary.

/**
 * Simultaneously copies the real and imaginary parts of the ComplexNumber elements to the destinations.
 */
public static void copyComplexRealAndImaginary(final Access2D<ComplexNumber> source, final ElementsConsumer<?> realDest, final ElementsConsumer<?> imagDest) {
    final long tmpCount = FunctionUtils.min(source.count(), realDest.count(), imagDest.count());
    ComplexNumber tmpComplexNumber;
    for (long i = 0L; i < tmpCount; i++) {
        tmpComplexNumber = source.get(i);
        realDest.set(i, tmpComplexNumber.getReal());
        imagDest.set(i, tmpComplexNumber.getImaginary());
    }
}
Also used : 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