本文整理汇总了Java中org.apache.commons.math3.linear.RealMatrix.setEntry方法的典型用法代码示例。如果您正苦于以下问题:Java RealMatrix.setEntry方法的具体用法?Java RealMatrix.setEntry怎么用?Java RealMatrix.setEntry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.linear.RealMatrix
的用法示例。
在下文中一共展示了RealMatrix.setEntry方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: multiplyElementWise
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
public static RealMatrix multiplyElementWise(final RealMatrix matrix1,
final RealMatrix matrix2) {
if (matrix1.getRowDimension() != matrix2.getRowDimension() || matrix1
.getColumnDimension() != matrix2.getColumnDimension()) {
throw new IllegalArgumentException(
"The matrices must be of the same dimensions!");
}
final RealMatrix result = matrix1.createMatrix(
matrix1.getRowDimension(), matrix1.getColumnDimension());
for (int r = 0; r < matrix1.getRowDimension(); r++) {
for (int c = 0; c < matrix1.getColumnDimension(); c++) {
result.setEntry(r, c,
matrix1.getEntry(r, c) * matrix2.getEntry(r, c));
}
}
return result;
}
示例2: 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;
}
示例3: createX
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
* Creates the X design matrix for this regression model
* @return the X design matrix
*/
RealMatrix createX() {
final int n = frame.rows().count();
final int offset = hasIntercept() ? 1 : 0;
final int p = hasIntercept() ? regressors.size() + 1 : regressors.size();
final int[] colIndexes = regressors.stream().mapToInt(k -> frame.cols().ordinalOf(k)).toArray();
final RealMatrix x = new Array2DRowRealMatrix(n, p);
for (int i = 0; i < n; ++i) {
x.setEntry(i, 0, 1d);
for (int j = offset; j < p; ++j) {
final double value = frame.data().getDouble(i, colIndexes[j - offset]);
x.setEntry(i, j, value);
}
}
return x;
}
示例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: squared_euclidean_distances
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
private RealMatrix squared_euclidean_distances(final RealMatrix x,
final RealMatrix y) {
final RealMatrix distmat = MatrixUtils
.createRealMatrix(x.getRowDimension(), y.getRowDimension());
for (int i = 0; i < x.getRowDimension(); i++) {
for (int j = 0; j < y.getRowDimension(); j++) {
final RealVector buff =
x.getRowVector(i).subtract(y.getRowVector(j));
distmat.setEntry(i, j, buff.dotProduct(buff));
}
}
return distmat;
}
示例6: pow
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
public static RealMatrix pow(final RealMatrix matrix, final double power) {
final RealMatrix result = matrix.createMatrix(matrix.getRowDimension(),
matrix.getColumnDimension());
for (int r = 0; r < result.getRowDimension(); r++) {
for (int c = 0; c < result.getColumnDimension(); c++) {
result.setEntry(r, c, Math.pow(matrix.getEntry(r, c), power));
}
}
return result;
}
示例7: fit
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
@Override
public void fit(List<double[]> X, List<double[]> Y) { // fits n-dimensional data sets with affine model
if (X.size() != Y.size())
throw new IllegalArgumentException("point sequences X, Y must have same length");
this.m = X.size();
this.n = X.get(0).length;
RealMatrix M = MatrixUtils.createRealMatrix(2 * m, 2 * (n + 1));
RealVector b = new ArrayRealVector(2 * m);
// mount matrix M:
int row = 0;
for (double[] x : X) {
for (int j = 0; j < n; j++) {
M.setEntry(row, j, x[j]);
M.setEntry(row, n, 1);
row++;
}
for (int j = 0; j < n; j++) {
M.setEntry(row, j + n + 1, x[j]);
M.setEntry(row, 2 * n + 1, 1);
row++;
}
}
// mount vector b
row = 0;
for (double[] y : Y) {
for (int j = 0; j < n; j++) {
b.setEntry(row, y[j]);
row++;
}
}
SingularValueDecomposition svd = new SingularValueDecomposition(M);
DecompositionSolver solver = svd.getSolver();
RealVector a = solver.solve(b);
A = makeTransformationMatrix(a);
}
示例8: makeTransformationMatrix
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
private RealMatrix makeTransformationMatrix(RealVector a) {
RealMatrix A = MatrixUtils.createRealMatrix(n, n + 1);
int i = 0;
for (int row = 0; row < n; row++) {
// get (n+1) elements from a and place in row
for (int j = 0; j <= n; j++) {
A.setEntry(row, j, a.getEntry(i));
i++;
}
}
A.setEntry(n - 1, n, 1);
return A;
}
示例9: conditionCovarianceMatrix
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
* Conditions the supplied covariance matrix by enforcing
* positive eigenvalues along its main diagonal.
* @param S original covariance matrix
* @return modified covariance matrix
*/
private RealMatrix conditionCovarianceMatrix(RealMatrix S) {
EigenDecomposition ed = new EigenDecomposition(S); // S -> V . D . V^T
RealMatrix V = ed.getV();
RealMatrix D = ed.getD(); // diagonal matrix of eigenvalues
RealMatrix VT = ed.getVT();
for (int i = 0; i < D.getRowDimension(); i++) {
D.setEntry(i, i, Math.max(D.getEntry(i, i), 10E-6)); // setting eigenvalues to zero is not enough!
}
return V.multiply(D).multiply(VT);
}
示例10: 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;
}
}
示例11: 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;
}
示例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;
}