本文整理汇总了Java中org.apache.commons.math3.linear.Array2DRowRealMatrix.setEntry方法的典型用法代码示例。如果您正苦于以下问题:Java Array2DRowRealMatrix.setEntry方法的具体用法?Java Array2DRowRealMatrix.setEntry怎么用?Java Array2DRowRealMatrix.setEntry使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.linear.Array2DRowRealMatrix
的用法示例。
在下文中一共展示了Array2DRowRealMatrix.setEntry方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computePriorProbability
import org.apache.commons.math3.linear.Array2DRowRealMatrix; //导入方法依赖的package包/类
@Override
public PriorProbability computePriorProbability() {
List<String> groupNames = new ArrayList<>(table.getGroupsNames());
int groupCount = groupNames.size();
allMaskValues = new ArrayList<>(table.getTable().keySet());
this.observedFrequencies = new ArrayRealVector(allMaskValues.size());
for (int i = 0; i < allMaskValues.size(); i++) {
BigDecimal maskFrequency = maskToFrequency.get(allMaskValues.get(i));
if (maskFrequency == null) maskFrequency = BigDecimal.ZERO;
this.observedFrequencies.setEntry(i, maskFrequency.doubleValue());
}
observedFrequencies = normalizeVector(observedFrequencies);
// TODO normalize
maskProbabilitiesForSources = new Array2DRowRealMatrix(allMaskValues.size(), groupCount);
for (int i = 0; i < allMaskValues.size(); i++) {
ClassificationRow row = table.getTable().get(allMaskValues.get(i));
// TODO NO row.normalize();
for (String groupName : table.getGroupsNames()) {
int groupIndex = groupNames.indexOf(groupName);
BigDecimal maskGroupProbability = row.getValues().get(groupName);
if (maskGroupProbability == null) {
// impossible mask
maskGroupProbability = BigDecimal.ZERO;
}
maskProbabilitiesForSources.setEntry(i, groupIndex, maskGroupProbability.doubleValue());
}
}
double[] startingPoints = new double[groupCount];
for (int i = 0; i < groupCount; i++) startingPoints[i] = 1d / groupCount;
double[] targetDistances = new double[allMaskValues.size()];
for (int i = 0; i < allMaskValues.size(); i++) targetDistances[i] = 0d;
LeastSquaresProblem problem = new LeastSquaresBuilder()
.start(startingPoints)
.model(distanceToRealDataset)
.target(targetDistances)
.parameterValidator(LeastSquaresFitPriorProbabilityEstimator::zeroNegativeElements)//realVector -> normalizeVector(positifyVector(realVector)))//LeastSquaresFitPriorProbabilityEstimator::normalizeVector)
.maxEvaluations(10000)
.maxIterations(10000)
.build();
LeastSquaresOptimizer optimizer = new LevenbergMarquardtOptimizer().
withCostRelativeTolerance(1.0e-12).
withParameterRelativeTolerance(1.0e-12);
//optimizer = new GaussNewtonOptimizer();//.withDecomposition(GaussNewtonOptimizer.Decomposition.QR);
LeastSquaresOptimizer.Optimum optimum = optimizer.optimize(problem);
if (optimum.getPoint().getDimension() != groupCount) {
throw new IllegalStateException("Incorrect solution size");
}
//System.out.println(optimum.getResiduals());
System.out.println(optimum.getPoint());
PriorProbability priorProbability = new PriorProbability();
for (int i = 0; i < groupCount; i++) {
priorProbability.put(groupNames.get(i), BigDecimal.valueOf(optimum.getPoint().getEntry(i)));
}
return priorProbability;
}
示例2: initializeHighOrderDerivatives
import org.apache.commons.math3.linear.Array2DRowRealMatrix; //导入方法依赖的package包/类
/** Initialize the high order scaled derivatives at step start.
* @param h step size to use for scaling
* @param t first steps times
* @param y first steps states
* @param yDot first steps derivatives
* @return Nordieck vector at start of first step (h<sup>2</sup>/2 y''<sub>n</sub>,
* h<sup>3</sup>/6 y'''<sub>n</sub> ... h<sup>k</sup>/k! y<sup>(k)</sup><sub>n</sub>)
*/
public Array2DRowRealMatrix initializeHighOrderDerivatives(final double h, final double[] t,
final double[][] y,
final double[][] yDot) {
// using Taylor series with di = ti - t0, we get:
// y(ti) - y(t0) - di y'(t0) = di^2 / h^2 s2 + ... + di^k / h^k sk + O(h^k)
// y'(ti) - y'(t0) = 2 di / h^2 s2 + ... + k di^(k-1) / h^k sk + O(h^(k-1))
// we write these relations for i = 1 to i= 1+n/2 as a set of n + 2 linear
// equations depending on the Nordsieck vector [s2 ... sk rk], so s2 to sk correspond
// to the appropriately truncated Taylor expansion, and rk is the Taylor remainder.
// The goal is to have s2 to sk as accurate as possible considering the fact the sum is
// truncated and we don't want the error terms to be included in s2 ... sk, so we need
// to solve also for the remainder
final double[][] a = new double[c1.length + 1][c1.length + 1];
final double[][] b = new double[c1.length + 1][y[0].length];
final double[] y0 = y[0];
final double[] yDot0 = yDot[0];
for (int i = 1; i < y.length; ++i) {
final double di = t[i] - t[0];
final double ratio = di / h;
double dikM1Ohk = 1 / h;
// linear coefficients of equations
// y(ti) - y(t0) - di y'(t0) and y'(ti) - y'(t0)
final double[] aI = a[2 * i - 2];
final double[] aDotI = (2 * i - 1) < a.length ? a[2 * i - 1] : null;
for (int j = 0; j < aI.length; ++j) {
dikM1Ohk *= ratio;
aI[j] = di * dikM1Ohk;
if (aDotI != null) {
aDotI[j] = (j + 2) * dikM1Ohk;
}
}
// expected value of the previous equations
final double[] yI = y[i];
final double[] yDotI = yDot[i];
final double[] bI = b[2 * i - 2];
final double[] bDotI = (2 * i - 1) < b.length ? b[2 * i - 1] : null;
for (int j = 0; j < yI.length; ++j) {
bI[j] = yI[j] - y0[j] - di * yDot0[j];
if (bDotI != null) {
bDotI[j] = yDotI[j] - yDot0[j];
}
}
}
// solve the linear system to get the best estimate of the Nordsieck vector [s2 ... sk],
// with the additional terms s(k+1) and c grabbing the parts after the truncated Taylor expansion
final QRDecomposition decomposition = new QRDecomposition(new Array2DRowRealMatrix(a, false));
final RealMatrix x = decomposition.getSolver().solve(new Array2DRowRealMatrix(b, false));
// extract just the Nordsieck vector [s2 ... sk]
final Array2DRowRealMatrix truncatedX = new Array2DRowRealMatrix(x.getRowDimension() - 1, x.getColumnDimension());
for (int i = 0; i < truncatedX.getRowDimension(); ++i) {
for (int j = 0; j < truncatedX.getColumnDimension(); ++j) {
truncatedX.setEntry(i, j, x.getEntry(i, j));
}
}
return truncatedX;
}
示例3: createTableau
import org.apache.commons.math3.linear.Array2DRowRealMatrix; //导入方法依赖的package包/类
/**
* Create the tableau by itself.
* @param maximize if true, goal is to maximize the objective function
* @return created tableau
*/
protected RealMatrix createTableau(final boolean maximize) {
// create a matrix of the correct size
int width = numDecisionVariables + numSlackVariables +
numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
int height = constraints.size() + getNumObjectiveFunctions();
Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);
// initialize the objective function rows
if (getNumObjectiveFunctions() == 2) {
matrix.setEntry(0, 0, -1);
}
int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
RealVector objectiveCoefficients =
maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
matrix.setEntry(zIndex, width - 1,
maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());
if (!restrictToNonNegative) {
matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
getInvertedCoefficientSum(objectiveCoefficients));
}
// initialize the constraint rows
int slackVar = 0;
int artificialVar = 0;
for (int i = 0; i < constraints.size(); i++) {
LinearConstraint constraint = constraints.get(i);
int row = getNumObjectiveFunctions() + i;
// decision variable coefficients
copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);
// x-
if (!restrictToNonNegative) {
matrix.setEntry(row, getSlackVariableOffset() - 1,
getInvertedCoefficientSum(constraint.getCoefficients()));
}
// RHS
matrix.setEntry(row, width - 1, constraint.getValue());
// slack variables
if (constraint.getRelationship() == Relationship.LEQ) {
matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1); // slack
} else if (constraint.getRelationship() == Relationship.GEQ) {
matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
}
// artificial variables
if ((constraint.getRelationship() == Relationship.EQ) ||
(constraint.getRelationship() == Relationship.GEQ)) {
matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
}
}
return matrix;
}
示例4: createTableau
import org.apache.commons.math3.linear.Array2DRowRealMatrix; //导入方法依赖的package包/类
/**
* Create the tableau by itself.
* @param maximize if true, goal is to maximize the objective function
* @return created tableau
*/
protected Array2DRowRealMatrix createTableau(final boolean maximize) {
// create a matrix of the correct size
int width = numDecisionVariables + numSlackVariables +
numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS
int height = constraints.size() + getNumObjectiveFunctions();
Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width);
// initialize the objective function rows
if (getNumObjectiveFunctions() == 2) {
matrix.setEntry(0, 0, -1);
}
int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1;
matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1);
RealVector objectiveCoefficients = maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients();
copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]);
matrix.setEntry(zIndex, width - 1, maximize ? f.getConstantTerm() : -1 * f.getConstantTerm());
if (!restrictToNonNegative) {
matrix.setEntry(zIndex, getSlackVariableOffset() - 1,
getInvertedCoefficientSum(objectiveCoefficients));
}
// initialize the constraint rows
int slackVar = 0;
int artificialVar = 0;
for (int i = 0; i < constraints.size(); i++) {
LinearConstraint constraint = constraints.get(i);
int row = getNumObjectiveFunctions() + i;
// decision variable coefficients
copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]);
// x-
if (!restrictToNonNegative) {
matrix.setEntry(row, getSlackVariableOffset() - 1,
getInvertedCoefficientSum(constraint.getCoefficients()));
}
// RHS
matrix.setEntry(row, width - 1, constraint.getValue());
// slack variables
if (constraint.getRelationship() == Relationship.LEQ) {
matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1); // slack
} else if (constraint.getRelationship() == Relationship.GEQ) {
matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess
}
// artificial variables
if ((constraint.getRelationship() == Relationship.EQ) ||
(constraint.getRelationship() == Relationship.GEQ)) {
matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1);
matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1);
matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
}
}
return matrix;
}