-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cs
More file actions
233 lines (174 loc) · 4.92 KB
/
Copy pathMatrix.cs
File metadata and controls
233 lines (174 loc) · 4.92 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
223
224
225
226
227
228
229
230
231
232
233
using System;
namespace GMatrix
{
public class Matrix
{
public int n,m;
public int[,] matrix;
//TODO работает только для квадратных
public Matrix(int[,] a)
{
n = Convert.ToInt32(Math.Round(Math.Sqrt(a.Length)));
m = n;
matrix = a;
}
#region Operaions
public static Matrix operator + (Matrix a, Matrix b){
Matrix c = new Matrix(a.n,a.m);
if (a.n == b.n && a.m == b.m){
for (int i=0; i<a.n; i++){
for (int j=0;j<a.m;j++){
c[i,j] = a[i,j]+b[i,j];
}
}
}
return c;
}
public static Matrix operator - (Matrix a, Matrix b){
Matrix c = new Matrix(a.n,a.m);
if (a.n == b.n && a.m == b.m){
for (int i=0; i<a.n; i++){
for (int j=0;j<a.m;j++){
c[i,j] = a[i,j]-b[i,j];
}
}
}
return c;
}
public static Matrix operator * (Matrix a , Matrix b){
if ((a.n == b.m) || (a.m == b.n)) {
Matrix c = new Matrix(a.n,Math.Min(a.m,b.m));
for (int i=0; i<a.n; i++) {
for (int k=0; k<b.m; k++) {
for (int j=0; j<a.m; j++){
Console.WriteLine("c[{0},{1}] = {2} + a[{0},{3}]*b[{0},{1}]",i+1,k+1,c[i,k],j+1);
//Console.WriteLine("c[{0},{1}] = {2} + {3}*{4}", i+1,k+1,c[i,k],a[i+1,j+1],b[i+1,k+1]);
Console.WriteLine();
c[i,k]+=a[i,j]*b[j,k];
}
}
}
return c;
} else {
throw new Exception("Умножение матриц невозможно");
//Console.Write("Умножение матриц невозможно"); return null;
}
}
#endregion
#region I/O
public void input(){
for (int i=0; i<n; i++){
for (int j=0;j<m;j++){
matrix[i,j] = Convert.ToInt32(Console.ReadLine());
}
}
}
public void output(){
for (int i=0; i<this.n; i++){
for (int j=0;j<this.m;j++){
Console.Write("{0,5}",this.matrix[i,j]);
}
Console.WriteLine();
}
}
#endregion
private double Det2x2(){
if ((this.n == 2) && (this.m == 2)){
return this[0,0]*this[1,1]-this[0,1]*this[1,0];
} else {
throw new Exception("Ошибка, матрица не 2x2");
}
}
public Matrix Minor( int a, int b){
//Console.WriteLine("Minor called");
int i, j, p, q;
Matrix MEN = new Matrix(n - 1, n - 1);
for (j = 0, q = 0; q < MEN.m; j++, q++)
for (i = 0, p = 0; p < MEN.n; i++, p++)
{
if (i == a) i++;
if (j == b) j++;
MEN[p, q] = this[i, j];
}
return MEN;
}
public static double Det(Matrix B)
{
int n;
int signo;
double det = 0;
if (B.n != B.m)
{
throw new Exception("Матрица должна быть квадратной");
}
else
if (B.n == 1)
return B[0, 0];
else
if (B.n == 2)
return B.Det2x2();
else
for (n = 0; n < B.m; n++)
{
if ((n % 2) == 0)
{
signo = 1;
}
else
{
signo = -1;
}
Matrix mm = B.Minor(0, n);
//det = det + signo * Convert.ToByte(B[0, n]) * Det(B.Minor(0, n));
det = det + signo * B[0, n] * Det(B.Minor(0, n));
}
return det;
}
#region inverse
/*
public Matrix InverseMatrix(){
bool[,] flags = new bool[this.n,this.m];
int i,j;
double tmp;
Matrix nM = new Matrix(this.n,this.m);
for (i = 0; i < this.n; i++){
for (j = 0; j < this.m; j++)
{
nM[i,j] = Det(this.Minor(i,j));
}
}
for (i = 0; i < this.n; i++){
for (j = 0; j < this.m; j++) {
if (flags[i,j]==false){
nM[j,i] = nM[i,j];
} else {
tmp = nM[i,j];
nM[i,j] = nM[j,i];
nM[j,i] = tmp;
flags[j,i] = true;
}
}
}
for (i=0;i< nM.n; i++){
for (j=0; j<nM.m;j++) {
nM[i,j] = nM[i,j] / Det(this);
}
}
return nM;
}
*/
#endregion
public int this[int i, int j]
{
get { return matrix[i,j]; }
set { matrix[i,j] = value; }
}
public Matrix(int n, int m)
{
this.n = Convert.ToByte(n);
this.m = Convert.ToByte(m);
matrix = new int[n,m];
}
}
}