List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix getEntry
@Override public double getEntry(final int row, final int column) throws OutOfRangeException
From source file:jmbench.PackageMatrixConversion.java
public static void convertToEjml(final Array2DRowRealMatrix src, final DenseMatrix64F dst) { if ((src.getRowDimension() != dst.getNumRows()) || (src.getColumnDimension() != dst.getNumCols())) { throw new IllegalArgumentException("Matrices are not the same shape"); }// w ww .j av a 2s .c om for (int y = 0; y < src.getRowDimension(); y++) { for (int x = 0; x < src.getColumnDimension(); x++) { dst.set(y, x, src.getEntry(y, x)); } } }
From source file:dataGen.DataGen.java
/** * Saves the Data/*from w w w . j av a2 s .c o m*/ * @param values * The values which should be saved. * @param fw * The File-Writer including the File location * @param nf * How should the data get formatted? * @throws IOException * If Stream to a File couldn't be written/closed */ public static void saveData(Array2DRowRealMatrix values, Writer fw, NumberFormat nf) throws IOException { for (int i = 0; i < values.getRowDimension(); i++) { String row = i + 1 + " "; //String row = ""; for (int j = 0; j < values.getColumnDimension(); j++) { row = row + nf.format(values.getEntry(i, j)) + " "; } fw.write(row); fw.append(System.getProperty("line.separator")); } fw.close(); }
From source file:fp.overlapr.algorithmen.StressMajorization.java
@Deprecated private static ArrayRealVector conjugateGradientsMethod(Array2DRowRealMatrix A, ArrayRealVector b, ArrayRealVector werte) {/*from ww w . j a va2s. c o m*/ Array2DRowRealMatrix preJacobi = new Array2DRowRealMatrix(A.getRowDimension(), A.getColumnDimension()); // Predconditioner berechnen preJacobi.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row == column) { return 1 / A.getEntry(row, column); } else { return 0.0; } } }); // x_k beliebig whlen ArrayRealVector x_k = new ArrayRealVector(werte); // r_k berechnen ArrayRealVector r_k = b.subtract(A.operate(x_k)); // h_k berechnen ArrayRealVector h_k = (ArrayRealVector) preJacobi.operate(r_k); // d_k = r_k ArrayRealVector d_k = h_k; // x_k+1 und r_k+1 und d_k+1, sowie alpha und beta und z erzeugen ArrayRealVector x_k1; ArrayRealVector r_k1; ArrayRealVector d_k1; ArrayRealVector h_k1; double alpha; double beta; ArrayRealVector z; do { // Speichere Matrix-Vektor-Produkt, um es nur einmal auszurechnen z = (ArrayRealVector) A.operate(d_k); // Finde von x_k in Richtung d_k den Ort x_k1 des Minimums der // Funktion E // und aktualisere den Gradienten bzw. das Residuum alpha = r_k.dotProduct(h_k) / d_k.dotProduct(z); x_k1 = x_k.add(d_k.mapMultiply(alpha)); r_k1 = r_k.subtract(z.mapMultiply(alpha)); h_k1 = (ArrayRealVector) preJacobi.operate(r_k1); // Korrigiere die Suchrichtung d_k1 beta = r_k1.dotProduct(h_k1) / r_k.dotProduct(h_k); d_k1 = h_k1.add(d_k.mapMultiply(beta)); // Werte "eins" weitersetzen x_k = x_k1; r_k = r_k1; d_k = d_k1; h_k = h_k1; } while (r_k1.getNorm() > TOL); return x_k1; }
From source file:fp.overlapr.algorithmen.StressMajorization.java
/** * Fhrt die Stress-Majorization durch. siehe: Gansner, Koren, North: Graph * Drawing by Stress Majorization, 2004//ww w .j a v a 2 s . co m * * @param graph * Graph, dessen Knoten-Positionen neu berechnet werden sollen * @param d * Matrix, die die idealen Distanzen (d_ij) zwischen den Knoten * enthlt * @return Matrix, die die neuen x- und y-Werte der einzelnen Knoten enthlt */ public static double[][] doStressMajorization(Graph graph, double[][] d) { int iter = 0; // X holen Array2DRowRealMatrix X = new Array2DRowRealMatrix(graph.getKnotenAnzahl(), 2); for (int i = 0; i < graph.getKnotenAnzahl(); i++) { X.setEntry(i, 0, graph.getKnoten().get(i).getX()); X.setEntry(i, 1, graph.getKnoten().get(i).getY()); } // D holen Array2DRowRealMatrix D = new Array2DRowRealMatrix(d); // W berechnen Array2DRowRealMatrix W = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension()); W.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (D.getEntry(row, column) == 0) { return 0.0; } else { return 1.0 / (D.getEntry(row, column) * D.getEntry(row, column)); } } }); // LW berechnen Array2DRowRealMatrix LW = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension()); LW.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row != column) { return (-1) * W.getEntry(row, column); } else { return value; } } }); LW.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row == column) { double sum = 0; for (int k = 0; k < LW.getColumnDimension(); k++) { if (k != row) { sum = sum + W.getEntry(row, k); } } return sum; } else { return value; } } }); double[][] x = null; while (iter < ITER) { iter++; // LX berechnen Array2DRowRealMatrix LX = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension()); LX.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row != column) { // norm 2 double term1 = FastMath.pow((X.getEntry(row, 0) - X.getEntry(column, 0)), 2); double term2 = FastMath.pow((X.getEntry(row, 1) - X.getEntry(column, 1)), 2); double abst = Math.sqrt(term1 + term2); return (-1) * W.getEntry(row, column) * D.getEntry(row, column) * inv(abst); } else { return value; } } }); LX.walkInRowOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row == column) { double sum = 0; for (int k = 0; k < LX.getColumnDimension(); k++) { if (k != row) { sum = sum + LX.getEntry(row, k); } } return (-1) * sum; } else { return value; } } }); /* * Lineare Gleichungssysteme lsen */ // x-Werte holen ArrayRealVector xWerte = new ArrayRealVector(X.getColumn(0)); // y-Werte holen ArrayRealVector yWerte = new ArrayRealVector(X.getColumn(1)); // b_x berechnen ArrayRealVector b_x = (ArrayRealVector) LX.operate(xWerte); // b_y berechnen ArrayRealVector b_y = (ArrayRealVector) LX.operate(yWerte); /* * CG-Verfahren anwenden */ // neue x-Werte berechnen mittels PCG // xWerte = conjugateGradientsMethod(LW, b_x, xWerte); // neue y-Werte berechnen mittels PCG // yWerte = conjugateGradientsMethod(LW, b_y, yWerte); ConjugateGradient cg = new ConjugateGradient(Integer.MAX_VALUE, TOL, false); xWerte = (ArrayRealVector) cg.solve(LW, JacobiPreconditioner.create(LW), b_x); yWerte = (ArrayRealVector) cg.solve(LW, JacobiPreconditioner.create(LW), b_y); /* * neue Positiones-Werte zurckgeben */ x = new double[X.getRowDimension()][2]; for (int i = 0; i < x.length; i++) { x[i][0] = xWerte.getEntry(i); x[i][1] = yWerte.getEntry(i); X.setEntry(i, 0, xWerte.getEntry(i)); X.setEntry(i, 1, yWerte.getEntry(i)); } } return x; }
From source file:com.clust4j.algo.ClustTests.java
@Test public void mutabilityTest1() { final double eps = 0.3; final Array2DRowRealMatrix mat = getRandom(5, 5); StandardScaler scaler = new StandardScaler().fit(mat); final double val11 = mat.getEntry(0, 0); DBSCAN db1 = new DBSCAN(mat, eps); // No scaling DBSCAN db2 = new DBSCAN(scaler.transform(mat), eps); // Testing mutability of scaling assertTrue(db1.getData().getEntry(0, 0) == val11); assertFalse(db2.getData().getEntry(0, 0) == val11); }
From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java
void printMatrix(Array2DRowRealMatrix a) { for (int i = 0; i < a.getRowDimension(); i++) { for (int j = 0; j < a.getColumnDimension(); j++) System.out.print(a.getEntry(i, j) + " "); System.out.println();/*from w ww .j a v a2 s . c om*/ } }
From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java
/** * @param Clustering//from ww w.ja va 2s .c o m * The array of clusters * @param posi * The index of the merged cluster * @param posj * The index of the cluster that will be eliminated from the * clustering * @param distance * The distance matrix that will be updated */ void updateDistances(ArrayList<SpeakerCluster> clustering, int posi, int posj, Array2DRowRealMatrix distance) { int clusterCount = clustering.size(); for (int i = 0; i < clusterCount; i++) { distance.setEntry(i, posi, computeDistance(clustering.get(i), clustering.get(posi))); distance.setEntry(posi, i, distance.getEntry(i, posi)); } for (int i = posj; i < clusterCount - 1; i++) for (int j = 0; j < clusterCount; j++) distance.setEntry(i, j, distance.getEntry(i + 1, j)); for (int i = 0; i < clusterCount; i++) for (int j = posj; j < clusterCount - 1; j++) distance.setEntry(i, j, distance.getEntry(i, j + 1)); }
From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java
/** * @param Clustering//from ww w . jav a2 s. c o m * The array of clusters */ Array2DRowRealMatrix updateDistances(ArrayList<SpeakerCluster> clustering) { int clusterCount = clustering.size(); Array2DRowRealMatrix distance = new Array2DRowRealMatrix(clusterCount, clusterCount); for (int i = 0; i < clusterCount; i++) { for (int j = 0; j <= i; j++) { distance.setEntry(i, j, computeDistance(clustering.get(i), clustering.get(j))); distance.setEntry(j, i, distance.getEntry(i, j)); } } return distance; }
From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java
/** * @param features The feature vectors to be used for clustering * @return A cluster for each speaker detected based on the feature vectors provided *//*from w w w.jav a 2s . com*/ public ArrayList<SpeakerCluster> cluster(ArrayList<float[]> features) { ArrayList<SpeakerCluster> ret = new ArrayList<SpeakerCluster>(); Array2DRowRealMatrix featuresMatrix = ArrayToRealMatrix(features, features.size()); LinkedList<Integer> l = getAllChangingPoints(featuresMatrix); Iterator<Integer> it = l.iterator(); int curent, previous = it.next(); while (it.hasNext()) { curent = it.next(); Segment s = new Segment(previous * Segment.FRAME_LENGTH, (curent - previous) * (Segment.FRAME_LENGTH)); Array2DRowRealMatrix featuresSubset = (Array2DRowRealMatrix) featuresMatrix.getSubMatrix(previous, curent - 1, 0, 12); ret.add(new SpeakerCluster(s, featuresSubset, getBICValue(featuresSubset))); previous = curent; } int clusterCount = ret.size(); Array2DRowRealMatrix distance; distance = new Array2DRowRealMatrix(clusterCount, clusterCount); distance = updateDistances(ret); while (true) { double distmin = 0; int imin = -1, jmin = -1; for (int i = 0; i < clusterCount; i++) for (int j = 0; j < clusterCount; j++) if (i != j) distmin += distance.getEntry(i, j); distmin /= (clusterCount * (clusterCount - 1) * 4); for (int i = 0; i < clusterCount; i++) { for (int j = 0; j < clusterCount; j++) { if (distance.getEntry(i, j) < distmin && i != j) { distmin = distance.getEntry(i, j); imin = i; jmin = j; } } } if (imin == -1) { break; } ret.get(imin).mergeWith(ret.get(jmin)); updateDistances(ret, imin, jmin, distance); ret.remove(jmin); clusterCount--; } return ret; }
From source file:lanchester.MultiArena3.java
public double[] project(double deltaT, double[] coeffs, double[] eVals, Array2DRowRealMatrix eVects) { int numFoes = coeffs.length; double[] updated = new double[numFoes]; for (int i1 = 0; i1 < numFoes; i1++) { double updatedForce = 0.; if (forces.get(i1).getNumber() > lb) { for (int i2 = 0; i2 < numFoes; i2++) { // updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * timeStep); updatedForce += coeffs[i2] * eVects.getEntry(i1, i2) * Math.exp(eVals[i2] * deltaT); if (updatedForce < 1.) { updatedForce = 0.;/*w ww .j a v a2s . co m*/ // numGone++; } // updatedForce+=coeffs[i2]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i2]*timeStep); // updatedForce+=coeffs[i1]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i1]*timeStep); } } else { updatedForce = lb / 2.; // numGone++; } updated[i1] = updatedForce; } return updated; }