|
25 | 25 | public class AlgorithmUtils { |
26 | 26 | public final static int Max_Int = 2100000000; |
27 | 27 |
|
28 | | - |
29 | 28 | /** |
30 | 29 | * sets all vertex colors to 0. |
31 | 30 | */ |
@@ -296,7 +295,6 @@ public static double getAngle(GPoint rootp, GPoint v1p, GPoint v2p) { |
296 | 295 | double qx = v2p.x - rootp.x; |
297 | 296 | double qy = v2p.y - rootp.y; |
298 | 297 |
|
299 | | - |
300 | 298 | double pDOTq = px * qx + py * qy; |
301 | 299 | double plength = getLength(px, py); |
302 | 300 | double qlength = getLength(qx, qy); |
@@ -417,7 +415,6 @@ public static double round(double value, int decimalPlace) { |
417 | 415 | / power_of_ten; |
418 | 416 | } |
419 | 417 |
|
420 | | - |
421 | 418 | public static double[] round (double[] array, int prec) |
422 | 419 | { |
423 | 420 | for(int i=0;i<array.length;i++) |
@@ -446,48 +443,21 @@ public static String getEigenValues(GraphModel g) { |
446 | 443 | return res.toString(); |
447 | 444 | } |
448 | 445 |
|
449 | | - /** |
450 | | - * Computes the sum of the eigenvalues of A |
451 | | - * |
452 | | - * @param A the given matrix |
453 | | - * @return the sum of the eigenvalues of A |
454 | | - */ |
455 | | - public static double sumOfExpOfEigenValues(Matrix A) { |
456 | | - EigenvalueDecomposition ed = A.eig(); |
457 | | - double[] rv = ed.getRealEigenvalues(); |
| 446 | + private static double sumOfTransformedEigenvalues(Matrix A, java.util.function.DoubleUnaryOperator transform) { |
| 447 | + double[] rv = A.eig().getRealEigenvalues(); |
458 | 448 | double sum = 0; |
459 | | - |
460 | | - //positiv RV |
461 | | - Double[] prv = new Double[rv.length]; |
462 | | - for (int i = 0; i < rv.length; i++) { |
463 | | - prv[i] = Math.exp(rv[i]); |
464 | | - prv[i] = (double)Math.round(prv[i] * 100000d) / 100000d; |
465 | | - sum += prv[i]; |
| 449 | + for (double v : rv) { |
| 450 | + sum += round(transform.applyAsDouble(v), 5); |
466 | 451 | } |
467 | | - |
468 | 452 | return sum; |
469 | 453 | } |
470 | 454 |
|
471 | | - /** |
472 | | - * Computes the sum of the eigenvalues of A |
473 | | - * |
474 | | - * @param A the given matrix |
475 | | - * @return the sum of the eigenvalues of A |
476 | | - */ |
477 | | - public static double sumOfEigenValues(Matrix A) { |
478 | | - EigenvalueDecomposition ed = A.eig(); |
479 | | - double[] rv = ed.getRealEigenvalues(); |
480 | | - double sum = 0; |
481 | | - |
482 | | - //positiv RV |
483 | | - Double[] prv = new Double[rv.length]; |
484 | | - for (int i = 0; i < rv.length; i++) { |
485 | | - prv[i] = Math.abs(rv[i]); |
486 | | - prv[i] = (double)Math.round(prv[i] * 100000d) / 100000d; |
487 | | - sum += prv[i]; |
488 | | - } |
| 455 | + public static double sumOfExpOfEigenValues(Matrix A) { |
| 456 | + return sumOfTransformedEigenvalues(A, Math::exp); |
| 457 | + } |
489 | 458 |
|
490 | | - return sum; |
| 459 | + public static double sumOfEigenValues(Matrix A) { |
| 460 | + return sumOfTransformedEigenvalues(A, Math::abs); |
491 | 461 | } |
492 | 462 |
|
493 | 463 | /** |
@@ -552,6 +522,29 @@ public static String getEigenValues(Matrix A) { |
552 | 522 | return res.toString(); |
553 | 523 | } |
554 | 524 |
|
| 525 | + /** |
| 526 | + * Returns eigenvalues (formatted as "re + im·i" or plain real) and eigenvectors |
| 527 | + * of {@code a}, as a list of display strings, ready to append to a spectrum report. |
| 528 | + */ |
| 529 | + public static ArrayList<String> formatEigenDecomposition(Matrix a) { |
| 530 | + ArrayList<String> result = new ArrayList<>(); |
| 531 | + EigenvalueDecomposition ed = a.eig(); |
| 532 | + double[] rv = ed.getRealEigenvalues(); |
| 533 | + double[] iv = ed.getImagEigenvalues(); |
| 534 | + for (int i = 0; i < rv.length; i++) { |
| 535 | + if (iv[i] != 0) { |
| 536 | + result.add(round(rv[i], 5) + " + " + round(iv[i], 5) + "i"); |
| 537 | + } else { |
| 538 | + result.add(String.valueOf(round(rv[i], 5))); |
| 539 | + } |
| 540 | + } |
| 541 | + result.add("Eigen Vectors:\n"); |
| 542 | + for (double[] vec : ed.getV().getArray()) { |
| 543 | + result.add(Arrays.toString(round(vec, 5))); |
| 544 | + } |
| 545 | + return result; |
| 546 | + } |
| 547 | + |
555 | 548 | // get kth minimum degree |
556 | 549 | public static double getMinNonPendentDegree(GraphModel g) { |
557 | 550 | ArrayList<Integer> al = getDegreesList(g); |
@@ -646,7 +639,6 @@ public static GraphModel createComplementGraph(GraphModel g1) { |
646 | 639 | g2.addVertex(tmp); |
647 | 640 | } |
648 | 641 |
|
649 | | - |
650 | 642 | for(Vertex v1 : g1.getVertexArray()) { |
651 | 643 | for(Vertex v2 : g1.getVertexArray()) { |
652 | 644 | if(v1.getId() != v2.getId()) { |
@@ -728,7 +720,6 @@ public static Matrix getDistanceAdjacencyMatrix (GraphModel g) { |
728 | 720 | return adj; |
729 | 721 | } |
730 | 722 |
|
731 | | - |
732 | 723 | /** |
733 | 724 | * Undirected Laplacian. |
734 | 725 | * |
@@ -801,7 +792,6 @@ public static Matrix getLaplacian(GraphModel g) { |
801 | 792 | return D; |
802 | 793 | } |
803 | 794 |
|
804 | | - |
805 | 795 | public static Matrix getSignlessLaplacian(Matrix A) { |
806 | 796 | //double[][] res=new double[g.numOfVertices()][g.numOfVertices()]; |
807 | 797 | int n = A.getArray().length; |
@@ -935,4 +925,29 @@ public static int[] getEccentricities(GraphModel g) { |
935 | 925 | } |
936 | 926 | return ecc; |
937 | 927 | } |
| 928 | + |
| 929 | + public static int eccentricityOf(GraphModel g, int v, int[][] dist) { |
| 930 | + int max = 0; |
| 931 | + for (int j = 0; j < g.getVerticesCount(); j++) { |
| 932 | + if (max < dist[v][j]) { |
| 933 | + max = dist[v][j]; |
| 934 | + } |
| 935 | + } |
| 936 | + return max; |
| 937 | + } |
| 938 | + |
| 939 | + public static Matrix eccentricityMatrix(GraphModel g, int[][] dist) { |
| 940 | + int n = g.getVerticesCount(); |
| 941 | + Matrix m = new Matrix(n, n); |
| 942 | + for (int i = 0; i < n; i++) { |
| 943 | + for (int j = 0; j < n; j++) { |
| 944 | + int ei = eccentricityOf(g, i, dist); |
| 945 | + int ej = eccentricityOf(g, j, dist); |
| 946 | + if (dist[i][j] == Math.min(ei, ej)) { |
| 947 | + m.set(i, j, dist[i][j]); |
| 948 | + } |
| 949 | + } |
| 950 | + } |
| 951 | + return m; |
| 952 | + } |
938 | 953 | } |
0 commit comments