|
| 1 | +import org.apache.commons.math3.linear.Array2DRowRealMatrix; |
| 2 | +import org.apache.commons.math3.linear.RealMatrix; |
| 3 | +import org.apache.commons.math3.linear.SingularValueDecomposition; |
| 4 | + |
| 5 | +import javax.swing.*; |
| 6 | +import java.awt.*; |
| 7 | +import java.awt.event.ActionEvent; |
| 8 | +import java.awt.event.ActionListener; |
| 9 | + |
| 10 | +public class GaussianDistributionVisualization extends JFrame { |
| 11 | + // Define the parameters for the Gaussian distribution |
| 12 | + private static final double[] mean = new double[]{0, 0}; |
| 13 | + private static final double[][] covarianceMatrix = {{1, 0.5}, {0.5, 1}}; |
| 14 | + |
| 15 | + // Define the function f(x) = ac - cx^2 |
| 16 | + private static final double a = 2; |
| 17 | + private static final double c = 1; |
| 18 | + |
| 19 | + public GaussianDistributionVisualization() { |
| 20 | + // Create a meshgrid for the 2D plot |
| 21 | + int resolution = 100; |
| 22 | + double[] x = linspace(-5, 5, resolution); |
| 23 | + double[] y = linspace(-5, 5, resolution); |
| 24 | + double[][] pos = meshgrid(x, y); |
| 25 | + |
| 26 | + // Calculate the Gaussian probability at each point in the meshgrid |
| 27 | + RealMatrix covarianceMatrixObj = new Array2DRowRealMatrix(covarianceMatrix); |
| 28 | + RealMatrix invCovarianceMatrix = new SingularValueDecomposition(covarianceMatrixObj).getSolver().getInverse(); |
| 29 | + double[][] ZGaussian = gaussianPDF(pos, mean, invCovarianceMatrix); |
| 30 | + |
| 31 | + // Combine Gaussian with f(x) and g(y) to get the desired distribution |
| 32 | + double[][] Z = combineFunctions(ZGaussian, x, y); |
| 33 | + |
| 34 | + // Create the 3D plot |
| 35 | + setTitle("2D Gaussian Distribution with Rotational Symmetry and Hersehel Maxwell Divination"); |
| 36 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 37 | + |
| 38 | + // Create the surface plot |
| 39 | + SurfacePlot surface = new SurfacePlot(Z, x, y); |
| 40 | + add(surface, BorderLayout.CENTER); |
| 41 | + |
| 42 | + // Start animation timer |
| 43 | + Timer timer = new Timer(50, new ActionListener() { |
| 44 | + double t = 0; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void actionPerformed(ActionEvent e) { |
| 48 | + double timeParam = Math.sin(t * 0.1); // Replace this with your 4D data source |
| 49 | + double[][] updatedZ = applyTimeParameter(Z, timeParam); |
| 50 | + surface.setZData(updatedZ); |
| 51 | + surface.repaint(); |
| 52 | + t += 0.1; |
| 53 | + } |
| 54 | + }); |
| 55 | + timer.start(); |
| 56 | + |
| 57 | + // Set the size of the window and display it |
| 58 | + setSize(800, 600); |
| 59 | + setVisible(true); |
| 60 | + } |
| 61 | + |
| 62 | + private double[] linspace(double start, double end, int numPoints) { |
| 63 | + double[] array = new double[numPoints]; |
| 64 | + double step = (end - start) / (numPoints - 1); |
| 65 | + for (int i = 0; i < numPoints; i++) { |
| 66 | + array[i] = start + i * step; |
| 67 | + } |
| 68 | + return array; |
| 69 | + } |
| 70 | + |
| 71 | + private double[][] meshgrid(double[] x, double[] y) { |
| 72 | + int nx = x.length; |
| 73 | + int ny = y.length; |
| 74 | + double[][] grid = new double[nx][ny]; |
| 75 | + for (int i = 0; i < nx; i++) { |
| 76 | + for (int j = 0; j < ny; j++) { |
| 77 | + grid[i][j] = x[i]; |
| 78 | + } |
| 79 | + } |
| 80 | + for (int j = 0; j < ny; j++) { |
| 81 | + for (int i = 0; i < nx; i++) { |
| 82 | + grid[i][j] = y[j]; |
| 83 | + } |
| 84 | + } |
| 85 | + return grid; |
| 86 | + } |
| 87 | + |
| 88 | + private double[][] gaussianPDF(double[][] pos, double[] mean, RealMatrix invCovarianceMatrix) { |
| 89 | + int n = pos.length; |
| 90 | + int m = pos[0].length; |
| 91 | + double[][] Z = new double[n][m]; |
| 92 | + for (int i = 0; i < n; i++) { |
| 93 | + for (int j = 0; j < m; j++) { |
| 94 | + RealMatrix diff = new Array2DRowRealMatrix(new double[]{pos[i][j] - mean[0], pos[i][j] - mean[1]}); |
| 95 | + RealMatrix diffT = diff.transpose(); |
| 96 | + RealMatrix exponent = diffT.multiply(invCovarianceMatrix).multiply(diff); |
| 97 | + Z[i][j] = Math.exp(-0.5 * exponent.getEntry(0, 0)) / (2 * Math.PI * Math.sqrt(covarianceMatrixDeterminant())); |
| 98 | + } |
| 99 | + } |
| 100 | + return Z; |
| 101 | + } |
| 102 | + |
| 103 | + private double covarianceMatrixDeterminant() { |
| 104 | + return covarianceMatrix[0][0] * covarianceMatrix[1][1] - covarianceMatrix[0][1] * covarianceMatrix[1][0]; |
| 105 | + } |
| 106 | + |
| 107 | + private double[][] combineFunctions(double[][] ZGaussian, double[] x, double[] y) { |
| 108 | + int n = ZGaussian.length; |
| 109 | + int m = ZGaussian[0].length; |
| 110 | + double[][] Z = new double[n][m]; |
| 111 | + for (int i = 0; i < n; i++) { |
| 112 | + for (int j = 0; j < m; j++) { |
| 113 | + Z[i][j] = ZGaussian[i][j] * (a * c - c * x[i] * x[i]) * (a * c - c * y[j] * y[j]); |
| 114 | + } |
| 115 | + } |
| 116 | + return Z; |
| 117 | + } |
| 118 | + |
| 119 | + private double[][] applyTimeParameter(double[][] Z, double timeParam) { |
| 120 | + int n = Z.length; |
| 121 | + int m = Z[0].length; |
| 122 | + double[][] updatedZ = new double[n][m]; |
| 123 | + for (int i = 0; i < n; i++) { |
| 124 | + for (int j = 0; j < m; j++) { |
| 125 | + updatedZ[i][j] = Z[i][j] * timeParam; |
| 126 | + } |
| 127 | + } |
| 128 | + return updatedZ; |
| 129 | + } |
| 130 | + |
| 131 | + public static void main(String[] args) { |
| 132 | + SwingUtilities.invokeLater(GaussianDistributionVisualization::new); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +class SurfacePlot extends JPanel { |
| 137 | + private double[][] Z; |
| 138 | + private double[] x; |
| 139 | + private double[] y; |
| 140 | + |
| 141 | + public SurfacePlot(double[][] Z, double[] x, double[] y) { |
| 142 | + this.Z = Z; |
| 143 | + this.x = x; |
| 144 | + this.y = y; |
| 145 | + } |
| 146 | + |
| 147 | + public void setZData(double[][] Z) { |
| 148 | + this.Z = Z; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + protected void paintComponent(Graphics g) { |
| 153 | + super.paintComponent(g); |
| 154 | + if (Z == null || Z.length == 0 || Z[0].length == 0) { |
| 155 | + return; |
| 156 | + } |
| 157 | + int width = getWidth(); |
| 158 | + int height = getHeight(); |
| 159 | + |
| 160 | + double xStep = width / (double) (x.length - 1); |
| 161 | + double yStep = height / (double) (y.length - 1); |
| 162 | + |
| 163 | + double maxValue = Double.MIN_VALUE; |
| 164 | + double minValue = Double.MAX_VALUE; |
| 165 | + |
| 166 | + for (int i = 0; i < x.length; i++) { |
| 167 | + for (int j = 0; j < y.length; j++) { |
| 168 | + maxValue = Math.max(maxValue, Z[i][j]); |
| 169 | + minValue = Math.min(minValue, Z[i][j]); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + for (int i = 0; i < x.length - 1; i++) { |
| 174 | + for (int j = 0; j < y.length - 1; j++) { |
| 175 | + double[] XPoints = {i * xStep, (i + 1) * xStep, (i + 1) * xStep, i * xStep}; |
| 176 | + double[] YPoints = {j * yStep, j * yStep, (j + 1) * yStep, (j + 1) * yStep}; |
| 177 | + |
| 178 | + int[] screenXPoints = new int[4]; |
| 179 | + int[] screenYPoints = new int[4]; |
| 180 | + |
| 181 | + for (int k = 0; k < 4; k++) { |
| 182 | + screenXPoints[k] = (int) XPoints[k]; |
| 183 | + screenYPoints[k] = height - (int) YPoints[k]; |
| 184 | + } |
| 185 | + |
| 186 | + double value = Z[i][j]; |
| 187 | + int colorValue = (int) (255 * (value - minValue) / (maxValue - minValue)); |
| 188 | + Color color = new Color(colorValue, colorValue, colorValue); |
| 189 | + |
| 190 | + g.setColor(color); |
| 191 | + g.fillPolygon(screenXPoints, screenYPoints, 4); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments