|
| 1 | +package math; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.nio.file.*; |
| 5 | +import java.util.*; |
| 6 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 7 | +import org.w3c.dom.*; |
| 8 | + |
| 9 | +/** |
| 10 | + * EigenMultiplier — Reads national observation vectors from math/observations/ |
| 11 | + * and multiplies them through Eigenvector matrices defined in .CDNS files. |
| 12 | + * |
| 13 | + * Usage: java math.EigenMultiplier [observation-file] |
| 14 | + * If no file specified, processes all files in observations directory. |
| 15 | + * |
| 16 | + * Observation file format (one vector per line, space-separated): |
| 17 | + * 1.0 2.0 3.0 4.0 5.0 |
| 18 | + * |
| 19 | + * Output: resultant vector after matrix multiplication (Ax = y) |
| 20 | + */ |
| 21 | +public class EigenMultiplier { |
| 22 | + |
| 23 | + private static final String CONFIG_PATH = "math/eigen-config.xml"; |
| 24 | + |
| 25 | + private String eigenlocatorDir; |
| 26 | + private String observationsDir; |
| 27 | + private String outputDir; |
| 28 | + private final Map<String, double[][]> matrices = new LinkedHashMap<>(); |
| 29 | + |
| 30 | + public static void main(String... args) throws Exception { |
| 31 | + EigenMultiplier em = new EigenMultiplier(); |
| 32 | + em.loadConfig(); |
| 33 | + em.loadMatrices(); |
| 34 | + |
| 35 | + if (args.length > 0) { |
| 36 | + em.processObservation(Path.of(em.observationsDir, args[0])); |
| 37 | + } else { |
| 38 | + em.processAllObservations(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private void loadConfig() throws Exception { |
| 43 | + Document doc = DocumentBuilderFactory.newInstance() |
| 44 | + .newDocumentBuilder().parse(new File(CONFIG_PATH)); |
| 45 | + doc.getDocumentElement().normalize(); |
| 46 | + |
| 47 | + eigenlocatorDir = doc.getElementsByTagName("eigenlocator-dir").item(0).getTextContent(); |
| 48 | + observationsDir = doc.getElementsByTagName("observations-dir").item(0).getTextContent(); |
| 49 | + outputDir = doc.getElementsByTagName("output-dir").item(0).getTextContent(); |
| 50 | + |
| 51 | + new File(outputDir).mkdirs(); |
| 52 | + } |
| 53 | + |
| 54 | + private void loadMatrices() throws Exception { |
| 55 | + File dir = new File(eigenlocatorDir); |
| 56 | + for (File f : dir.listFiles((d, n) -> n.endsWith(".CDNS"))) { |
| 57 | + parseCDNS(f); |
| 58 | + } |
| 59 | + System.out.println("[EigenMultiplier] Loaded " + matrices.size() + " matrix(es)"); |
| 60 | + } |
| 61 | + |
| 62 | + private void parseCDNS(File file) throws IOException { |
| 63 | + List<String> lines = Files.readAllLines(file.toPath()); |
| 64 | + String name = null; |
| 65 | + List<double[]> rows = new ArrayList<>(); |
| 66 | + |
| 67 | + for (String line : lines) { |
| 68 | + String trimmed = line.trim(); |
| 69 | + if (trimmed.startsWith("[EV:") && !trimmed.startsWith("[/EV:")) { |
| 70 | + name = trimmed.substring(4, trimmed.length() - 1); |
| 71 | + rows.clear(); |
| 72 | + } else if (trimmed.startsWith("[/EV:")) { |
| 73 | + if (name != null && !rows.isEmpty()) { |
| 74 | + matrices.put(name, rows.toArray(new double[0][])); |
| 75 | + } |
| 76 | + name = null; |
| 77 | + } else if (name != null && !trimmed.isEmpty() && !trimmed.startsWith("#")) { |
| 78 | + String[] parts = trimmed.split("\\s+"); |
| 79 | + double[] row = new double[parts.length]; |
| 80 | + for (int i = 0; i < parts.length; i++) row[i] = Double.parseDouble(parts[i]); |
| 81 | + rows.add(row); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void processAllObservations() throws Exception { |
| 87 | + File dir = new File(observationsDir); |
| 88 | + File[] files = dir.listFiles((d, n) -> !n.startsWith(".")); |
| 89 | + if (files == null || files.length == 0) { |
| 90 | + System.out.println("[EigenMultiplier] No observation files in " + observationsDir); |
| 91 | + return; |
| 92 | + } |
| 93 | + for (File f : files) { |
| 94 | + processObservation(f.toPath()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void processObservation(Path obsPath) throws Exception { |
| 99 | + if (!Files.exists(obsPath)) { |
| 100 | + System.err.println("[EigenMultiplier] Not found: " + obsPath); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + System.out.println("[EigenMultiplier] Processing: " + obsPath.getFileName()); |
| 105 | + List<String> lines = Files.readAllLines(obsPath); |
| 106 | + StringBuilder output = new StringBuilder(); |
| 107 | + output.append("# Results for: ").append(obsPath.getFileName()).append("\n"); |
| 108 | + |
| 109 | + for (Map.Entry<String, double[][]> entry : matrices.entrySet()) { |
| 110 | + String matName = entry.getKey(); |
| 111 | + double[][] matrix = entry.getValue(); |
| 112 | + output.append("# Matrix: ").append(matName).append("\n"); |
| 113 | + |
| 114 | + for (String line : lines) { |
| 115 | + String trimmed = line.trim(); |
| 116 | + if (trimmed.isEmpty() || trimmed.startsWith("#")) continue; |
| 117 | + |
| 118 | + double[] vec = parseVector(trimmed); |
| 119 | + if (vec.length != matrix[0].length) { |
| 120 | + output.append("# ERROR: vector length ").append(vec.length) |
| 121 | + .append(" != matrix cols ").append(matrix[0].length).append("\n"); |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + double[] result = multiply(matrix, vec); |
| 126 | + output.append(formatVector(result)).append("\n"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Path outPath = Path.of(outputDir, obsPath.getFileName().toString() + ".result"); |
| 131 | + Files.writeString(outPath, output.toString()); |
| 132 | + System.out.println("[EigenMultiplier] Output: " + outPath); |
| 133 | + } |
| 134 | + |
| 135 | + private double[] multiply(double[][] matrix, double[] vec) { |
| 136 | + double[] result = new double[matrix.length]; |
| 137 | + for (int i = 0; i < matrix.length; i++) { |
| 138 | + for (int j = 0; j < vec.length; j++) { |
| 139 | + result[i] += matrix[i][j] * vec[j]; |
| 140 | + } |
| 141 | + } |
| 142 | + return result; |
| 143 | + } |
| 144 | + |
| 145 | + private double[] parseVector(String line) { |
| 146 | + String[] parts = line.trim().split("\\s+"); |
| 147 | + double[] v = new double[parts.length]; |
| 148 | + for (int i = 0; i < parts.length; i++) v[i] = Double.parseDouble(parts[i]); |
| 149 | + return v; |
| 150 | + } |
| 151 | + |
| 152 | + private String formatVector(double[] v) { |
| 153 | + StringBuilder sb = new StringBuilder(); |
| 154 | + for (int i = 0; i < v.length; i++) { |
| 155 | + if (i > 0) sb.append(" "); |
| 156 | + sb.append(String.format("%.4f", v[i])); |
| 157 | + } |
| 158 | + return sb.toString(); |
| 159 | + } |
| 160 | +} |
0 commit comments