List of usage examples for org.apache.commons.math.linear RealMatrix subtract
RealMatrix subtract(RealMatrix m) throws IllegalArgumentException;
From source file:net.sf.jtmt.clustering.Cluster.java
/** * Gets the radius.// w ww . j a v a2s. co m * * @return the radius */ public double getRadius() { double radius = 0.0D; if (centroid != null) { for (String docName : docNames) { RealMatrix doc = getDocument(docName); radius += doc.subtract(centroid).getFrobeniusNorm(); } } return radius / docNames.size(); }
From source file:net.sf.jtmt.clustering.Cluster.java
/** * Gets the eucledian distance.// w w w . j a va2 s . co m * * @param doc the doc * @return the eucledian distance */ public double getEucledianDistance(RealMatrix doc) { if (centroid != null) { return doc.subtract(centroid).getFrobeniusNorm(); } return 0.0D; }
From source file:net.sf.jtmt.clustering.Cluster.java
/** * Gets the complete linkage distance./* w w w.ja v a 2 s . co m*/ * * @param doc the doc * @return the complete linkage distance */ public double getCompleteLinkageDistance(RealMatrix doc) { Max max = new Max(); if (docs.size() == 0) { return 0.0D; } double[] distances = new double[docs.size()]; for (int i = 0; i < distances.length; i++) { RealMatrix clusterDoc = docs.get(docNames.get(i)); distances[i] = clusterDoc.subtract(doc).getFrobeniusNorm(); } return max.evaluate(distances); }
From source file:dr.math.Procrustes.java
public Procrustes(RealMatrix X, RealMatrix Xstar, boolean allowTranslation, boolean allowDilation) { rowDimension = X.getRowDimension();//from w w w.jav a 2s .c o m columnDimension = X.getColumnDimension(); if (Xstar.getRowDimension() != rowDimension) { throw new IllegalArgumentException("X and Xstar do not have the same number of rows"); } if (Xstar.getColumnDimension() != columnDimension) { throw new IllegalArgumentException("X and Xstar do not have the same number of columns"); } RealMatrix J = new Array2DRowRealMatrix(rowDimension, rowDimension); if (allowTranslation) { // J <- diag(n) - 1/n * matrix(1, n, n) // for n = 3, J = {{1, -2/3, -2/3}, {-2/3, 1, -2/3}, {-2/3, -2/3, 1}} for (int i = 0; i < rowDimension; i++) { J.setEntry(i, i, 1.0 - (1.0 / rowDimension)); for (int j = i + 1; j < rowDimension; j++) { J.setEntry(i, j, -1.0 / rowDimension); J.setEntry(j, i, -1.0 / rowDimension); } } } else { // J <- diag(n) for (int i = 0; i < rowDimension; i++) { J.setEntry(i, i, 1); } } // C <- t(Xstar) %*% J %*% X RealMatrix C = Xstar.transpose().multiply(J.multiply(X)); // svd.out <- svd(C) // R <- svd.out$v %*% t(svd.out$u) // NB: Apache math does a different SVD from R. TODO Should use Colt library SingularValueDecomposition SVD = new SingularValueDecompositionImpl(C); R = SVD.getV().multiply(SVD.getUT()); // s <- 1 double s = 1.0; // scale = 1 unless dilation is being used if (allowDilation) { // mat1 <- t(Xstar) %*% J %*% X %*% R RealMatrix mat1 = Xstar.transpose().multiply(J.multiply(X.multiply(R))); // mat2 <- t(X) %*% J %*% X RealMatrix mat2 = X.transpose().multiply(J.multiply(X)); // s.numer <- 0 // s.denom <- 0 double numer = 0.0; double denom = 0.0; // for (i in 1:m) { // s.numer <- s.numer + mat1[i, i] // s.denom <- s.denom + mat2[i, i] // } for (int i = 0; i < columnDimension; i++) { numer = numer + mat1.getEntry(i, i); denom = denom + mat2.getEntry(i, i); } // s <- s.numer/s.denom s = numer / denom; } this.s = s; // tt <- matrix(0, m, 1) RealMatrix tmpT = new Array2DRowRealMatrix(columnDimension, 1); // a translation vector of zero unless translation is being used if (allowTranslation) { // tt <- 1/n * t(Xstar - s * X %*% R) %*% matrix(1, n, 1) RealMatrix tmp = new Array2DRowRealMatrix(rowDimension, 1); for (int i = 0; i < rowDimension; i++) { tmp.setEntry(i, 0, 1); } tmpT = Xstar.subtract(X.multiply(R).scalarMultiply(s)).transpose().scalarMultiply(1.0 / rowDimension) .multiply(tmp); } T = tmpT; }
From source file:tools.help.java
public static RealMatrix makeRealKronReduction(RealMatrix source, int ground_wires) { if (ground_wires == 0) { //no Kron Reduction needed return source; } else {//from w ww . j a v a2 s . c om int rows_total = source.getRowDimension(); int cols_total = source.getColumnDimension(); int nn_rows = rows_total - ground_wires; int nn_cols = cols_total - ground_wires; int nm_rows = rows_total - ground_wires; int nm_cols = ground_wires; int mn_rows = ground_wires; int mn_cols = cols_total - ground_wires; int mm_rows = ground_wires; int mm_cols = ground_wires; RealMatrix NN = new Array2DRowRealMatrix(nn_rows, nn_cols); RealMatrix MN = new Array2DRowRealMatrix(mn_rows, mn_cols); RealMatrix NM = new Array2DRowRealMatrix(nm_rows, nm_cols); RealMatrix MM = new Array2DRowRealMatrix(mm_rows, mm_cols); for (int i = 0; i < rows_total; i++) { for (int j = 0; j < cols_total; j++) { if (i < nn_rows && j < nn_cols) { NN.setEntry(i, j, source.getEntry(i, j)); } else if (i >= nn_rows && j < nn_cols) { MN.setEntry(i - nn_rows, j, source.getEntry(i, j)); } else if (i < nn_rows && j >= nn_cols) { NM.setEntry(i, j - nn_cols, source.getEntry(i, j)); } else if (i >= nn_rows && j >= nn_cols) { MM.setEntry(i - nn_rows, j - nn_cols, source.getEntry(i, j)); } } } return NN.subtract(NM.multiply(MM.inverse()).multiply(MN)); } }