【发布时间】:2023-03-06 01:59:02
【问题描述】:
我需要编写一个程序,从逗号分隔值文件中读取矩阵,然后使用高斯消元法计算逆矩阵并将该逆矩阵写入新文件。
读进去没问题,写回来也一样。我想我了解高斯消除的工作原理,并且能够通过使用数组来做到这一点。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
// main program starts here
int main() {
// create a vector of a vector to store original matrix
// and matrices after each calculation
vector<vector<double>> data;
int n_com = 0;
// try to read input file
ifstream readFile("test_data.txt");
if (readFile.is_open()){
while (!readFile.eof()){
int i;
// declare temporary line vector and line string
vector<double> vline;
string aLine;
// assign line of file to line string
getline(readFile, aLine);
// count number of commas in line string
n_com = count(aLine.begin(), aLine.end(), ',');
// define integer for start of each element
int start = 0;
// loop over all but final element
for (i=0; i < n_com; i++) {
// declare and find position of next comma
int comma_pos;
comma_pos = aLine.find(',', start);
// declare string for element and assign substring to it
string elems;
elems = aLine.substr(start, comma_pos - start);
// convert string to double
double elemd = atof(elems.c_str());
// push back double to temporary vector
vline.push_back(elemd);
// redefine start for next iteration
start = comma_pos + 1;
}
// assign final element to string
string final_elems = aLine.substr(start, aLine.length() - start);
// convert final element to double and push back to vector
double final_elemd = atof(final_elems.c_str());
vline.push_back(final_elemd);
// push back line vector to data vector
data.push_back(vline);
}
}
else {
// print error if unable to open file
printf("Error unable to open input file!\n");
// exit program
exit(1);
}
// close input file
readFile.close();
这就是我在原始矩阵中的读取方式。
这是我对数组进行高斯消除的代码
// calculate width and length of original data (no. of rows and columns)
int length = data.size();
int width = n_com + 1;
// create new file to write to
ofstream writeFile ("tranpose.txt");
// check outfile is open
if (writeFile.is_open()){
// declare indices
// width (columns)
int i;
// length (rows)0
int j;
// k
int k;
// declare a float?
float data[10][10] = {0},d;
// identity matrix
for (i=1; i <= length; i++){
for (j=1; j <= 2 * length; j++){
if (j == (i + length)){
data[i][j] = 1;
}
}
}
// partial pivoting
for (i=length; i > 1; i--){
if (data[i-1][1] < data[i][1]){
for(j=1;j <= length * 2; j++){
d = data[i][j];
data[i][j] = data[i-1][j];
data[i-1][j] = d;
}
}
}
cout<<"Augmented Matrix: "<<endl;
for (i=1; i <= length; i++){
for (j=1;j <= length * 2; j++){
cout<<data[i][j]<<" ";
}
cout<<endl;
}
// reducing to diagonal matrix
for (i=1; i <= length; i++){
for (j=1; j <= length * 2; j++){
if (j != i){
d = data[j][i] / data [i][i];
for (k=1; k<= length * 2; k++){
data[j][k] = data[j][k] - (data[i][k] * d);
}
}
}
}
// reducing to unit matrix
for (i=1; i <= length; i++){
d = data[i][i];
for (j=1; j <= length * 2; j++){
data[i][j] = data[i][j] / d;
}
}
// print inverse matrix in console
cout<<"Inverse Matrix "<<endl;
for (i=1; i <= length; i++){
for (j = length + 1; j <= length * 2; j++){
cout<<data[i][j]<<" ";
}
cout<<endl;
}
// loop over all rows
for (i=1; i <= length; i++){
// loop over all columns
for (j = length + 1; j <= length * 2; j++){
// print data in transposed positions excluding last value
// i.e. [j][i] instead of [i][j]
writeFile << setw(4) << fixed << setprecision(2) << data[i][j] << ",";
}
// print onto new line
int i = length - 1;
writeFile << setw(4) << fixed << setprecision(2) << data[i][j] << "\n";
}
// close written file
writeFile.close();
我该如何编写它,以便它使用从我的向量或矩阵文件中存储的数据,而不是普通的数字数组?
【问题讨论】:
-
不确定您要问什么。在元素访问方面,向量的向量的行为与二维数组完全相同。
-
感谢您的回复。我也是这么想的,但是这段代码只会将 0 的矩阵作为其输入,然后将其反转,得到一个“nan”矩阵。我只想知道这段代码中的问题以及如何正确编写高斯消除,并将其应用于我的输入文本文件。
-
尝试注释掉这一行
float data[10][10] = {0},d; -
谢谢,刚刚尝试过,构建失败,因为我使用“未声明的标识符 d” // 部分旋转 for (i=length; i > 1; i--){ if (data[i- 1][1]
-
好吧,我的意思是把
data的重新定义注释掉,不要管d。
标签: c++ csv matrix gaussian inverse