use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class LinearDesignTestCases method testDuality.
/**
* http://web.mit.edu/15.053/www/AMP-Chapter-04.pdf
* https://web.fe.up.pt/~mac/ensino/docs/OT20122013/Chapter%204%20-%20Duality%20in%20Linear%20Programming.pdf
*/
@Test
public void testDuality() {
final Factory<Double, PrimitiveDenseStore> factory = PrimitiveDenseStore.FACTORY;
final PrimitiveDenseStore expPrimSol = factory.rows(new double[] { 36.0, 0.0, 6.0 });
final PrimitiveDenseStore expDualSol = factory.rows(new double[] { 11.0, 0.5 });
final double expOptVal = 294.0;
final LinearSolver.Builder primal = LinearSolver.getBuilder();
// Negated since actual problem is max and algorithm expects min
final PrimitiveDenseStore pC = factory.makeZero(5, 1);
pC.set(0, -6.0);
pC.set(1, -14.0);
pC.set(2, -13.0);
pC.set(3, 0);
pC.set(4, 0);
primal.objective(pC);
final PrimitiveDenseStore pAE = factory.makeZero(2, 5);
pAE.set(0, 0, 0.5);
pAE.set(0, 1, 2.0);
pAE.set(0, 2, 1.0);
pAE.set(0, 3, 1.0);
pAE.set(0, 4, 0.0);
pAE.set(1, 0, 1.0);
pAE.set(1, 1, 2.0);
pAE.set(1, 2, 4.0);
pAE.set(1, 3, 0.0);
pAE.set(1, 4, 1.0);
final PrimitiveDenseStore pBE = factory.makeZero(2, 1);
pBE.set(0, 24.0);
pBE.set(1, 60.0);
primal.equalities(pAE, pBE);
final LinearSolver primalSolver = primal.build();
// primalSolver.options.debug(LinearSolver.class);
final Result pRes = primalSolver.solve();
final Access1D<?> pMultipliers = factory.columns(pRes.getMultipliers().get());
TestUtils.assertStateNotLessThanOptimal(pRes);
// Negated since actual problem is max and algorithm expects min
TestUtils.assertEquals(expOptVal, -pRes.getValue());
for (int i = 0; i < expPrimSol.count(); i++) {
TestUtils.assertEquals(expPrimSol.doubleValue(i), pRes.doubleValue(i));
}
for (int i = 0; i < expDualSol.count(); i++) {
// Negated since actual problem is max and algorithm expects min
TestUtils.assertEquals(expDualSol.doubleValue(i), -pMultipliers.doubleValue(i));
}
final LinearSolver.Builder dual = LinearSolver.getBuilder();
final PrimitiveDenseStore dC = factory.makeZero(5, 1);
dC.set(0, 24.0);
dC.set(1, 60.0);
dC.set(2, 0.0);
dC.set(3, 0.0);
dC.set(4, 0.0);
dual.objective(dC);
final PrimitiveDenseStore dAE = factory.makeZero(3, 5);
dAE.set(0, 0, 0.5);
dAE.set(0, 1, 1.0);
dAE.set(0, 2, -1.0);
dAE.set(0, 3, 0.0);
dAE.set(0, 4, 0.0);
dAE.set(1, 0, 2.0);
dAE.set(1, 1, 2.0);
dAE.set(1, 2, 0.0);
dAE.set(1, 3, -1.0);
dAE.set(1, 4, 0.0);
dAE.set(2, 0, 1.0);
dAE.set(2, 1, 4.0);
dAE.set(2, 2, 0.0);
dAE.set(2, 3, 0.0);
dAE.set(2, 4, -1.0);
final PrimitiveDenseStore dBE = factory.makeZero(3, 1);
dBE.set(0, 6.0);
dBE.set(1, 14.0);
dBE.set(2, 13.0);
dual.equalities(dAE, dBE);
final LinearSolver dualSolver = dual.build();
// dualSolver.options.debug(LinearSolver.class);
final Result dRes = dualSolver.solve();
final Access1D<?> dMultipliers = factory.columns(dRes.getMultipliers().get());
TestUtils.assertStateNotLessThanOptimal(dRes);
TestUtils.assertEquals(expOptVal, dRes.getValue());
for (int i = 0; i < expDualSol.count(); i++) {
TestUtils.assertEquals(expDualSol.doubleValue(i), dRes.doubleValue(i));
}
for (int i = 0; i < expPrimSol.count(); i++) {
TestUtils.assertEquals(expPrimSol.doubleValue(i), dMultipliers.doubleValue(i));
}
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore 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.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class EigenvalueTest method testPrimitiveAsComplex.
@Test
public void testPrimitiveAsComplex() {
final double[][] tmpData = new double[][] { { 1, 0, 3 }, { 0, 4, 1 }, { -5, 1, 0 } };
final PrimitiveDenseStore tmpA = PrimitiveDenseStore.FACTORY.rows(tmpData);
final int tmpLength = tmpData.length;
final Eigenvalue<Double> tmpEvD = Eigenvalue.PRIMITIVE.make(tmpA, false);
tmpEvD.decompose(tmpA);
final MatrixStore<Double> tmpD = tmpEvD.getD();
final MatrixStore<Double> tmpV = tmpEvD.getV();
final Array1D<ComplexNumber> tmpValues = tmpEvD.getEigenvalues();
final MatrixStore<ComplexNumber> tmpVectors = tmpEvD.getEigenvectors();
final ComplexDenseStore tmpCmplA = ComplexDenseStore.FACTORY.copy(tmpA);
final ComplexDenseStore tmpCmplD = ComplexDenseStore.FACTORY.copy(tmpD);
final ComplexDenseStore tmpCmplV = ComplexDenseStore.FACTORY.copy(tmpV);
final MatrixStore<ComplexNumber> tmpExp1 = tmpCmplA.multiply(tmpCmplV);
final MatrixStore<ComplexNumber> tmpAct1 = tmpCmplV.multiply(tmpCmplD);
TestUtils.assertEquals(tmpExp1, tmpAct1);
final ComplexDenseStore tmpAltD = ComplexDenseStore.FACTORY.makeZero(tmpLength, tmpLength);
final MatrixStore<ComplexNumber> tmpAltV = tmpVectors;
for (int j = 0; j < tmpLength; j++) {
tmpAltD.set(j, j, tmpValues.get(j));
}
final MatrixStore<ComplexNumber> tmpExp2 = tmpCmplA.multiply(tmpAltV);
final MatrixStore<ComplexNumber> tmpAct2 = tmpAltV.multiply(tmpAltD);
TestUtils.assertEquals(tmpExp2, tmpAct2);
tmpEvD.computeValuesOnly(tmpA);
final Array1D<ComplexNumber> tmpEigenvaluesOnly = tmpEvD.getEigenvalues();
TestUtils.assertEquals(tmpValues, tmpEigenvaluesOnly);
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class ExtremeElementsCase method doTestSolve.
static void doTestSolve(final boolean large) {
for (int precision = 1; precision <= 16; precision++) {
final NumberContext tmpContext = NumberContext.getGeneral(precision, Integer.MIN_VALUE);
for (int dim = 1; dim <= 10; dim++) {
// exp = 308 could potentially create numbers that are 2E308 which is larger than Double.MAX_VALUE
for (int exp = 0; exp < 308; exp++) {
final double tmpScale = PrimitiveFunction.POWER.invoke(PrimitiveMath.TEN, large ? exp : -exp);
final PrimitiveDenseStore tmpBody = MatrixUtils.makeSPD(dim);
final PrimitiveDenseStore tmpRHS = PrimitiveDenseStore.FACTORY.makeFilled(dim, 1, new Uniform());
if (DEBUG) {
BasicLogger.debug("Scale exp={} => factor={} and context={}", exp, tmpScale, tmpContext);
BasicLogger.debug("Body (unscaled) {}", tmpBody.toString());
BasicLogger.debug("RHS (unscaled) {}", tmpRHS.toString());
}
final UnaryFunction<Double> tmpModifier = PrimitiveFunction.MULTIPLY.second(tmpScale);
tmpBody.modifyAll(tmpModifier);
tmpRHS.modifyAll(tmpModifier);
ExtremeElementsCase.performSolveTest(tmpBody, tmpRHS, SolverTask.PRIMITIVE.make(tmpBody, tmpRHS), tmpContext);
final List<MatrixDecomposition<Double>> tmpAllDecomps = MatrixDecompositionTests.getAllPrimitive();
for (final MatrixDecomposition<Double> tmpDecomp : tmpAllDecomps) {
if (DEBUG) {
BasicLogger.debug("{} at dim={} for scale={}", tmpDecomp.getClass(), dim, tmpScale);
}
if (tmpDecomp instanceof MatrixDecomposition.Solver) {
ExtremeElementsCase.performSolveTest(tmpBody, tmpRHS, (SolverTask<Double>) tmpDecomp, tmpContext);
}
}
}
}
}
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class ExtremeElementsCase method testSolveLU_1_16_1.
@Test
public void testSolveLU_1_16_1() {
final PrimitiveDenseStore tmpBody = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.7259687987824925 } });
final PrimitiveDenseStore tmpRHS = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.6533251061005759 } });
final UnaryFunction<Double> tmpSecond = PrimitiveFunction.MULTIPLY.second(PrimitiveFunction.POWER.invoke(PrimitiveMath.TEN, -16));
tmpBody.modifyAll(tmpSecond);
tmpRHS.modifyAll(tmpSecond);
final SolverTask<Double> tmpAlgorithm = new LUDecomposition.Primitive();
final NumberContext tmpContext = NumberContext.getGeneral(1, Integer.MIN_VALUE);
ExtremeElementsCase.performSolveTest(tmpBody, tmpRHS, tmpAlgorithm, tmpContext);
}
Aggregations