List of usage examples for weka.core.matrix Matrix plus
public Matrix plus(Matrix B)
From source file:org.mitre.clustering.AffinityPropagation.java
License:Open Source License
public void run() throws Exception { /*/*w ww . ja v a 2 s . c o m*/ * Allocate space for messages */ Matrix ds; try { ds = getDiag(s); } catch (Exception ex) { ex.printStackTrace(); } A = new Matrix(count, count, 0.0); R = new Matrix(count, count, 0.0); int t = 1; /* * Run parralel affinity prop. updates */ Matrix e = new Matrix(count, convits, 0); Boolean dn = false; int i = -1; // adjusted due to difference in MATLAB indexing Matrix st; if (!symmetric) { st = s; } else { st = s.transpose(); } while (!dn) { i = i + 1; A = A.transpose(); R = R.transpose(); /* * Compute responsibilities */ for (int ii = 0; ii < count; ii++) { Matrix old = R.getMatrix(0, count - 1, ii, ii); Matrix AS = A.getMatrix(0, count - 1, ii, ii).plus(st.getMatrix(0, count - 1, ii, ii)); int I = getMaxIndex(AS); double Y = AS.get(I, 0); /* * Need to use -Double.MAX_VALUE here, as this will return * the smallest NEGATIVE number, as opposed to smallest absolute * floating point number */ AS.set(I, 0, -Double.MAX_VALUE); int I2 = getMaxIndex(AS); double Y2 = AS.get(I2, 0); /* * Set R */ Matrix yMat = new Matrix(count, 1, Y); Matrix stVect = st.getMatrix(0, count - 1, ii, ii); R.setMatrix(0, count - 1, ii, ii, stVect.minus(yMat)); R.set(I, ii, st.get(I, ii) - Y2); Matrix rVect = R.getMatrix(0, count - 1, ii, ii); rVect = rVect.times(1 - lam).plus(old.times(lam)); R.setMatrix(0, count - 1, ii, ii, rVect); } A = A.transpose(); R = R.transpose(); /* * Compute availabilities */ for (int jj = 0; jj < count; jj++) { Matrix old = A.getMatrix(0, count - 1, jj, jj); Matrix Rp = new Matrix(count, 1, 0); for (int c = 0; c < count; c++) { if (R.get(c, jj) < 0.0) { Rp.set(c, 0, 0.0); } else { Rp.set(c, 0, R.get(c, jj)); } } Rp.set(jj, 0, R.get(jj, jj)); Matrix sumM = new Matrix(count, 1, getSum(Rp)); A.setMatrix(0, count - 1, jj, jj, sumM.minus(Rp)); double dA = A.get(jj, jj); Matrix aVect = A.getMatrix(0, count - 1, jj, jj); for (int c = 0; c < count; c++) { if (aVect.get(c, 0) > 0) { aVect.set(c, 0, 0.0); } } A.setMatrix(0, count - 1, jj, jj, aVect); A.set(jj, jj, dA); aVect = A.getMatrix(0, count - 1, jj, jj); aVect = aVect.times(1 - lam).plus(old.times(lam)); A.setMatrix(0, count - 1, jj, jj, aVect); } /* * Check for convergence */ Matrix E = null; try { E = getDiag(A).plus(getDiag(R)); } catch (Exception ex) { ex.printStackTrace(); } for (int c = 0; c < count; c++) { if (E.get(c, 0) > 0) { E.set(c, 0, 1.0); } else { E.set(c, 0, 0.0); } } int index = i % convits; e.setMatrix(0, count - 1, index, index, E); K = getSum(E); if (i >= convits || i >= maxits) { Matrix se = new Matrix(count, 1); for (int c = 0; c < count; c++) { double sum = 0; for (int c2 = 0; c2 < convits; c2++) { sum += e.get(c, c2); } se.set(c, 0, sum); } Matrix seC = matrixIsEqual(se, convits); Matrix seZ = matrixIsEqual(se, 0.0); double value = getSum(seC.plus(seZ)); if (value == count) { unconverged = false; } else { unconverged = true; } if ((!unconverged && K > 0) || i == maxits) { dn = true; } } } /* * Identify exemplars */ Matrix diag = null; try { diag = getDiag(A).plus(getDiag(R)); } catch (Exception ex) { ex.printStackTrace(); } exemplars = new Vector<Integer>(); for (int c = 0; c < count; c++) { if (diag.get(c, 0) > 0) { exemplars.add(c); } } }