Symmetric matrices:
Set a n*n square a,a any element aij, when and only if AIJ = = Aji (0 <= i <= N-1 && 0 <= J <= N-1), then matrix A is a symmetric matrix. Separated by the diagonal of the matrix, it is divided into upper and lower triangles.
such as the following matrix:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/ED/wKioL1cNhDTx1gApAAAO50iPSO8800.png "title=" ey[j]x {) g_) ~om ' 8odt2s (t.png "alt=" Wkiol1cnhdtx1gapaaao50ipso8800.png "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "spacer.gif"/> Symmetric matrix compresses storage only requires storage of i* (i+1)/2 data.
The relationship between a symmetric matrix and a compression matrix is:
Symmetric matrix symmetricmatrix[i][j] = compression Matrix Array (i* (i+1)/2+j).
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/> Below I implement the code that the symmetric matrix is stored in the compression matrix and the elements in the compression matrix are restored to the symmetric matrix.
The code is as follows:
#include <iostream>using namespace std;template<class t>class symmtrixmatrix{ Public: symmtrixmatrix (t* a, size_t size) :_a (new t[size* (size+1)/2]) , _size (size* (size + 1) / 2) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (I&NBSP;>=&NBSP;J) &nbsP { //Converting a symmetric matrix to a compression matrix _a[i* (i + 1) / 2+j] = a[i*size+j]; } } } }//compression matrix access print void print (size_t size) { for (int i = 0; i < size; i++) { for  (int j = 0; j < size; j++) { int row = i; int col = j; if (Row < col) { swap (Row, col); } cout << _a [Row* (row + 1)/2&Nbsp;+ col] << " "; } cout << endl; } cout << endl; }private: T* _a; size_t _size; //is n, symmetric matrix is square};void test () { 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 }, }; symmtrixmatrix<int >&NBSP;SM ((int*) a, 5); sm. Print (5); }int main () { test (); System ("pause"); return 0;}
This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1763267
The "Data structure" symmetric matrix and the compressed storage of symmetric matrices