-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy path7D_Data_onto_3D_(Fourth_Dimension).java
108 lines (86 loc) · 3.88 KB
/
7D_Data_onto_3D_(Fourth_Dimension).java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.Random;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.EigenDecomposition;
import org.apache.commons.math3.linear.RealMatrix;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int numSamples = 100;
double[][] data7d = generateRandomData(numSamples);
// Project 7D data to 3D
double[][] projectedData3d = project7dTo3d(data7d);
// Create the plot
PlotPanel plotPanel = new PlotPanel(projectedData3d, data7d);
plotPanel.showPlot();
}
private static double[][] generateRandomData(int numSamples) {
double[][] data7d = new double[numSamples][7];
Random random = new Random(42);
for (int i = 0; i < numSamples; i++) {
for (int j = 0; j < 7; j++) {
data7d[i][j] = random.nextDouble();
}
}
return data7d;
}
private static double[][] project7dTo3d(double[][] data7d) {
RealMatrix dataMatrix = new Array2DRowRealMatrix(data7d);
EigenDecomposition decomposition = new EigenDecomposition(dataMatrix.transpose().multiply(dataMatrix));
RealMatrix eigenVectors = decomposition.getV();
RealMatrix projectedDataMatrix = dataMatrix.multiply(eigenVectors.getSubMatrix(0, 6, 0, 2));
return projectedDataMatrix.getData();
}
private static class PlotPanel extends JPanel {
private final double[][] projectedData3d;
private final double[][] data7d;
public PlotPanel(double[][] projectedData3d, double[][] data7d) {
this.projectedData3d = projectedData3d;
this.data7d = data7d;
setPreferredSize(new Dimension(800, 800));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int numSamples = data7d.length;
// Scatter plot with colors representing the additional dimensions (4th, 5th, 6th, and 7th)
for (int i = 0; i < numSamples; i++) {
int x = (int) (projectedData3d[i][0] * getWidth());
int y = (int) (projectedData3d[i][1] * getHeight());
int z = (int) (projectedData3d[i][2] * 255);
Color color = new Color(z, z, z);
g2d.setColor(color);
g2d.fillOval(x - 5, y - 5, 10, 10);
}
// Add a color bar for the 4th dimension
int colorBarWidth = 30;
int colorBarHeight = 200;
int colorBarX = getWidth() - colorBarWidth - 10;
int colorBarY = getHeight() / 2 - colorBarHeight / 2;
g2d.setColor(Color.WHITE);
g2d.fillRect(colorBarX, colorBarY, colorBarWidth, colorBarHeight);
for (int i = 0; i < colorBarHeight; i++) {
int colorValue = (int) (i / (double) colorBarHeight * 255);
Color color = new Color(colorValue, colorValue, colorValue);
g2d.setColor(color);
g2d.fillRect(colorBarX, colorBarY + i, colorBarWidth, 1);
}
g2d.setColor(Color.BLACK);
g2d.drawRect(colorBarX, colorBarY, colorBarWidth, colorBarHeight);
g2d.drawString("Fourth Dimension", colorBarX + colorBarWidth / 2 - 30, colorBarY - 10);
}
public void showPlot() {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Scatter Plot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
}