对称矩阵和稀疏矩阵
对称矩阵
在南明等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站建设、网站设计 网站设计制作按需求定制网站,公司网站建设,企业网站建设,高端网站设计,营销型网站,外贸网站建设,南明网站建设费用合理。
Matrix.h
#pragma once templateclass SymmetricMatrix { public: SymmetricMatrix(const T* a, size_t N) //对称矩阵 只存下三角 :_a(new T[N*(N + 1) / 2]) ,_n(N) { size_t index = 0; for (size_t i = 0; i < N; ++i) { for (size_t j = 0; j < N; ++j) { if (i >= j) { _a[index++] = a[i*N + j]; } else { break; } } } } ~SymmetricMatrix() { } void Display() { for (size_t i = 0; i < _n; ++i) { for (size_t j = 0; j <_n; ++j) { if (i >= j)//访问下三角 { cout << _a[i*(i + 1) / 2 + j] << " "; } else //访问上三角 { cout << _a[j*(j + 1) / 2 + i] << " "; } } cout << endl; } cout << endl; } T& Access(size_t i, size_t j)//访问i,j这里的数据 { if (i < j)//若为上三角,交换 { swap(i, j); } return _a[i(i + 1) / 2 + j]; } protected: T* _a; size_t _n; }; void Test1() { int a[5][5] = { { 0, 1, 2, 3, 4 }, { 1, 0, 1, 2, 3 }, { 2, 1, 0, 1, 2 }, { 3, 2, 1, 0, 1 }, { 4, 3, 2, 1, 0 }, }; SymmetricMatrix sm((int*)a, 5); sm.Display(); } Test.cpp #include using namespace std; #include"Matrix.h" int main() { Test1(); system("pause"); return 0; }
稀疏矩阵
Matrix.h
#includetemplate struct Triple //三元组 { size_t _row; size_t _col; T _value; Triple(size_t row=0, size_t col=0, const T& value=T()) :_row(row) ,_col(col) , _value(value) {} }; template class SqarseMatrix //稀疏矩阵 { public: SqarseMatrix(size_t M, size_t N, const T& invalid) :_M(M) , _N(N) , _invalid(invalid) {} SqarseMatrix(const T* a, size_t M, size_t N,const T& invalid) :_M(M) , _N(N) , _invalid(invalid) { for (size_t i = 0; i < M; ++i) { for (size_t j = 0; j < N; ++j) { if (a[i*N + j] != invalid) { Triple t(i, j, a[i*N + j]); _a.push_back(t); } } } } void Display() { size_t index = 0; for (size_t i = 0; i < _M; ++i) { for (size_t j = 0; j <_N; ++j) { if (index<_a.size()&&i == _a[index]._row && j == _a[index]._col)//不能超出有效值的范围 { cout << _a[index]._value << " "; ++index; } else //不是有效值则输出非法值 { cout << _invalid << " "; } } cout << endl; } cout << endl; } SqarseMatrix Transport() //转置 { //o(有效数据的个数*列数) SqarseMatrix sm(_N,_M,_invalid); for (size_t i = 0; i < _N; ++i) { size_t index = 0; while (index < _a.size()) { if (_a[index]._col == i) { Triple t(_a[index]._col, _a[index]._row, _a[index]._value); sm._a.push_back(t); } ++index; } } return sm; } SqarseMatrix FastTransport() //快速转置 { //O(2*有效数据个数+列数) SqarseMatrix sm(_N, _M, _invalid); int* rowCounts = new int[_N]; memset(rowCounts, 0, sizeof(int)*_N);//初始化 size_t index = 0; while (index < _a.size()) { rowCounts[_a[index]._col]++; ++index; } int* rowStart = new int[_N]; rowStart[0] = 0; for (size_t i = 1; i < _N; ++i) { rowStart[i] = rowStart[i - 1] + rowCounts[i - 1]; } index = 0; sm._a.resize(_a.size()); while (index < _a.size()) { size_t row = _a[index]._col;//转置前的列就是转置后的行 int& start = rowStart[row];//每行的起始位置,第一个起始位置就是0 Triple t(_a[index]._col, _a[index]._row, _a[index]._value); sm._a[start] = t; ++start; ++index; } delete[] rowCounts; delete[] rowStart; return sm; } protected: vector > _a; size_t _M; size_t _N; T _invalid; }; void Test2() { int a[6][5] = { { 1, 0, 3, 0, 5 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 2, 0, 4, 0, 6 }, { 0, 0, 0, 0, 0 }}; SqarseMatrix sm((int*)a, 6, 5, 0); sm.Display(); SqarseMatrix sm2 = sm.Transport(); sm2.Display(); SqarseMatrix sm3 = sm.FastTransport(); sm3.Display(); } Test.cpp #include using namespace std; #include"Matrix.h" int main() { //Test1(); Test2(); system("pause"); return 0; }
分享标题:对称矩阵和稀疏矩阵
当前网址:http://pwwzsj.com/article/iiphpd.html