use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class GaussianField method getDistribution.
public Normal1D getDistribution(final boolean cleanCovariances, final K... evaluationPoint) {
final MatrixStore<Double> tmpRegCoef = this.getRegressionCoefficients(evaluationPoint);
final MatrixStore<Double> tmpM1 = this.getM1(evaluationPoint);
final MatrixStore<Double> tmpM2differenses = this.getM2differenses();
final PrimitiveDenseStore tmpLocations = FACTORY.makeZero(tmpM1.countRows(), tmpM1.countColumns());
tmpLocations.fillMatching(tmpM1, ADD, tmpRegCoef.multiply(tmpM2differenses));
final MatrixStore<Double> tmpC11 = this.getC11(evaluationPoint);
final MatrixStore<Double> tmpC21 = this.getC21(evaluationPoint);
final PrimitiveDenseStore tmpCovariances = FACTORY.makeZero(tmpC11.countRows(), tmpC11.countColumns());
tmpCovariances.fillMatching(tmpC11, SUBTRACT, tmpRegCoef.multiply(tmpC21));
if (cleanCovariances) {
final Eigenvalue<Double> tmpEvD = Eigenvalue.PRIMITIVE.make(true);
tmpEvD.decompose(tmpCovariances);
final MatrixStore<Double> tmpV = tmpEvD.getV();
final PhysicalStore<Double> tmpD = tmpEvD.getD().copy();
final double tmpLargest = tmpD.doubleValue(0, 0);
final double tmpLimit = PrimitiveFunction.MAX.invoke(PrimitiveMath.MACHINE_EPSILON * tmpLargest, 1E-12);
final int tmpLength = (int) Math.min(tmpD.countRows(), tmpD.countColumns());
for (int ij = 0; ij < tmpLength; ij++) {
if (tmpD.doubleValue(ij, ij) < tmpLimit) {
tmpD.set(ij, ij, tmpLimit);
}
}
tmpCovariances.fillMatching(tmpV.multiply(tmpD).multiply(tmpV.logical().transpose().get()));
}
return new Normal1D(tmpLocations, tmpCovariances);
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class GaussianField method getC21.
MatrixStore<Double> getC21(final K[] args) {
final List<ComparableToDouble<K>> tmpObservations = this.getObservations();
final int tmpRowDim = tmpObservations.size();
final int tmpColDim = args.length;
final PrimitiveDenseStore retVal = FACTORY.makeZero(tmpRowDim, tmpColDim);
for (int j = 0; j < tmpColDim; j++) {
for (int i = 0; i < tmpRowDim; i++) {
retVal.set(i, j, myCovarianceFunction.invoke(tmpObservations.get(i).key, args[j]));
}
}
return retVal;
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class GaussianField method getM2.
MatrixStore<Double> getM2() {
final List<ComparableToDouble<K>> tmpObservations = this.getObservations();
final int tmpSize = tmpObservations.size();
final PrimitiveDenseStore retVal = FACTORY.makeZero(tmpSize, 1);
for (int i = 0; i < tmpSize; i++) {
retVal.set(i, 0, myMeanFunction.invoke(tmpObservations.get(i).key));
}
return retVal;
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class AbstractSolver method leastSquares.
static void leastSquares(final Access2D<?> body, final Access1D<?> rhs, final PhysicalStore<?> solution) {
final PrimitiveDenseStore tmpTranspBody = PrimitiveDenseStore.FACTORY.transpose(body);
final int tmpCountRows = (int) tmpTranspBody.countRows();
final PrimitiveDenseStore tmpBody = PrimitiveDenseStore.FACTORY.makeZero(tmpCountRows, tmpCountRows);
tmpTranspBody.multiply(tmpTranspBody.transpose(), tmpBody);
final PrimitiveDenseStore tmpRHS = PrimitiveDenseStore.FACTORY.makeZero(tmpCountRows, solution.countColumns());
tmpTranspBody.multiply((Access1D<Double>) rhs, tmpRHS);
switch(tmpCountRows) {
case 1:
AbstractSolver.full1X1(tmpBody, tmpRHS, solution);
break;
case 2:
AbstractSolver.symmetric2X2(tmpBody, tmpRHS, solution);
break;
case 3:
AbstractSolver.symmetric3X3(tmpBody, tmpRHS, solution);
break;
case 4:
AbstractSolver.symmetric4X4(tmpBody, tmpRHS, solution);
break;
case 5:
AbstractSolver.symmetric5X5(tmpBody, tmpRHS, solution);
break;
default:
throw new IllegalArgumentException();
}
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class ConvexProblems method testP20081014.
/**
* <p>
* I'm trying to solve some quadratic programming systems using version 24. The ActiveSetSolver does not
* always converge to a solution, but throws an exception, "Matrix is singular" (The exception is thrown
* by org.ojalgo.matrix.jama.LUDecomposition). The thing is that if I run Matlabs quadprog method on the
* exact same system, a solution is found without problems. Here is the code that produces the exception:
* </p>
* <p>
* 2015-02-21: Extended the test case with a few alternatives using ExpressionsBasedModel. Numerically
* difficult problem as the formulation includes both large and very small parameters (like 1000000000 and
* -7.646043242556307E-15).
* </p>
*/
@Test
public void testP20081014() {
final PhysicalStore.Factory<Double, PrimitiveDenseStore> tmpFactory = PrimitiveDenseStore.FACTORY;
final PrimitiveDenseStore[] tmpSystem = new PrimitiveDenseStore[6];
// {[AE], [BE], [Q], [C], [AI], [BI]}
tmpSystem[0] = tmpFactory.rows(new double[][] { { -0.0729971273939726, -0.31619624199405116, -0.14365990081105298, -3.4914813388431334E-15, 0.9963066090106673, 0.9989967493404447, 1.0, 0.0, 0.0 }, { -2.5486810808521023E-16, 3.6687950405257466, 3.2047109656515507, 1.0, 0.08586699506600544, 0.04478275122437895, 0.0, 1.0, 0.0 }, // AE
{ -7.646043242556307E-15, -107.21808503782593, -97.434268076846, 30.0, -11.54276933307617, 7.647488207332634, 0.0, 0, 1.0 } });
// BE
tmpSystem[1] = tmpFactory.rows(new double[][] { { 10.461669614447484 }, { -0.5328532701990767 }, { 15.782527136201711 } });
final PrimitiveDenseStore tmpQ = tmpFactory.makeEye(9, 9);
tmpQ.set(3, 3, 10);
tmpQ.set(4, 4, 10);
tmpQ.set(5, 5, 10);
tmpQ.set(6, 6, 1000000000);
tmpQ.set(7, 7, 1000000000);
tmpQ.set(8, 8, 1000000000);
// Q
tmpSystem[2] = tmpQ;
// C
tmpSystem[3] = tmpFactory.rows(new double[][] { { 0 }, { 0 }, { 0 }, { -1 }, { -1 }, { -1 }, { 0 }, { 0 }, { 0 } });
final double[][] tmpAI = new double[18][9];
for (int i = 0; i < 9; i++) {
tmpAI[i][i] = 1;
tmpAI[i + 9][i] = -1;
}
// AI
tmpSystem[4] = tmpFactory.rows(tmpAI);
tmpSystem[5] = tmpFactory.rows(new double[][] { { 0 }, { 0.0175 }, { 0.0175 }, { 5 }, { 5 }, { 5 }, { 100000 }, { 100000 }, { 100000 }, { 0 }, { 0.0175 }, { 0.0175 }, { 5 }, { 5 }, { 5 }, { 100000 }, { 100000 }, // BI
{ 100000 } });
final PrimitiveDenseStore tmpMatlabSolution = tmpFactory.columns(new double[] { 0.00000000000000, -0.01750000000000, -0.01750000000000, 0.88830035195990, 4.56989525276369, 5.00000000000000, 0.90562154243124, -1.91718419629399, 0.06390614020590 });
// Compare to MatLab using 3 digits and 6 decimal places
final NumberContext tmpAccuracy = NumberContext.getGeneral(3, 6);
ConvexProblems.builAndTestModel(tmpSystem, tmpMatlabSolution, tmpAccuracy, false);
}
Aggregations