List of usage examples for org.apache.commons.math3.linear RealMatrix getRowDimension
int getRowDimension();
From source file:lsafunctions.LSA.java
public static RealMatrix calculateTfIdf(RealMatrix M) { int tf;/*from w ww . ja va 2 s . c om*/ double idf; int df; double ndf; for (int j = 0; j < M.getRowDimension(); j++) { df = calcDf(j, M); // System.out.println("J:"+j+" df:"+df); for (int k = 0; k < M.getColumnDimension(); k++) { tf = (int) M.getEntry(j, k); ndf = M.getColumnDimension() / df; idf = Math.log(ndf) / Math.log(2); M.setEntry(j, k, idf * tf); } } //M.print(NumberFormat.INTEGER_FIELD, M.getColumnDimension()); M = normalizeMatrix(M); return M; }
From source file:edu.byu.nlp.stats.DirichletMLEOptimizableTest.java
/** * Computes a Newton-Raphson update in-place to alpha. */// w w w. j a v a 2s .c om private static RealVector newtonRaphsonUpdate(final double[][] data, double[] alpha) { // We'll compute the gold-standard value the "long" way (taking the inverse of the Hessian) RealMatrix hessian = new Array2DRowRealMatrix(alpha.length, alpha.length); for (int r = 0; r < hessian.getRowDimension(); r++) { for (int c = 0; c < hessian.getColumnDimension(); c++) { hessian.addToEntry(r, c, data.length * Gamma.trigamma(DoubleArrays.sum(alpha))); if (r == c) { hessian.addToEntry(r, c, -data.length * Gamma.trigamma(alpha[r])); } } } RealVector derivative = new ArrayRealVector(alpha.length); for (int k = 0; k < alpha.length; k++) { derivative.setEntry(k, data.length * (Gamma.digamma(DoubleArrays.sum(alpha)) - Gamma.digamma(alpha[k]))); for (double[] theta : data) { derivative.addToEntry(k, theta[k]); } } RealMatrix hessianInverse = new LUDecomposition(hessian).getSolver().getInverse(); RealVector negDiff = hessianInverse.preMultiply(derivative); negDiff.mapMultiplyToSelf(-1.0); RealVector expected = new ArrayRealVector(alpha, true); return expected.add(negDiff); }
From source file:edu.byu.nlp.stats.GammaDistribution.java
/** * Samples a new Gamma distributed random variate for each parameter setting specified as elements * of the shapes matrix.//from ww w. ja v a 2 s .c o m */ public static double[][] sample(RealMatrix shapes, RandomGenerator rnd) { double[][] gammas = new double[shapes.getRowDimension()][shapes.getColumnDimension()]; for (int i = 0; i < shapes.getRowDimension(); i++) { for (int j = 0; j < shapes.getColumnDimension(); j++) { gammas[i][j] = sample(shapes.getEntry(i, j), rnd); } } return gammas; }
From source file:lsafunctions.LSA.java
public static double cosinSim(int v1, int v2, RealMatrix Vt) { double sim = 0.0; double sumNum = 0.0; double fdenom = 0.0; double sdenom = 0.0; for (int j = 0; j < Vt.getRowDimension(); j++) { sumNum += Vt.getEntry(j, v1) * Vt.getEntry(j, v2); fdenom += Math.pow(Vt.getEntry(j, v1), 2); sdenom += Math.pow(Vt.getEntry(j, v2), 2); }/*from w w w . jav a2 s . c om*/ sim = sumNum / (Math.sqrt(fdenom) * Math.sqrt(sdenom)); return sim; }
From source file:edu.washington.gs.skyline.model.quantification.DesignMatrix.java
private static RealMatrix matrixFromColumnVectors(double[][] columnVectors) { RealMatrix realMatrix = MatrixUtils.createRealMatrix(columnVectors[0].length, columnVectors.length); for (int iRow = 0; iRow < realMatrix.getRowDimension(); iRow++) { for (int iCol = 0; iCol < realMatrix.getColumnDimension(); iCol++) { realMatrix.setEntry(iRow, iCol, columnVectors[iCol][iRow]); }//from ww w . j ava 2 s.c o m } return realMatrix; }
From source file:cooccurrence.Omer_Levy.java
/** * Generic Method to take squareRoot of individual Values. * @param coVariance/*from w w w .ja va 2 s . co m*/ * @return */ private static RealMatrix squareRoot(RealMatrix coVariance) { RealMatrix squareRoot = MatrixUtils.createRealMatrix(coVariance.getRowDimension(), coVariance.getColumnDimension()); for (int i = 0; i < coVariance.getRowDimension(); i++) { for (int j = 0; j < coVariance.getColumnDimension(); j++) { double val = coVariance.getEntry(i, j); val = Math.sqrt(val); squareRoot.addToEntry(i, j, val); } } return squareRoot; }
From source file:game.utils.Utils.java
public static String matrixToString(RealMatrix matrix) { StringBuilder ret = new StringBuilder(); for (int i = 0; i < matrix.getRowDimension(); i++) { for (int j = 0; j < matrix.getColumnDimension(); j++) { ret.append(String.format("%.5f", matrix.getEntry(i, j))); if (j == matrix.getColumnDimension() - 1) ret.append('\n'); else/*from w w w. j av a 2s.c o m*/ ret.append(", "); } } return ret.toString(); }
From source file:IO.java
public static void display(RealMatrix mat, String name) { int row = mat.getRowDimension(); int col = mat.getColumnDimension(); System.out.println(name + ":" + row + "x" + col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { System.out.print(mat.getEntry(i, j) + ","); }//from w w w. j a v a 2 s . co m System.out.println(); } }
From source file:net.sf.dsp4j.octave_3_2_4.m.polynomial.Roots.java
public static Complex[] roots(RealVector v) { if (v.isInfinite() || v.isNaN()) { throw new RuntimeException("roots: inputs must not contain Inf or NaN"); }// ww w .j a v a 2s .c om int n = v.getDimension(); // ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the // ## leading k zeros and n - k - l roots of the polynomial are zero. int[] f = new int[v.getDimension()]; if (v.getDimension() > 0) { int fI = 0; double max = v.getMaxValue(); double min = FastMath.abs(v.getMinValue()); if (min > max) { max = min; } RealVector v1 = v.mapDivide(max); f = OctaveBuildIn.find(v1); } Complex[] r = new Complex[0]; if (f.length > 0 && n > 1) { v = v.getSubVector(f[0], f[f.length - 1] - f[0] + 1); if (v.getDimension() > 1) { double[] ones = new double[v.getDimension() - 2]; Arrays.fill(ones, 1); RealMatrix A = OctaveBuildIn.diag(ones, -1); for (int i = 0; i < A.getRowDimension(); i++) { A.setEntry(i, 0, -v.getEntry(i + 1) / v.getEntry(0)); } try { r = Eig.eig(A); } catch (Exception e) { throw new RuntimeException(e); } if (f[f.length - 1] < n) { int diffLength = n - 1 - f[f.length - 1]; if (diffLength > 0) { int rl = r.length; r = Arrays.copyOf(r, r.length + diffLength); Arrays.fill(r, rl, r.length, Complex.ZERO); } } } else { r = new Complex[n - f[f.length - 1]]; Arrays.fill(r, Complex.ZERO); } } else { r = new Complex[0]; } return r; }
From source file:ch.zhaw.iamp.rct.weights.Weights.java
private static RealMatrix addNoise(final RealMatrix A) { RealMatrix buffer = A.copy();//from ww w . j av a 2 s .c o m RealMatrix noise = MatrixUtils.createRealMatrix(A.getRowDimension(), A.getColumnDimension()); for (int i = 0; i < A.getRowDimension(); ++i) { for (int j = 0; j < A.getColumnDimension(); ++j) { double noiseSign = Math.random() > 0.5 ? 1 : -1; double noiseAmplitude = 1; noise.setEntry(i, j, noiseSign * Math.random() * noiseAmplitude); } } buffer = buffer.add(noise); return buffer; }