本文整理汇总了Java中org.apache.commons.math3.linear.RealMatrix.getEntry方法的典型用法代码示例。如果您正苦于以下问题:Java RealMatrix.getEntry方法的具体用法?Java RealMatrix.getEntry怎么用?Java RealMatrix.getEntry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.linear.RealMatrix
的用法示例。
在下文中一共展示了RealMatrix.getEntry方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: concatHorizontally
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
public static RealMatrix concatHorizontally(final RealMatrix left,
final RealMatrix right) {
if (left.getRowDimension() != right.getRowDimension()) {
throw new IllegalArgumentException(
"The matrices must have the same row dimension!");
}
final double[][] result =
new double[left.getRowDimension()][left.getColumnDimension()
+ right.getColumnDimension()];
final int lc = left.getColumnDimension();
for (int r = 0; r < left.getRowDimension(); r++) {
for (int c = 0; c < left.getColumnDimension(); c++) {
result[r][c] = left.getEntry(r, c);
}
for (int c = 0; c < right.getColumnDimension(); c++) {
result[r][lc + c] = right.getEntry(r, c);
}
}
return MatrixUtils.createRealMatrix(result);
}
示例2: concatVertically
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
public static RealMatrix concatVertically(final RealMatrix top,
final RealMatrix bottom) {
if (top.getColumnDimension() != bottom.getColumnDimension()) {
throw new IllegalArgumentException(
"The matrices must have the same column dimension!");
}
final double[][] result = new double[top.getRowDimension()
+ bottom.getRowDimension()][top.getColumnDimension()];
final int tr = top.getRowDimension();
for (int c = 0; c < top.getColumnDimension(); c++) {
for (int r = 0; r < top.getRowDimension(); r++) {
result[r][c] = top.getEntry(r, c);
}
for (int r = 0; r < bottom.getRowDimension(); r++) {
result[tr + r][c] = bottom.getEntry(r, c);
}
}
return MatrixUtils.createRealMatrix(result);
}
示例3: correlation2Distance
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
public static RealMatrix correlation2Distance(RealMatrix rMat) {
// Copy to retain Dimensions
RealMatrix dMat = rMat.copy();
for (int row = 0; row < rMat.getRowDimension(); row++) {
for (int col = 0; col < rMat.getColumnDimension(); col++) {
double r = rMat.getEntry(row, col);
//Apply cosine theorem:
//https://stats.stackexchange.com/questions/165194/using-correlation-as-distance-metric-for-hierarchical-clustering
double d = Math.sqrt(2*(1-r));
dMat.setEntry(row, col, d);
}
}
return dMat;
}
示例4: testRealMatrixUpdates
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
@Test()
public void testRealMatrixUpdates() {
final DataFrame<String,String> frame = TestDataFrames.random(double.class, 100, 100);
final RealMatrix matrix = frame.export().asApacheMatrix();
Assert.assertEquals(frame.rowCount(), matrix.getRowDimension(), "Row count matches");
Assert.assertEquals(frame.colCount(), matrix.getColumnDimension(), "Column count matches");
for (int i=0; i<frame.rowCount(); ++i) {
for (int j = 0; j<frame.colCount(); ++j) {
matrix.setEntry(i, j, Math.random());
}
}
for (int i=0; i<frame.rowCount(); ++i) {
for (int j = 0; j<frame.colCount(); ++j) {
final double v1 = frame.data().getDouble(i, j);
final double v2 = matrix.getEntry(i, j);
Assert.assertEquals(v1, v2, "Values match at " + i + "," + j);
}
}
}
示例5: printX
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
@SuppressWarnings("unused")
private void printX(RealMatrix x, int iteration) {
for (int i = 0; i < x.getRowDimension(); i++) {
double value = x.getEntry(i, 0);
System.out.print(value);
if ( i < x.getRowDimension() - 1 ){
System.out.print(',');
}
}
System.out.print('\n');
}
示例6: traceDot
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
double traceDot(RealMatrix A) {
int d = meanMean.getDimension();
double result = 0.0;
for (int i=0; i<d; i++) {
for (int j=0; j<d; j++) {
result += A.getEntry(i, j)*precisionInvScale.getEntry(i, j);
}
}
return result;
}
示例7: computeBeta
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
*
* @param y the response vector
* @param x the design matrix
*/
private RealMatrix computeBeta(RealVector y, RealMatrix x) {
if (solver == Solver.QR) {
return computeBetaQR(y, x);
} else {
final int n = x.getRowDimension();
final int p = x.getColumnDimension();
final int offset = hasIntercept() ? 1 : 0;
final RealMatrix xT = x.transpose();
final RealMatrix xTxInv = new LUDecomposition(xT.multiply(x)).getSolver().getInverse();
final RealVector betaVector = xTxInv.multiply(xT).operate(y);
final RealVector residuals = y.subtract(x.operate(betaVector));
this.rss = residuals.dotProduct(residuals);
this.errorVariance = rss / (n - p);
this.stdError = Math.sqrt(errorVariance);
this.residuals = createResidualsFrame(residuals);
final RealMatrix covMatrix = xTxInv.scalarMultiply(errorVariance);
final RealMatrix result = new Array2DRowRealMatrix(p, 2);
if (hasIntercept()) {
result.setEntry(0, 0, betaVector.getEntry(0)); //Intercept coefficient
result.setEntry(0, 1, covMatrix.getEntry(0, 0)); //Intercept variance
}
for (int i = 0; i < getRegressors().size(); i++) {
final int index = i + offset;
final double variance = covMatrix.getEntry(index, index);
result.setEntry(index, 1, variance);
result.setEntry(index, 0, betaVector.getEntry(index));
}
return result;
}
}
示例8: computeBetaQR
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
* Computes model parameters and parameter variance using a QR decomposition of the X matrix
* @param y the response vector
* @param x the design matrix
*/
private RealMatrix computeBetaQR(RealVector y, RealMatrix x) {
final int n = x.getRowDimension();
final int p = x.getColumnDimension();
final int offset = hasIntercept() ? 1 : 0;
final QRDecomposition decomposition = new QRDecomposition(x, threshold);
final RealVector betaVector = decomposition.getSolver().solve(y);
final RealVector residuals = y.subtract(x.operate(betaVector));
this.rss = residuals.dotProduct(residuals);
this.errorVariance = rss / (n - p);
this.stdError = Math.sqrt(errorVariance);
this.residuals = createResidualsFrame(residuals);
final RealMatrix rAug = decomposition.getR().getSubMatrix(0, p - 1, 0, p - 1);
final RealMatrix rInv = new LUDecomposition(rAug).getSolver().getInverse();
final RealMatrix covMatrix = rInv.multiply(rInv.transpose()).scalarMultiply(errorVariance);
final RealMatrix result = new Array2DRowRealMatrix(p, 2);
if (hasIntercept()) {
result.setEntry(0, 0, betaVector.getEntry(0)); //Intercept coefficient
result.setEntry(0, 1, covMatrix.getEntry(0, 0)); //Intercept variance
}
for (int i = 0; i < getRegressors().size(); i++) {
final int index = i + offset;
final double variance = covMatrix.getEntry(index, index);
result.setEntry(index, 1, variance);
result.setEntry(index, 0, betaVector.getEntry(index));
}
return result;
}
示例9: testRealMatrixRead
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
@Test()
public void testRealMatrixRead() {
final DataFrame<String,String> frame = TestDataFrames.random(double.class, 100, 100);
final RealMatrix matrix = frame.export().asApacheMatrix();
Assert.assertEquals(frame.rowCount(), matrix.getRowDimension(), "Row count matches");
Assert.assertEquals(frame.colCount(), matrix.getColumnDimension(), "Column count matches");
for (int i=0; i<frame.rowCount(); ++i) {
for (int j = 0; j<frame.colCount(); ++j) {
final double v1 = frame.data().getDouble(i, j);
final double v2 = matrix.getEntry(i, j);
Assert.assertEquals(v1, v2, "Values match at " + i + "," + j);
}
}
}
示例10: testRealMatrixReadAfterModify
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
@Test()
public void testRealMatrixReadAfterModify() {
final DataFrame<String,String> frame = TestDataFrames.random(double.class, 100, 100);
final RealMatrix matrix = frame.export().asApacheMatrix();
Assert.assertEquals(frame.rowCount(), matrix.getRowDimension(), "Row count matches");
Assert.assertEquals(frame.colCount(), matrix.getColumnDimension(), "Column count matches");
frame.applyDoubles(v -> Math.random());
for (int i=0; i<frame.rowCount(); ++i) {
for (int j = 0; j<frame.colCount(); ++j) {
final double v1 = frame.data().getDouble(i, j);
final double v2 = matrix.getEntry(i, j);
Assert.assertEquals(v1, v2, "Values match at " + i + "," + j);
}
}
}
示例11: testRealMatrixCopy
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
@Test()
public void testRealMatrixCopy() {
final DataFrame<String,String> frame = TestDataFrames.random(double.class, 100, 100);
final RealMatrix matrix = frame.export().asApacheMatrix().copy();
frame.applyDoubles(v -> Math.random() * 10);
for (int i=0; i<frame.rowCount(); ++i) {
for (int j = 0; j<frame.colCount(); ++j) {
final double v1 = frame.data().getDouble(i, j);
final double v2 = matrix.getEntry(i, j);
Assert.assertTrue(v1 != v2, "Values do not match at " + i + "," + j);
}
}
}
示例12: weightedLinearCorr
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
*
* @param y
* @param x
* @param sigmaRhoY
* @return
*/
public static WeightedLinearCorrResults weightedLinearCorr(double[] y, double[] x, double[][] sigmaRhoY) {
WeightedLinearCorrResults weightedLinearCorrResults = new WeightedLinearCorrResults();
RealMatrix omega = new BlockRealMatrix(convertCorrelationsToCovariances(sigmaRhoY));
RealMatrix invOmega = MatrixUtils.inverse(omega);
int n = y.length;
double mX = 0;
double pX = 0;
double pY = 0;
double pXY = 0;
double w = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
double invOm = invOmega.getEntry(i, j);
w += invOm;
pX += (invOm * (x[i] + x[j]));
pY += (invOm * (y[i] + y[j]));
pXY += (invOm * (((x[i] * y[j]) + (x[j] * y[i]))));
mX += (invOm * x[i] * x[j]);
}
}
double slope = ((2 * pXY * w) - (pX * pY)) / ((4 * mX * w) - (pX * pX));
double intercept = (pY - (slope * pX)) / (2 * w);
RealMatrix fischer = new BlockRealMatrix(new double[][]{{mX, pX / 2.0}, {pX / 2.0, w}});
RealMatrix fischerInv = MatrixUtils.inverse(fischer);
double slopeSig = Math.sqrt(fischerInv.getEntry(0, 0));
double interceptSig = Math.sqrt(fischerInv.getEntry(1, 1));
double slopeInterceptCov = fischerInv.getEntry(0, 1);
RealMatrix resid = new BlockRealMatrix(n, 1);
for (int i = 0; i < n; i++) {
resid.setEntry(i, 0, y[i] - (slope * x[i]) - intercept);
}
RealMatrix residT = resid.transpose();
RealMatrix mM = residT.multiply(invOmega).multiply(resid);
double sumSqWtdResids = mM.getEntry(0, 0);
double mswd = sumSqWtdResids / (n - 2);
// http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/FDistribution.html
FDistribution fdist = new org.apache.commons.math3.distribution.FDistribution((n - 2), 1E9);
double prob = 1.0 - fdist.cumulativeProbability(mswd);
weightedLinearCorrResults.setBad(false);
weightedLinearCorrResults.setSlope(slope);
weightedLinearCorrResults.setIntercept(intercept);
weightedLinearCorrResults.setSlopeSig(slopeSig);
weightedLinearCorrResults.setInterceptSig(interceptSig);
weightedLinearCorrResults.setSlopeInterceptCov(slopeInterceptCov);
weightedLinearCorrResults.setMswd(mswd);
weightedLinearCorrResults.setProb(prob);
return weightedLinearCorrResults;
}
示例13: wtdAvCorr
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
*
* @param values
* @param varCov
* @return
*/
public static WtdAvCorrResults wtdAvCorr(double[] values, double[][] varCov) {
// assume varCov is variance-covariance matrix (i.e. SigRho = false)
WtdAvCorrResults results = new WtdAvCorrResults();
int n = varCov.length;
RealMatrix omegaInv = new BlockRealMatrix(varCov);
RealMatrix omega = MatrixUtils.inverse(omegaInv);
double numer = 0.0;
double denom = 0.0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
numer += (values[i] + values[j]) * omega.getEntry(i, j);
denom += omega.getEntry(i, j);
}
}
// test denom
if (denom > 0.0) {
double meanVal = numer / denom / 2.0;
double meanValSigma = Math.sqrt(1.0 / denom);
double[][] unwtdResidsArray = new double[n][1];
for (int i = 0; i < n; i++) {
unwtdResidsArray[i][0] = values[i] - meanVal;
}
RealMatrix unwtdResids = new BlockRealMatrix(unwtdResidsArray);
RealMatrix transUnwtdResids = unwtdResids.transpose();
RealMatrix product = transUnwtdResids.multiply(omega);
RealMatrix sumWtdResids = product.multiply(unwtdResids);
double mswd = 0.0;
double prob = 0.0;
if (n > 1) {
mswd = sumWtdResids.getEntry(0, 0) / (n - 1);
// http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/FDistribution.html
FDistribution fdist = new org.apache.commons.math3.distribution.FDistribution((n - 1), 1E9);
prob = 1.0 - fdist.cumulativeProbability(mswd);
}
results.setBad(false);
results.setMeanVal(meanVal);
results.setSigmaMeanVal(meanValSigma);
results.setMswd(mswd);
results.setProb(prob);
}
return results;
}