use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class DegenerateLUCase method testBig.
@Test
public void testBig() {
final NumberContext tmpEvalContext = new NumberContext(7, 4);
final BasicMatrix tmpMtrxA = RationalMatrix.FACTORY.makeZero(SimpleEquationCase.getBody().countRows(), (int) SimpleEquationCase.getBody().countColumns()).mergeColumns(SimpleEquationCase.getBody()).mergeColumns(SimpleEquationCase.getBody());
final LU<BigDecimal> tmpBigDecomp = LU.BIG.make();
tmpBigDecomp.decompose(BigDenseStore.FACTORY.copy(tmpMtrxA));
// System.out.println("A: " + tmpMtrxA.enforce(tmpEvalContext));
// System.out.println("P: " + new RationalMatrix(tmpBigDecomp.getP()).enforce(tmpEvalContext));
// System.out.println("L: " + new RationalMatrix(tmpBigDecomp.getL()).enforce(tmpEvalContext));
// System.out.println("PL: " + new RationalMatrix(tmpBigDecomp.getP().multiplyRight(tmpBigDecomp.getL())).enforce(tmpEvalContext));
// System.out.println("D: " + new RationalMatrix(tmpBigDecomp.getD()).enforce(tmpEvalContext));
// System.out.println("U: " + new RationalMatrix(tmpBigDecomp.getU()).enforce(tmpEvalContext));
// System.out.println("DU: " + new RationalMatrix(tmpBigDecomp.getD().multiplyRight(tmpBigDecomp.getU())).enforce(tmpEvalContext));
TestUtils.assertEquals(BigDenseStore.FACTORY.copy(tmpMtrxA), tmpBigDecomp, tmpEvalContext);
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class DegenerateLUCase method testJama.
@Test
public void testJama() {
final NumberContext tmpEvalContext = new NumberContext(7, 4);
final BasicMatrix tmpMtrxA = RationalMatrix.FACTORY.makeZero(SimpleEquationCase.getBody().countRows(), (int) SimpleEquationCase.getBody().countColumns()).mergeColumns(SimpleEquationCase.getBody()).mergeColumns(SimpleEquationCase.getBody());
final LU<Double> tmpDoubleDecomp = new RawLU();
tmpDoubleDecomp.decompose(PrimitiveDenseStore.FACTORY.copy(tmpMtrxA));
// System.out.println("A: " + tmpMtrxA.enforce(tmpEvalContext));
// System.out.println("P: " + new PrimitiveMatrix(tmpDoubleDecomp.getP()).enforce(tmpEvalContext));
// System.out.println("L: " + new PrimitiveMatrix(tmpDoubleDecomp.getL()).enforce(tmpEvalContext));
// System.out.println("PL: " + new PrimitiveMatrix(tmpDoubleDecomp.getP().multiplyRight(tmpDoubleDecomp.getL())).enforce(tmpEvalContext));
// System.out.println("D: " + new PrimitiveMatrix(tmpDoubleDecomp.getD()).enforce(tmpEvalContext));
// System.out.println("U: " + new PrimitiveMatrix(tmpDoubleDecomp.getU()).enforce(tmpEvalContext));
// System.out.println("DU: " + new PrimitiveMatrix(tmpDoubleDecomp.getD().multiplyRight(tmpDoubleDecomp.getU())).enforce(tmpEvalContext));
TestUtils.assertEquals(PrimitiveDenseStore.FACTORY.copy(tmpMtrxA), tmpDoubleDecomp, tmpEvalContext);
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class DesignCase method testWikipediaNullspace.
/**
* http://en.wikipedia.org/wiki/Kernel_%28matrix%29
*/
@Test
public void testWikipediaNullspace() {
final PhysicalStore<Double> tmpA = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 2, 3, 5 }, { -4, 2, 3 } });
final QR<Double> tmpQR = QR.PRIMITIVE.make(tmpA);
tmpQR.setFullSize(true);
tmpQR.decompose(tmpA.transpose());
final SingularValue<Double> tmpSVD = new SingularValueDecomposition.Primitive();
// Supports full size
tmpSVD.setFullSize(true);
tmpSVD.decompose(tmpA);
final PhysicalStore<Double> tmpNullspaceQR = tmpQR.getQ().logical().offsets(0, tmpQR.getRank()).get().copy();
final PhysicalStore<Double> tmpNullspaceSVD = tmpSVD.getQ2().logical().offsets(0, tmpSVD.getRank()).get().copy();
final double tmpScaleQR = PrimitiveFunction.ABS.invoke(tmpNullspaceQR.doubleValue(0));
tmpNullspaceQR.modifyAll(PrimitiveFunction.DIVIDE.second(tmpScaleQR));
final double tmpScaleSVD = PrimitiveFunction.ABS.invoke(tmpNullspaceSVD.doubleValue(0));
tmpNullspaceSVD.modifyAll(PrimitiveFunction.DIVIDE.second(tmpScaleSVD));
final PrimitiveDenseStore tmpExpected = PrimitiveDenseStore.FACTORY.columns(new double[] { -1, -26, 16 });
final NumberContext tmpPrecision = new NumberContext(14, 8);
TestUtils.assertEquals(tmpExpected, tmpNullspaceQR, tmpPrecision);
TestUtils.assertEquals(tmpExpected, tmpNullspaceSVD, tmpPrecision);
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class DesignCase method testWikipediaSVD.
/**
* http://en.wikipedia.org/wiki/Singular_value_decomposition
*/
@Test
public void testWikipediaSVD() {
final PhysicalStore<Double> tmpOriginalMatrix = 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 } });
Array1D.PRIMITIVE64.copy(new double[] { 4.0, 3.0, PrimitiveFunction.SQRT.invoke(5.0), 0.0 });
final SingularValue<Double> tmpOldDecomp = new SingularValueDecomposition.Primitive();
tmpOldDecomp.decompose(tmpOriginalMatrix);
tmpOldDecomp.getD();
tmpOldDecomp.getQ1();
tmpOldDecomp.getQ2();
final SingularValue<Double> tmpNewDecomp = new RawSingularValue();
tmpNewDecomp.decompose(tmpOriginalMatrix);
tmpNewDecomp.getD();
tmpNewDecomp.getQ1();
tmpNewDecomp.getQ2();
TestUtils.assertEquals(tmpOriginalMatrix, tmpNewDecomp, new NumberContext(7, 6));
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class EigenvalueTest method testP20061119Case.
@Test
public void testP20061119Case() {
final PhysicalStore<Double> tmpOriginalMatrix = PrimitiveDenseStore.FACTORY.copy(P20061119Case.getProblematic());
final ComplexNumber tmp00 = ComplexNumber.valueOf(26.14421883828456);
final ComplexNumber tmp11 = ComplexNumber.of(2.727890580857718, 3.6223578444417908);
final ComplexNumber tmp22 = tmp11.conjugate();
final ComplexNumber tmp33 = ComplexNumber.ZERO;
final ComplexNumber tmp44 = tmp33;
final Array1D<ComplexNumber> tmpExpectedDiagonal = Array1D.COMPLEX.copy(new ComplexNumber[] { tmp00, tmp11, tmp22, tmp33, tmp44 });
final NumberContext accuracyContext = new NumberContext(7, 6);
MatrixStore<Double> tmpRecreatedMatrix;
final Eigenvalue<Double> tmpDecomposition = Eigenvalue.PRIMITIVE.make(tmpOriginalMatrix);
tmpDecomposition.decompose(tmpOriginalMatrix);
final Array1D<ComplexNumber> tmpEigenvalues = tmpDecomposition.getEigenvalues();
final MatrixStore<Double> tmpD = tmpDecomposition.getD();
final MatrixStore<Double> tmpV = tmpDecomposition.getV();
if (MatrixDecompositionTests.DEBUG) {
BasicLogger.debug("Eigenvalues = {}", tmpEigenvalues);
BasicLogger.debug("D = {}", tmpD);
BasicLogger.debug("V = {}", tmpV);
}
tmpRecreatedMatrix = tmpV.multiply(tmpDecomposition.getD()).multiply(tmpV.transpose());
if (MatrixDecompositionTests.DEBUG) {
BasicLogger.debug("Original = {}", tmpOriginalMatrix);
BasicLogger.debug("Recreated = {}", tmpRecreatedMatrix);
}
TestUtils.assertEquals(tmpOriginalMatrix.multiply(tmpV), tmpV.multiply(tmpDecomposition.getD()), accuracyContext);
tmpExpectedDiagonal.sortDescending();
tmpEigenvalues.sortDescending();
TestUtils.assertEquals(tmpExpectedDiagonal, tmpEigenvalues, accuracyContext);
tmpDecomposition.computeValuesOnly(tmpOriginalMatrix);
final Array1D<ComplexNumber> tmpEigenvaluesOnly = tmpDecomposition.getEigenvalues();
TestUtils.assertEquals(tmpExpectedDiagonal, tmpEigenvaluesOnly, accuracyContext);
}
Aggregations