本文整理汇总了Java中org.apache.commons.math3.linear.MatrixUtils类的典型用法代码示例。如果您正苦于以下问题:Java MatrixUtils类的具体用法?Java MatrixUtils怎么用?Java MatrixUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MatrixUtils类属于org.apache.commons.math3.linear包,在下文中一共展示了MatrixUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeWeightedJacobian
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
/**
* Computes the Jacobian matrix.
*
* @param params Model parameters at which to compute the Jacobian.
* @return the weighted Jacobian: W<sup>1/2</sup> J.
* @throws DimensionMismatchException if the Jacobian dimension does not
* match problem dimension.
* @since 3.1
*/
protected RealMatrix computeWeightedJacobian(double[] params) {
++jacobianEvaluations;
final DerivativeStructure[] dsPoint = new DerivativeStructure[params.length];
final int nC = params.length;
for (int i = 0; i < nC; ++i) {
dsPoint[i] = new DerivativeStructure(nC, 1, i, params[i]);
}
final DerivativeStructure[] dsValue = jF.value(dsPoint);
final int nR = getTarget().length;
if (dsValue.length != nR) {
throw new DimensionMismatchException(dsValue.length, nR);
}
final double[][] jacobianData = new double[nR][nC];
for (int i = 0; i < nR; ++i) {
int[] orders = new int[nC];
for (int j = 0; j < nC; ++j) {
orders[j] = 1;
jacobianData[i][j] = dsValue[i].getPartialDerivative(orders);
orders[j] = 0;
}
}
return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(jacobianData));
}
示例2: getTruncatedSVD
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
/**
* truncated SVD as taken from http://stackoverflow.com/questions/19957076/best-way-to-compute-a-truncated-singular-value-decomposition-in-java
*/
static double[][] getTruncatedSVD(double[][] matrix, final int k) {
SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(matrix));
double[][] truncatedU = new double[svd.getU().getRowDimension()][k];
svd.getU().copySubMatrix(0, truncatedU.length - 1, 0, k - 1, truncatedU);
double[][] truncatedS = new double[k][k];
svd.getS().copySubMatrix(0, k - 1, 0, k - 1, truncatedS);
double[][] truncatedVT = new double[k][svd.getVT().getColumnDimension()];
svd.getVT().copySubMatrix(0, k - 1, 0, truncatedVT[0].length - 1, truncatedVT);
RealMatrix approximatedSvdMatrix = (MatrixUtils.createRealMatrix(truncatedU)).multiply(
MatrixUtils.createRealMatrix(truncatedS)).multiply(MatrixUtils.createRealMatrix(truncatedVT));
return approximatedSvdMatrix.getData();
}
示例3: concatHorizontally
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的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);
}
示例4: concatVertically
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的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);
}
示例5: makeDataMatrix
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
private RealMatrix makeDataMatrix(List<double[]> X, double[] meanX) {
if (meanX == null) {
return makeDataMatrix(X);
}
final int m = X.size();
final int n = X.get(0).length;
RealMatrix M = MatrixUtils.createRealMatrix(n, m);
RealVector mean = MatrixUtils.createRealVector(meanX);
int i = 0;
for (double[] x : X) {
RealVector xi = MatrixUtils.createRealVector(x).subtract(mean);
M.setColumnVector(i, xi);
i++;
}
return M;
}
示例6: MahalanobisDistance
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
/**
* Create a new instance from an array of m-dimensional samples, e.g.
* samples = {{x1,y1}, {x2,y2},...,{xn,yn}} for a vector of n two-dimensional
* samples.
* @param samples A vector of length n with m-dimensional samples, i.e.
* samples[k][i] represents the i-th component of the k-th sample.
* @param diagonalIncrement Quantity added to the diagonal values of the
* covariance matrix to avoid singularity.
*/
public MahalanobisDistance(double[][] samples, double diagonalIncrement) {
n = samples.length;
m = samples[0].length;
if (n < 1 || m < 1) {
throw new IllegalArgumentException("dimension less than 1");
}
if (diagonalIncrement < 0) {
throw new IllegalArgumentException("diagonal increment must be positive");
}
mean = makeMeanVector(samples);
Covariance cov = new Covariance(samples, BIAS_CORRECTION);
RealMatrix S = cov.getCovarianceMatrix();
// condition the covariance matrix to avoid singularity:
S = conditionCovarianceMatrix(S);
Cov = S.getData();
// get the inverse covariance matrix
iCov = MatrixUtils.inverse(S).getData(); // not always symmetric?
}
示例7: 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));
}
示例8: setValues
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
@Override
public void setValues(double[] y, double[] x) {
if (x.length != y.length) {
throw new IllegalArgumentException(String.format("The numbers of y and x values must be equal (%d != %d)", y.length, x.length));
}
double[][] xData = new double[x.length][];
for (int i = 0; i < x.length; i++) {
// the implementation determines how to produce a vector of predictors from a single x
xData[i] = xVector(x[i]);
}
if (logY()) { // in some models we are predicting ln y, so we replace each y with ln y
y = Arrays.copyOf(y, y.length); // user might not be finished with the array we were given
for (int i = 0; i < x.length; i++) {
y[i] = Math.log(y[i]);
}
}
final OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
ols.setNoIntercept(true); // let the implementation include a constant in xVector if desired
ols.newSampleData(y, xData); // provide the data to the model
coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs
last_error_rate = ols.estimateErrorVariance();
Log.d(TAG, getClass().getSimpleName() + " Forecast Error rate: errorvar:"
+ JoH.qs(last_error_rate, 4)
+ " regssionvar:" + JoH.qs(ols.estimateRegressandVariance(), 4)
+ " stderror:" + JoH.qs(ols.estimateRegressionStandardError(), 4));
}
示例9: render
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
public void render(World world, RenderCallback callback) {
new BukkitRunnable() {
public void run() {
long msBegin = System.currentTimeMillis();
renderNode(world, scene, MatrixUtils.createRealIdentityMatrix(4));
long msEnd = System.currentTimeMillis();
System.out.println("Filling calculated (" + (msEnd - msBegin) + "ms)");
if (callback != null)
renderer.addCallback(callback);
}
}.runTaskAsynchronously(plugin);
}
示例10: getVectorVarByName
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
public List<RealVector> getVectorVarByName(String name) {
String varName = "V_" + name;
JsonNode obj = redisService.getValue(spaceIdentifier, varName);
int size = obj.get("size").asInt();
int dim = obj.get("dim").asInt();
double initial = obj.get("initial").asDouble();
boolean randomize = obj.get("randomize").asBoolean();
boolean normalize = obj.get("normalize").asBoolean();
List<RealVector> vars = new ArrayList<>(size);
for (int i=0; i<size; i++) {
RealVector var = MatrixUtils.createRealVector(new double[dim]);
initializeVector(var, initial, randomize, normalize);
vars.add(var);
}
String varIdxName = "IDX_V_" + name + "_";
List<String> keys = redisService.keysWithPrefixPattern(spaceIdentifier, varIdxName);
List<JsonNode> values = redisService.bulkGet(keys);
for (JsonNode one : values) {
setValue(vars, one, dim);
}
return vars;
}
示例11: ensureVectorVar
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
final public void ensureVectorVar(String name, int size, int dim, double initial,
boolean randomize, boolean normalize) {
writeLock.lock();
try {
int curSize = vectorVars.get(name).size();
if (curSize < size) {
for (int i=curSize; i<size; i++) {
RealVector vec = MatrixUtils.createRealVector(new double[dim]);
initializeVector(vec, initial, randomize, normalize);
vectorVars.get(name).add(vec);
}
}
curSize = readLocks.size();
SpaceUtilities.fillReadWriteLocks(readLocks, writeLocks, curSize, size);
} finally {
writeLock.unlock();
}
}
示例12: getStochasticOracle
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
public List<StochasticOracle> getStochasticOracle(List<LearningInstance> instances) {
List<StochasticOracle> oracles = new ArrayList<>(instances.size());
for (LearningInstance inIns : instances) {
SVDFeatureInstance ins = (SVDFeatureInstance) inIns;
StochasticOracle orc = new StochasticOracle();
RealVector ufactSum = MatrixUtils.createRealVector(new double[factDim]);
RealVector ifactSum = MatrixUtils.createRealVector(new double[factDim]);
double pred = predict(ins, orc, ufactSum, ifactSum);
RealVector leftGrad = ifactSum;
RealVector rightGrad = ufactSum;
for (int i = 0; i < ins.ufeas.size(); i++) {
orc.addVectorOracle(SVDFeatureKey.FACTORS.get(),
ins.ufeas.get(i).getIndex(),
leftGrad.mapMultiply(ins.ufeas.get(i).getValue()));
}
for (int i = 0; i < ins.ifeas.size(); i++) {
orc.addVectorOracle(SVDFeatureKey.FACTORS.get(),
ins.ifeas.get(i).getIndex(),
rightGrad.mapMultiply(ins.ifeas.get(i).getValue()));
}
orc.setValues(pred, ins.label, ins.weight);
oracles.add(orc);
}
return oracles;
}
示例13: testDelta
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
/**
* If we rotate the covariance matrix by the angle delta, we should get a diagonal matrix.
*/
@Test
public void testDelta() {
assertTrue("Covariance matrix should not be diagonal",
covarianceMatrix.getEntry(0, 1) != 0 && covarianceMatrix.getEntry(1, 0) != 0);
double delta = poser.calculateDelta(eig);
double[][] rot = {{Math.cos(delta), Math.sin(delta)},
{-Math.sin(delta), Math.cos(delta)}
};
RealMatrix rotMatrix = MatrixUtils.createRealMatrix(rot);
RealMatrix b = (rotMatrix.multiply(covarianceMatrix)).multiply(rotMatrix.transpose());
//check if non diagonal elements are close to zero.
assertEquals(b.getEntry(1, 0), 0.0, EPSILON);
assertEquals(b.getEntry(0, 1), 0.0, EPSILON);
}
示例14: applyStackManifold
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
public ImagePlus applyStackManifold(ImageStack imStack, ImagePlus manifold){
int dimW = imStack.getWidth();
int dimH = imStack.getHeight();
RealMatrix projMnold = MatrixUtils.createRealMatrix(SME_ENS_Utils.convertFloatMatrixToDoubles(manifold.getProcessor().getFloatArray(),dimW,dimH)).transpose();
for(int j=0;j<dimH;j++){
for(int i=0;i<dimW;i++){
int zIndex = ((int) Math.round(stackSize*(projMnold.getEntry(j,i)/255)));
projMnold.setEntry (j,i,imStack.getVoxel(i,j,zIndex-1));
}
}
float[][] mfoldFlaot = SME_ENS_Utils.convertDoubleMatrixToFloat(projMnold.transpose().getData(),dimW,dimH);
ImagePlus smeManifold = new ImagePlus("",((ImageProcessor) new FloatProcessor(mfoldFlaot)));
return(smeManifold);
}
示例15: padSymetricMatrix
import org.apache.commons.math3.linear.MatrixUtils; //导入依赖的package包/类
public static RealMatrix padSymetricMatrix(RealMatrix inMatrix, Boolean transMatr){
RealMatrix padedMatrix = null;
RealMatrix templateMatrix = null;
//transpose if necessary
if(transMatr) {
templateMatrix = inMatrix.copy().transpose();
}else{
templateMatrix = inMatrix.copy();
}
padedMatrix = MatrixUtils.createRealMatrix(templateMatrix.getRowDimension()+2,
templateMatrix.getColumnDimension());
int iPadStart = 1,iPadEnd = padedMatrix.getRowDimension()-1;
padedMatrix.setRowVector(0,templateMatrix.getRowVector(0));
padedMatrix.setRowVector(iPadEnd,templateMatrix.getRowVector(templateMatrix.getRowDimension()-1));
// symetrical padding
for(int i=iPadStart;i<iPadEnd;i++){
padedMatrix.setRowVector(i,templateMatrix.getRowVector(i-1));
}
return(padedMatrix);
}