本文整理汇总了Java中org.apache.commons.math3.linear.MatrixUtils.inverse方法的典型用法代码示例。如果您正苦于以下问题:Java MatrixUtils.inverse方法的具体用法?Java MatrixUtils.inverse怎么用?Java MatrixUtils.inverse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.linear.MatrixUtils
的用法示例。
在下文中一共展示了MatrixUtils.inverse方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public static void main(String[] args) {
MihcConfigData conf = new MihcConfigData();
RealMatrix inverse = MatrixUtils.inverse(MatrixUtils.createRealMatrix(conf.filterNewXeon4fake));
printMat(inverse);
// Array2DRowRealMatrix AsnInv = (Array2DRowRealMatrix) MatrixUtils.createRealMatrix(conf.inverse);
// printMat(AsnInv);
// double[] gain = new double[]{1,1,1,1,2,1};
// for (int i=0; i<gain.length; i++) gain[i] = 1d/gain[i];
// RealMatrix GiiInv = MatrixUtils.createRealDiagonalMatrix(gain);
// printMat(GiiInv);
// Array2DRowRealMatrix AsnInvNorm = (Array2DRowRealMatrix) AsnInv.multiply(GiiInv);
// printMat(AsnInvNorm);
// double[] out = new double[gain.length];
//
// //double[] in = new double[]{0,000,0,0,100,39};
// double[] in = new double[]{0,0,0,1.554,100,36.232}; // -> 0,0,0,0,100,0
// fastMultiply(AsnInvNorm,in,out);
// System.out.println(Arrays.toString(out));
}
示例2: inv
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
* Calculate inverse matrix
*
* @param a The matrix
* @return Inverse matrix array
*/
public static Array inv(Array a) {
double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a);
RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
RealMatrix invm = MatrixUtils.inverse(matrix);
if (invm == null) {
return null;
}
int m = invm.getRowDimension();
int n = invm.getColumnDimension();
Array r = Array.factory(DataType.DOUBLE, new int[]{m, n});
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
r.setDouble(i * n + j, invm.getEntry(i, j));
}
}
return r;
}
示例3: invertMatrix
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
*
* @param matrix
* @return
*/
public static double[][] invertMatrix(double[][] matrix){
Array2DRowRealMatrix rMatrix=new Array2DRowRealMatrix(matrix);
RealMatrix inv=MatrixUtils.inverse(rMatrix);
double[][]invHermite=inv.getData();
return invHermite;
}
示例4: updateReward
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public void updateReward(User user, Article a, boolean clicked) {
String aId = a.getId();
// Collect Variables
RealMatrix xta = MatrixUtils.createColumnRealMatrix(a.getFeatures());
RealMatrix zta = makeZta(
MatrixUtils.createColumnRealMatrix(user.getFeatures()), xta);
RealMatrix Aa = AMap.get(aId);
RealMatrix ba = bMap.get(aId);
RealMatrix Ba = BMap.get(aId);
// Find common transpose/inverse to save computation
RealMatrix AaInverse = MatrixUtils.inverse(Aa);
RealMatrix BaTranspose = Ba.transpose();
RealMatrix xtaTranspose = xta.transpose();
RealMatrix ztaTranspose = zta.transpose();
// Update
A0 = A0.add(BaTranspose.multiply(AaInverse).multiply(Ba));
b0 = b0.add(BaTranspose.multiply(AaInverse).multiply(ba));
Aa = Aa.add(xta.multiply(xtaTranspose));
AMap.put(aId, Aa);
Ba = Ba.add(xta.multiply(ztaTranspose));
BMap.put(aId, Ba);
if (clicked) {
ba = ba.add(xta);
bMap.put(aId, ba);
}
// Update A0 and b0 with the new values
A0 = A0.add(zta.multiply(ztaTranspose)).subtract(
Ba.transpose().multiply(MatrixUtils.inverse(Aa).multiply(Ba)));
b0 = b0.subtract(Ba.transpose().multiply(MatrixUtils.inverse(Aa))
.multiply(ba));
if (clicked) {
b0 = b0.add(zta);
}
}
示例5: inverse
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
* @param A a square matrix.
* @return the inverse of A or null if A is non-square or singular.
*/
public static double[][] inverse(final double[][] A) {
RealMatrix M = MatrixUtils.createRealMatrix(A);
if (!M.isSquare())
return null;
else {
double[][] Ai = null;
try {
RealMatrix Mi = MatrixUtils.inverse(M); //new LUDecomposition(M).getSolver().getInverse();
Ai = Mi.getData();
} catch (SingularMatrixException e) {}
return Ai;
}
}
示例6: weightedLinearCorr
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的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;
}
示例7: wtdAvCorr
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的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;
}
示例8: chooseArm
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public Article chooseArm(User user, List<Article> articles) {
Article bestA = null;
double bestArmP = Double.MIN_VALUE;
RealMatrix Aa;
RealMatrix Ba;
RealMatrix ba;
for (Article a : articles) {
String aId = a.getId();
if (!AMap.containsKey(aId)) {
Aa = MatrixUtils.createRealIdentityMatrix(6);
AMap.put(aId, Aa); // set as identity for now and we will update
// in reward
double[] zeros = { 0, 0, 0, 0, 0, 0 };
ba = MatrixUtils.createColumnRealMatrix(zeros);
bMap.put(aId, ba);
double[][] BMapZeros = new double[6][36];
for (double[] row : BMapZeros) {
Arrays.fill(row, 0.0);
}
Ba = MatrixUtils.createRealMatrix(BMapZeros);
BMap.put(aId, Ba);
} else {
Aa = AMap.get(aId);
ba = bMap.get(aId);
Ba = BMap.get(aId);
}
// Make column vector out of features
RealMatrix xta = MatrixUtils
.createColumnRealMatrix(a.getFeatures());
RealMatrix zta = makeZta(
MatrixUtils.createColumnRealMatrix(user.getFeatures()), xta);
// Set up common variables
RealMatrix A0Inverse = MatrixUtils.inverse(A0);
RealMatrix AaInverse = MatrixUtils.inverse(Aa);
RealMatrix ztaTranspose = zta.transpose();
RealMatrix BaTranspose = Ba.transpose();
RealMatrix xtaTranspose = xta.transpose();
// Find theta
RealMatrix theta = AaInverse.multiply(ba.subtract(Ba
.multiply(BetaHat)));
// Find sta
RealMatrix staMatrix = ztaTranspose.multiply(A0Inverse).multiply(
zta);
staMatrix = staMatrix.subtract(ztaTranspose.multiply(A0Inverse)
.multiply(BaTranspose).multiply(AaInverse).multiply(xta)
.scalarMultiply(2));
staMatrix = staMatrix.add(xtaTranspose.multiply(AaInverse)
.multiply(xta));
staMatrix = staMatrix.add(xtaTranspose.multiply(AaInverse)
.multiply(Ba).multiply(A0Inverse).multiply(BaTranspose)
.multiply(AaInverse).multiply(xta));
// Find pta for arm
RealMatrix ptaMatrix = ztaTranspose.multiply(BetaHat);
ptaMatrix = ptaMatrix.add(xtaTranspose.multiply(theta));
double ptaVal = ptaMatrix.getData()[0][0];
double staVal = staMatrix.getData()[0][0];
ptaVal = ptaVal + alpha * Math.sqrt(staVal);
// Update argmax
if (ptaVal > bestArmP) {
bestArmP = ptaVal;
bestA = a;
}
}
return bestA;
}