/*================================================================ * Filename:QRmatrix.cpp * Author: KCN_yu * Createtime:Tue 06 Jul 2021 11:54:55 PM CST ================================================================*/ #include #include #include #include #include #include #include #include using namespace std; using chrono::duration; using chrono::duration_cast; using chrono::high_resolution_clock; template class Matrix_QR{ private: vector> data; int row, col; public: Matrix_QR(const char *filename); Matrix_QR(int row, int col); void InitData(); void ReadBvec(const char *filename, vector& b_data); Matrix_QR& Transpose(); int GetMethod(const char *filename); Matrix_QR H(const Matrix_QR& M); double SquareNorm(); }; template Matrix_QR::Matrix_QR(int row, int col){ this->row = row; this->col = col; data.resize(row); for (auto &r : data) { r.resize(col); } } template void ReadFile(const char *filename, vector>& data){ ifstream matrix(filename); if (!matrix.is_open()) { cerr << "open failed" << endl; exit(1); } string temp; vector row_data; getline(matrix,temp); while (matrix >> temp) { char front = temp.front(); char back = temp.back(); if (front == '[') { temp.erase(temp.begin()); row_data.push_back(atof(temp.c_str())); } else if (isdigit(back)) { row_data.push_back(atof(temp.c_str())); } else if (back == ';') { temp.pop_back(); if (temp.back() == ']') { temp.pop_back(); } row_data.push_back(atof(temp.c_str())); data.emplace_back(row_data); row_data.clear(); } } matrix.close(); } template <> void ReadFile(const char *filename, vector>>& data){ ifstream matrix(filename); if (!matrix.is_open()) { cerr << "open failed" << endl; exit(1); } string temp; vector> row_data; getline(matrix,temp); while (matrix >> temp) { char back = temp.back(); ssize_t pos_left = temp.find('['); ssize_t pos_right = temp.find(']'); if (pos_right != string::npos) { if (temp[pos_right + 1] == ',') { row_data.push_back(atof(temp.substr(0, pos_right).c_str())); data.emplace_back(row_data); row_data.clear(); if (pos_left != string::npos) { int cow_index = 0; int col_index = 0; data[cow_index][col_index++].imag( atof(temp.substr(pos_left + 1).c_str())); while (matrix >> temp) { if (isdigit(temp.back())) { data[cow_index][col_index++].imag(atof(temp.c_str())); } else if (temp.back() == ';') { temp.pop_back(); if (temp.back() == ')') { pos_right = temp.find(']'); data[cow_index][col_index++].imag( atof(temp.substr(0, pos_right).c_str())); continue; } data[cow_index++][col_index].imag(atof(temp.c_str())); col_index = 0; } } } } } else if (pos_left != string::npos) { row_data.push_back(atof(temp.substr(pos_left + 1).c_str())); } else if (back == ';') { temp.pop_back(); row_data.push_back(atof(temp.c_str())); data.emplace_back(row_data); row_data.clear(); matrix >> temp; row_data.push_back(atof(temp.c_str())); } } matrix.close(); } template void Matrix_QR::InitData() { for(auto &r : data){ for(auto elem: r){ cout << elem << " "; } cout << endl; } row = data.size(); for (auto &r : data) { if (r.size() != row) { cerr << "row != col failed" << endl; exit(1); } } col = data.back().size(); } template Matrix_QR::Matrix_QR(const char *filename) { ReadFile(filename,data); InitData(); } template void Matrix_QR::ReadBvec(const char *filename, vector& b_data) { string temp; ifstream vec(filename); if (!vec.is_open()) { cerr << "open failed" << endl; exit(1); } while (vec >> temp) { char front = temp.front(); char back = temp.back(); if (front == '[') { temp.erase(temp.begin()); temp.pop_back(); b_data.push_back(atof(temp.c_str())); } else if (back == ';') { temp.pop_back(); if (temp.back() == ']') { temp.pop_back(); } b_data.push_back(atof(temp.c_str())); } } } template int Matrix_QR::GetMethod(const char* filename){ ifstream mat; mat.open(filename); string tmp; getline(mat,tmp); int pos = tmp.find("=")+1; mat.close(); return tmp[pos]-'0'; } template Matrix_QR& Matrix_QR::Transpose(){ Matrix_QR temp = *this; data.clear(); row = temp.col; col = temp.row; for (int i = 0;i < row;i++) { vector vec; for (int j = 0;j < col;j++) { vec.push_back(temp.data[j][i]); } data.push_back(vec); } return *this; } template Matrix_QR Matrix_QR::H(const Matrix_QR& M){ Matrix_QR > res(M.row,M.col); for (int i = 0;i < M.row;i++) { for (int j = 0;j < M.col;j++) { res.data[i][j] = conj(M.data[i][j]); } } res.Transpose(); return res; } template double Matrix_QR::SquareNorm(){ double res = 0; for (int i = 0;i < row;i++) { for (int j = 0;j < col;j++) { res += norm(data[i][j]); } } return res; } template double alpha(const Matrix_QR& A) { double norm = sqrt(SquareNorm()); return A.M[0][0] > 0 ? -norm : norm; } template <> complex alpha(const matrix >& A) { double norm = sqrt(squareOfNorm2(A)); double x = A.M[0][0].real(),y = A.M[0][0].imag(); double z = mold(A.M[0][0]); if (z == complex(0,0)) { return norm; } return complex(norm * (x/z),norm * (y/z)); } int main() { Matrix_QR> M("Amat6.m"); cout << M.SquareNorm() << endl; }