List of usage examples for org.apache.commons.math3.linear RealMatrix getColumnDimension
int getColumnDimension();
From source file:edu.oregonstate.eecs.mcplan.ml.WekaGlue.java
public static SequentialProjectionHashLearner createSequentialProjectionHashLearner(final RandomGenerator rng, final Instances labeled, final Instances unlabeled, final int K, final double eta, final double alpha) { assert (labeled.classIndex() >= 0); final int Nfeatures = labeled.numAttributes() - 1; final RealMatrix X = new Array2DRowRealMatrix(Nfeatures, labeled.size() + unlabeled.size()); final RealMatrix XL = new Array2DRowRealMatrix(Nfeatures, labeled.size() * 2); final RealMatrix S = new Array2DRowRealMatrix(XL.getColumnDimension(), XL.getColumnDimension()); for (int j = 0; j < labeled.size(); ++j) { final Instance inst = labeled.get(j); for (int i = 0; i < XL.getRowDimension(); ++i) { X.setEntry(i, j, inst.value(i)); XL.setEntry(i, j, inst.value(i)); }/*from w ww .jav a 2 s . co m*/ int sj = -1; Instance s = null; do { sj = rng.nextInt(labeled.size()); s = labeled.get(sj); } while (s == inst || s.classValue() != inst.classValue()); S.setEntry(j, sj, 1); int dj = -1; Instance d = null; do { dj = rng.nextInt(labeled.size()); d = labeled.get(dj); } while (d == inst || d.classValue() == inst.classValue()); S.setEntry(j, dj, -1); } for (int j = 0; j < unlabeled.size(); ++j) { final Instance inst = unlabeled.get(j); for (int i = 0; i < X.getRowDimension(); ++i) { X.setEntry(i, labeled.size() + j, inst.value(i)); } } return new SequentialProjectionHashLearner(X, XL, S, K, eta, alpha); }
From source file:edu.oregonstate.eecs.mcplan.ml.HilbertSpace.java
public static double inner_prod(final RealVector x, final RealMatrix M, final RealVector y) { // return x.dotProduct( M.operate( y ) ); double s = 0.0; for (int i = 0; i < M.getRowDimension(); ++i) { for (int j = 0; j < M.getColumnDimension(); ++j) { s += x.getEntry(i) * M.getEntry(i, j) * y.getEntry(j); }//from w w w . ja v a 2 s. c o m } return s; }
From source file:lsafunctions.LSA.java
private static RealMatrix normalizeMatrix(RealMatrix M) { double sumColumn = 0; // Matrix row = new Matrix(1, M.getColumnDimension()); RealMatrix row = MatrixUtils.createRealMatrix(1, M.getColumnDimension()); for (int j = 0; j < M.getColumnDimension(); j++) { sumColumn = 0;/*w w w. ja v a 2s . co m*/ for (int k = 0; k < M.getRowDimension(); k++) { sumColumn += Math.pow(M.getEntry(k, j), 2); } sumColumn = Math.sqrt(sumColumn); row.setEntry(0, j, sumColumn); } for (int j = 0; j < M.getColumnDimension(); j++) { for (int k = 0; k < M.getRowDimension(); k++) { M.setEntry(k, j, M.getEntry(k, j) / row.getEntry(0, j)); } } return M; }
From source file:com.rapidminer.tools.math.LinearRegression.java
/** Calculates the coefficients of linear ridge regression. */ public static double[] performRegression(Matrix a, Matrix b, double ridge) { RealMatrix x = MatrixUtils.createRealMatrix(a.getArray()); RealMatrix y = MatrixUtils.createRealMatrix(b.getArray()); int numberOfColumns = x.getColumnDimension(); double[] coefficients = new double[numberOfColumns]; RealMatrix xTransposed = x.transpose(); Matrix result;/*from w ww . j a v a2 s . c om*/ boolean finished = false; while (!finished) { RealMatrix xTx = xTransposed.multiply(x); for (int i = 0; i < numberOfColumns; i++) { xTx.addToEntry(i, i, ridge); } RealMatrix xTy = xTransposed.multiply(y); coefficients = xTy.getColumn(0); try { // do not use Apache LUDecomposition for solve instead because it creates different // results result = new Matrix(xTx.getData()).solve(new Matrix(coefficients, coefficients.length)); for (int i = 0; i < numberOfColumns; i++) { coefficients[i] = result.get(i, 0); } finished = true; } catch (Exception ex) { double ridgeOld = ridge; if (ridge > 0) { ridge *= 10; } else { ridge = 0.0000001; } finished = false; logger.warning("Error during calculation: " + ex.getMessage() + ": Increasing ridge factor from " + ridgeOld + " to " + ridge); } } return coefficients; }
From source file:gedi.util.MathUtils.java
public static double colSum(RealMatrix m, int c) { double re = 0; for (int i = 0; i < m.getColumnDimension(); i++) re += m.getEntry(i, c);/*from w ww. ja v a2s .co m*/ return re; }
From source file:edu.byu.nlp.stats.DirichletMLEOptimizableTest.java
/** * Computes a Newton-Raphson update in-place to alpha. *//*from w ww. j a va 2 s . co m*/ 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:ch.zhaw.iamp.rct.weights.Weights.java
private static RealMatrix addNoise(final RealMatrix A) { RealMatrix buffer = A.copy();//from w w w . j a va2 s .com 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; }
From source file:cooccurrence.Omer_Levy.java
/** * Generic Method to take squareRoot of individual Values. * @param coVariance// w ww. ja v a 2 s . c om * @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: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 . java 2s .com System.out.println(); } }
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// w ww .jav a2s. com ret.append(", "); } } return ret.toString(); }