Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- class Matrix {
- public static double[][] inverse(double[][] ix) {
- double[][] ma = new double[ix.length][ix.length];
- for (int i = 0; i < ma.length; i++) {
- for (int j = 0; j < ma[0].length; j++) {
- ma[i][j] = ix[i][j];
- }
- }
- double[][] re = new double[ma.length][ma.length];
- int n = ma.length;
- for (int i = 0; i < n; i++) {
- for (int j = i + 1; j < n; j++) {
- ma[j][i] /= ma[i][i];
- for (int k = i + 1; k < n; k++) {
- ma[j][k] -= ma[i][k] * ma[j][i];
- }
- }
- }
- for (int k = 0; k < n; k++) {
- for (int i = 0; i < n; i++) {
- if (i == k) {
- re[i][k] = 1;
- } else {
- re[i][k] = 0;
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = i + 1; j < n; j++) {
- re[j][k] -= re[i][k] * ma[j][i];
- }
- }
- for (int i = n - 1; i >= 0; i--) {
- for (int j = i + 1; j < n; j++) {
- re[i][k] -= ma[i][j] * re[j][k];
- }
- re[i][k] /= ma[i][i];
- if (Double.isNaN(re[i][k])) {
- ;
- }
- }
- }
- return re;
- }
- }
- class MatrixPanel extends JPanel {
- private static final long serialVersionUID = 1L;
- private int n;
- private JTextField text[][];
- public MatrixPanel(int n, String tittle) {
- this.n = n;
- text = new JTextField[n][n];
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(n, n));
- setLayout(new BorderLayout());
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- text[i][j] = new JTextField(5);
- text[i][j].setText(Double.toString(0));
- panel.add(text[i][j]);
- }
- }
- add(new JLabel(tittle), BorderLayout.NORTH);
- add(panel, BorderLayout.CENTER);
- }
- public void getMatrix(double[][] a) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- a[i][j] = Double.parseDouble(text[i][j].getText());
- }
- }
- }
- public void setMatrix(double[][] a) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- text[i][j].setText(Double.toString(a[i][j]));
- }
- }
- }
- }
- public class MatrixGUI {
- private JButton bt1, bt2;
- private JPanel com;
- private JFrame f;
- private MatrixPanel mpB, mpA;
- private final int n = 5;
- private double[][] inv = new double[n][n];
- private double[][] mat = new double[n][n];
- private final double[][] zero = new double[n][n];
- public MatrixGUI() {
- f = new JFrame("行列計算");
- f.setSize(600, 400);
- Container c = f.getContentPane();
- mpA = new MatrixPanel(n, "行列A");
- mpB = new MatrixPanel(n, "行列B");
- c.add(mpA, BorderLayout.WEST);
- c.add(mpB, BorderLayout.EAST);
- c.add(new JLabel("=>"));
- com = new JPanel();
- bt1 = new JButton("逆行列");
- bt1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- mpA.getMatrix(mat);
- inv = Matrix.inverse(mat);
- mpB.setMatrix(inv);
- }
- });
- bt2 = new JButton("リセット");
- bt2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- mpA.setMatrix(zero);
- mpB.setMatrix(zero);
- }
- });
- com.add(bt1);
- com.add(bt2);
- c.add(com, BorderLayout.SOUTH);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setVisible(true);
- }
- public static void main(String args[]) {
- new MatrixGUI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement