List of usage examples for org.apache.commons.math3.linear RealVector getDimension
public abstract int getDimension();
From source file:com.github.thorbenlindhauer.factor.CanonicalGaussianFactor.java
public static CanonicalGaussianFactor fromMomentForm(Scope scope, RealVector meanVector, RealMatrix covarianceMatrix) {/*from w w w . j av a2 s . c o m*/ // TODO: perform cardinality checks etc. MathUtil mathUtil = new MathUtil(covarianceMatrix); RealMatrix precisionMatrix = mathUtil.invert(); RealVector scaledMeanVector = precisionMatrix.operate(meanVector); int dimension = meanVector.getDimension(); double normalizationConstant = -(0.5d * scaledMeanVector.dotProduct(meanVector)) - (Math .log(Math.pow(2.0d * Math.PI, (double) dimension / 2.0d) * Math.sqrt(mathUtil.determinant()))); return new CanonicalGaussianFactor(scope, precisionMatrix, scaledMeanVector, normalizationConstant); }
From source file:ImageProc.java
public static float[][] colormapJet(int nClass) { int n;/* w ww . j ava 2 s .c o m*/ n = (int) Math.ceil((float) nClass / 4); ArrayRealVector u, r, g, b; RealVector R, G, B; u = new ArrayRealVector(3 * n - 1); for (int i = 0; i < n; i++) { u.setEntry(i, (i + 1.0) / n); u.setEntry(u.getDimension() - i - 1, (i + 1.0) / n); } u.setSubVector(n, new ArrayRealVector(n, 1)); g = new ArrayRealVector(u.getDimension()); float m; m = (float) Math.ceil((float) n / 2); if (nClass % 4 == 1) m = m - 1; for (int i = 0; i < g.getDimension(); i++) { g.setEntry(i, (i + 1 + m)); } r = g.add(new ArrayRealVector(g.getDimension(), n)); b = g.subtract(new ArrayRealVector(g.getDimension(), n)); R = new ArrayRealVector(); G = new ArrayRealVector(); B = new ArrayRealVector(); for (int i = 0; i < r.getDimension(); i++) { if (r.getEntry(i) <= nClass) R = R.append(r.getEntry(i)); } for (int i = 0; i < g.getDimension(); i++) { if (g.getEntry(i) <= nClass) G = G.append(g.getEntry(i)); } for (int i = 0; i < b.getDimension(); i++) { if (b.getEntry(i) >= 1) B = B.append(b.getEntry(i)); } float[][] J = new float[nClass][3]; int index; for (int i = 0; i < R.getDimension(); i++) { index = (int) R.getEntry(i); J[index - 1][0] = (float) u.getEntry(i); } for (int i = 0; i < G.getDimension(); i++) { index = (int) G.getEntry(i); J[index - 1][1] = (float) u.getEntry(i); } for (int i = u.getDimension() - B.getDimension(), j = 0; i < u.getDimension(); i++, j++) { index = (int) B.getEntry(j); J[index - 1][2] = (float) u.getEntry(i); } /* System.out.println("\nRGB Matrix:"); for(int i=0;i<nClass;i++){ for(int j=0;j<3;j++){ System.out.print(J[i][j]+"\t"); } System.out.println(); } */ return J; }
From source file:com.cloudera.oryx.kmeans.computation.covariance.CoMomentTracker.java
public void update(RealVector v) { for (int i = 0; i < v.getDimension(); i++) { double vi = v.getEntry(i); for (int j = i; j < v.getDimension(); j++) { Index idx = new Index(i, j); CoMoment cm = cache.get(idx); if (cm == null) { cm = new CoMoment(); cache.put(idx, cm);/*from w w w .j a va 2 s . com*/ } cm.update(vi, v.getEntry(j)); } } }
From source file:edu.ucsf.valelab.saim.calculations.SaimParameterValidator.java
/** * Very simple validator that uses lower and upper bounds to restrict * parameter values// w ww . j a v a 2 s .co m * @param params * @return */ @Override public RealVector validate(RealVector params) { if (params.getDimension() != 3) throw new DimensionMismatchException(params.getDimension(), 3); RealVector n = params.copy(); for (int i = 0; i < 3; i++) { if (n.getEntry(i) < lowerBounds_[i]) n.setEntry(i, lowerBounds_[i]); if (n.getEntry(i) > upperBounds_[i]) n.setEntry(i, upperBounds_[i]); } return n; }
From source file:com.cloudera.oryx.common.math.VectorsTest.java
@Test public void testDense() { RealVector dense = Vectors.dense(10); assertEquals(10, dense.getDimension()); }
From source file:com.cloudera.oryx.common.math.VectorsTest.java
@Test public void testOf() { RealVector of = Vectors.of(1.0, -2.0, 7.0); assertEquals(3, of.getDimension()); assertEquals(1.0, of.getEntry(0));/*w ww .j a v a 2 s . c o m*/ assertEquals(-2.0, of.getEntry(1)); assertEquals(7.0, of.getEntry(2)); }
From source file:com.cloudera.oryx.common.math.VectorsTest.java
@Test public void testSparse() { RealVector sparse = Vectors.sparse(Integer.MAX_VALUE); assertEquals(Integer.MAX_VALUE, sparse.getDimension()); assertEquals(0.0, sparse.getEntry(0)); }
From source file:io.warp10.script.functions.VECTO.java
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop();//from w w w .j a va 2 s. co m if (!(o instanceof RealVector)) { throw new WarpScriptException(getName() + " expects a vector on top of the stack."); } RealVector vector = (RealVector) o; List<Object> elts = new ArrayList<Object>(vector.getDimension()); for (int i = 0; i < vector.getDimension(); i++) { elts.add(vector.getEntry(i)); } stack.push(elts); return stack; }
From source file:com.cloudera.oryx.common.math.VectorsTest.java
@Test public void testLike() { RealVector of = Vectors.of(1.0, -2.0, 7.0); RealVector like = Vectors.like(of);/*from ww w . j ava 2 s .c o m*/ assertEquals(of.getDimension(), like.getDimension()); assertEquals(0.0, like.getEntry(0)); assertSame(of.getClass(), like.getClass()); }
From source file:edu.oregonstate.eecs.mcplan.domains.blackjack.AbstractionDiscovery.java
private static void writeClustering(final MetricConstrainedKMeans kmeans, final File root, final int iter, final BlackjackParameters params, final String[][] hard_actions, final String[][] soft_actions) throws FileNotFoundException { Csv.write(new PrintStream(new File(root, "M" + iter + ".csv")), kmeans.metric); {//from w ww. ja v a 2s .c om final Csv.Writer writer = new Csv.Writer(new PrintStream(new File(root, "mu" + iter + ".csv"))); for (int i = 0; i < kmeans.d; ++i) { for (int j = 0; j < kmeans.k; ++j) { writer.cell(kmeans.mu()[j].getEntry(i)); } writer.newline(); } } // Lt.operate( x ) maps x to the space defined by the metric final RealMatrix Lt = new CholeskyDecomposition(kmeans.metric).getLT(); { final Csv.Writer writer = new Csv.Writer(new PrintStream(new File(root, "X" + iter + ".csv"))); writer.cell("cluster").cell("label").cell("x1").cell("x2").cell("x3").cell("Ax1").cell("Ax2") .cell("Ax3").newline(); for (int cluster = 0; cluster < kmeans.k; ++cluster) { for (int i = 0; i < kmeans.N; ++i) { if (kmeans.assignments()[i] == cluster) { writer.cell(cluster); final RealVector phi = kmeans.X_.get(i); //Phi.get( i ); final int pv = (int) phi.getEntry(0); final int paces = (int) phi.getEntry(1); final int dv = (int) phi.getEntry(2); if (paces > 0) { writer.cell(soft_actions[pv - params.soft_hand_min][dv - params.dealer_showing_min]); } else { writer.cell(hard_actions[pv - params.hard_hand_min][dv - params.dealer_showing_min]); } for (int j = 0; j < phi.getDimension(); ++j) { writer.cell(phi.getEntry(j)); } final RealVector trans = Lt.operate(phi); for (int j = 0; j < trans.getDimension(); ++j) { writer.cell(trans.getEntry(j)); } writer.newline(); } } } } }