eryar@163.com

## 二、矩阵的运算 Matrix Calculation

### 7. 线性方程组的数值解法

```/*
*
*        File    : Main.cpp
*        Author  : eryar@163.com
*        Date    : 2013-07-17 21:46
*        Version : 1.0v
*
*    Description : Demonstrate how to use math_Matrix class.
*                  题目来自《线性代数》同济 第四版
*
*/

#include <math_Matrix.hxx>

#pragma comment(lib, \"TKernel.lib\")
#pragma comment(lib, \"TKMath.lib\")

int main(int argc, char* argv[])
{
math_Matrix A(1, 3, 1, 3);
math_Matrix B(1, 3, 1, 3);
math_Matrix C(1, 3, 1, 3);

// 1. Test Matrix Transpose and multiply.
// 题目来自P53习题二 第3题
// Set value of Matrix A
A.Init(1);
A(2, 3) = -1;
A(3, 2) = -1;

// Set value of Matrix B
B(1, 1) = 1;    B(1, 2) = 2;    B(1, 3) = 3;
B(2, 1) = -1;   B(2, 2) = -2;   B(2, 3) = 4;
B(3, 1) = 0;    B(3, 2) = 5;    B(3, 3) = 1;

C.TMultiply(A, B);

cout<<\"Matrix A: \"<<endl;
cout<<A<<endl;
cout<<\"Matrix B: \"<<endl;
cout<<B<<endl;
cout<<\"Matrix C: \"<<endl;
cout<<C<<endl;

// 2. Test the Determinant of the Matrix.
// 题目来自P12 例7
math_Matrix D(1, 4, 1, 4);
D(1, 1) = 3;    D(1, 2) = 1;    D(1, 3) = -1;    D(1, 4) = 2;
D(2, 1) = -5;   D(2, 2) = 1;    D(2, 3) = 3;     D(2, 4) = -4;
D(3, 1) = 2;    D(3, 2) = 0;    D(3, 3) = 1;     D(3, 4) = -1;
D(4, 1) = 1;    D(4, 2) = -5;   D(4, 3) = 3;     D(4, 4) = -3;

cout<<\"Matrix D: \"<<endl;
cout<<D<<endl;
cout<<\"Determinant of Matrix D: \"<<D.Determinant()<<endl;

// 3. Calculate Inverse of the Matrix.
// 题目来自P54 11题(3) 和P56 30题(1)
math_Matrix E(1, 3, 1, 3);

E(1, 1) = 1;    E(1, 2) = 2;    E(1, 3) = -1;
E(2, 1) = 3;    E(2, 2) = 4;    E(2, 3) = -2;
E(3, 1) = 5;    E(3, 2) = -4;   E(3, 3) = 1;

cout<<\"Inverse of the Matrix E: \"<<endl;
cout<<E.Inverse()<<endl;

// P56 30题(1)
math_Matrix F(1, 4, 1, 4);

F(1, 1) = 5;    F(1, 2) = 2;
F(2, 1) = 2;    F(2, 2) = 1;
F(3, 3) = 8;    F(3, 4) = 3;
F(4, 3) = 5;    F(4, 4) = 2;

cout<<\"Inverse of the Matrix F: \"<<endl;
cout<<F.Inverse()<<endl;

return 0;
}```

``` 1 Matrix A:
2 math_Matrix of RowNumber = 3 and ColNumber = 3
3 math_Matrix ( 1, 1 ) = 1
4 math_Matrix ( 1, 2 ) = 1
5 math_Matrix ( 1, 3 ) = 1
6 math_Matrix ( 2, 1 ) = 1
7 math_Matrix ( 2, 2 ) = 1
8 math_Matrix ( 2, 3 ) = -1
9 math_Matrix ( 3, 1 ) = 1
10 math_Matrix ( 3, 2 ) = -1
11 math_Matrix ( 3, 3 ) = 1
12
13 Matrix B:
14 math_Matrix of RowNumber = 3 and ColNumber = 3
15 math_Matrix ( 1, 1 ) = 1
16 math_Matrix ( 1, 2 ) = 2
17 math_Matrix ( 1, 3 ) = 3
18 math_Matrix ( 2, 1 ) = -1
19 math_Matrix ( 2, 2 ) = -2
20 math_Matrix ( 2, 3 ) = 4
21 math_Matrix ( 3, 1 ) = 0
22 math_Matrix ( 3, 2 ) = 5
23 math_Matrix ( 3, 3 ) = 1
24
25 Matrix C:
26 math_Matrix of RowNumber = 3 and ColNumber = 3
27 math_Matrix ( 1, 1 ) = 0
28 math_Matrix ( 1, 2 ) = 5
29 math_Matrix ( 1, 3 ) = 8
30 math_Matrix ( 2, 1 ) = 0
31 math_Matrix ( 2, 2 ) = -5
32 math_Matrix ( 2, 3 ) = 6
33 math_Matrix ( 3, 1 ) = 2
34 math_Matrix ( 3, 2 ) = 9
35 math_Matrix ( 3, 3 ) = 0
36
37 Matrix D:
38 math_Matrix of RowNumber = 4 and ColNumber = 4
39 math_Matrix ( 1, 1 ) = 3
40 math_Matrix ( 1, 2 ) = 1
41 math_Matrix ( 1, 3 ) = -1
42 math_Matrix ( 1, 4 ) = 2
43 math_Matrix ( 2, 1 ) = -5
44 math_Matrix ( 2, 2 ) = 1
45 math_Matrix ( 2, 3 ) = 3
46 math_Matrix ( 2, 4 ) = -4
47 math_Matrix ( 3, 1 ) = 2
48 math_Matrix ( 3, 2 ) = 0
49 math_Matrix ( 3, 3 ) = 1
50 math_Matrix ( 3, 4 ) = -1
51 math_Matrix ( 4, 1 ) = 1
52 math_Matrix ( 4, 2 ) = -5
53 math_Matrix ( 4, 3 ) = 3
54 math_Matrix ( 4, 4 ) = -3
55
56 Determinant of Matrix D: 40
57 Inverse of the Matrix E:
58 math_Matrix of RowNumber = 3 and ColNumber = 3
59 math_Matrix ( 1, 1 ) = -2
60 math_Matrix ( 1, 2 ) = 1
61 math_Matrix ( 1, 3 ) = -8.88178e-017
62 math_Matrix ( 2, 1 ) = -6.5
63 math_Matrix ( 2, 2 ) = 3
64 math_Matrix ( 2, 3 ) = -0.5
65 math_Matrix ( 3, 1 ) = -16
66 math_Matrix ( 3, 2 ) = 7
67 math_Matrix ( 3, 3 ) = -1
68
69 Inverse of the Matrix F:
70 math_Matrix of RowNumber = 4 and ColNumber = 4
71 math_Matrix ( 1, 1 ) = 1
72 math_Matrix ( 1, 2 ) = -2
73 math_Matrix ( 1, 3 ) = 0
74 math_Matrix ( 1, 4 ) = 0
75 math_Matrix ( 2, 1 ) = -2
76 math_Matrix ( 2, 2 ) = 5
77 math_Matrix ( 2, 3 ) = -0
78 math_Matrix ( 2, 4 ) = -0
79 math_Matrix ( 3, 1 ) = 0
80 math_Matrix ( 3, 2 ) = 0
81 math_Matrix ( 3, 3 ) = 2
82 math_Matrix ( 3, 4 ) = -3
83 math_Matrix ( 4, 1 ) = -0
84 math_Matrix ( 4, 2 ) = -0
85 math_Matrix ( 4, 3 ) = -5
86 math_Matrix ( 4, 4 ) = 8
87
88 Press any key to continue . . .```

http://www.cppblog.com/eryar/archive/2012/06/21/179629.html

## 五、参考文献 Bibliography

1. 同济大学应用数学系 线性代数 高等教育出版社 2003

2. 易大义，沈云宝，李有法 计算方法 浙江大学出版社 2002