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;
}
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;
}
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);
}
}
}
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);
}
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());
}
}
Aggregations