This repository was archived by the owner on Sep 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInverse.java
More file actions
98 lines (96 loc) · 3.56 KB
/
Inverse.java
File metadata and controls
98 lines (96 loc) · 3.56 KB
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
import java.awt.event.*;
import javax.swing.*;
public class Inverse implements ActionListener{
public float[][] getConfactor(float[][] data, int h, int v) {
int H = data.length;
int V = data[0].length;
float[][] newdata = new float[H-1][V-1];
for(int i=0; i<newdata.length; i++) {
if(i < h-1) {
for(int j=0; j<newdata[i].length; j++) {
if(j < v-1) {
newdata[i][j] = data[i][j];
}else {
newdata[i][j] = data[i][j+1];
}
}
}else {
for(int j=0; j<newdata[i].length; j++) {
if(j < v-1) {
newdata[i][j] = data[i+1][j];
}else {
newdata[i][j] = data[i+1][j+1];
}
}
}
}
return newdata;
}
public float getMartrixResult(float[][] data) {
if(data.length == 2) {
return data[0][0]*data[1][1] - data[0][1]*data[1][0];
}
float result = 0;
int num = data.length;
float[] nums = new float[num];
for(int i=0; i<data.length; i++) {
if(i%2 == 0) {
nums[i] = data[0][i] * getMartrixResult(getConfactor(data, 1, i+1));
}else {
nums[i] = -data[0][i] * getMartrixResult(getConfactor(data, 1, i+1));
}
}
for(int i=0; i<data.length; i++) {
result += nums[i];
}
// System.out.println(result);
return result;
}
public float[][] getReverseMartrix(float[][] data) {
float[][] newdata = new float[data.length][data[0].length];
float A = getMartrixResult(data);
if(A==0){
JOptionPane.showMessageDialog(null,"可逆矩阵不存在,程序终止!","警告",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
// System.out.println(A);
for(int i=0; i<data.length; i++) {
for(int j=0; j<data[0].length; j++) {
if((i+j)%2 == 0) {
newdata[i][j] = getMartrixResult(getConfactor(data, i+1, j+1)) / A;
}else {
newdata[i][j] = -getMartrixResult(getConfactor(data, i+1, j+1)) / A;
}
}
}
newdata = trans(newdata);
return newdata;
}
private float[][] trans(float[][] newdata) {
float[][] newdata2 = new float[newdata[0].length][newdata.length];
for(int i=0; i<newdata.length; i++)
for(int j=0; j<newdata[0].length; j++) {
newdata2[j][i] = newdata[i][j];
}
return newdata2;
}
public void actionPerformed(ActionEvent e){
Scan sc=new Scan();
int c[][]=new int[1][1];
int ds[][]=new int[1][1];
sc.zhen3=getReverseMartrix(Convert(sc.zhen1, sc.ju1[0], sc.ju1[1]));
sc.zhen4=getReverseMartrix(Convert(sc.zhen2, sc.ju2[0], sc.ju2[1]));
sc.Printf(sc.zhen3, sc.zhen3.length, sc.zhen3[0].length);
sc.Printf(sc.zhen4, sc.zhen4.length, sc.zhen4[0].length);
sc.Show("逆", sc.zhen3,sc.zhen4 ,sc.zhen1, sc.zhen2, c, ds, sc.ju1[0], sc.ju1[1], sc.ju2[0], sc.ju2[1]);
}
public float[][] Convert(int [][]d,int m,int n){
float [][]data=new float[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
data[i][j]=(float)d[i][j];
}
}
return data;
}
}