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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace L1
{
class QR
{
public Matrix QRfunc(Matrix A)
{
int n = A.GetLength(0),
m = A.GetLength(1);
Matrix y, Q = new Matrix(n, m).MakeIdentity();
for (int i = 0; i < n - 1; ++i)
{
y = A.GetColumn(i, i);
Matrix curV = new Matrix(n - i, m - i).MakeIdentity(),
curQ = new Matrix(n, m).MakeIdentity(),
omega = new Matrix(n - i, 1),
e = new Matrix(n - i, 1); e[0, 0] = 1;
double yNorm = Math.Sqrt((y.GetTransposed() * y)[0, 0]),
omegaNorm = 0.0;
int sign = Math.Sign(y[0, 0]);
omega = y + sign * (yNorm * e);
omegaNorm = Math.Sqrt((omega.GetTransposed() * omega)[0, 0]);
omega *= (1.0 / omegaNorm);
curV = curV - (omega * omega.GetTransposed()) * 2.0;
curQ.SetMinor2(curV, i);
A = curQ * A;
Q = curQ * Q;
}
return Q.GetTransposed();
}
public Matrix solve(Matrix A, Matrix b, double eps)
{
int n = A.GetLength(0);
Matrix Q = QRfunc(A),
R = Q.GetTransposed() * A,
x = new Matrix(n, 1),
nb = new Matrix(n, 1);
Console.WriteLine("Матрица Q:");
Console.WriteLine(Q);
Console.WriteLine("Матрица R:");
Console.WriteLine(R);
for (int i = 0; i < n; i++)
{
nb[i, 0] = 0;
for (int j = 0; j < n; j++)
nb[i, 0] += b[j, 0] * Q[j, i];
}
for (int i = n - 1; i >= 0; --i)
{
double s = 0.0;
for (int j = i + 1; j < n; ++j)
s += R[i, j] * x[j, 0];
x[i, 0] = (nb[i, 0] - s) / R[i, i];
}
Console.WriteLine("Решение СЛАУ:");
Console.WriteLine(x);
return x;
}
public Matrix newSolve(Matrix A, Matrix b, double eps)
{
int n = A.GetLength(0);
Matrix Q = QRfunc(A),
R = Q.GetTransposed() * A,
x = new Matrix(n, 1),
nb = new Matrix(n, 1);
for (int i = 0; i < n; i++)
{
nb[i, 0] = 0;
for (int j = 0; j < n; j++)
nb[i, 0] += b[j, 0] * Q[j, i];
}
x[A.GetLength(0) - 1, 0] = 1.0;
for (int i = n - 2; i >= 0; --i)
{
double s = 0.0;
for (int j = i + 1; j < n; ++j)
s += R[i, j] * x[j, 0];
x[i, 0] = (nb[i, 0] - s) / R[i, i];
}
for (int i = 0; i < n; ++i)
x[i, 0] /= Math.Sqrt(x.GetSummColumn(0, 0));
return x;
}
public List<double> GetEigenvalues(Matrix A, double eps)
{
Matrix reserve = new Matrix(A);
List<double> retValue = new List<double>();
int countIter = 0;
int indColumn = A.GetLength(0) - 2;
while (true)
{
bool stop = true;
for (int i = indColumn; i >= 0; --i)
if (A.GetColumn(i, i + 1).GetNorm() >= eps)
stop = false;
if (stop)
break;
if (countIter > 100)
{
countIter = 0;
indColumn--;
A = reserve;
}
Matrix Q = QRfunc(A),
R = Q.GetTransposed() * A;
A = R * Q;
countIter++;
}
for (int i = 0; i < A.GetLength(0) - 1; ++i)
{
if (A.GetColumn(i, i + 1).GetNorm() < eps)
{
retValue.Add(A[i, i]);
}
if (retValue.Count == A.GetLength(0) - 1)
retValue.Add(A[A.GetLength(0) - 1, A.GetLength(1) - 1]);
}
return retValue;
}
public List<Matrix> GetEigenvectors(Matrix A, List<double> eigenvalues)
{
List<Matrix> retValue = new List<Matrix>();
Matrix b = new Matrix(A.GetLength(0), 1);
for (int i = 0; i < A.GetLength(0); ++i)
b[i, 0] = 0;
Matrix reserve = new Matrix(A);
for (int i = 0; i < eigenvalues.Count; ++i)
{
A = new Matrix(reserve);
A = A - A.MakeIdentity() * eigenvalues[i];
retValue.Add(newSolve(A, b, 0.001));
}
return retValue;
}
}
}