本文整理汇总了Java中org.apache.commons.math.linear.Array2DRowRealMatrix类的典型用法代码示例。如果您正苦于以下问题:Java Array2DRowRealMatrix类的具体用法?Java Array2DRowRealMatrix怎么用?Java Array2DRowRealMatrix使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Array2DRowRealMatrix类属于org.apache.commons.math.linear包,在下文中一共展示了Array2DRowRealMatrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPower
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
/**
* Returns a real matrix raised to some real power
* Currently this method is limited to symmetric matrices only as Commons Math does not support the diagonalization of asymmetric matrices
* @param m The <strong>symmetric</strong> matrix to take the power of.
* @param p The power to raise to matrix to
* @return The result
*/
@Override
public DoubleMatrix2D getPower(final Matrix<?> m, final double p) {
if (m instanceof DoubleMatrix2D) {
final RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix2D) m);
final EigenDecomposition eigen = new EigenDecompositionImpl(temp, 0.0);
final double[] rEigenValues = eigen.getRealEigenvalues();
final double[] iEigenValues = eigen.getImagEigenvalues();
final int n = rEigenValues.length;
final double[][] d = new double[n][n];
for (int i = n - 1; i >= 0; --i) {
d[i][i] = Math.pow(rEigenValues[i], p);
if (iEigenValues[i] != 0.0) {
throw new NotImplementedException("Cannot handle complex eigenvalues in getPower");
}
}
final RealMatrix res = eigen.getV().multiply((new Array2DRowRealMatrix(d)).multiply(eigen.getVT()));
return CommonsMathWrapper.unwrap(res);
}
throw new IllegalArgumentException("Can only find pow of DoubleMatrix2D; have " + m.getClass());
}
示例2: computeUserFoldInMatrix
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
/**
* http://www.slideshare.net/fullscreen/srowen/matrix-factorization/16
* @param recentitemInteractions
* @param productFeaturesInverse
* @param idMap
* @return
*/
private double[][] computeUserFoldInMatrix(double[][] itemFactors)
{
try
{
RealMatrix Y = new Array2DRowRealMatrix(itemFactors);
RealMatrix YTY = Y.transpose().multiply(Y);
RealMatrix YTYInverse = new LUDecompositionImpl(YTY).getSolver().getInverse();
return Y.multiply(YTYInverse).getData();
}
catch (InvalidMatrixException e)
{
logger.warn("Failed to create inverse of products feature matrix",e);
return null;
}
}
示例3: testLeastSquares1
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
@Test
public void testLeastSquares1()
throws FunctionEvaluationException, ConvergenceException {
final RealMatrix factors =
new Array2DRowRealMatrix(new double[][] {
{ 1.0, 0.0 },
{ 0.0, 1.0 }
}, false);
LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
public double[] value(double[] variables) {
return factors.operate(variables);
}
}, new double[] { 2.0, -3.0 });
NelderMead optimizer = new NelderMead();
optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
optimizer.setMaxIterations(200);
RealPointValuePair optimum =
optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
assertEquals( 2.0, optimum.getPointRef()[0], 3.0e-5);
assertEquals(-3.0, optimum.getPointRef()[1], 4.0e-4);
assertTrue(optimizer.getEvaluations() > 60);
assertTrue(optimizer.getEvaluations() < 80);
assertTrue(optimum.getValue() < 1.0e-6);
}
示例4: testLeastSquares2
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
@Test
public void testLeastSquares2()
throws FunctionEvaluationException, ConvergenceException {
final RealMatrix factors =
new Array2DRowRealMatrix(new double[][] {
{ 1.0, 0.0 },
{ 0.0, 1.0 }
}, false);
LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
public double[] value(double[] variables) {
return factors.operate(variables);
}
}, new double[] { 2.0, -3.0 }, new double[] { 10.0, 0.1 });
NelderMead optimizer = new NelderMead();
optimizer.setConvergenceChecker(new SimpleScalarValueChecker(-1.0, 1.0e-6));
optimizer.setMaxIterations(200);
RealPointValuePair optimum =
optimizer.optimize(ls, GoalType.MINIMIZE, new double[] { 10.0, 10.0 });
assertEquals( 2.0, optimum.getPointRef()[0], 5.0e-5);
assertEquals(-3.0, optimum.getPointRef()[1], 8.0e-4);
assertTrue(optimizer.getEvaluations() > 60);
assertTrue(optimizer.getEvaluations() < 80);
assertTrue(optimum.getValue() < 1.0e-6);
}
示例5: testLeastSquares1
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
@Test
public void testLeastSquares1() {
final RealMatrix factors =
new Array2DRowRealMatrix(new double[][] {
{ 1, 0 },
{ 0, 1 }
}, false);
LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
public double[] value(double[] variables) {
return factors.operate(variables);
}
}, new double[] { 2.0, -3.0 });
SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
optimizer.setSimplex(new NelderMeadSimplex(2));
RealPointValuePair optimum =
optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
assertEquals( 2, optimum.getPointRef()[0], 3e-5);
assertEquals(-3, optimum.getPointRef()[1], 4e-4);
assertTrue(optimizer.getEvaluations() > 60);
assertTrue(optimizer.getEvaluations() < 80);
assertTrue(optimum.getValue() < 1.0e-6);
}
示例6: testLeastSquares2
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
@Test
public void testLeastSquares2() {
final RealMatrix factors =
new Array2DRowRealMatrix(new double[][] {
{ 1, 0 },
{ 0, 1 }
}, false);
LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
public double[] value(double[] variables) {
return factors.operate(variables);
}
}, new double[] { 2, -3 }, new double[] { 10, 0.1 });
SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
optimizer.setSimplex(new NelderMeadSimplex(2));
RealPointValuePair optimum =
optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
assertEquals( 2, optimum.getPointRef()[0], 5e-5);
assertEquals(-3, optimum.getPointRef()[1], 8e-4);
assertTrue(optimizer.getEvaluations() > 60);
assertTrue(optimizer.getEvaluations() < 80);
assertTrue(optimum.getValue() < 1e-6);
}
示例7: testLeastSquares3
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
@Test
public void testLeastSquares3() {
final RealMatrix factors =
new Array2DRowRealMatrix(new double[][] {
{ 1, 0 },
{ 0, 1 }
}, false);
LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorialFunction() {
public double[] value(double[] variables) {
return factors.operate(variables);
}
}, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
{ 1, 1.2 }, { 1.2, 2 }
}));
SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
optimizer.setSimplex(new NelderMeadSimplex(2));
RealPointValuePair optimum =
optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
assertEquals( 2, optimum.getPointRef()[0], 2e-3);
assertEquals(-3, optimum.getPointRef()[1], 8e-4);
assertTrue(optimizer.getEvaluations() > 60);
assertTrue(optimizer.getEvaluations() < 80);
assertTrue(optimum.getValue() < 1e-6);
}
示例8: calculateHat
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
/**
* <p>Compute the "hat" matrix.
* </p>
* <p>The hat matrix is defined in terms of the design matrix X
* by X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup>
* </p>
* <p>The implementation here uses the QR decomposition to compute the
* hat matrix as Q I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the
* p-dimensional identity matrix augmented by 0's. This computational
* formula is from "The Hat Matrix in Regression and ANOVA",
* David C. Hoaglin and Roy E. Welsch,
* <i>The American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp. 17-22.
*
* @return the hat matrix
*/
public RealMatrix calculateHat() {
// Create augmented identity matrix
RealMatrix Q = qr.getQ();
final int p = qr.getR().getColumnDimension();
final int n = Q.getColumnDimension();
Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n);
double[][] augIData = augI.getDataRef();
for (int i = 0; i < n; i++) {
for (int j =0; j < n; j++) {
if (i == j && i < p) {
augIData[i][j] = 1d;
} else {
augIData[i][j] = 0d;
}
}
}
// Compute and return Hat matrix
return Q.multiply(augI).multiply(Q.transpose());
}
示例9: discardArtificialVariables
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
/**
* Removes the phase 1 objective function and artificial variables from this tableau.
*/
protected void discardArtificialVariables() {
if (numArtificialVariables == 0) {
return;
}
int width = getWidth() - numArtificialVariables - 1;
int height = getHeight() - 1;
double[][] matrix = new double[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width - 1; j++) {
matrix[i][j] = getEntry(i + 1, j + 1);
}
matrix[i][width - 1] = getEntry(i + 1, getRhsOffset());
}
this.tableau = new Array2DRowRealMatrix(matrix);
this.numArtificialVariables = 0;
}
示例10: calculateSubtrahendForZIN
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
/**
* Calculates the subtrahend used to calculate the ZIN matrix.
* @return the subtrahend
*/
protected RealMatrix calculateSubtrahendForZIN() {
int matrixSize = numberOfCoefficients - 1;
double[][] subtrahendData = new double[matrixSize][matrixSize];
/* first column are the a coefficients (omitting the first one)
* multiplied by -1.
*/
for (int i = 0; i < matrixSize; i++) {
subtrahendData[i][0] = -1 * aCoefficients[i+1];
}
for (int row = 0; row < matrixSize-1; row++)
subtrahendData[row][row+1] = 1;
return new Array2DRowRealMatrix(subtrahendData);
}
示例11: SimplexTableau
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
/**
* Build a tableau for a linear problem.
* @param f linear objective function
* @param constraints linear constraints
* @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
* or {@link GoalType#MINIMIZE}
* @param restrictToNonNegative whether to restrict the variables to non-negative values
* @param epsilon amount of error to accept in floating point comparisons
*/
SimplexTableau(final LinearObjectiveFunction f,
final Collection<LinearConstraint> constraints,
final GoalType goalType, final boolean restrictToNonNegative,
final double epsilon) {
this.f = f;
this.constraints = constraints;
this.restrictToNonNegative = restrictToNonNegative;
this.epsilon = epsilon;
this.numDecisionVariables = getNumVariables() + (restrictToNonNegative ? 0 : 1);
this.numSlackVariables = getConstraintTypeCounts(Relationship.LEQ) +
getConstraintTypeCounts(Relationship.GEQ);
this.numArtificialVariables = getConstraintTypeCounts(Relationship.EQ) +
getConstraintTypeCounts(Relationship.GEQ);
this.tableau = new Array2DRowRealMatrix(createTableau(goalType == GoalType.MAXIMIZE));
initialize();
}
示例12: computeCorrelation
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
private static double[] computeCorrelation(List<Double> allX, List<Double> allY)
{
if (allX.size() < 2) {
return new double[] { Double.NaN, Double.NaN };
}
double[][] matrix = new double[allX.size()][];
for (int i = 0; i < allX.size(); i++) {
matrix[i] = new double[2];
matrix[i][0] = allX.get(i);
matrix[i][1] = allY.get(i);
}
PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation(matrix);
try {
double pValue = pearsonsCorrelation.getCorrelationPValues().getEntry(0, 1);
double correlation = pearsonsCorrelation.getCorrelationMatrix().getEntry(0, 1);
SpearmansCorrelation sc = new SpearmansCorrelation(new Array2DRowRealMatrix(matrix));
double[] result = new double[2];
double pValSC = sc.getRankCorrelation().getCorrelationPValues().getEntry(0, 1);
double corrSC = sc.getCorrelationMatrix().getEntry(0, 1);
result[0] = corrSC;
result[1] = pValSC;
return result;
}
catch (MathException e) {
throw new RuntimeException(e);
}
}
开发者ID:UKPLab,项目名称:argument-reasoning-comprehension-task,代码行数:36,代码来源:Step10bUpperBoundStatistics.java
示例13: wrapAsMatrix
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
/**
* @param x An OG 1-D vector of doubles, not null
* @return A Commons matrix
*/
public static RealMatrix wrapAsMatrix(final DoubleMatrix1D x) {
Validate.notNull(x);
final int n = x.getNumberOfElements();
final double[][] y = new double[n][1];
for (int i = 0; i < n; i++) {
y[i][0] = x.getEntry(i);
}
return new Array2DRowRealMatrix(x.getData());
}
示例14: runTest
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
private void runTest(double[][] X, double[][] Xstar, double[][] goldStandard,
boolean allowTranslation, boolean allowDilation, double tolerance, String header) {
RealMatrix rmX = new Array2DRowRealMatrix(X);
RealMatrix rmXstar = new Array2DRowRealMatrix(Xstar);
Procrustes procrustes = new Procrustes(rmX, rmXstar, allowTranslation, allowDilation);
RealMatrix rmXnew = procrustes.procrustinate(rmX);
System.out.println(header);
System.out.print("Translation:\n" + new Matrix(procrustes.getTranslation().getData()));
System.out.println("Dilation = " + procrustes.getDilation());
System.out.println("Xnew:");
System.out.println(new Matrix(rmXnew.getData()));
assertEquals(rmXnew.getData(), goldStandard, tolerance);
}
示例15: initializeHighOrderDerivatives
import org.apache.commons.math.linear.Array2DRowRealMatrix; //导入依赖的package包/类
/** Initialize the high order scaled derivatives at step start.
* @param first first scaled derivative at step start
* @param multistep scaled derivatives after step start (hy'1, ..., hy'k-1)
* will be modified
* @return high order derivatives at step start
*/
public Array2DRowRealMatrix initializeHighOrderDerivatives(final double[] first,
final double[][] multistep) {
for (int i = 0; i < multistep.length; ++i) {
final double[] msI = multistep[i];
for (int j = 0; j < first.length; ++j) {
msI[j] -= first[j];
}
}
return initialization.multiply(new Array2DRowRealMatrix(multistep, false));
}