QR 分解求矩阵特征值、特征向量 C 语言
cpp] view plain copy
1. //------------------------------------头文件
---------------------------------
2. #include <stdio.h>
3. #include <stdlib.h>
4. #include <math.h>
5. #include <time.h>
6.
7. //--------------------------这里是一些定义的结构体和数据类型---------
8. //纯 C 里面定义的布尔类型
9. typedef enum{False = 0,True = 1}Bool;
10. //定义矩阵元素的类型为 matrixType
11. typedef double matrixType;
12.
13. //此结构体用来表示矩阵,其中 row 为行,column 为列,height 为高,array 用来存放矩阵元
素(用一维来模拟二维/三维)
14. typedef struct
15. {
16. unsigned row,column,height;
17. matrixType *array; //使用时,必须对*array 进行初始化
18. }Matrix;
19.
20. //---------下面是 QR 分解,求解线性方程所用到的一些函数-----------
21. /*
22. matrix 为要设置大小并分配内存的矩阵,row、column、height 分别为行,列,高。
23. 函数调用成功则则返回 true,否则返回 false
24. */
25. Bool SetMatrixSize(Matrix *matrix ,const unsigned row,const unsigned colum
n,const unsigned height)
26. {
27. unsigned size = row * column * height * sizeof(matrixType);
28. if(size <= 0 )
29. {
30. return False;
31. }
32.
33. matrix->array = (matrixType*)malloc(size);
34.
35. //如果分配内存成功
36. if(matrix->array)
37. {
38. matrix->row = row;
评论11