├── 报告.pdf
├── 空间后方交会.xmind
├── 后方交会
├── Debug
│ ├── Space Resection.exe
│ └── Space Resection.pdb
├── Space Resection
│ ├── 影像坐标.txt
│ ├── Debug
│ │ ├── Main.obj
│ │ ├── matrix.obj
│ │ ├── Space Resection.ilk
│ │ ├── Space Resection.tlog
│ │ │ ├── CL.read.1.tlog
│ │ │ ├── CL.write.1.tlog
│ │ │ ├── link.read.1.tlog
│ │ │ ├── CL.command.1.tlog
│ │ │ ├── link.command.1.tlog
│ │ │ ├── link.write.1.tlog
│ │ │ └── Space Resection.lastbuildstate
│ │ ├── Space Resection.exe.recipe
│ │ ├── Space Resection.log
│ │ └── Space Resection.Build.CppClean.log
│ ├── SpaceResection.h
│ ├── SpaceResection.cpp
│ ├── 地面坐标.txt
│ ├── Space Resection.vcxproj.user
│ ├── Main.cpp
│ ├── Space Resection.vcxproj.filters
│ ├── matrix.h
│ ├── Space Resection.vcxproj
│ └── matrix.cpp
└── Space Resection.sln
└── README.md
/报告.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/报告.pdf
--------------------------------------------------------------------------------
/空间后方交会.xmind:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/空间后方交会.xmind
--------------------------------------------------------------------------------
/后方交会/Debug/Space Resection.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Debug/Space Resection.exe
--------------------------------------------------------------------------------
/后方交会/Debug/Space Resection.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Debug/Space Resection.pdb
--------------------------------------------------------------------------------
/后方交会/Space Resection/影像坐标.txt:
--------------------------------------------------------------------------------
1 | x y
2 | -86.15 -68.99
3 | -53.40 82.21
4 | -14.78 -76.63
5 | 10.46 64.43
6 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Main.obj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/Main.obj
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/matrix.obj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/matrix.obj
--------------------------------------------------------------------------------
/后方交会/Space Resection/SpaceResection.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/SpaceResection.h
--------------------------------------------------------------------------------
/后方交会/Space Resection/SpaceResection.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/SpaceResection.cpp
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.ilk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/Space Resection.ilk
--------------------------------------------------------------------------------
/后方交会/Space Resection/地面坐标.txt:
--------------------------------------------------------------------------------
1 | X Y Z
2 | 36589.41 25273.32 2195.17
3 | 37631.08 31324.51 728.69
4 | 39100.71 24934.98 2386.50
5 | 40426.54 30319.81 757.31
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.tlog/CL.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/Space Resection.tlog/CL.read.1.tlog
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.tlog/CL.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/Space Resection.tlog/CL.write.1.tlog
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.tlog/link.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/Space Resection.tlog/link.read.1.tlog
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.tlog/CL.command.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/Space Resection.tlog/CL.command.1.tlog
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.tlog/link.command.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/Space Resection.tlog/link.command.1.tlog
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.tlog/link.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/HEAD/后方交会/Space Resection/Debug/Space Resection.tlog/link.write.1.tlog
--------------------------------------------------------------------------------
/后方交会/Space Resection/Space Resection.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.tlog/Space Resection.lastbuildstate:
--------------------------------------------------------------------------------
1 | PlatformToolSet=v142:VCToolArchitecture=Native32Bit:VCToolsVersion=14.29.30133:TargetPlatformVersion=10.0.19041.0:
2 | Debug|Win32|C:\AImportantFile\AStudy\大三上\摄影测量学\Space Resection\|
3 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.exe.recipe:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | C:\AImportantFile\AStudy\大三上\摄影测量学\Space Resection\Debug\Space Resection.exe
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/Main.cpp:
--------------------------------------------------------------------------------
1 | // Space Resection.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
2 | //
3 | #include"matrix.h"
4 | #include
5 | #include"SpaceResection.h"
6 | #include
7 | using namespace std;
8 | int main()
9 | {
10 | double m = 15000, f = 0.15324;//m=15000 f=153.24mm
11 | double x0 = 0, y0 = 0;
12 |
13 | SpaceResection Rt(m,f,x0,y0);
14 | Rt.ReadCoordinate();
15 | Rt.GetStart();
16 | Rt.calculate();
17 |
18 | }
19 |
20 | // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
21 | // 调试程序: F5 或调试 >“开始调试”菜单
22 |
23 | // 入门使用技巧:
24 | // 1. 使用解决方案资源管理器窗口添加/管理文件
25 | // 2. 使用团队资源管理器窗口连接到源代码管理
26 | // 3. 使用输出窗口查看生成输出和其他消息
27 | // 4. 使用错误列表窗口查看错误
28 | // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
29 | // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
30 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.log:
--------------------------------------------------------------------------------
1 | SpaceResection.cpp
2 | C:\AImportantFile\AStudy\大三上\摄影测量学\Space Resection\Space Resection\SpaceResection.cpp(105,22): warning C4244: “参数”: 从“double”转换到“int”,可能丢失数据
3 | C:\AImportantFile\AStudy\大三上\摄影测量学\Space Resection\Space Resection\SpaceResection.cpp(138,23): warning C4244: “参数”: 从“double”转换到“int”,可能丢失数据
4 | C:\AImportantFile\AStudy\大三上\摄影测量学\Space Resection\Space Resection\SpaceResection.cpp(180,25): warning C4244: “参数”: 从“double”转换到“const unsigned int”,可能丢失数据
5 | C:\AImportantFile\AStudy\大三上\摄影测量学\Space Resection\Space Resection\SpaceResection.cpp(180,38): warning C4244: “参数”: 从“double”转换到“const unsigned int”,可能丢失数据
6 | C:\AImportantFile\AStudy\大三上\摄影测量学\Space Resection\Space Resection\SpaceResection.cpp(180,51): warning C4244: “参数”: 从“double”转换到“const unsigned int”,可能丢失数据
7 | Space Resection.vcxproj -> C:\AImportantFile\AStudy\大三上\摄影测量学\Space Resection\Debug\Space Resection.exe
8 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/Debug/Space Resection.Build.CppClean.log:
--------------------------------------------------------------------------------
1 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\vc142.pdb
2 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\vc142.idb
3 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\spaceresection.obj
4 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\main.obj
5 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\matrix.obj
6 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\debug\space resection.exe
7 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\space resection.ilk
8 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\debug\space resection.pdb
9 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\space resection.tlog\cl.command.1.tlog
10 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\space resection.tlog\cl.read.1.tlog
11 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\space resection.tlog\cl.write.1.tlog
12 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\space resection.tlog\link.command.1.tlog
13 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\space resection.tlog\link.read.1.tlog
14 | c:\aimportantfile\astudy\大三上\摄影测量学\space resection\space resection\debug\space resection.tlog\link.write.1.tlog
15 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/Space Resection.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 | 源文件
23 |
24 |
25 | 源文件
26 |
27 |
28 |
29 |
30 | 头文件
31 |
32 |
33 | 头文件
34 |
35 |
36 |
--------------------------------------------------------------------------------
/后方交会/Space Resection.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31613.86
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Space Resection", "Space Resection\Space Resection.vcxproj", "{93E59DAC-7F42-4526-BE51-FF6BCD279E01}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {93E59DAC-7F42-4526-BE51-FF6BCD279E01}.Debug|x64.ActiveCfg = Debug|x64
17 | {93E59DAC-7F42-4526-BE51-FF6BCD279E01}.Debug|x64.Build.0 = Debug|x64
18 | {93E59DAC-7F42-4526-BE51-FF6BCD279E01}.Debug|x86.ActiveCfg = Debug|Win32
19 | {93E59DAC-7F42-4526-BE51-FF6BCD279E01}.Debug|x86.Build.0 = Debug|Win32
20 | {93E59DAC-7F42-4526-BE51-FF6BCD279E01}.Release|x64.ActiveCfg = Release|x64
21 | {93E59DAC-7F42-4526-BE51-FF6BCD279E01}.Release|x64.Build.0 = Release|x64
22 | {93E59DAC-7F42-4526-BE51-FF6BCD279E01}.Release|x86.ActiveCfg = Release|Win32
23 | {93E59DAC-7F42-4526-BE51-FF6BCD279E01}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {EBF55EA7-4FF4-4994-AE02-8509CDE0E9C9}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 摄影测量-空间后方交会
2 |
3 | The Program of Space-Resection(Photogrammetry).
4 |
5 | This is a Homework of Course Photogrammetry in Wuhan University.
6 |
7 | 武大遥感院摄影测量学作业-空间后方交会
8 |
9 | `@date:2021.9.17` `@language:C++`
10 |
11 | **原理:**
12 |
13 | **[空间后方交会.xmind](https://github.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/blob/main/%E7%A9%BA%E9%97%B4%E5%90%8E%E6%96%B9%E4%BA%A4%E4%BC%9A.xmind)**
14 |
15 | **[报告.pdf](https://github.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/blob/main/%E6%8A%A5%E5%91%8A.pdf)**
16 |
17 | **数据:**
18 |
19 | [**地面坐标.txt**](https://github.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/blob/main/%E5%90%8E%E6%96%B9%E4%BA%A4%E4%BC%9A/Space%20Resection/%E5%9C%B0%E9%9D%A2%E5%9D%90%E6%A0%87.txt)
20 |
21 | **[影像坐标.txt](https://github.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/blob/main/后方交会/Space%20Resection/影像坐标.txt)**
22 |
23 | ## 项目文件:
24 |
25 | - **[Main.cpp](https://github.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/blob/main/%E5%90%8E%E6%96%B9%E4%BA%A4%E4%BC%9A/Space%20Resection/Main.cpp)**
26 |
27 | > main
28 | >
29 | > main函数
30 |
31 | - **[SpaceResection.cpp](https://github.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/blob/main/%E5%90%8E%E6%96%B9%E4%BA%A4%E4%BC%9A/Space%20Resection/SpaceResection.cpp)**
32 |
33 | > SpaceResection
34 | >
35 | > 前方交会程序]
36 |
37 | - [**matrix.cpp**](https://github.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/blob/main/%E5%90%8E%E6%96%B9%E4%BA%A4%E4%BC%9A/Space%20Resection/matrix.cpp)/**[matrix.h](https://github.com/Raymond1030/WHURS-Photogrammetry-Space-Resection/blob/main/%E5%90%8E%E6%96%B9%E4%BA%A4%E4%BC%9A/Space%20Resection/matrix.h)**
38 |
39 | > Class of Matrix
40 | >
41 | > 别人写的一个矩阵类
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/matrix.h:
--------------------------------------------------------------------------------
1 | /*
2 | * matrix.h
3 | */
4 |
5 | #ifndef __MATRIX_H__
6 | #define __MATRIX_H__
7 |
8 | #include
9 |
10 | class Matrix {
11 | public:
12 | Matrix(int, int);
13 | Matrix(double**, int, int);
14 | Matrix();
15 | ~Matrix();
16 | Matrix(const Matrix&);
17 | Matrix& operator=(const Matrix&);
18 |
19 | inline double& operator()(int x, int y) { return p[x][y]; }
20 |
21 | Matrix& operator+=(const Matrix&);
22 | Matrix& operator-=(const Matrix&);
23 | Matrix& operator*=(const Matrix&);
24 | Matrix& operator*=(double);
25 | Matrix& operator/=(double);
26 | Matrix operator^(int);
27 |
28 | friend std::ostream& operator<<(std::ostream&, const Matrix&);
29 | friend std::istream& operator>>(std::istream&, Matrix&);
30 |
31 | void swapRows(int, int);
32 | Matrix transpose();
33 |
34 | static Matrix createIdentity(int);
35 | static Matrix solve(Matrix, Matrix);
36 | static Matrix bandSolve(Matrix, Matrix, int);
37 |
38 | // functions on vectors
39 | static double dotProduct(Matrix, Matrix);
40 |
41 | // functions on augmented matrices
42 | static Matrix augment(Matrix, Matrix);
43 | Matrix gaussianEliminate();
44 | Matrix rowReduceFromGaussian();
45 | void readSolutionsFromRREF(std::ostream& os);
46 | Matrix inverse();
47 |
48 | private:
49 | int rows_, cols_;
50 | double **p;
51 |
52 | void allocSpace();
53 | Matrix expHelper(const Matrix&, int);
54 | };
55 |
56 | Matrix operator+(const Matrix&, const Matrix&);
57 | Matrix operator-(const Matrix&, const Matrix&);
58 | Matrix operator*(const Matrix&, const Matrix&);
59 | Matrix operator*(const Matrix&, double);
60 | Matrix operator*(double, const Matrix&);
61 | Matrix operator/(const Matrix&, double);
62 |
63 | #endif
64 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/Space Resection.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | Win32Proj
24 | {93e59dac-7f42-4526-be51-ff6bcd279e01}
25 | SpaceResection
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 | true
75 |
76 |
77 | false
78 |
79 |
80 | true
81 |
82 |
83 | false
84 |
85 |
86 |
87 | Level3
88 | true
89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
90 | true
91 | 4996
92 |
93 |
94 | Console
95 | true
96 |
97 |
98 |
99 |
100 | Level3
101 | true
102 | true
103 | true
104 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
105 | true
106 |
107 |
108 | Console
109 | true
110 | true
111 | true
112 |
113 |
114 |
115 |
116 | Level3
117 | true
118 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
119 | true
120 |
121 |
122 | Console
123 | true
124 |
125 |
126 |
127 |
128 | Level3
129 | true
130 | true
131 | true
132 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
133 | true
134 |
135 |
136 | Console
137 | true
138 | true
139 | true
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
--------------------------------------------------------------------------------
/后方交会/Space Resection/matrix.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * matrix.cpp
3 | */
4 |
5 | #include
6 | #include "matrix.h"
7 |
8 | #define EPS 1e-10
9 |
10 | using std::ostream; using std::istream; using std::endl;
11 | using std::domain_error;
12 |
13 | /* PUBLIC MEMBER FUNCTIONS
14 | ********************************/
15 |
16 | Matrix::Matrix(int rows, int cols) : rows_(rows), cols_(cols)
17 | {
18 | allocSpace();
19 | for (int i = 0; i < rows_; ++i) {
20 | for (int j = 0; j < cols_; ++j) {
21 | p[i][j] = 0;
22 | }
23 | }
24 | }
25 |
26 | Matrix::Matrix(double** a, int rows, int cols) : rows_(rows), cols_(cols)
27 | {
28 | allocSpace();
29 | for (int i = 0; i < rows_; ++i) {
30 | for (int j = 0; j < cols_; ++j) {
31 | p[i][j] = a[i][j];
32 | }
33 | }
34 | }
35 |
36 | Matrix::Matrix() : rows_(1), cols_(1)
37 | {
38 | allocSpace();
39 | p[0][0] = 0;
40 | }
41 |
42 | Matrix::~Matrix()
43 | {
44 | for (int i = 0; i < rows_; ++i) {
45 | delete[] p[i];
46 | }
47 | delete[] p;
48 | }
49 |
50 | Matrix::Matrix(const Matrix& m) : rows_(m.rows_), cols_(m.cols_)
51 | {
52 | allocSpace();
53 | for (int i = 0; i < rows_; ++i) {
54 | for (int j = 0; j < cols_; ++j) {
55 | p[i][j] = m.p[i][j];
56 | }
57 | }
58 | }
59 |
60 | Matrix& Matrix::operator=(const Matrix& m)
61 | {
62 | if (this == &m) {
63 | return *this;
64 | }
65 |
66 | if (rows_ != m.rows_ || cols_ != m.cols_) {
67 | for (int i = 0; i < rows_; ++i) {
68 | delete[] p[i];
69 | }
70 | delete[] p;
71 |
72 | rows_ = m.rows_;
73 | cols_ = m.cols_;
74 | allocSpace();
75 | }
76 |
77 | for (int i = 0; i < rows_; ++i) {
78 | for (int j = 0; j < cols_; ++j) {
79 | p[i][j] = m.p[i][j];
80 | }
81 | }
82 | return *this;
83 | }
84 |
85 | Matrix& Matrix::operator+=(const Matrix& m)
86 | {
87 | for (int i = 0; i < rows_; ++i) {
88 | for (int j = 0; j < cols_; ++j) {
89 | p[i][j] += m.p[i][j];
90 | }
91 | }
92 | return *this;
93 | }
94 |
95 | Matrix& Matrix::operator-=(const Matrix& m)
96 | {
97 | for (int i = 0; i < rows_; ++i) {
98 | for (int j = 0; j < cols_; ++j) {
99 | p[i][j] -= m.p[i][j];
100 | }
101 | }
102 | return *this;
103 | }
104 |
105 | Matrix& Matrix::operator*=(const Matrix& m)
106 | {
107 | Matrix temp(rows_, m.cols_);
108 | for (int i = 0; i < temp.rows_; ++i) {
109 | for (int j = 0; j < temp.cols_; ++j) {
110 | for (int k = 0; k < cols_; ++k) {
111 | temp.p[i][j] += (p[i][k] * m.p[k][j]);
112 | }
113 | }
114 | }
115 | return (*this = temp);
116 | }
117 |
118 | Matrix& Matrix::operator*=(double num)
119 | {
120 | for (int i = 0; i < rows_; ++i) {
121 | for (int j = 0; j < cols_; ++j) {
122 | p[i][j] *= num;
123 | }
124 | }
125 | return *this;
126 | }
127 |
128 | Matrix& Matrix::operator/=(double num)
129 | {
130 | for (int i = 0; i < rows_; ++i) {
131 | for (int j = 0; j < cols_; ++j) {
132 | p[i][j] /= num;
133 | }
134 | }
135 | return *this;
136 | }
137 |
138 | Matrix Matrix::operator^(int num)
139 | {
140 | Matrix temp(*this);
141 | return expHelper(temp, num);
142 | }
143 |
144 | void Matrix::swapRows(int r1, int r2)
145 | {
146 | double *temp = p[r1];
147 | p[r1] = p[r2];
148 | p[r2] = temp;
149 | }
150 |
151 | Matrix Matrix::transpose()
152 | {
153 | Matrix ret(cols_, rows_);
154 | for (int i = 0; i < rows_; ++i) {
155 | for (int j = 0; j < cols_; ++j) {
156 | ret.p[j][i] = p[i][j];
157 | }
158 | }
159 | return ret;
160 | }
161 |
162 |
163 | /* STATIC CLASS FUNCTIONS
164 | ********************************/
165 |
166 | Matrix Matrix::createIdentity(int size)
167 | {
168 | Matrix temp(size, size);
169 | for (int i = 0; i < temp.rows_; ++i) {
170 | for (int j = 0; j < temp.cols_; ++j) {
171 | if (i == j) {
172 | temp.p[i][j] = 1;
173 | } else {
174 | temp.p[i][j] = 0;
175 | }
176 | }
177 | }
178 | return temp;
179 | }
180 |
181 | Matrix Matrix::solve(Matrix A, Matrix b)
182 | {
183 | // Gaussian elimination
184 | for (int i = 0; i < A.rows_; ++i) {
185 | if (A.p[i][i] == 0) {
186 | // pivot 0 - throw error
187 | throw domain_error("Error: the coefficient matrix has 0 as a pivot. Please fix the input and try again.");
188 | }
189 | for (int j = i + 1; j < A.rows_; ++j) {
190 | for (int k = i + 1; k < A.cols_; ++k) {
191 | A.p[j][k] -= A.p[i][k] * (A.p[j][i] / A.p[i][i]);
192 | if (A.p[j][k] < EPS && A.p[j][k] > -1*EPS)
193 | A.p[j][k] = 0;
194 | }
195 | b.p[j][0] -= b.p[i][0] * (A.p[j][i] / A.p[i][i]);
196 | if (A.p[j][0] < EPS && A.p[j][0] > -1*EPS)
197 | A.p[j][0] = 0;
198 | A.p[j][i] = 0;
199 | }
200 | }
201 |
202 | // Back substitution
203 | Matrix x(b.rows_, 1);
204 | x.p[x.rows_ - 1][0] = b.p[x.rows_ - 1][0] / A.p[x.rows_ - 1][x.rows_ - 1];
205 | if (x.p[x.rows_ - 1][0] < EPS && x.p[x.rows_ - 1][0] > -1*EPS)
206 | x.p[x.rows_ - 1][0] = 0;
207 | for (int i = x.rows_ - 2; i >= 0; --i) {
208 | int sum = 0;
209 | for (int j = i + 1; j < x.rows_; ++j) {
210 | sum += A.p[i][j] * x.p[j][0];
211 | }
212 | x.p[i][0] = (b.p[i][0] - sum) / A.p[i][i];
213 | if (x.p[i][0] < EPS && x.p[i][0] > -1*EPS)
214 | x.p[i][0] = 0;
215 | }
216 |
217 | return x;
218 | }
219 |
220 | Matrix Matrix::bandSolve(Matrix A, Matrix b, int k)
221 | {
222 | // optimized Gaussian elimination
223 | int bandsBelow = (k - 1) / 2;
224 | for (int i = 0; i < A.rows_; ++i) {
225 | if (A.p[i][i] == 0) {
226 | // pivot 0 - throw exception
227 | throw domain_error("Error: the coefficient matrix has 0 as a pivot. Please fix the input and try again.");
228 | }
229 | for (int j = i + 1; j < A.rows_ && j <= i + bandsBelow; ++j) {
230 | int k = i + 1;
231 | while (k < A.cols_ && A.p[j][k]) {
232 | A.p[j][k] -= A.p[i][k] * (A.p[j][i] / A.p[i][i]);
233 | k++;
234 | }
235 | b.p[j][0] -= b.p[i][0] * (A.p[j][i] / A.p[i][i]);
236 | A.p[j][i] = 0;
237 | }
238 | }
239 |
240 | // Back substitution
241 | Matrix x(b.rows_, 1);
242 | x.p[x.rows_ - 1][0] = b.p[x.rows_ - 1][0] / A.p[x.rows_ - 1][x.rows_ - 1];
243 | for (int i = x.rows_ - 2; i >= 0; --i) {
244 | int sum = 0;
245 | for (int j = i + 1; j < x.rows_; ++j) {
246 | sum += A.p[i][j] * x.p[j][0];
247 | }
248 | x.p[i][0] = (b.p[i][0] - sum) / A.p[i][i];
249 | }
250 |
251 | return x;
252 | }
253 |
254 | // functions on VECTORS
255 | double Matrix::dotProduct(Matrix a, Matrix b)
256 | {
257 | double sum = 0;
258 | for (int i = 0; i < a.rows_; ++i) {
259 | sum += (a(i, 0) * b(i, 0));
260 | }
261 | return sum;
262 | }
263 |
264 | // functions on AUGMENTED matrices
265 | Matrix Matrix::augment(Matrix A, Matrix B)
266 | {
267 | Matrix AB(A.rows_, A.cols_ + B.cols_);
268 | for (int i = 0; i < AB.rows_; ++i) {
269 | for (int j = 0; j < AB.cols_; ++j) {
270 | if (j < A.cols_)
271 | AB(i, j) = A(i, j);
272 | else
273 | AB(i, j) = B(i, j - B.cols_);
274 | }
275 | }
276 | return AB;
277 | }
278 |
279 | Matrix Matrix::gaussianEliminate()
280 | {
281 | Matrix Ab(*this);
282 | int rows = Ab.rows_;
283 | int cols = Ab.cols_;
284 | int Acols = cols - 1;
285 |
286 | int i = 0; // row tracker
287 | int j = 0; // column tracker
288 |
289 | // iterate through the rows
290 | while (i < rows)
291 | {
292 | // find a pivot for the row
293 | bool pivot_found = false;
294 | while (j < Acols && !pivot_found)
295 | {
296 | if (Ab(i, j) != 0) { // pivot not equal to 0
297 | pivot_found = true;
298 | } else { // check for a possible swap
299 | int max_row = i;
300 | double max_val = 0;
301 | for (int k = i + 1; k < rows; ++k)
302 | {
303 | double cur_abs = Ab(k, j) >= 0 ? Ab(k, j) : -1 * Ab(k, j);
304 | if (cur_abs > max_val)
305 | {
306 | max_row = k;
307 | max_val = cur_abs;
308 | }
309 | }
310 | if (max_row != i) {
311 | Ab.swapRows(max_row, i);
312 | pivot_found = true;
313 | } else {
314 | j++;
315 | }
316 | }
317 | }
318 |
319 | // perform elimination as normal if pivot was found
320 | if (pivot_found)
321 | {
322 | for (int t = i + 1; t < rows; ++t) {
323 | for (int s = j + 1; s < cols; ++s) {
324 | Ab(t, s) = Ab(t, s) - Ab(i, s) * (Ab(t, j) / Ab(i, j));
325 | if (Ab(t, s) < EPS && Ab(t, s) > -1*EPS)
326 | Ab(t, s) = 0;
327 | }
328 | Ab(t, j) = 0;
329 | }
330 | }
331 |
332 | i++;
333 | j++;
334 | }
335 |
336 | return Ab;
337 | }
338 |
339 | Matrix Matrix::rowReduceFromGaussian()
340 | {
341 | Matrix R(*this);
342 | int rows = R.rows_;
343 | int cols = R.cols_;
344 |
345 | int i = rows - 1; // row tracker
346 | int j = cols - 2; // column tracker
347 |
348 | // iterate through every row
349 | while (i >= 0)
350 | {
351 | // find the pivot column
352 | int k = j - 1;
353 | while (k >= 0) {
354 | if (R(i, k) != 0)
355 | j = k;
356 | k--;
357 | }
358 |
359 | // zero out elements above pivots if pivot not 0
360 | if (R(i, j) != 0) {
361 |
362 | for (int t = i - 1; t >= 0; --t) {
363 | for (int s = 0; s < cols; ++s) {
364 | if (s != j) {
365 | R(t, s) = R(t, s) - R(i, s) * (R(t, j) / R(i, j));
366 | if (R(t, s) < EPS && R(t, s) > -1*EPS)
367 | R(t, s) = 0;
368 | }
369 | }
370 | R(t, j) = 0;
371 | }
372 |
373 | // divide row by pivot
374 | for (int k = j + 1; k < cols; ++k) {
375 | R(i, k) = R(i, k) / R(i, j);
376 | if (R(i, k) < EPS && R(i, k) > -1*EPS)
377 | R(i, k) = 0;
378 | }
379 | R(i, j) = 1;
380 |
381 | }
382 |
383 | i--;
384 | j--;
385 | }
386 |
387 | return R;
388 | }
389 |
390 | void Matrix::readSolutionsFromRREF(ostream& os)
391 | {
392 | Matrix R(*this);
393 |
394 | // print number of solutions
395 | bool hasSolutions = true;
396 | bool doneSearching = false;
397 | int i = 0;
398 | while (!doneSearching && i < rows_)
399 | {
400 | bool allZeros = true;
401 | for (int j = 0; j < cols_ - 1; ++j) {
402 | if (R(i, j) != 0)
403 | allZeros = false;
404 | }
405 | if (allZeros && R(i, cols_ - 1) != 0) {
406 | hasSolutions = false;
407 | os << "NO SOLUTIONS" << endl << endl;
408 | doneSearching = true;
409 | } else if (allZeros && R(i, cols_ - 1) == 0) {
410 | os << "INFINITE SOLUTIONS" << endl << endl;
411 | doneSearching = true;
412 | } else if (rows_ < cols_ - 1) {
413 | os << "INFINITE SOLUTIONS" << endl << endl;
414 | doneSearching = true;
415 | }
416 | i++;
417 | }
418 | if (!doneSearching)
419 | os << "UNIQUE SOLUTION" << endl << endl;
420 |
421 | // get solutions if they exist
422 | if (hasSolutions)
423 | {
424 | Matrix particular(cols_ - 1, 1);
425 | Matrix special(cols_ - 1, 1);
426 |
427 | for (int i = 0; i < rows_; ++i) {
428 | bool pivotFound = false;
429 | bool specialCreated = false;
430 | for (int j = 0; j < cols_ - 1; ++j) {
431 | if (R(i, j) != 0) {
432 | // if pivot variable, add b to particular
433 | if (!pivotFound) {
434 | pivotFound = true;
435 | particular(j, 0) = R(i, cols_ - 1);
436 | } else { // otherwise, add to special solution
437 | if (!specialCreated) {
438 | special = Matrix(cols_ - 1, 1);
439 | specialCreated = true;
440 | }
441 | special(j, 0) = -1 * R(i, j);
442 | }
443 | }
444 | }
445 | os << "Special solution:" << endl << special << endl;
446 | }
447 | os << "Particular solution:" << endl << particular << endl;
448 | }
449 | }
450 |
451 | Matrix Matrix::inverse()
452 | {
453 | Matrix I = Matrix::createIdentity(rows_);
454 | Matrix AI = Matrix::augment(*this, I);
455 | Matrix U = AI.gaussianEliminate();
456 | Matrix IAInverse = U.rowReduceFromGaussian();
457 | Matrix AInverse(rows_, cols_);
458 | for (int i = 0; i < AInverse.rows_; ++i) {
459 | for (int j = 0; j < AInverse.cols_; ++j) {
460 | AInverse(i, j) = IAInverse(i, j + cols_);
461 | }
462 | }
463 | return AInverse;
464 | }
465 |
466 |
467 | /* PRIVATE HELPER FUNCTIONS
468 | ********************************/
469 |
470 | void Matrix::allocSpace()
471 | {
472 | p = new double*[rows_];
473 | for (int i = 0; i < rows_; ++i) {
474 | p[i] = new double[cols_];
475 | }
476 | }
477 |
478 | Matrix Matrix::expHelper(const Matrix& m, int num)
479 | {
480 | if (num == 0) {
481 | return createIdentity(m.rows_);
482 | } else if (num == 1) {
483 | return m;
484 | } else if (num % 2 == 0) { // num is even
485 | return expHelper(m * m, num/2);
486 | } else { // num is odd
487 | return m * expHelper(m * m, (num-1)/2);
488 | }
489 | }
490 |
491 | /* NON-MEMBER FUNCTIONS
492 | ********************************/
493 |
494 | Matrix operator+(const Matrix& m1, const Matrix& m2)
495 | {
496 | Matrix temp(m1);
497 | return (temp += m2);
498 | }
499 |
500 | Matrix operator-(const Matrix& m1, const Matrix& m2)
501 | {
502 | Matrix temp(m1);
503 | return (temp -= m2);
504 | }
505 |
506 | Matrix operator*(const Matrix& m1, const Matrix& m2)
507 | {
508 | Matrix temp(m1);
509 | return (temp *= m2);
510 | }
511 |
512 | Matrix operator*(const Matrix& m, double num)
513 | {
514 | Matrix temp(m);
515 | return (temp *= num);
516 | }
517 |
518 | Matrix operator*(double num, const Matrix& m)
519 | {
520 | return (m * num);
521 | }
522 |
523 | Matrix operator/(const Matrix& m, double num)
524 | {
525 | Matrix temp(m);
526 | return (temp /= num);
527 | }
528 |
529 | ostream& operator<<(ostream& os, const Matrix& m)
530 | {
531 | for (int i = 0; i < m.rows_; ++i) {
532 | os << m.p[i][0];
533 | for (int j = 1; j < m.cols_; ++j) {
534 | os << " " << m.p[i][j];
535 | }
536 | os << endl;
537 | }
538 | return os;
539 | }
540 |
541 | istream& operator>>(istream& is, Matrix& m)
542 | {
543 | for (int i = 0; i < m.rows_; ++i) {
544 | for (int j = 0; j < m.cols_; ++j) {
545 | is >> m.p[i][j];
546 | }
547 | }
548 | return is;
549 | }
550 |
--------------------------------------------------------------------------------