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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumMethods
{
class Mtr
{
double[,] data;
int n, k;
public Mtr(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 Mtr(Mtr 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 Mtr(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 Mtr operator *(Mtr inp1, Mtr inp2)
{
if (inp1.k != inp2.n)
{
Console.WriteLine("MATRIX MULT ERROR");
return new Mtr(1, 1);
}
Mtr rs = new Mtr(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 Mtr operator *(Mtr inp1, double alpha)
{
Mtr rs = new Mtr(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 Mtr operator -(Mtr inp1, double alpha)
{
Mtr rs = new Mtr(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 Mtr operator -(Mtr inp1, Mtr inp2)
{
if (inp1.n != inp2.n || inp1.k != inp2.k)
{
Console.WriteLine("Operator- ERROR: Matr size not equal");
return new Mtr(1, 1);
}
Mtr rs = new Mtr(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 Mtr operator +(Mtr inp1, Mtr inp2)
{
if (inp1.n != inp2.n || inp1.k != inp2.k)
{
Console.WriteLine("Operator+ ERROR: Matr size not equal");
return new Mtr(1, 1);
}
Mtr rs = new Mtr(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 Mtr operator ^(Mtr inp1, Mtr inp2)
{
if (inp1.k != inp2.n || inp1.n != inp2.k)
{
Console.WriteLine("Operator^ ERROR: Matr size not equal");
return new Mtr(1, 1);
}
Mtr rs = new Mtr(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 Mtr getTranspozed()
{
Mtr ret = new Mtr(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 Mtr 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 Mtr(1, 1);
}
Mtr retMtr = new Mtr(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;
}
}
class Program
{
static int mSign(double vl)
{
if (vl >= 0)
return 1;
else
return -1;
}
static bool isNull(Mtr col)
{
for (int i = 1; i < col.getRows(); ++i)
if (col.getValue(i, 0) != 0)
return false;
return true;
}
static Mtr uniteMtr(Mtr A, Mtr B)
{
int bi = A.getRows() - 1;
for (int i = B.getRows() - 1; i >= 0; --i)
{
int bj = A.getColumns() - 1;
for (int j = B.getColumns() - 1; j >= 0; --j)
{
A.setValue(B.getValue(i, j), bi, bj);
--bj;
}
--bi;
}
return A;
}
static void planarRotationMethod(Mtr A)
{
const double EPS = 0.001;
A.printMatrix();
int ii = 0, jj = 0;
Mtr B = A;
int n = A.getRows();
int k = A.getColumns();
Mtr globalT = new Mtr(n, k);
globalT.makeIdentity();
for (int iteration = 0; iteration < 300; ++iteration)
{
Mtr locT = new Mtr(n, k);
locT.makeIdentity();
double aii = B.getMaximumAboveDiag(ref ii, ref jj);
if (Math.Abs(aii) < EPS)
break;
double p = 2 * aii;
double q = B.getValue(ii, ii) - B.getValue(jj, jj);
double d = Math.Sqrt(p * p + q * q);
double r = 0, c = 0, s = 0;
if (Math.Abs(q) > EPS)
{
r = Math.Abs(q) / (2 * d);
c = Math.Sqrt(0.5f + r);
s = Math.Sqrt(0.5f - r) * Math.Sign(p * q);
}
else
{
c = s = Math.Sqrt(2.0f) / 2.0;
}
locT.setValue(c, ii, ii);
locT.setValue(c, jj, jj);
locT.setValue(-s, ii, jj);
locT.setValue(s, jj, ii);
globalT = globalT * locT;
B = locT.getTranspozed() * B * locT;
}
Console.WriteLine("Eigen numbers");
B.printMatrix();
Console.WriteLine("Eigen vectors");
globalT.printMatrix();
}
static Mtr QR_Decomposition(ref Mtr A)
{
int si, sj;
si = A.getRows();
sj = A.getColumns();
Mtr y;
Mtr U = new Mtr(si, sj);
U.makeIdentity();
for (int i = 0; i < A.getColumns() - 1; ++i)
{
y = A.getSubMatrix(i, i, A.getRows() - 1, i);
if (isNull(y))
continue;
Mtr locV = new Mtr(si - i, sj - i);
Mtr locU = new Mtr(si, sj);
locU.makeIdentity();
locV.makeIdentity();
Mtr omega = new Mtr(si - i, 1);
Mtr vecx = new Mtr(si - i, 1);
vecx.setValue(1, 0, 0);
double yNorm = Math.Sqrt((y.getTranspozed() * y).getValue(0, 0));
int sgn = mSign(y.getValue(0, 0));
omega = y + vecx * (sgn * yNorm);
double omegaNorm = Math.Sqrt((omega.getTranspozed() * omega).getValue(0, 0));
omega = omega * (1 / omegaNorm);
locV = locV - (omega ^ omega.getTranspozed()) * 2;
locU = uniteMtr(locU, locV);
A = locU * A;
U = locU * U;
}
return U;
}
static void LuDecomposition(Mtr A, ref Mtr L, ref Mtr U)
{
if (A.getRows() != A.getColumns())
return;
int n = A.getRows();
L.makeIdentity();
for (int i = 0; i < n; ++i)
{
for (int j = i; j < n; ++j)
{
double temp = A.getValue(i, j);
for (int k = 0; k < i; ++k)
{
temp = temp - U.getValue(k, j) * L.getValue(i, k);
}
U.setValue(temp, i, j);
}
for (int j = i + 1; j < n; ++j)
{
double temp = A.getValue(j, i);
for (int k = 0; k < i; ++k)
{
temp = temp - U.getValue(k, i) * L.getValue(j, k);
}
temp = temp / U.getValue(i, i);
L.setValue(temp, j, i);
}
}
}
static void inversedLuSolver(Mtr A, Mtr b)
{
int n = A.getColumns();
Mtr L = new Mtr(n, n);
Mtr U = new Mtr(n, n);
LuDecomposition(A,ref L, ref U);
Mtr inversed = new Mtr(n, n);
for (int j = n - 1; j >= 0; --j)
{
double tmp = 1;
for (int k = j + 1; k < n; ++k)
tmp -= U.getValue(j, k) * inversed.getValue(k, j);
tmp /= U.getValue(j, j);
inversed.setValue(tmp, j, j);
for (int i = j - 1; i >= 0; --i)
{
tmp = 0;
for (int k = i + 1; k < n; ++k)
tmp += U.getValue(i, k) * inversed.getValue(k, j);
tmp = -tmp / U.getValue(i, i);
inversed.setValue(tmp, i, j);
}
for (int i = j - 1; i >= 0; --i)
{
tmp = 0;
for (int k = i + 1; k < n; ++k)
tmp += inversed.getValue(j, k) * L.getValue(k, i);
inversed.setValue(-tmp, j, i);
}
}
Mtr answer = inversed * b;
Console.WriteLine("LU DECOMPOSITION SOLVER: ");
answer.printMatrix();
Console.WriteLine("-------------------------------------");
}
static void QRDecomposeSolver(Mtr A, Mtr b)
{
int SIZE = b.getRows();
Mtr U;
U = QR_Decomposition(ref A);
b = U * b;
double[] results = new double[SIZE];
for (int x = SIZE - 1; x >= 0; --x)
{
double vl = b.getValue(x, 0);
for (int j = x + 1; j < SIZE; ++j)
{
vl -= A.getValue(x, j) * results[j];
}
double dividor = A.getValue(x, x);
results[x] = vl / dividor;
}
Console.WriteLine("QR decomposition solver:");
for (int i = 0; i < SIZE; ++i)
{
Console.WriteLine("X: {0, 3:f} ", results[i]);
}
Console.WriteLine("-------------------------------------");
}
static int CubeDecompositionSolver(Mtr A, Mtr b)
{
int n = A.getRows();
Mtr U = new Mtr(n, n);
double uii;
for (int i = 0; i < n; ++i)
{
double tmp = A.getValue(i, i);
for (int k = 0; k < i; ++k)
tmp -= U.getValue(k, i) * U.getValue(k, i);
if (tmp < 0)
{
Console.WriteLine("Negative tmp in square root!");
return -1;
}
tmp = Math.Sqrt(tmp);
uii = tmp;
if (uii == 0)
{
Console.WriteLine("Zero division in uii!");
return -1;
}
U.setValue(uii, i, i);
for (int j = i + 1; j < n; ++j)
{
tmp = A.getValue(i, j);
for (int k = 0; k < i; ++k)
tmp -= U.getValue(k, i) * U.getValue(k, j);
tmp /= uii;
U.setValue(tmp, i, j);
}
}
double[] y = new double[n];
for (int i = 0; i < n; ++i)
{
double tmp = b.getValue(i, 0);
for (int j = 0; j < i; ++j)
{
tmp -= U.getValue(j, i) * y[j];
}
tmp /= U.getValue(i, i);
y[i] = tmp;
}
Mtr ans = new Mtr(n, 1);
for (int i = n - 1; i >= 0; --i)
{
double tmp = y[i];
for (int k = i + 1; k < n; ++k)
{
tmp -= U.getValue(i, k) * ans.getValue(k, 0);
}
tmp /= U.getValue(i, i);
ans.setValue(tmp, i, 0);
}
Console.WriteLine("Cube decomposition solver:");
ans.printMatrix();
Console.WriteLine("-------------------------------------");
return 1;
}
static void SimpleIteration(Mtr A, Mtr b)
{
int n = A.getRows();
Mtr B = new Mtr(n, n);
Mtr C = new Mtr(n, 1);
Mtr X = new Mtr(n, 1);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (i == j)
continue;
B.setValue(-A.getValue(i, j) / A.getValue(i, i), i, j);
}
C.setValue(b.getValue(i, 0) / A.getValue(i, i), i, 0);
X.setValue(1, i, 0);
}
for (int i = 0; i < 100; ++i)
{
X = B * X + C;
}
Console.WriteLine("Simple iteration solver:");
X.printMatrix();
Console.WriteLine("-------------------------------------");
}
static double getNorm(Mtr a)
{
return Math.Sqrt((a.getTranspozed() * a).getValue(0,0));
}
static int pmAlgorithm(Mtr A, ref double bEigen, ref Mtr vecEigen)
{
int n = A.getRows();
const double pmEPS = 0.0000001;
Mtr y = new Mtr(n, 1);
double[] diffs = new double[n];
for (int i = 0; i < n; ++i)
{ y.setValue(1, i, 0);
diffs[i] = 100;
}
double lastSum = 0;
for (int i = 0; i < 1000; ++i)
{
lastSum = 0;
Mtr x = y * (1 / getNorm(y));
Mtr ny = A * x;
bool stop = true;
for (int j = 0; j < n; ++j)
{
if (Math.Abs(x.getValue(j, 0)) > pmEPS)
{
if (Math.Abs(ny.getValue(j, 0) / x.getValue(j, 0) - diffs[j]) > pmEPS)
stop = false;
diffs[j] = ny.getValue(j, 0) / x.getValue(j, 0);
lastSum += diffs[j];
}
}
y = ny;
if (stop)
break;
}
vecEigen = y * (1 / getNorm(y)); ;
bEigen = lastSum / n;
return 1;
}
static void findMinimalByPm(Mtr A)
{
double maxEigen = 0;
Mtr res = new Mtr(1, 1);
pmAlgorithm(A, ref maxEigen, ref res);
Mtr substr = new Mtr(A.getRows(), A.getColumns());
substr.makeIdentity();
A = A - substr * maxEigen;
double nEigen = 0;
pmAlgorithm(A, ref nEigen, ref res);
Console.WriteLine(nEigen + maxEigen);
res.printMatrix();
}
static void Main(string[] args)
{
int SIZE = 2;
Mtr A = new Mtr(SIZE, SIZE);
Mtr b = new Mtr(SIZE, 1);
Mtr L = new Mtr(SIZE, SIZE);
Mtr P = new Mtr(SIZE, SIZE);
Mtr symmetrical = new Mtr(SIZE, SIZE);
Random rndd = new Random(13335);
for (int i = 0; i < A.getRows(); ++i)
{
for (int j = 0; j < A.getColumns(); ++j)
{
int vl = rndd.Next(100);
A.setValue(vl, i, j);
if (i == j)
A.setValue(vl * 30, i, j);
symmetrical.setValue(vl, i, j);
symmetrical.setValue(vl, j, i);
}
b.setValue(rndd.Next(100), i, 0);
}
inversedLuSolver(A, b);
QRDecomposeSolver(A, b);
SimpleIteration(A, b);
symmetrical.setValue(5, 0, 0);
symmetrical.setValue(1, 0, 1);
// symmetrical.setValue(2, 0, 2);
symmetrical.setValue(1, 1, 0);
symmetrical.setValue(4, 1, 1);
// symmetrical.setValue(1, 1, 2);
// symmetrical.setValue(2, 2, 0);
// symmetrical.setValue(1, 2, 1);
// symmetrical.setValue(3, 2, 2);
planarRotationMethod(symmetrical);
findMinimalByPm(symmetrical);
}
}
}
/*
* Matrices for cube decompose solver
* 5x5
694,00 690,00 140,00 611,00 466,00
690,00 962,00 529,00 693,00 815,00
140,00 529,00 911,00 243,00 638,00
611,00 693,00 243,00 674,00 527,00
466,00 815,00 638,00 527,00 983,00
* 4x4
377,00 207,00 262,00 250,00
207,00 883,00 178,00 1,00
262,00 178,00 823,00 358,00
250,00 1,00 358,00 412,00
*/