List of usage examples for org.apache.commons.math3.linear RealVector getDimension
public abstract int getDimension();
From source file:org.rhwlab.dispim.datasource.MicroCluster.java
License:asdf
public static RealMatrix precision(List<MicroCluster> data, RealVector mu) { RealMatrix ret = new Array2DRowRealMatrix(mu.getDimension(), mu.getDimension()); RealVector v = new ArrayRealVector(mu.getDimension()); long n = 0;//ww w . j a v a2 s . c o m for (MicroCluster micro : data) { for (int p = 0; p < micro.points.length; ++p) { for (int d = 0; d < mu.getDimension(); ++d) { v.setEntry(d, micro.points[p][d]); } RealVector del = v.subtract(mu); ret = ret.add(del.outerProduct(del)); ++n; } } ret = ret.scalarMultiply(1.0 / n); LUDecomposition lud = new LUDecomposition(ret); RealMatrix prec = null; if (lud.getSolver().isNonSingular()) { prec = lud.getSolver().getInverse(); } return prec; }
From source file:playground.sergioo.facilitiesGenerator2012.WorkFacilitiesGeneration.java
private static Set<PointPerson> getPCATransformation(Collection<PointPerson> points) { RealMatrix pointsM = new Array2DRowRealMatrix(points.iterator().next().getDimension(), points.size()); int k = 0;//from w w w . jav a 2s . c om for (PointND<Double> point : points) { for (int f = 0; f < point.getDimension(); f++) pointsM.setEntry(f, k, point.getElement(f)); k++; } RealMatrix means = new Array2DRowRealMatrix(pointsM.getRowDimension(), 1); for (int r = 0; r < means.getRowDimension(); r++) { double mean = 0; for (int c = 0; c < pointsM.getColumnDimension(); c++) mean += pointsM.getEntry(r, c) / pointsM.getColumnDimension(); means.setEntry(r, 0, mean); } RealMatrix deviations = new Array2DRowRealMatrix(pointsM.getRowDimension(), pointsM.getColumnDimension()); for (int r = 0; r < deviations.getRowDimension(); r++) for (int c = 0; c < deviations.getColumnDimension(); c++) deviations.setEntry(r, c, pointsM.getEntry(r, c) - means.getEntry(r, 0)); RealMatrix covariance = deviations.multiply(deviations.transpose()) .scalarMultiply(1 / (double) pointsM.getColumnDimension()); EigenDecomposition eigenDecomposition = new EigenDecomposition(covariance, 0); RealMatrix eigenVectorsT = eigenDecomposition.getVT(); RealVector eigenValues = new ArrayRealVector(eigenDecomposition.getD().getRowDimension()); for (int r = 0; r < eigenDecomposition.getD().getRowDimension(); r++) eigenValues.setEntry(r, eigenDecomposition.getD().getEntry(r, r)); for (int i = 0; i < eigenValues.getDimension(); i++) { for (int j = i + 1; j < eigenValues.getDimension(); j++) if (eigenValues.getEntry(i) < eigenValues.getEntry(j)) { double tempValue = eigenValues.getEntry(i); eigenValues.setEntry(i, eigenValues.getEntry(j)); eigenValues.setEntry(j, tempValue); RealVector tempVector = eigenVectorsT.getRowVector(i); eigenVectorsT.setRowVector(i, eigenVectorsT.getRowVector(j)); eigenVectorsT.setRowVector(j, tempVector); } eigenVectorsT.setRowVector(i, eigenVectorsT.getRowVector(i).mapMultiply(Math.sqrt(1 / eigenValues.getEntry(i)))); } RealVector standardDeviations = new ArrayRealVector(pointsM.getRowDimension()); for (int r = 0; r < covariance.getRowDimension(); r++) standardDeviations.setEntry(r, Math.sqrt(covariance.getEntry(r, r))); double zValue = standardDeviations.dotProduct(new ArrayRealVector(pointsM.getRowDimension(), 1)); RealMatrix zScore = deviations.scalarMultiply(1 / zValue); pointsM = eigenVectorsT.multiply(zScore); Set<PointPerson> pointsC = new HashSet<PointPerson>(); k = 0; for (PointPerson point : points) { PointPerson pointC = new PointPerson(point.getId(), point.getOccupation(), new Double[] { pointsM.getEntry(0, k), pointsM.getEntry(1, k) }, point.getPlaceType()); pointC.setWeight(point.getWeight()); pointsC.add(pointC); k++; } return pointsC; }
From source file:rcdemo.math.ClosedHermiteSpline.java
public static RealMatrix generate(RealVector p) { int n = p.getDimension(); RealMatrix A = MatrixUtils.createRealMatrix(n, n); RealMatrix B = MatrixUtils.createRealMatrix(n, n); for (int i = 0; i < n; i++) { int ip = (i + 1) % n; int im = (i - 1 + n) % n; A.setEntry(i, im, 2);//from w w w . j a v a 2s . c o m A.setEntry(i, i, 8); A.setEntry(i, ip, 2); B.setEntry(i, im, -6); B.setEntry(i, ip, 6); } RealVector Bp = B.operate(p); DecompositionSolver solver = new CholeskyDecomposition(A).getSolver(); RealVector m = solver.solve(Bp); RealMatrix a = MatrixUtils.createRealMatrix(4, n); for (int i = 0; i < n; i++) { int ip = (i + 1) % n; a.setEntry(0, i, p.getEntry(i)); a.setEntry(1, i, m.getEntry(i)); a.setEntry(2, i, p.getEntry(ip)); a.setEntry(3, i, m.getEntry(ip)); } return a; }
From source file:rcdemo.physics.CombinedForceModel.java
@Override public RealVector getForce(RealVector x, RealVector v) { ArrayRealVector F = new ArrayRealVector(x.getDimension()); for (int i = 0; i < models.size(); i++) { ForceModel model = models.get(i); F.combineToSelf(1.0, factors.get(i), model.getForce(x, v)); }//from w w w . j a v a 2 s .c o m return F; }
From source file:rcdemo.physics.ZeroForceModel.java
@Override public RealVector getForce(RealVector x, RealVector v) { return new ArrayRealVector(x.getDimension()); }
From source file:syncleus.gremlann.model.som.SOM.java
private void updateBounds(final RealVector position) { //make sure we have the proper dimentionality if (position.getDimension() != getDimension()) throw new IllegalArgumentException("Dimentionality mismatch"); for (int dimensionIndex = 0; dimensionIndex < position.getDimension(); dimensionIndex++) { final double v = position.getEntry(dimensionIndex); if (this.getUpperBounds().getEntry(dimensionIndex) < v) this.getUpperBounds().setEntry(dimensionIndex, v); if (this.getLowerBounds().getEntry(dimensionIndex) > v) this.getLowerBounds().setEntry(dimensionIndex, v); }/*from w w w. j ava 2s . c o m*/ }
From source file:syncleus.gremlann.model.som.SOM.java
/** * Creates a new point in the output lattice at the given position. This will * automatically have all inputs connected to it. * * @param position The position of the new output in the lattice. * @since 2.0/*from w ww. j a v a 2 s . c o m*/ */ public Vertex addOutput(final RealVector position) { ensureCorrectOutputDimensions(position.getDimension()); // increase the upper bounds if needed this.updateBounds(position); // create and add the new output neuron Vertex output = newNeuronVertex("output", meta.graph()); outputs.add(output); setPosition(output, position); // connect all inputs to the new neuron for (final Vertex input : this.getInputs()) { weight.set(input, output, getRandomInitialSynapseWeight()); } return output; }