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