using System using System Collections Generic using System Linq using

  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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Methods_First_Task
{
#region Matrix_Class
class Matrix
{
double[,] data;
int n, k;
public Matrix(int n, int k)
{
this.n = n;
this.k = k;
data = new double[n, k];
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
data[i, j] = 0;
}
public Matrix(Matrix c)
{
this.n = c.n;
this.k = c.k;
data = new double[n, k];
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
data[i, j] = c.data[i, j];
}
public int getColumns()
{
return k;
}
public int getRows()
{
return n;
}
public void makeIdentity()
{
if (n != k)
{
Console.WriteLine("MAKE IDENTITY ERROR: N != K");
return;
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
{
data[i, j] = ((i == j) ? 1 : 0);
}
}
public Matrix(double[,] d)
{
n = d.GetLength(0);
k = d.GetLength(1);
data = new double[n, k];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
data[i, j] = d[i, j];
}
public void setValue(double v, int i, int j)
{
data[i, j] = v;
}
public double getValue(int i, int j)
{
return data[i, j];
}
static public Matrix operator *(Matrix inp1, Matrix inp2)
{
if (inp1.k != inp2.n)
{
Console.WriteLine("MATRIX MULT ERROR");
return new Matrix(1, 1);
}
Matrix rs = new Matrix(inp1.n, inp2.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp2.k; ++j)
{
double tmp = 0;
for (int c = 0; c < inp1.k; ++c)
tmp += inp1.data[i, c] * inp2.data[c, j];
rs.setValue(tmp, i, j);
}
}
return rs;
}
static public Matrix operator *(Matrix inp1, double alpha)
{
Matrix rs = new Matrix(inp1.n, inp1.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp1.k; ++j)
{
rs.setValue(inp1.data[i, j] * alpha, i, j);
}
}
return rs;
}
static public Matrix operator -(Matrix inp1, double alpha)
{
Matrix rs = new Matrix(inp1.n, inp1.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp1.k; ++j)
{
rs.setValue(inp1.data[i, j] - alpha, i, j);
}
}
return rs;
}
static public Matrix operator -(Matrix inp1, Matrix inp2)
{
if (inp1.n != inp2.n || inp1.k != inp2.k)
{
Console.WriteLine("Operator- ERROR: Matr size not equal");
return new Matrix(1, 1);
}
Matrix rs = new Matrix(inp1.n, inp1.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp1.k; ++j)
{
rs.setValue(inp1.data[i, j] - inp2.data[i, j], i, j);
}
}
return rs;
}
static public Matrix operator +(Matrix inp1, Matrix inp2)
{
if (inp1.n != inp2.n || inp1.k != inp2.k)
{
Console.WriteLine("Operator+ ERROR: Matr size not equal");
return new Matrix(1, 1);
}
Matrix rs = new Matrix(inp1.n, inp1.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp1.k; ++j)
{
rs.setValue(inp1.data[i, j] + inp2.data[i, j], i, j);
}
}
return rs;
}
/// Tensor multiplication of vectors
static public Matrix operator ^(Matrix inp1, Matrix inp2)
{
if (inp1.k != inp2.n || inp1.n != inp2.k)
{
Console.WriteLine("Operator^ ERROR: Matr size not equal");
return new Matrix(1, 1);
}
Matrix rs = new Matrix(inp1.n, inp2.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp2.k; ++j)
{
rs.setValue(inp1.data[i, 0] * inp2.data[0, j], i, j);
}
}
return rs;
}
//транспонированная матрица
public Matrix getTranspozed()
{
Matrix ret = new Matrix(k, n);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < k; ++j)
{
ret.setValue(data[i, j], j, i);
}
}
return ret;
}
public void transpoze()
{
double[,] ret = new double[k, n];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < k; ++j)
{
ret[j, i] = data[i, j];
}
}
data = ret;
}
public Matrix getSubMatrix(int si, int sj, int ei, int ej)
{
if (si >= n || sj >= k || ei >= n || ej >= k ||
si < 0 || sj < 0 || ei < 0 || ej < 0)
{
Console.WriteLine("getSubMatrix RANGE ERROR");
return new Matrix(1, 1);
}
Matrix retMtr = new Matrix(ei - si + 1, ej - sj + 1);
int fi = 0;
int fj = 0;
for (int i = si; i <= ei; ++i)
{
fj = 0;
for (int j = sj; j <= ej; ++j)
{
retMtr.setValue(data[i, j], fi, fj);
++fj;
}
++fi;
}
return retMtr;
}
public bool isZeroMatrix()
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
if (data[i, j] != 0)
return false;
return true;
}
public void printMatrix()
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < k; ++j)
{
Console.Write("{0, 3:f} ", data[i, j]);
}
Console.WriteLine();
}
Console.WriteLine(); Console.WriteLine(); Console.WriteLine();
}
public double getMaximumAboveDiag(ref int ii, ref int jj)
{
double val = -1000 * 1000 * 1000;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < k; ++j)
{
if (val < Math.Abs(data[i, j]))
{
val = Math.Abs(data[i, j]);
ii = i;
jj = j;
}
}
}
return val;
}
}
#endregion
class Methods
{
public double[,] rightSide;
int str = 0;
int stl = 0;
public Methods(int str, int stl)
{
rightSide = Methods.rightSideMatrix(str, stl);
this.str = str;
}
// единичная матрица
public static double[,] rightSideMatrix(int str, int stl)
{
double[,] rightSide = new double[str, stl];
for (int i = 0; i < str; i++)
{
for (int j = 0; j < stl; j++)
{
if (i == j)
rightSide[i, j] = 1;
else
rightSide[i, j] = 0;
}
}
return rightSide;
}
//public double[,] equationMatrix = new double[3, 3] { { 3, 45, 1 }, { 2, 15, 3 }, { 9, 8, 12 } };
// матрица A
public double[,] equationMatrix = new double[3, 3] { { 5, 1, 2 }, { 1, 4, 1 }, { 2, 1, 3 } };
// Метод Гаусса
public void gauss_solver()
{
int n = str;//this.rightSide.Length;
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
{
double value = equationMatrix[i, i];
double koeff = equationMatrix[j, i] / value;
for (int c = 0; c < n; ++c)
equationMatrix[j, c] -= koeff * equationMatrix[i, c];
// для всех элементов строки
for (int k = 0; k < n; k++)
{
rightSide[j, k] -= koeff * rightSide[i, k];
}
}
for (int i = n - 1; i >= 0; --i)
for (int j = i - 1; j >= 0; --j)
{
double value = equationMatrix[i, i];
double koeff = equationMatrix[j, i] / value;
for (int c = 0; c < n; ++c)
equationMatrix[j, c] -= koeff * equationMatrix[i, c];
for (int k = 0; k < n; k++)
{
rightSide[j, k] -= koeff * rightSide[i, k];
}
}
for (int i = 0; i < n; ++i)
{
for (int k = 0; k < n; k++)
{
rightSide[i, k] = rightSide[i, k] / equationMatrix[i, i];
}
}
}
static double getNorm(Matrix a)
{
return Math.Sqrt((a.getTranspozed() * a).getValue(0, 0));
}
//public double
//Matrix[] triada = new Matrix[3];
// метод скалярных произведений
public void smAlgorithm(Matrix A, Matrix y, ref Matrix x, ref double v, bool secondIter = false)
{
int n = A.getColumns();
double EPS = 0.000001;
double s = (y.getTranspozed() * y).getValue(0, 0);
double yNorm = Math.Sqrt(s);
Matrix lx = y * (1.0f / yNorm);
double lamdda = 0;
double lastLamda = 0;
for (int iter = 0; iter < 100000; ++iter)
{
y = A * lx;
if (secondIter)
y = y - x * ((x.getTranspozed() * y).getValue(0, 0));
s = (y.getTranspozed() * y).getValue(0, 0);
double t = (lx.getTranspozed() * y).getValue(0, 0);
yNorm = Math.Sqrt(s);
lx = y * (1.0 / yNorm);
lastLamda = lamdda;
lamdda = s / t;
if (Math.Abs(lastLamda - lamdda) < EPS)
break;
}
v = lamdda;
x = lx;
}
public void findSecondBySm(Matrix A)
{
Matrix y = new Matrix(A.getColumns(), 1);
Matrix x1 = new Matrix(A.getColumns(), 1);
double vl = 0;
for (int i = 0; i < A.getColumns(); ++i) y.setValue(1, i, 0);
smAlgorithm(A, y, ref x1, ref vl);
Console.WriteLine("Максимально собственное значение: {0}: ", vl);
Console.WriteLine("Собственный вектор максимального числа:");
x1.printMatrix();
for (int i = 0; i < A.getColumns(); ++i) y.setValue(123, i, 0);
y = y - x1 * ((x1.getTranspozed() * y).getValue(0, 0));
smAlgorithm(A, y, ref x1, ref vl, true);
//Console.WriteLine("adfasdf");
Console.WriteLine("Второе по величине собственное число: {0}", vl);
//Console.WriteLine(vl);
Console.WriteLine("Его собственный вектор:");
x1.printMatrix();
}
// Метод Якоби - не работает
public static void Jacobi(int n, double[][] A, double[] F, double[] X)
{
double EPS = 0.000001;
double[] TempX = new double[n];
double norm;
for (int i = 0; i < n; i++)
{
TempX[i] = X[i];
}
do
{
for (int i = 0; i < n; i++)
{
TempX[i] = F[i];
for (int g = 0; g < n; g++)
{
if (i != g)
TempX[i] -= A[i][g] * X[g];
}
TempX[i] /= A[i][i];
}
norm = Math.Abs(X[0] - TempX[0]);
for (int h = 0; h < n; h++)
{
if (Math.Abs(X[h] - TempX[h]) > norm)
norm = Math.Abs(X[h] - TempX[h]);
X[h] = TempX[h];
}
}
while (norm > EPS);
}
// Метод Зейделя
static bool ZeidelEnd(double[] xk, double[] xp, int n, double EPS)
{
double norm = 0;
for (int i = 0; i < n; i++)
{
norm += (xk[i] - xp[i]) * (xk[i] - xp[i]);
}
if (Math.Sqrt(norm) >= EPS)
return false;
return true;
}
public static void Zeidel(int n, double[,] a, double[] x, double[] p, double[] rightSide)
{
do
{
for (int i = 0; i < n; i++)
{
p[i] = x[i];
}
for (int i = 0; i < n; i++)
{
double var = 0;
for (int j = 0; j < i; j++)
{
var += (a[i, j] * x[j]);
}
for (int j = i + 1; j < n; j++)
{
var += (a[i, j] * p[j]);
}
x[i] = (rightSide[i] - var) / a[i,i];
}
}
while(!ZeidelEnd(x, p, n, 0.0001));
}
}
class Program
{
static void Main(string[] args)
{
double[,] test = Methods.rightSideMatrix(3, 3);
Methods a = new Methods(3, 3);
double[,] test2 = a.rightSide;
a.gauss_solver();
Console.WriteLine("Gauss:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(a.rightSide[i, j].ToString("0.00"));
Console.Write(" ");
}
Console.WriteLine();
}
double[,] res = new double[3, 3] { { 5, 1, 2 }, { 1, 4, 1 }, { 2, 1, 3 } };
Matrix eqMatrix = new Matrix(res);
a.findSecondBySm(eqMatrix);
// Метод Якоби
//double[][] JA = new double[3][] { new double[3]{ 1, 1, 1 }, new double[3]{4, 2, 1 }, new double[3]{9,3,1 } };
//double[] JFree = new double[] { 0, 1, 3 };
//double[] JRes = new double[3] {1,1,1};
//Methods.Jacobi(3, JA, JFree, JRes);
//Console.WriteLine("{0} {1} {2}", JRes[0], JRes[1], JRes[2]);
//Метод Зейделя
double[,] ZA = new double[3, 3] { { 1, 1, 1 }, { 4, 2, 1 }, { 9, 3, 1 } };
double[] ZFree = new double[] { 0, 1, 3 };
double[] ZRes = new double[3] { 0, 0, 0 };
double[] Zpred = new double[3];
Methods.Zeidel(3, ZA, ZRes, Zpred, ZFree);
Console.ReadLine();
}
}
}