QR_Decomposition_2

QR分解(2)

QR分解是线性代数中一种常用的矩阵分解,即把矩阵分解为一个正规正交矩阵和一个上梯形矩阵——QR分解是求一般矩阵全部特征值的最有效并广泛应用的方法,同时也是许多迭代算法的基础。(QR分解续篇)

豪斯霍尔的变换法

  1. 豪斯霍尔德变换定义

  2. 豪斯霍尔德矩阵的性质

    • H矩阵是一个正交矩阵

    • 直观的来看,豪斯霍尔德变换是一个镜像变换,如图

      mark

      豪斯霍尔德矩阵的作用是把向量x映射到以π为镜面的像,其中平面π的法向量为u

基于豪斯霍尔德变换的QR分解

引理:

证明:

注:其中计算σ时,为避免舍入误差,应取$\sigma=-sgn(x_{1})||x||_{2}$

实际上根据豪斯霍尔德变换的几何意义,我们可以把x看做原向量,v 看成变换后镜像向量的一个单位向量,则u向量方向一定为原向量(x)减去原向量(想)模长倍的镜像向量(v)。单位化后即为所要求的u。

QR分解的过程:

  • 当m=n时,R是n阶可逆上三角矩阵

  • 当m>n时,R是$m \times n$阶上梯形矩阵

$\bigstar$ 矩阵的QR分解具有唯一性

$\bigstar$ QR分解常用于求解病态线性方程组Ax=b,即化为

通过这种方法求解线性方程是非常稳定的,得到的结果往往比三角分解好很多。

豪斯霍尔德法QR分解的程序

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
class Program
{
static void Main(string[] args)
{
//验证程序正确性的demo
Matrix m = new Matrix(3, 3);
Matrix[] result_QR = m.QR_Decomposition();
result_QR[0].DisPlay();
Console.WriteLine();
result_QR[1].DisPlay();
Matrix n = result_QR[0] * result_QR[1];
Console.WriteLine();
n.DisPlay();
}
//单位阶跃函数
public static int Sgn(float num)
{
if (num < 0)
{
return -1;
}
else if (num == 0)
{
return 0;
}
else
{
return 1;
}
}
\\类的封装
class Matrix
{
\\初始化及构造函数
public int row = 0;
public int column = 0;
private float[,] matrix;
public Matrix(int row, int column, int option)
{
this.row = row;
this.column = column;
matrix = new float[row, column];
if (option == 1)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (i == j)
{
matrix[i, i] = 1;
}
else
{
matrix[i, j] = 0;
}
}
}
}

}
public Matrix(int row, int column)
{
this.row = row;
this.column = column;
Console.WriteLine($"Please enter a matrix with {this.row}rows,{this.column}colums");
matrix = new float[row, column];
try
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
string mid = Console.ReadLine();
var rows = mid.Split(' ');
foreach (var item in rows)
{
matrix[i, j++] = float.Parse(item);
}

}

}
Console.WriteLine("\r\n");
}
catch (Exception e)
{
Console.WriteLine($"something wrong!\n{e.Message}");
}
}

\\矩阵运算的重载
public static Matrix operator -(Matrix p1, Matrix p2)
{
if (p1.row == p2.row && p1.column == p2.column)
{
Matrix result = new Matrix(p1.row, p1.column, 0);
for (int i = 0; i < p1.row; i++)
{
for (int j = 0; j < p1.column; j++)
{
result.matrix[i, j] = p1.matrix[i, j] - p2.matrix[i, j];
}
}
return result;
}
else
{
throw new Exception();

}
}
public static Matrix operator *(float multip, Matrix p1)
{

Matrix result = new Matrix(p1.row, p1.column, 0);
for (int i = 0; i < p1.row; i++)
{

for (int j = 0; j < p1.column; j++)
{

result.matrix[i, j] = p1.matrix[i, j] * multip;

}

}
return result;

}
public static Matrix operator *(Matrix p1, Matrix p2)
{
if (p1.column != p2.row)
{
//Matrix a = new Matrix(1, 2) { matrix = p1.matrix };
throw new Exception();
}
else
{
Matrix result = new Matrix(p1.row, p2.column, 0);
for (int i = 0; i < p1.row; i++)
{

for (int j = 0; j < p2.column; j++)
{
float temp = 0;
for (int k = 0; k < p1.column; k++)
{
temp += p1.matrix[i, k] * p2.matrix[k, j];

}
result.matrix[i, j] = temp;
}

}
return result;
}
}
//矩阵转置
public void Transpose()
{
this.row = column + row;
this.column = row - column;
this.row = row - column;
float[,] temp = new float[row, column];
for (int i = 0; i < this.row; i++)
{
for (int j = 0; j < this.column; j++)
{
temp[i, j] = matrix[j, i];
}
}
matrix = temp;

}
\\生成反射矩阵(豪斯霍尔德矩阵)
public Matrix ReflectMatrix(int order, Matrix column_vec, int suborder)
{
Matrix Reflect_matrix = new Matrix(order, order, 1);
\\生成单位向量
Matrix unit_vec = new Matrix(column_vec.row, column_vec.column, 1);
\\生成单位矩阵
Matrix unit_matrix = new Matrix(suborder, suborder, 1);

\\需要使用临时变量存储转置矩阵(临时变量初始化不能直接赋值column_vec),原因是:matrix是引用类型,赋值的方式只是使变量指向同一个对象,进行转置操作时会丢失原来的矩阵,下同

Matrix temp_1 = new Matrix(column_vec.row, column_vec.column, 0);
temp_1.matrix = column_vec.matrix;
temp_1.Transpose();
\\根据算法,计算相应参数
float sigma = -Sgn(column_vec.matrix[0, 0]) * (float)Math.Sqrt((temp_1 * column_vec).matrix[0, 0]);
float alpha = sigma * (sigma - column_vec.matrix[0, 0]);
Matrix omega = column_vec - sigma * unit_vec;
Matrix temp_2 = new Matrix(omega.row, omega.column, 0);
temp_2.matrix = omega.matrix;
temp_2.Transpose();
if (order == suborder)
{
Reflect_matrix = unit_matrix - (1 / alpha) * omega * temp_2;
}
else
{
Matrix temp_matrix = unit_matrix - (1 / alpha) * omega * temp_2;
for (int i = order - suborder; i < order; i++)
{
int start_value = order - suborder;
for (int j = start_value; j < order; j++)
{
Reflect_matrix.matrix[i, j] = temp_matrix.matrix[i - start_value, j - start_value];
}
}
}
return Reflect_matrix;
}
//QR分解主程序
public Matrix[] QR_Decomposition()
{
//准备储存结果的容器
Matrix result_Q = new Matrix(this.column, this.column, 1);
Matrix result_R = new Matrix(this.row, this.column, 0);
result_R.matrix = this.matrix;
//按列把待分解的矩阵化为上三角矩阵或上梯形矩阵
for (int i = 0; i < column; i++)
{
Matrix column_vec = new Matrix(row - i, 1, 0);
for (int j = 0; j < row - i; j++)
{
column_vec.matrix[j, 0] = result_R.matrix[j + i, i];
}
Matrix reflect_matrix = ReflectMatrix(row, column_vec, row - i);
result_Q = reflect_matrix * result_Q;
result_R = reflect_matrix * result_R;
}
result_Q.Transpose();
Matrix[] result = { result_Q, result_R };
return result;
}
//仅用于矩阵的打印
public void DisPlay()
{
for (int i = 0; i < this.row; i++)
{
for (int j = 0; j < this.column; j++)
{
Console.Write(string.Format("{0:F3}", matrix[i, j]) + " ");
}
Console.WriteLine("\r");
}
}
}
}

同样使用两个例子验证算法的正确性:

  1. 输入矩阵

    得到输出如下图

    mark

  2. 输入矩阵

    得到结果如下图:

    mark

    经过检验,基本可以确定程序是正确的。

文章目录
  1. 1. QR分解(2)
    1. 1.1. 豪斯霍尔的变换法
    2. 1.2. 基于豪斯霍尔德变换的QR分解
      1. 1.2.1. 豪斯霍尔德法QR分解的程序