-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
224 lines (194 loc) · 9.53 KB
/
Copy pathMatrix.java
File metadata and controls
224 lines (194 loc) · 9.53 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* @author Петров Дмитрий;
* Класс Matrix, содержащий набор методов для работы с матрицами.
*/
public class Matrix {
/**
* Функция, преобразующая две строки в объекты класса ComplexNumber и производящая умножение двух чмсел. Необходима для работы с комплексными числами.
* @param first_num Первое число типа String.
* @param second_num Второе число типа String.
* @param what_operation Какой тип операции с числами мы собираемся выполнить.
* @return Возвращает объект типа String.
*/
private int numb_of_rows, numb_of_col;
String[][] matrix = new String[1000][1000];
/**
* Метод для получения количества строк матрицы.
* @return Возращает количество строк.
*/
public int getNumb_of_rows() {
return numb_of_rows;
}
/**
* Метод для установления количества строк матрицы объекта Matrix.
* @param numb_of_rows Передаваемое количество строк в матрице.
*/
public void setNumb_of_rows(int numb_of_rows) {
this.numb_of_rows = numb_of_rows;
}
/**
* Метод для получения количества столбцов матрицы.
* @return Возвращает количество столбцов.
*/
public int getNumb_of_col() {
return numb_of_col;
}
/**
* Метод для установления количества столбцов матрицы объекта Matrix.
* @param numb_of_col Передаваемое количество столбцов в матрице.
*/
public void setNumb_of_col(int numb_of_col) {
this.numb_of_col = numb_of_col;
}
/**
* Конструктор класса Matrix, создающий ссылочную переменную, которая указывает на пустой объект класса Matrix.
*/
public Matrix(){}
/**
* Конструктор класса Matrix, создающий матрицу размерами, передаваемыми в конструктор в качестве аргументов.
* @param numb_of_rows Количество строк.
* @param numb_of_col Количество столбцов.
*/
public Matrix(int numb_of_rows, int numb_of_col){
this.setNumb_of_rows(numb_of_rows);
this.setNumb_of_col(numb_of_col);
}
/**
* Метод для вывода матрицы на экран
*/
public void printMatrix(){
for (int i = 0; i < this.getNumb_of_rows(); ++i) {
for (int j = 0; j < this.getNumb_of_col(); ++j) {
if (j == this.getNumb_of_col() - 1) {
System.out.printf("%-15s"+'\n', this.matrix[i][j]);
} else {
System.out.printf("%-15s" + " ", this.matrix[i][j]);
}
}
}
}
/**
* Метод, выводящий транспонированную матрицу от той, к которой он применялся.
*/
public void matrixTransposition(){
Matrix test = new Matrix(this.getNumb_of_col(), this.getNumb_of_rows());
for(int i=0; i < this.getNumb_of_col();i++){
for(int j=0; j < this.getNumb_of_rows();j++){
test.matrix[i][j] = this.matrix[j][i];
}
}
test.printMatrix();
}
/**
* Метод, умножающий, если это возможно, одну матрицу на другую и выводящий полученную матрицу на экран.
* @param h1 Матрица, элементы которой умножаются на матрицу, к которой применяется данный метод.
* @return Возвращает строку, которая говорит о результате умножения.
*/
public String productOfMatrix(Matrix h1){
if(this.getNumb_of_col() != h1.getNumb_of_rows()){
return "Нельзя перемножать матрицы разных размерностей.";
}
else{
Matrix test = new Matrix(this.getNumb_of_rows(), h1.getNumb_of_col());
ComplexNumber h = new ComplexNumber();
String newElement = "0";
for(int i = 0; i < this.getNumb_of_rows();++i){
for(int j = 0; j < h1.getNumb_of_col(); ++j){
for(int k = 0; k < this.getNumb_of_col();++k){
newElement = h.funcForOperations(newElement, h.funcForOperations(this.matrix[i][k], h1.matrix[k][j], "prod"), "sum");
}
test.matrix[i][j] = newElement;
newElement = "0";
}
}
test.printMatrix();
return "Умножение прошло успешно!";
}
}
/**
* Метод, складывающий, если это возможно, две матрицы и выводящий полученную матрицу на экран.
* @param h1 Матрица, элементы которой прибавляются к матрице, к которой применяется данный метод.
* @return Возвращает строку, которая говорит о результате сложения.
*/
public String sumOfMatrix(Matrix h1){
if((this.getNumb_of_rows() == h1.getNumb_of_rows()) && (this.getNumb_of_col() == h1.getNumb_of_col())){
Matrix test = new Matrix(this.getNumb_of_rows(), this.getNumb_of_col());
ComplexNumber h = new ComplexNumber();
for(int i = 0; i < this.getNumb_of_rows();i++){
for(int j = 0; j < this.getNumb_of_col(); j++){
test.matrix[i][j] = h.funcForOperations(this.matrix[i][j], h1.matrix[i][j], "sum");
}
}
test.printMatrix();
return "Сложение прошло успешно!";
}
else{
return "Нельзя сложить матрицы разной размерности!";
}
}
/**
* Метод, меняющий местами две строки данной матрицы.
* @param first_row Номер первой строки в матрице.
* @param second_row Номер второй строки в матрице.
*/
public void swapRows(int first_row, int second_row){
String value = "";
for(int i = 0; i < this.getNumb_of_col(); i++){
value = this.matrix[first_row][i];
this.matrix[first_row][i] = this.matrix[second_row][i];
this.matrix[second_row][i] = value;
value = "";
}
}
/**
* Метод, позволяющий, если это возможно, посчитать определитель матрицы.
* @return Возвращает значение определителя в виде строки.
*/
public String detOfMatrix() {
if (this.getNumb_of_rows() != this.getNumb_of_col()) {
return "Нельзя посчитать определитель у данной матрицы.";
}
else {
int countOfChange = 0;
ComplexNumber h = new ComplexNumber();
Matrix test = new Matrix(this.getNumb_of_rows(), this.getNumb_of_col());
String det = "1";
String a = "0";
for (int i = 0; i < test.getNumb_of_rows(); ++i) {
for (int j = 0; j < test.getNumb_of_col(); ++j) {
test.matrix[i][j] = this.matrix[i][j];
}
}
for (int i = 0; i < this.getNumb_of_rows() - 1; i++) {
int k = i+1;
while (test.matrix[i][i].equals("0")) {
if (test.matrix[i][i].equals("0")) {
test.swapRows(i, k);
countOfChange++;
}
k++;
if (k == this.getNumb_of_rows() && test.matrix[i][i].equals("0")) {
return a;
}
}
String kef = "";
int index = 1;
for (int j = i + 1; j < this.getNumb_of_rows(); j++) {
kef = h.funcForOperations(test.matrix[j][i], test.matrix[j - index][i], "div");
for (int l = i; l < this.getNumb_of_rows(); l++) {
test.matrix[j][l] = h.funcForOperations(test.matrix[j][l], h.funcForOperations(test.matrix[j - index][l], kef, "prod"), "sub");
}
index++;
}
}
for (int j = 0; j < this.getNumb_of_rows(); j++) {
det = h.funcForOperations(det, test.matrix[j][j], "prod");
}
ComplexNumber h1 = new ComplexNumber();
h1.setReal_part(Math.pow(-1, countOfChange));
return h.funcForOperations(det, h1.returnNumberInAlgForm(), "prod");
}
}
}