Repository navigation
Expand file tree
/
Copy pathMatrix.java
More file actions
129 lines (129 loc) · 2.59 KB
/
Copy pathMatrix.java
File metadata and controls
129 lines (129 loc) · 2.59 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package code;
import java.util.Scanner;
public class Matrix {
public int[][] mat;
private int wide;
private int high;
Matrix(int high,int wide){
Scanner sc=new Scanner(System.in);
this.mat=new int[high][wide];
this.high=high;
this.wide=wide;
for(int i=0;i<high;i++) {
for(int j=0;j<wide;j++) {
mat[i][j]=sc.nextInt();
}
}
}
Matrix(int[][]arr){
wide=arr[0].length;
high=arr.length;
mat=new int[high][wide];
for(int i=0;i<high;i++) {
for(int j=0;j<wide;j++) {
mat[i][j]=arr[i][j];
}
}
}
boolean cross(Matrix b) {
if(this.getWide()!=this.getHigh()) {
return false;
}
int[][] one=this.mat;
int[][] two=b.mat;
int [][]arr=new int[this.getHigh()][b.getWide()];
for(int i=0;i<this.getHigh();i++) {
for(int j=0;j<b.getWide();j++) {
for(int k=0;k<this.getWide();k++) {
arr[i][j]+=one[i][k]*two[k][j];
}
}
}
this.mat=new int[arr.length][arr[0].length];
this.wide=this.mat[0].length;
this.high=this.mat.length;
for(int i=0;i<high;i++) {
for(int j=0;j<high;j++) {
this.mat[i][j]=arr[i][j];
}
}
return true;
}
boolean plus(Matrix b) {
if(this.getWide()!=b.getWide()||this.getHigh()!=b.getHigh()) {
return false;
}
for(int i=0;i<this.high;i++) {
for(int j=0;j<this.wide;j++) {
this.mat[i][j]+=b.mat[i][j];
}
}
return true;
}
void showMat() {
for(int i=0;i<high;i++) {
for(int j=0;j<wide;j++) {
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
}
private long subdet(int[][] a) {
long sum=0;
if(a.length==1) {
sum=a[0][0];
return sum;
}
for(int i=0;i<a.length;i++) {
int[][] t=new int[a.length-1][a.length-1];
for(int j=1;j<a.length;j++) {
int minus=0;
for(int k=0;k<a.length;k++) {
if(k==i) {
minus=1;
continue;
}
t[j-1][k-minus]=a[j][k];
}
}
sum+=(i%2==1?-1:1)*a[0][i]*subdet(t);
}
return sum;
}
public long det() {
if(this.wide!=this.high) {
return 1987654321;
}
long sum=0;
for(int i=0;i<this.wide;i++) {
int[][] a=new int[this.wide-1][this.wide-1];
for(int j=1;j<this.wide;j++) {
int minus=0;
for(int k=0;k<this.wide;k++) {
if(k==i) {
minus=1;
continue;
}
a[j-1][k-minus]=this.mat[j][k];
}
}
sum+=(i%2==1?-1:1)*this.mat[0][i]*subdet(a);
}
return sum;
}
int getWide() {
if(mat==null) {
return 0;
}
if(mat[0]==null) {
return 0;
}
return wide;
}
int getHigh() {
if(mat==null) {
return 0;
}
return high;
}
}