-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
93 lines (84 loc) · 2.99 KB
/
Copy pathMatrix.java
File metadata and controls
93 lines (84 loc) · 2.99 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
import java.util.Random;
public class Matrix {
private int row, col;
private ComplexNumber[][] elem;
public Matrix(int row, int col) {
this.row = row;
this.col = col;
this.elem = new ComplexNumber[row][col];
}
public void completion() {
Random random = new Random();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
this.elem[i][j] = new ComplexNumber(random.nextInt(-10,11), random.nextInt(-10,11));
}
}
}
public void initial(int row, int col, ComplexNumber num) {
elem[row][col] = num;
}
public Matrix sum(Matrix other) {
if (this.row != other.row || this.col != other.col) {
System.out.println("Ошибка: матрицы имеют разный размер.");
return null;
}
Matrix newMatrix = new Matrix(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
newMatrix.initial(i, j, this.elem[i][j].add(other.elem[i][j]));
}
}
return newMatrix;
}
public Matrix differ(Matrix other) {
if (this.row != other.row || this.col != other.col) {
System.out.println("Ошибка: матрицы имеют разный размер.");
return null;
}
Matrix newMatrix = new Matrix(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
newMatrix.initial(i, j, this.elem[i][j].sub(other.elem[i][j]));
}
}
return newMatrix;
}
public Matrix trans() {
Matrix newMatrix = new Matrix(col, row);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
newMatrix.initial(j, i, this.elem[i][j]);
}
}
return newMatrix;
}
public Matrix product(Matrix other) {
if (this.col != other.row) {
System.out.println("Ошибка: количество столбцов в первой матрице не равно количеству строк во второй матрице.");
return null;
}
Matrix newMatrix = new Matrix(this.row, other.col);
for (int i = 0; i < this.row; i++) {
for (int j = 0; j < other.col; j++) {
ComplexNumber sum_elem = new ComplexNumber(0, 0);
for (int k = 0; k < this.col; k++) {
sum_elem = sum_elem.add(this.elem[i][k].mult(other.elem[k][j]));
}
newMatrix.initial(i, j, sum_elem);
}
}
return newMatrix;
}
public void print_matrix() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print("\t");
elem[i][j].print_elem();
System.out.print("\t");
}
System.out.println();
}
System.out.println();
}
}