use of org.ojalgo.type.context.NumberContext 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 } });
}
use of org.ojalgo.type.context.NumberContext 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.type.context.NumberContext 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.type.context.NumberContext in project ojAlgo by optimatika.
the class TestSolveAndInvert method testSimpleEquationCase.
@Test
public void testSimpleEquationCase() {
final MatrixStore<Double> tmpBody = PrimitiveDenseStore.FACTORY.copy(SimpleEquationCase.getBody());
final MatrixStore<Double> tmpRHS = PrimitiveDenseStore.FACTORY.copy(SimpleEquationCase.getRHS());
final MatrixStore<Double> tmpSolution = PrimitiveDenseStore.FACTORY.copy(SimpleEquationCase.getSolution());
for (final MatrixDecomposition.Solver<Double> tmpDecomp : TestSolveAndInvert.getAllSquare()) {
this.doTest(tmpDecomp, tmpBody, tmpRHS, tmpSolution, new NumberContext(7, 6));
}
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class TestSolveAndInvert method testInverseOfRandomCase.
@Test
public void testInverseOfRandomCase() {
final NumberContext tmpEqualsNumberContext = new NumberContext(7, 10);
final int tmpDim = 99;
final PhysicalStore<Double> tmpRandom = PrimitiveDenseStore.FACTORY.copy(MatrixUtils.makeRandomComplexStore(tmpDim, tmpDim));
final PhysicalStore<Double> tmpIdentity = PrimitiveDenseStore.FACTORY.makeEye(tmpDim, tmpDim);
final MatrixDecomposition.Solver<Double>[] tmpAllDecomps = TestSolveAndInvert.getAllSquare();
final LU<Double> tmpRefDecomps = new RawLU();
tmpRefDecomps.decompose(tmpRandom);
final MatrixStore<Double> tmpExpected = tmpRefDecomps.getInverse();
for (final MatrixDecomposition.Solver<Double> tmpDecomp : tmpAllDecomps) {
final String tmpName = tmpDecomp.getClass().getName();
if (MatrixDecompositionTests.DEBUG) {
BasicLogger.debug(tmpName);
}
tmpDecomp.decompose(tmpRandom);
final MatrixStore<Double> tmpActual = tmpDecomp.getInverse();
TestUtils.assertEquals(tmpName, tmpExpected, tmpActual, tmpEqualsNumberContext);
TestUtils.assertEquals(tmpName, tmpIdentity, tmpActual.multiply(tmpRandom), tmpEqualsNumberContext);
TestUtils.assertEquals(tmpName, tmpIdentity, tmpRandom.multiply(tmpActual), tmpEqualsNumberContext);
}
}
Aggregations