UF_MTX_to_tridiag(A, n) 函数和UF_MTX_tqli(T, w, v, n)函数和UF_MTX_from_tri_eig(A, T, w, v, n)分别实现这三个函数
时间: 2023-07-31 12:07:23 浏览: 141
1. UF_MTX_to_tridiag(A, n)函数:将一个$n\times n$的对称矩阵$A$转化为对称三对角矩阵$T$。代码如下:
```
void UF_MTX_to_tridiag(double **A, int n, double **T) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
T[i][j] = A[i][j];
}
}
for (int k = 0; k < n - 2; k++) {
double *x = new double[n - k];
double *v = new double[n - k];
for (int i = k + 1; i < n; i++) {
x[i - k - 1] = T[i][k];
}
double alpha = 0.0;
for (int i = 0; i < n - k - 1; i++) {
alpha += x[i] * x[i];
}
alpha = sqrt(alpha);
if (x[0] > 0) {
alpha = -alpha;
}
double beta = 1.0 / (alpha * (alpha - x[0]));
T[k + 1][k] = -alpha;
for (int i = k + 2; i < n; i++) {
T[i][k] = 0.0;
}
for (int j = k + 1; j < n; j++) {
double sum = 0.0;
for (int i = k + 1; i < n; i++) {
sum += T[i][j] * x[i - k - 1];
}
double tau = beta * sum;
for (int i = k + 1; i < n; i++) {
T[i][j] -= tau * x[i - k - 1];
}
}
delete[] x;
delete[] v;
}
}
```
2. UF_MTX_tqli(T, w, v, n)函数:对对称三对角矩阵$T$进行求解,得到其特征值和特征向量。代码如下:
```
void UF_MTX_tqli(double **T, double *w, double **v, int n) {
for (int i = 0; i < n; i++) {
v[i][i] = 1.0;
}
for (int i = 1; i < n; i++) {
w[i - 1] = T[i - 1][i];
T[i - 1][i - 1] = T[i][i - 1] = 0.0;
double alpha = T[i][i];
double beta = sqrt(alpha * alpha + w[i - 1] * w[i - 1]);
if (beta == 0.0) {
continue;
}
if (alpha < 0.0) {
beta = -beta;
}
w[i - 1] = (w[i - 1] / alpha) / beta;
T[i][i - 1] = -beta;
T[i][i] = alpha / beta;
for (int j = i + 1; j < n; j++) {
double sum = 0.0;
for (int k = i; k < n; k++) {
sum += T[k][i - 1] * T[k][j];
}
double tau = sum / T[i][i - 1];
for (int k = i; k < n; k++) {
T[k][j] -= tau * T[k][i - 1];
}
}
}
w[n - 1] = T[n - 1][n - 1];
T[n - 1][n - 1] = 0.0;
for (int i = 0; i < n; i++) {
double sum = 0.0;
for (int j = i; j < n; j++) {
sum += T[i][j] * v[j][n - 1];
}
double tau = sum / w[n - 1];
for (int j = i; j < n; j++) {
v[j][i] = v[j][n - 1] - tau * T[i][j];
}
}
}
```
3. UF_MTX_from_tri_eig(A, T, w, v, n)函数:利用特征值和特征向量构造出对称矩阵$A$。代码如下:
```
void UF_MTX_from_tri_eig(double **A, double **T, double *w, double **v, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = 0.0;
for (int k = 0; k < n; k++) {
A[i][j] += v[i][k] * w[k] * v[j][k];
}
}
}
}
```
阅读全文